diff --git a/internal/client/hub/hub.go b/internal/client/hub/hub.go index 73f4990cd..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, + 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) + 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, + 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) + payload, err := getExternalApi(displayName, description, endpoints, paths, externalUri, apiType, allowedValueID) 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, allowedValueID 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 := getAttributeAllowedValue(attribute, allowedValueID) + 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,39 @@ func getSpecIDList(s []byte) (sList []string) { return sList } + +func getAttributeAllowedValue(attributeID string, allowedValueID string) (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 + } + + 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/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 3decd00c8..e3bb6a15d 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, allowedValueID) return }, } var ( - externalApiID, displayName, description, externalURI, resourceURI string - endpoints, paths []string + externalApiID, displayName, description, externalURI, resourceURI, attribute, allowedValueID string + endpoints, paths []string ) func init() { @@ -52,12 +53,16 @@ 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") + CrtCmd.Flags().StringVarP(&allowedValueID, "attribute-allowed-value-id", "", + "", "The allowed value id for the attribute") + 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..04a39b980 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, allowedValueID) return }, } @@ -47,7 +48,11 @@ 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", "", + "", "The allowed value id for the attribute") 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..bd607ff0d --- /dev/null +++ b/test/allowed-values.json @@ -0,0 +1,8 @@ +[ + { + "id": "test", + "displayName": "test", + "description": "test", + "immutable": false + } +]