diff --git a/cmd/apps/apps.go b/cmd/apps/apps.go index ee69b4e50..02ab1c71d 100644 --- a/cmd/apps/apps.go +++ b/cmd/apps/apps.go @@ -44,4 +44,5 @@ func init() { Cmd.AddCommand(ExpCmd) Cmd.AddCommand(KeysCmd) Cmd.AddCommand(ManageCmd) + Cmd.AddCommand(UpdateCmd) } diff --git a/cmd/apps/updateapp.go b/cmd/apps/updateapp.go new file mode 100644 index 000000000..f8cc750b2 --- /dev/null +++ b/cmd/apps/updateapp.go @@ -0,0 +1,57 @@ +// Copyright 2023 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 apps + +import ( + "internal/apiclient" + + "internal/client/apps" + + "github.com/spf13/cobra" +) + +// UpdateCmd to update an app +var UpdateCmd = &cobra.Command{ + Use: "update", + Short: "Update a Developer App", + Long: "Update a Developer App", + Args: func(cmd *cobra.Command, args []string) (err error) { + return apiclient.SetApigeeOrg(org) + }, + RunE: func(cmd *cobra.Command, args []string) (err error) { + _, err = apps.Update(name, email, expires, callback, apiProducts, scopes, attrs) + return + }, +} + +func init() { + UpdateCmd.Flags().StringVarP(&name, "name", "n", + "", "Name of the developer app") + UpdateCmd.Flags().StringVarP(&email, "email", "e", + "", "The developer's email") + UpdateCmd.Flags().StringVarP(&expires, "expires", "x", + "", "A setting, in milliseconds, for the lifetime of the consumer key") + UpdateCmd.Flags().StringVarP(&callback, "callback", "c", + "", "The callbackUrl is used by OAuth") + UpdateCmd.Flags().StringArrayVarP(&apiProducts, "prods", "p", + []string{}, "A list of api products") + UpdateCmd.Flags().StringArrayVarP(&scopes, "scopes", "s", + []string{}, "OAuth scopes") + UpdateCmd.Flags().StringToStringVar(&attrs, "attrs", + nil, "Custom attributes") + + _ = UpdateCmd.MarkFlagRequired("name") + _ = UpdateCmd.MarkFlagRequired("email") +} diff --git a/internal/client/apps/apps.go b/internal/client/apps/apps.go index 4fb0177fb..2130957a9 100644 --- a/internal/client/apps/apps.go +++ b/internal/client/apps/apps.go @@ -79,8 +79,40 @@ type attribute struct { Value string `json:"value,omitempty"` } +type Action uint8 + +const ( + CREATE Action = iota + UPDATE +) + // Create func Create(name string, email string, expires string, callback string, apiProducts []string, scopes []string, attrs map[string]string) (respBody []byte, err error) { + return createOrUpdate(name, email, expires, callback, apiProducts, scopes, attrs, CREATE) +} + +// Delete +func Delete(name string, developerID string) (respBody []byte, err error) { + u, _ := url.Parse(apiclient.BaseURL) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", developerID, "apps", name) + respBody, err = apiclient.HttpClient(u.String(), "", "DELETE") + return respBody, err +} + +// Get +func Get(appID string) (respBody []byte, err error) { + u, _ := url.Parse(apiclient.BaseURL) + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "apps", appID) + respBody, err = apiclient.HttpClient(u.String()) + return respBody, err +} + +// Update +func Update(name string, email string, expires string, callback string, apiProducts []string, scopes []string, attrs map[string]string) (respBody []byte, err error) { + return createOrUpdate(name, email, expires, callback, apiProducts, scopes, attrs, UPDATE) +} + +func createOrUpdate(name string, email string, expires string, callback string, apiProducts []string, scopes []string, attrs map[string]string, action Action) (respBody []byte, err error) { u, _ := url.Parse(apiclient.BaseURL) app := []string{} @@ -113,24 +145,15 @@ func Create(name string, email string, expires string, callback string, apiProdu } payload := "{" + strings.Join(app, ",") + "}" - u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", email, "apps") - respBody, err = apiclient.HttpClient(u.String(), payload) - return respBody, err -} -// Delete -func Delete(name string, developerID string) (respBody []byte, err error) { - u, _ := url.Parse(apiclient.BaseURL) - u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", developerID, "apps", name) - respBody, err = apiclient.HttpClient(u.String(), "", "DELETE") - return respBody, err -} + if action == CREATE { + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", email, "apps") + respBody, err = apiclient.HttpClient(u.String(), payload) + } else { + u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", email, "apps", name) + respBody, err = apiclient.HttpClient(u.String(), payload, "PUT") + } -// Get -func Get(appID string) (respBody []byte, err error) { - u, _ := url.Parse(apiclient.BaseURL) - u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "apps", appID) - respBody, err = apiclient.HttpClient(u.String()) return respBody, err }