Skip to content

Commit

Permalink
Merge pull request #533 from apigee/issue532
Browse files Browse the repository at this point in the history
feat: adds attributes to ext apis #532
  • Loading branch information
ssvaidyanathan authored Sep 16, 2024
2 parents d201967 + 00e04ec commit 96575cf
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 20 deletions.
70 changes: 60 additions & 10 deletions internal/client/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -801,15 +801,15 @@ 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")
q := u.Query()
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
}
Expand All @@ -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{}

Expand Down Expand Up @@ -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
}
Expand All @@ -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 != "" {
Expand All @@ -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
Expand Down Expand Up @@ -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")
}
2 changes: 1 addition & 1 deletion internal/cmd/apihub/deployments/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/apihub/deployments/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
17 changes: 11 additions & 6 deletions internal/cmd/apihub/externalapis/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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")
}
9 changes: 7 additions & 2 deletions internal/cmd/apihub/externalapis/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
}
Expand All @@ -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")

Expand Down
8 changes: 8 additions & 0 deletions test/allowed-values.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"id": "test",
"displayName": "test",
"description": "test",
"immutable": false
}
]

0 comments on commit 96575cf

Please sign in to comment.