Skip to content

Commit

Permalink
Fixed comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nvnyale committed May 11, 2022
1 parent a17b0ef commit c48bb62
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 69 deletions.
76 changes: 16 additions & 60 deletions api/handlers_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,13 +449,13 @@ func (s *server) InstanceIDHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotImplemented)
}

func (s *server) CreateAssociationHandler(w http.ResponseWriter, r *http.Request) {
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 := &SSMCreateRequest{}
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))
Expand Down Expand Up @@ -495,21 +495,21 @@ func (s *server) CreateAssociationHandler(w http.ResponseWriter, r *http.Request
handleResponseOk(w, struct{ AssociationId string }{AssociationId: *out.AssociationDescription.AssociationId})
}

func (s *server) InstancesTagsHandler(w http.ResponseWriter, r *http.Request) {
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 := &Ec2ImageUpdateRequest{}
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 {
handleError(w, apierror.New(apierror.ErrBadRequest, "missing required field: tags", nil))
if len(req.Tags) == 0 && len(req.InstanceType) == 0 {
handleError(w, apierror.New(apierror.ErrBadRequest, "missing required fields", nil))
return
}

Expand Down Expand Up @@ -538,60 +538,16 @@ func (s *server) InstancesTagsHandler(w http.ResponseWriter, r *http.Request) {
ec2.WithOrg(s.org),
)

if err := service.UpdateTags(r.Context(), req.Tags, instanceId); err != nil {
handleError(w, err)
return
}

w.WriteHeader(http.StatusNoContent)
}

func (s *server) InstancesAttributeHandler(w http.ResponseWriter, r *http.Request) {
w = LogWriter{w}
vars := mux.Vars(r)
account := s.mapAccountNumber(vars["account"])
instanceId := vars["id"]

req := &Ec2InstancesAttributeUpdateRequest{}
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.InstanceType) == 0 {
handleError(w, apierror.New(apierror.ErrBadRequest, "missing required field: tags", nil))
return
}

role := fmt.Sprintf("arn:aws:iam::%s:role/%s", account, s.session.RoleName)
policy, err := tagCreatePolicy()
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 err := service.UpdateAttributes(r.Context(), req.InstanceType["value"], instanceId); err != nil {
handleError(w, err)
return
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)
Expand Down
6 changes: 3 additions & 3 deletions api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func (s *server) routes() {
api.HandleFunc("/{account}/instances/{id}", s.InstanceIDHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/power", s.InstanceStateHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/ssm/command", s.InstanceSendCommandHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/ssm/association", s.CreateAssociationHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/tags", s.InstancesTagsHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/attribute", s.InstancesAttributeHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/ssm/association", s.InstanceSSMAssociationHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/tags", s.InstanceUpdateHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/instances/{id}/attribute", s.InstanceUpdateHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/sgs/{id}", s.SecurityGroupUpdateHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/sgs/{id}/tags", s.ProxyRequestHandler).Methods(http.MethodPut)
api.HandleFunc("/{account}/volumes/{id}", s.ProxyRequestHandler).Methods(http.MethodPut)
Expand Down
13 changes: 7 additions & 6 deletions api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,11 +654,12 @@ func toSSMGetCommandInvocationOutput(rawOut *ssm.GetCommandInvocationOutput) *SS
}

type Ec2ImageUpdateRequest struct {
Tags map[string]string
Tags map[string]string `json:"tags"`
}

type Ec2InstancesAttributeUpdateRequest struct {
InstanceType map[string]string `json:"instance_type,omitempty"`
type Ec2InstanceUpdateRequest struct {
Tags map[string]string `json:"tags"`
InstanceType map[string]string `json:"instance_type"`
}
type AssociationDescription struct {
Name string `json:"name"`
Expand Down Expand Up @@ -729,11 +730,11 @@ func parseAssociationTargets(rawTgts []*ssm.Target) (tgts []AssociationTarget) {
}

type Ec2InstanceStateChangeRequest struct {
State string
State string `json:"state"`
}

type SSMCreateRequest struct {
Document string
type SSMAssociationRequest struct {
Document string `json:"document"`
}

type SsmCommandRequest struct {
Expand Down

0 comments on commit c48bb62

Please sign in to comment.