-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for updating instances #27
Merged
+395
−24
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
f223778
PUT SSM Association
nvnyale 2409e78
SPIN-2936: Implemented below update instances endpoints
nvnyale a17b0ef
Merge branch 'master' of https://github.com/YaleSpinup/ec2-api into P…
nvnyale c48bb62
Fixed comments
nvnyale 11782be
Added policies
nvnyale 3427772
Role updated
nvnyale a683732
updated PUT SSM
nvnyale f8c75ee
Print stack trace when encountering panic
nvnyale f67007e
Updated SSM Association
nvnyale 9cfb7a9
Fixed AssociationID output
nvnyale e049389
Fixed unit testcase
nvnyale d944c1b
Updated time format
nvnyale File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -443,3 +443,121 @@ func (s *server) InstanceSendCommandHandler(w http.ResponseWriter, r *http.Reque | |
handleResponseOk(w, out) | ||
|
||
} | ||
|
||
func (s *server) NotImplementedHandler(w http.ResponseWriter, r *http.Request) { | ||
w = LogWriter{w} | ||
w.WriteHeader(http.StatusNotImplemented) | ||
} | ||
|
||
func (s *server) InstanceSSMAssociationHandler(w http.ResponseWriter, r *http.Request) { | ||
w = LogWriter{w} | ||
vars := mux.Vars(r) | ||
account := s.mapAccountNumber(vars["account"]) | ||
instanceId := vars["id"] | ||
|
||
req := &SSMAssociationRequest{} | ||
if err := json.NewDecoder(r.Body).Decode(req); err != nil { | ||
msg := fmt.Sprintf("cannot decode body into ssm create input: %s", err) | ||
handleError(w, apierror.New(apierror.ErrBadRequest, msg, err)) | ||
return | ||
} | ||
|
||
if req.Document == "" { | ||
handleError(w, apierror.New(apierror.ErrBadRequest, "Document is mandatory", nil)) | ||
return | ||
} | ||
|
||
role := fmt.Sprintf("arn:aws:iam::%s:role/%s", account, s.session.RoleName) | ||
policy, err := ssmAssociationPolicy() | ||
if err != nil { | ||
handleError(w, err) | ||
return | ||
} | ||
|
||
session, err := s.assumeRole( | ||
r.Context(), | ||
s.session.ExternalID, | ||
role, | ||
policy, | ||
"arn:aws:iam::aws:policy/AmazonSSMReadOnlyAccess", | ||
"arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess", | ||
) | ||
if err != nil { | ||
msg := fmt.Sprintf("failed to assume role in account: %s", account) | ||
handleError(w, apierror.New(apierror.ErrForbidden, msg, err)) | ||
return | ||
} | ||
|
||
service := ssm.New( | ||
ssm.WithSession(session.Session), | ||
) | ||
|
||
out, err := service.CreateAssociation(r.Context(), instanceId, req.Document) | ||
if err != nil { | ||
handleError(w, err) | ||
return | ||
} | ||
|
||
handleResponseOk(w, out) | ||
} | ||
|
||
func (s *server) InstanceUpdateHandler(w http.ResponseWriter, r *http.Request) { | ||
w = LogWriter{w} | ||
vars := mux.Vars(r) | ||
account := s.mapAccountNumber(vars["account"]) | ||
instanceId := vars["id"] | ||
|
||
req := &Ec2InstanceUpdateRequest{} | ||
if err := json.NewDecoder(r.Body).Decode(req); err != nil { | ||
msg := fmt.Sprintf("cannot decode body into update image input: %s", err) | ||
handleError(w, apierror.New(apierror.ErrBadRequest, msg, err)) | ||
return | ||
} | ||
|
||
if len(req.Tags) == 0 && len(req.InstanceType) == 0 { | ||
handleError(w, apierror.New(apierror.ErrBadRequest, "missing required fields: tags or instance_type", nil)) | ||
return | ||
} else if len(req.Tags) > 0 && len(req.InstanceType) > 0 { | ||
handleError(w, apierror.New(apierror.ErrBadRequest, "only one of these fields should be provided: tags or instance_type", nil)) | ||
return | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should create and use a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated code. |
||
role := fmt.Sprintf("arn:aws:iam::%s:role/%s", account, s.session.RoleName) | ||
policy, err := instanceUpdatePolicy() | ||
if err != nil { | ||
handleError(w, err) | ||
return | ||
} | ||
|
||
session, err := s.assumeRole( | ||
r.Context(), | ||
s.session.ExternalID, | ||
role, | ||
policy, | ||
"arn:aws:iam::aws:policy/AmazonEC2ReadOnlyAccess", | ||
) | ||
if err != nil { | ||
msg := fmt.Sprintf("failed to assume role in account: %s", account) | ||
handleError(w, apierror.New(apierror.ErrForbidden, msg, err)) | ||
return | ||
} | ||
|
||
service := ec2.New( | ||
ec2.WithSession(session.Session), | ||
ec2.WithOrg(s.org), | ||
) | ||
|
||
if len(req.Tags) > 0 { | ||
if err := service.UpdateTags(r.Context(), req.Tags, instanceId); err != nil { | ||
handleError(w, err) | ||
return | ||
} | ||
} else if len(req.InstanceType) > 0 { | ||
if err := service.UpdateAttributes(r.Context(), req.InstanceType["value"], instanceId); err != nil { | ||
handleError(w, err) | ||
return | ||
} | ||
} | ||
|
||
w.WriteHeader(http.StatusNoContent) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ec2 | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/YaleSpinup/apierror" | ||
"github.com/YaleSpinup/ec2-api/common" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
func (e *Ec2) UpdateAttributes(ctx context.Context, instanceType, instanceId string) error { | ||
if len(instanceId) == 0 || len(instanceType) == 0 { | ||
return apierror.New(apierror.ErrBadRequest, "invalid input", nil) | ||
} | ||
|
||
log.Infof("updating attributes: %v with instance type %+v", instanceId, instanceType) | ||
|
||
input := ec2.ModifyInstanceAttributeInput{ | ||
InstanceType: &ec2.AttributeValue{Value: aws.String(instanceType)}, | ||
InstanceId: aws.String(instanceId), | ||
} | ||
|
||
if _, err := e.Service.ModifyInstanceAttributeWithContext(ctx, &input); err != nil { | ||
return common.ErrCode("updating attributes", err) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package ec2 | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/awserr" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/aws/aws-sdk-go/service/ec2/ec2iface" | ||
) | ||
|
||
func (m *mockEC2Client) ModifyInstanceAttributeWithContext(ctx aws.Context, inp *ec2.ModifyInstanceAttributeInput, opt ...request.Option) (*ec2.ModifyInstanceAttributeOutput, error) { | ||
if m.err != nil { | ||
return nil, m.err | ||
} | ||
return &ec2.ModifyInstanceAttributeOutput{}, nil | ||
|
||
} | ||
func TestEc2_UpdateAttributes(t *testing.T) { | ||
type fields struct { | ||
Service ec2iface.EC2API | ||
} | ||
type args struct { | ||
ctx context.Context | ||
instanceType string | ||
instanceId string | ||
} | ||
tests := []struct { | ||
name string | ||
fields fields | ||
e *Ec2 | ||
args args | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "success case", | ||
args: args{ctx: context.TODO(), instanceType: "Type1", instanceId: "i-123"}, | ||
fields: fields{Service: newmockEC2Client(t, nil)}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "aws error", | ||
args: args{ctx: context.TODO(), instanceType: "Type1", instanceId: "i-123"}, | ||
fields: fields{Service: newmockEC2Client(t, awserr.New("Bad Request", "boom.", nil))}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid input, instance id is empty", | ||
args: args{ctx: context.TODO(), instanceType: "Type1", instanceId: ""}, | ||
fields: fields{Service: newmockEC2Client(t, nil)}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "invalid input, instance type is empty", | ||
args: args{ctx: context.TODO(), instanceType: "", instanceId: "i-123"}, | ||
fields: fields{Service: newmockEC2Client(t, nil)}, | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
e := &Ec2{ | ||
Service: tt.fields.Service, | ||
} | ||
if err := e.UpdateAttributes(tt.args.ctx, tt.args.instanceType, tt.args.instanceId); (err != nil) != tt.wantErr { | ||
t.Errorf("Ec2.UpdateAttributes() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should also check that both are not > 0 and return an error
only one of these is expected: tags or instance_type
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.