Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions internal/client/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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+"\"")
Expand All @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion internal/client/apps/apps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion internal/client/apps/keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down