From 24af8a7cb59c8cfc65a67cafbb0a57d195be7354 Mon Sep 17 00:00:00 2001 From: srinandan Date: Fri, 6 Sep 2024 21:19:04 +0000 Subject: [PATCH 1/5] feat: adds attributes to ext apis #532 --- internal/client/hub/hub.go | 64 ++++++++++++++++++---- internal/cmd/apihub/externalapis/create.go | 13 +++-- internal/cmd/apihub/externalapis/update.go | 5 +- test/allowed-values.json | 8 +++ 4 files changed, 74 insertions(+), 16 deletions(-) create mode 100644 test/allowed-values.json diff --git a/internal/client/hub/hub.go b/internal/client/hub/hub.go index 73f4990cd..e1dc42653 100644 --- a/internal/client/hub/hub.go +++ b/internal/client/hub/hub.go @@ -801,7 +801,7 @@ func getDeployment(displayName string, description string, deploymentName string } func CreateExternalAPI(externalApiID string, displayName string, description string, - endpoints []string, paths []string, externalUri string, + endpoints []string, paths []string, externalUri string, attribute string, ) (respBody []byte, err error) { u, _ := url.Parse(apiclient.GetApigeeRegistryURL()) u.Path = path.Join(u.Path, "externalApis") @@ -809,7 +809,7 @@ func CreateExternalAPI(externalApiID string, displayName string, description str q.Set("externalApiId", externalApiID) u.RawQuery = q.Encode() - payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri) + payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri, attribute) if err != nil { return nil, err } @@ -836,7 +836,7 @@ func ListExternalAPIs(filter string, pageSize int, pageToken string) (respBody [ } func UpdateExternalAPI(externalApiID string, displayName string, description string, - endpoints []string, paths []string, externalUri string, + endpoints []string, paths []string, externalUri string, apiType string, ) (respBody []byte, err error) { updateMask := []string{} @@ -865,7 +865,7 @@ func UpdateExternalAPI(externalApiID string, displayName string, description str q.Set("updateMask", strings.Join(updateMask, ",")) u.RawQuery = q.Encode() - payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri) + payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri, apiType) if err != nil { return nil, err } @@ -875,18 +875,19 @@ func UpdateExternalAPI(externalApiID string, displayName string, description str } func getExternalApi(displayName string, description string, - endpoints []string, paths []string, externalUri string, + endpoints []string, paths []string, externalUri string, attribute string, ) (string, error) { type documentation struct { ExternalURI string `json:"externalUri,omitempty"` } type extapi struct { - DisplayName string `json:"displayName,omitempty"` - Description string `json:"description,omitempty"` - Documentation documentation `json:"documentation,omitempty"` - Paths []string `json:"paths,omitempty"` - Endpoints []string `json:"endpoints,omitempty"` + DisplayName string `json:"displayName,omitempty"` + Description string `json:"description,omitempty"` + Documentation documentation `json:"documentation,omitempty"` + Paths []string `json:"paths,omitempty"` + Endpoints []string `json:"endpoints,omitempty"` + Attributes map[string]interface{} `json:"attributes,omitempty"` } e := extapi{} if displayName != "" { @@ -905,6 +906,19 @@ func getExternalApi(displayName string, description string, e.Endpoints = endpoints } + a, err := getAttributeAllowedlue(attribute, 0) + if err != nil { + return "", err + } + + eAV := enumAttributeValue{} + eAV.EnumValues.Values = make([]allowedValue, 1) + eAV.EnumValues.Values[0] = a + + e.Attributes = make(map[string]interface{}) + n := fmt.Sprintf("projects/%s/locations/%s/attributes/%s", apiclient.GetApigeeOrg(), apiclient.GetRegion(), attribute) + e.Attributes[n] = eAV + payload, err := json.Marshal(&e) if err != nil { return "", err @@ -1290,3 +1304,33 @@ func getSpecIDList(s []byte) (sList []string) { return sList } + +func getAttributeAllowedlue(attributeID string, index int) (allowedValue, error) { + type attribute struct { + Name string `json:"name,omitempty"` + DisplayName string `json:"displayName,omitempty"` + DefinitionType string `json:"definitionType,omitempty"` + Scope string `json:"scope,omitempty"` + DataType string `json:"dataType,omitempty"` + Cardinality int `json:"cardinality,omitempty"` + AllowedValues []allowedValue `json:"allowedValues,omitempty"` + Mandatory bool `json:"mandatory,omitempty"` + CreateTime string `json:"createTime,omitempty"` + UpdateTime string `json:"updateTime,omitempty"` + } + + apiclient.ClientPrintHttpResponse.Set(false) + r, err := GetAttribute(attributeID) + apiclient.ClientPrintHttpResponse.Set(true) + if err != nil { + return allowedValue{}, err + } + + a := attribute{} + err = json.Unmarshal(r, &a) + if err != nil { + return allowedValue{}, err + } + + return a.AllowedValues[index], nil +} diff --git a/internal/cmd/apihub/externalapis/create.go b/internal/cmd/apihub/externalapis/create.go index 3decd00c8..536bfea71 100644 --- a/internal/cmd/apihub/externalapis/create.go +++ b/internal/cmd/apihub/externalapis/create.go @@ -32,14 +32,15 @@ var CrtCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) (err error) { cmd.SilenceUsage = true - _, err = hub.CreateExternalAPI(externalApiID, displayName, description, endpoints, paths, externalURI) + _, err = hub.CreateExternalAPI(externalApiID, displayName, description, + endpoints, paths, externalURI, attribute) return }, } var ( - externalApiID, displayName, description, externalURI, resourceURI string - endpoints, paths []string + externalApiID, displayName, description, externalURI, resourceURI, attribute string + endpoints, paths []string ) func init() { @@ -53,11 +54,13 @@ func init() { "", "The uri of the externally hosted documentation") CrtCmd.Flags().StringArrayVarP(&endpoints, "endpoints", "", []string{}, " The endpoints at which this deployment resource is listening for API requests") + + CrtCmd.Flags().StringVarP(&attribute, "attribute", "", + "", "The name of the attribute for the external api") + CrtCmd.Flags().StringArrayVarP(&paths, "paths", "", []string{}, " API base paths") _ = CrtCmd.MarkFlagRequired("id") _ = CrtCmd.MarkFlagRequired("display-name") - _ = CrtCmd.MarkFlagRequired("resource-uri") - _ = CrtCmd.MarkFlagRequired("endpoints") } diff --git a/internal/cmd/apihub/externalapis/update.go b/internal/cmd/apihub/externalapis/update.go index fd13dab8f..e7e3dd29f 100644 --- a/internal/cmd/apihub/externalapis/update.go +++ b/internal/cmd/apihub/externalapis/update.go @@ -32,7 +32,8 @@ var UpdateCmd = &cobra.Command{ }, RunE: func(cmd *cobra.Command, args []string) (err error) { cmd.SilenceUsage = true - _, err = hub.UpdateExternalAPI(externalApiID, displayName, description, endpoints, paths, externalURI) + _, err = hub.UpdateExternalAPI(externalApiID, displayName, + description, endpoints, paths, externalURI, attribute) return }, } @@ -48,6 +49,8 @@ func init() { "", "The uri of the externally hosted documentation") UpdateCmd.Flags().StringArrayVarP(&endpoints, "endpoints", "", []string{}, " The endpoints at which this deployment resource is listening for API requests") + UpdateCmd.Flags().StringVarP(&attribute, "attribute", "", + "", "The name of the attribute for the external api") UpdateCmd.Flags().StringArrayVarP(&paths, "paths", "", []string{}, " API base paths") diff --git a/test/allowed-values.json b/test/allowed-values.json new file mode 100644 index 000000000..48ba1df43 --- /dev/null +++ b/test/allowed-values.json @@ -0,0 +1,8 @@ +[ + { + "id": "nandan", + "displayName": "nandan", + "description": "nandan", + "immutable": false + } +] From 6317d19eca35054fc458b9700b00aac9d5729d94 Mon Sep 17 00:00:00 2001 From: srinandan Date: Fri, 6 Sep 2024 21:21:37 +0000 Subject: [PATCH 2/5] chore: fix test #532 --- test/allowed-values.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/allowed-values.json b/test/allowed-values.json index 48ba1df43..bd607ff0d 100644 --- a/test/allowed-values.json +++ b/test/allowed-values.json @@ -1,8 +1,8 @@ [ { - "id": "nandan", - "displayName": "nandan", - "description": "nandan", + "id": "test", + "displayName": "test", + "description": "test", "immutable": false } ] From c75b324f610022872df8146a5e7d590c30e0e006 Mon Sep 17 00:00:00 2001 From: srinandan Date: Fri, 13 Sep 2024 14:53:49 +0000 Subject: [PATCH 3/5] bug: fixes method name #532 --- internal/client/hub/hub.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/client/hub/hub.go b/internal/client/hub/hub.go index e1dc42653..72ea2a133 100644 --- a/internal/client/hub/hub.go +++ b/internal/client/hub/hub.go @@ -906,7 +906,7 @@ func getExternalApi(displayName string, description string, e.Endpoints = endpoints } - a, err := getAttributeAllowedlue(attribute, 0) + a, err := getAttributeAllowedValue(attribute, 0) if err != nil { return "", err } @@ -1305,7 +1305,7 @@ func getSpecIDList(s []byte) (sList []string) { return sList } -func getAttributeAllowedlue(attributeID string, index int) (allowedValue, error) { +func getAttributeAllowedValue(attributeID string, index int) (allowedValue, error) { type attribute struct { Name string `json:"name,omitempty"` DisplayName string `json:"displayName,omitempty"` From 139b817931a1bc8d17407b4323e91180c8fbab43 Mon Sep 17 00:00:00 2001 From: srinandan Date: Mon, 16 Sep 2024 17:44:39 +0000 Subject: [PATCH 4/5] feat: accept allowed value id from user #532 --- internal/client/hub/hub.go | 22 ++++++++++++++-------- internal/cmd/apihub/externalapis/create.go | 8 +++++--- internal/cmd/apihub/externalapis/update.go | 4 +++- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/internal/client/hub/hub.go b/internal/client/hub/hub.go index 72ea2a133..9555922df 100644 --- a/internal/client/hub/hub.go +++ b/internal/client/hub/hub.go @@ -801,7 +801,7 @@ func getDeployment(displayName string, description string, deploymentName string } func CreateExternalAPI(externalApiID string, displayName string, description string, - endpoints []string, paths []string, externalUri string, attribute string, + endpoints []string, paths []string, externalUri string, attribute string, allowedValueID string, ) (respBody []byte, err error) { u, _ := url.Parse(apiclient.GetApigeeRegistryURL()) u.Path = path.Join(u.Path, "externalApis") @@ -809,7 +809,7 @@ func CreateExternalAPI(externalApiID string, displayName string, description str q.Set("externalApiId", externalApiID) u.RawQuery = q.Encode() - payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri, attribute) + payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri, attribute, allowedValueID) if err != nil { return nil, err } @@ -836,7 +836,7 @@ func ListExternalAPIs(filter string, pageSize int, pageToken string) (respBody [ } func UpdateExternalAPI(externalApiID string, displayName string, description string, - endpoints []string, paths []string, externalUri string, apiType string, + endpoints []string, paths []string, externalUri string, apiType string, allowedValueID string, ) (respBody []byte, err error) { updateMask := []string{} @@ -865,7 +865,7 @@ func UpdateExternalAPI(externalApiID string, displayName string, description str q.Set("updateMask", strings.Join(updateMask, ",")) u.RawQuery = q.Encode() - payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri, apiType) + payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri, apiType, allowedValueID) if err != nil { return nil, err } @@ -875,7 +875,7 @@ func UpdateExternalAPI(externalApiID string, displayName string, description str } func getExternalApi(displayName string, description string, - endpoints []string, paths []string, externalUri string, attribute string, + endpoints []string, paths []string, externalUri string, attribute string, allowedValueID string, ) (string, error) { type documentation struct { ExternalURI string `json:"externalUri,omitempty"` @@ -906,7 +906,7 @@ func getExternalApi(displayName string, description string, e.Endpoints = endpoints } - a, err := getAttributeAllowedValue(attribute, 0) + a, err := getAttributeAllowedValue(attribute, allowedValueID) if err != nil { return "", err } @@ -1305,7 +1305,7 @@ func getSpecIDList(s []byte) (sList []string) { return sList } -func getAttributeAllowedValue(attributeID string, index int) (allowedValue, error) { +func getAttributeAllowedValue(attributeID string, allowedValueID string) (allowedValue, error) { type attribute struct { Name string `json:"name,omitempty"` DisplayName string `json:"displayName,omitempty"` @@ -1332,5 +1332,11 @@ func getAttributeAllowedValue(attributeID string, index int) (allowedValue, erro return allowedValue{}, err } - return a.AllowedValues[index], nil + for _, i := range a.AllowedValues { + if i.Id == allowedValueID { + return i, nil + } + } + + return allowedValue{}, fmt.Errorf("allowedValue not found") } diff --git a/internal/cmd/apihub/externalapis/create.go b/internal/cmd/apihub/externalapis/create.go index 536bfea71..106320d3c 100644 --- a/internal/cmd/apihub/externalapis/create.go +++ b/internal/cmd/apihub/externalapis/create.go @@ -33,14 +33,14 @@ var CrtCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { cmd.SilenceUsage = true _, err = hub.CreateExternalAPI(externalApiID, displayName, description, - endpoints, paths, externalURI, attribute) + endpoints, paths, externalURI, attribute, allowedValueID) return }, } var ( - externalApiID, displayName, description, externalURI, resourceURI, attribute string - endpoints, paths []string + externalApiID, displayName, description, externalURI, resourceURI, attribute, allowedValueID string + endpoints, paths []string ) func init() { @@ -57,6 +57,8 @@ func init() { CrtCmd.Flags().StringVarP(&attribute, "attribute", "", "", "The name of the attribute for the external api") + CrtCmd.Flags().StringVarP(&allowedValueID, "attribute-allowed-value-id", "", + "", "The allowed value id for the attribute") CrtCmd.Flags().StringArrayVarP(&paths, "paths", "", []string{}, " API base paths") diff --git a/internal/cmd/apihub/externalapis/update.go b/internal/cmd/apihub/externalapis/update.go index e7e3dd29f..f3159704a 100644 --- a/internal/cmd/apihub/externalapis/update.go +++ b/internal/cmd/apihub/externalapis/update.go @@ -33,7 +33,7 @@ var UpdateCmd = &cobra.Command{ RunE: func(cmd *cobra.Command, args []string) (err error) { cmd.SilenceUsage = true _, err = hub.UpdateExternalAPI(externalApiID, displayName, - description, endpoints, paths, externalURI, attribute) + description, endpoints, paths, externalURI, attribute, allowedValueID) return }, } @@ -51,6 +51,8 @@ func init() { []string{}, " The endpoints at which this deployment resource is listening for API requests") UpdateCmd.Flags().StringVarP(&attribute, "attribute", "", "", "The name of the attribute for the external api") + UpdateCmd.Flags().StringVarP(&allowedValueID, "attribute-allowed-value-id", "", + "", "The allowed value id for the attribute") UpdateCmd.Flags().StringArrayVarP(&paths, "paths", "", []string{}, " API base paths") From 00e04ececa65a4aff2385b14c7591db67f3cd31f Mon Sep 17 00:00:00 2001 From: Sai Saran Vaidyanathan Date: Mon, 16 Sep 2024 10:51:47 -0700 Subject: [PATCH 5/5] fix: spacing issue --- internal/cmd/apihub/deployments/create.go | 2 +- internal/cmd/apihub/deployments/update.go | 2 +- internal/cmd/apihub/externalapis/create.go | 2 +- internal/cmd/apihub/externalapis/update.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/cmd/apihub/deployments/create.go b/internal/cmd/apihub/deployments/create.go index 98bfac487..ccb23a984 100644 --- a/internal/cmd/apihub/deployments/create.go +++ b/internal/cmd/apihub/deployments/create.go @@ -60,7 +60,7 @@ func init() { CrtCmd.Flags().StringVarP(&resourceURI, "resource-uri", "", "", "A URI to the runtime resource") CrtCmd.Flags().StringArrayVarP(&endpoints, "endpoints", "", - []string{}, " The endpoints at which this deployment resource is listening for API requests") + []string{}, "The endpoints at which this deployment resource is listening for API requests") CrtCmd.Flags().Var(&d, "dep-type", "The type of deployment") CrtCmd.Flags().Var(&e, "env-type", "The environment mapping to this deployment") CrtCmd.Flags().Var(&s, "slo-type", "The SLO for this deployment") diff --git a/internal/cmd/apihub/deployments/update.go b/internal/cmd/apihub/deployments/update.go index fc794b21b..aeb052c69 100644 --- a/internal/cmd/apihub/deployments/update.go +++ b/internal/cmd/apihub/deployments/update.go @@ -49,7 +49,7 @@ func init() { UpdateCmd.Flags().StringVarP(&resourceURI, "resource-uri", "", "", "A URI to the runtime resource") UpdateCmd.Flags().StringArrayVarP(&endpoints, "endpoints", "", - []string{}, " The endpoints at which this deployment resource is listening for API requests") + []string{}, "The endpoints at which this deployment resource is listening for API requests") UpdateCmd.Flags().Var(&d, "dep-type", "The type of deployment") UpdateCmd.Flags().Var(&e, "env-type", "The environment mapping to this deployment") UpdateCmd.Flags().Var(&s, "slo-type", "The SLO for this deployment") diff --git a/internal/cmd/apihub/externalapis/create.go b/internal/cmd/apihub/externalapis/create.go index 106320d3c..e3bb6a15d 100644 --- a/internal/cmd/apihub/externalapis/create.go +++ b/internal/cmd/apihub/externalapis/create.go @@ -53,7 +53,7 @@ func init() { CrtCmd.Flags().StringVarP(&externalURI, "external-uri", "", "", "The uri of the externally hosted documentation") CrtCmd.Flags().StringArrayVarP(&endpoints, "endpoints", "", - []string{}, " The endpoints at which this deployment resource is listening for API requests") + []string{}, "The endpoints at which this deployment resource is listening for API requests") CrtCmd.Flags().StringVarP(&attribute, "attribute", "", "", "The name of the attribute for the external api") diff --git a/internal/cmd/apihub/externalapis/update.go b/internal/cmd/apihub/externalapis/update.go index f3159704a..04a39b980 100644 --- a/internal/cmd/apihub/externalapis/update.go +++ b/internal/cmd/apihub/externalapis/update.go @@ -48,7 +48,7 @@ func init() { UpdateCmd.Flags().StringVarP(&externalURI, "external-uri", "", "", "The uri of the externally hosted documentation") UpdateCmd.Flags().StringArrayVarP(&endpoints, "endpoints", "", - []string{}, " The endpoints at which this deployment resource is listening for API requests") + []string{}, "The endpoints at which this deployment resource is listening for API requests") UpdateCmd.Flags().StringVarP(&attribute, "attribute", "", "", "The name of the attribute for the external api") UpdateCmd.Flags().StringVarP(&allowedValueID, "attribute-allowed-value-id", "",