diff --git a/internal/client/apps/apps.go b/internal/client/apps/apps.go index 0f492422..92928464 100644 --- a/internal/client/apps/apps.go +++ b/internal/client/apps/apps.go @@ -261,6 +261,23 @@ func ListApps(productName string) (respBody []byte, err error) { func GenerateKey(name string, developerID string, apiProducts []string, callback string, expires string, scopes []string) (respBody []byte, err error) { u, _ := url.Parse(apiclient.GetApigeeBaseURL()) + // Fetch the existing app to preserve its attributes; the Apigee POST + // endpoint for key generation also updates the app, clearing any fields + // absent from the request body. + apiclient.ClientPrintHttpResponse.Set(false) + appURL, _ := url.Parse(apiclient.GetApigeeBaseURL()) + appURL.Path = path.Join(appURL.Path, apiclient.GetApigeeOrg(), "developers", developerID, "apps", name) + existingAppBody, err := apiclient.HttpClient(appURL.String()) + apiclient.ClientPrintHttpResponse.Set(apiclient.GetCmdPrintHttpResponseSetting()) + if err != nil { + return nil, err + } + + var existingApp application + if err = json.Unmarshal(existingAppBody, &existingApp); err != nil { + return nil, err + } + key := []string{} key = append(key, "\"name\":\""+name+"\"") @@ -278,6 +295,14 @@ func GenerateKey(name string, developerID string, apiProducts []string, callback key = append(key, "\"scopes\":[\""+getArrayStr(scopes)+"\"]") } + if len(existingApp.Attributes) > 0 { + attrBytes, err := json.Marshal(existingApp.Attributes) + if err != nil { + return nil, err + } + key = append(key, "\"attributes\":"+string(attrBytes)) + } + payload := "{" + strings.Join(key, ",") + "}" u.Path = path.Join(u.Path, apiclient.GetApigeeOrg(), "developers", developerID, "apps", name) respBody, err = apiclient.HttpClient(u.String(), payload) diff --git a/internal/client/apps/apps_test.go b/internal/client/apps/apps_test.go index fb4954f0..b635b6b8 100644 --- a/internal/client/apps/apps_test.go +++ b/internal/client/apps/apps_test.go @@ -131,9 +131,19 @@ func TestGenerateKey(t *testing.T) { expires := "-1" callback := "" scopes := []string{"test"} - if _, err := GenerateKey(name, devID, apiProducts, callback, expires, scopes); err != nil { + respBody, err := GenerateKey(name, devID, apiProducts, callback, expires, scopes) + if err != nil { t.Fatalf("%v", err) } + + var respJSONMap map[string]interface{} + if err = json.Unmarshal(respBody, &respJSONMap); err != nil { + t.Fatalf("%v", err) + } + attrs, ok := respJSONMap["attributes"].([]interface{}) + if !ok || len(attrs) == 0 { + t.Fatalf("expected app attributes to be preserved after genkey, got none") + } } func TestExport(t *testing.T) { diff --git a/internal/client/apps/keys_test.go b/internal/client/apps/keys_test.go index 527282f2..fbc04bfc 100644 --- a/internal/client/apps/keys_test.go +++ b/internal/client/apps/keys_test.go @@ -52,7 +52,7 @@ func TestManageKey(t *testing.T) { clienttest.SITEID_NOT_REQD, clienttest.CLIPATH_NOT_REQD); err != nil { t.Fatalf("%v", err) } - if _, err := ManageKey(name, appID, "key1", "approve"); err != nil { + if _, err := ManageKey(name, appID, "key1", "approve", ""); err != nil { t.Fatalf("%v", err) } }