diff --git a/cmd/developers/developers.go b/cmd/developers/developers.go index 8838ba3a1..f02f6a41e 100644 --- a/cmd/developers/developers.go +++ b/cmd/developers/developers.go @@ -38,6 +38,7 @@ func init() { Cmd.AddCommand(ListCmd) Cmd.AddCommand(GetCmd) Cmd.AddCommand(DelCmd) + Cmd.AddCommand(UpdateCmd) Cmd.AddCommand(CreateCmd) Cmd.AddCommand(ImpCmd) Cmd.AddCommand(ExpCmd) diff --git a/cmd/developers/upddev.go b/cmd/developers/upddev.go new file mode 100644 index 000000000..8e3cfefb6 --- /dev/null +++ b/cmd/developers/upddev.go @@ -0,0 +1,83 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package developers + +import ( + "fmt" + + "internal/apiclient" + + "internal/client/developers" + + "github.com/spf13/cobra" +) + +// UpdateCmd to update developer +var UpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update an Apigee developer configuration", + Long: "Update an Apigee developer configuration", + Args: func(cmd *cobra.Command, args []string) (err error) { + return apiclient.SetApigeeOrg(org) + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + _, err = developers.Update(email, firstName, lastName, userName, cmd.Flag("status").Value.String(), attrs) + return + }, +} + +type developerStatus string + +var status developerStatus + +const ( + active developerStatus = "active" + inactive developerStatus = "inactive" +) + +func (s *developerStatus) String() string { + return string(*s) +} + +func (s *developerStatus) Set(r string) error { + switch r { + case "active", "inactive": + *s = developerStatus(r) + return nil + default: + return fmt.Errorf("must be %s or %s", active, inactive) + } +} + +func (s *developerStatus) Type() string { + return "developerStatus" +} + +func init() { + UpdateCmd.Flags().StringVarP(&email, "email", "n", + "", "The developer's email") + UpdateCmd.Flags().StringVarP(&firstName, "first", "f", + "", "The first name of the developer") + UpdateCmd.Flags().StringVarP(&lastName, "last", "s", + "", "The last name of the developer") + UpdateCmd.Flags().StringVarP(&userName, "user", "u", + "", "The username of the developer") + UpdateCmd.Flags().Var(&status, "status", + fmt.Sprintf("must be %s or %s", active, inactive)) + UpdateCmd.Flags().StringToStringVar(&attrs, "attrs", + nil, "Custom attributes") + + _ = UpdateCmd.MarkFlagRequired("email") +} diff --git a/internal/client/apis/apis.go b/internal/client/apis/apis.go index 2e3c7adce..9c83f3a00 100644 --- a/internal/client/apis/apis.go +++ b/internal/client/apis/apis.go @@ -65,8 +65,7 @@ type violation struct { Description string `json:"description,omitempty"` } -type routingchanges struct { -} +type routingchanges struct{} type routingconflicts struct { EnvironmentGroup string `json:"environmentGroup,omitempty"` @@ -112,7 +111,6 @@ func DeleteProxyRevision(name string, revision int) (respBody []byte, err error) // DeployProxy func DeployProxy(name string, revision int, overrides bool, sequencedRollout bool, safeDeploy bool, serviceAccountName string) (respBody []byte, err error) { - if safeDeploy { var safeResp []byte d := deploychangereport{} diff --git a/internal/client/developers/developers.go b/internal/client/developers/developers.go index add2369b0..d168ddf25 100644 --- a/internal/client/developers/developers.go +++ b/internal/client/developers/developers.go @@ -39,6 +39,7 @@ type Appdeveloper struct { Attributes []Attribute `json:"attributes,omitempty"` Username string `json:"userName,omitempty"` DeveloperId string `json:"developerId,omitempty"` + Status *string `json:"status,omitempty"` } // Appdevelopers hold an array of developers @@ -94,6 +95,63 @@ func Get(email string) (respBody []byte, err error) { return respBody, err } +// Update +func Update(email string, firstName string, lastName string, username string, status string, attrs map[string]string) (respBody []byte, err error) { + apiclient.ClientPrintHttpResponse.Set(false) + devRespBody, err := Get(email) + if err != nil { + return nil, err + } + apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) + + d := Appdeveloper{} + if err = json.Unmarshal(devRespBody, &d); err != nil { + return nil, err + } + + if firstName != "" { + d.FirstName = firstName + } + + if lastName != "" { + d.LastName = lastName + } + + if username != "" { + d.Username = username + } + + if status != "" { + apiclient.ClientPrintHttpResponse.Set(false) + err = setDeveloperStatus(email, status) + apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) + if err != nil { + return nil, err + } + } + + reqBody, err := json.Marshal(d) + if err != nil { + return nil, err + } + + u, _ := url.Parse(apiclient.BaseURL) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", url.QueryEscape(email)) + respBody, err = apiclient.HttpClient(u.String(), string(reqBody), "PUT") + return respBody, err +} + +func setDeveloperStatus(email string, action string) (err error) { + u, _ := url.Parse(apiclient.BaseURL) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", url.QueryEscape(email)) + q := u.Query() + q.Set("action", action) + u.RawQuery = q.Encode() + + _, err = apiclient.HttpClient(u.String(), "", "POST", "application/octet-stream") + return err +} + // GetDeveloperId func GetDeveloperId(email string) (developerId string, err error) { apiclient.ClientPrintHttpResponse.Set(false) @@ -168,7 +226,6 @@ func Export() (respBody []byte, err error) { // Import func Import(conn int, filePath string) error { - entities, err := ReadDevelopersFile(filePath) if err != nil { clilog.Error.Println("Error reading file: ", err) diff --git a/internal/client/sharedflows/sharedflows.go b/internal/client/sharedflows/sharedflows.go index fe097f1a1..ce77b1528 100644 --- a/internal/client/sharedflows/sharedflows.go +++ b/internal/client/sharedflows/sharedflows.go @@ -345,7 +345,6 @@ func Export(conn int, folder string, allRevisions bool) (err error) { lastRevision := maxRevision(proxy.Revision) jobChan <- revision{name: proxy.Name, rev: lastRevision} } - } close(jobChan) fanOutWg.Wait()