Skip to content

Commit bcd16db

Browse files
Merge pull request #671 from apigee/issue668
feat: adds support to update attributes
2 parents 2c5b846 + b887f39 commit bcd16db

File tree

8 files changed

+191
-37
lines changed

8 files changed

+191
-37
lines changed

internal/client/hub/hub.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,87 @@ func DeleteAttribute(attributeID string) (respBody []byte, err error) {
10071007
return respBody, err
10081008
}
10091009

1010+
func UpdateAttribute(attributeID string, updateMask string, displayName string, description string, scope string,
1011+
dataType string, aValues []byte, cardinality int,
1012+
) (respBody []byte, err error) {
1013+
type attributeScope string
1014+
const (
1015+
API attributeScope = "API"
1016+
VERSION attributeScope = "VERSION"
1017+
SPEC attributeScope = "SPEC"
1018+
API_OPERATION attributeScope = "API_OPERATION"
1019+
DEPLOYMENT attributeScope = "DEPLOYMENT"
1020+
DEPENDENCY attributeScope = "DEPENDENCY"
1021+
DEFINITION attributeScope = "DEFINITION"
1022+
EXTERNAL_API attributeScope = "EXTERNAL_API"
1023+
PLUGIN attributeScope = "PLUGIN"
1024+
)
1025+
1026+
type attributeDataType string
1027+
const (
1028+
ENUM attributeDataType = "ENUM"
1029+
JSON attributeDataType = "JSON"
1030+
STRING attributeDataType = "STRING"
1031+
)
1032+
1033+
type attribute struct {
1034+
DisplayName *string `json:"displayName,omitempty"`
1035+
Description *string `json:"description,omitempty"`
1036+
Scope *attributeScope `json:"scope,omitempty"`
1037+
DataType *attributeDataType `json:"dataType,omitempty"`
1038+
AllowedValues []allowedValue `json:"allowedValues,omitempty"`
1039+
Cardinality *int `json:"cardinality,omitempty"`
1040+
}
1041+
1042+
u, _ := url.Parse(apiclient.GetApigeeRegistryURL())
1043+
u.Path = path.Join(u.Path, "attributes", attributeID)
1044+
q := u.Query()
1045+
q.Set("updateMask", updateMask)
1046+
u.RawQuery = q.Encode()
1047+
1048+
a := attribute{}
1049+
if displayName != "" {
1050+
a.DisplayName = new(string)
1051+
*a.DisplayName = displayName
1052+
}
1053+
if description != "" {
1054+
a.Description = new(string)
1055+
*a.Description = description
1056+
}
1057+
1058+
if scope != "" {
1059+
a.Scope = new(attributeScope)
1060+
*a.Scope = attributeScope(scope)
1061+
}
1062+
1063+
if dataType != "" {
1064+
a.DataType = new(attributeDataType)
1065+
*a.DataType = attributeDataType(dataType)
1066+
}
1067+
1068+
if cardinality != 1 {
1069+
a.Cardinality = new(int)
1070+
*a.Cardinality = cardinality
1071+
}
1072+
1073+
if aValues != nil {
1074+
var av []allowedValue
1075+
err = json.Unmarshal(aValues, &av)
1076+
if err != nil {
1077+
return nil, err
1078+
}
1079+
a.AllowedValues = av
1080+
}
1081+
1082+
payload, err := json.Marshal(&a)
1083+
if err != nil {
1084+
return nil, err
1085+
}
1086+
1087+
respBody, err = apiclient.HttpClient(u.String(), string(payload), "PATCH")
1088+
return respBody, err
1089+
}
1090+
10101091
func ListAttributes(filter string, pageSize int, pageToken string) (respBody []byte, err error) {
10111092
return list("attributes", filter, pageSize, pageToken)
10121093
}

internal/client/spaces/spaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@ func List() (respBody []byte, err error) {
8181
return respBody, err
8282
}
8383

84-
//TODO: Import, Export
84+
// TODO: Import, Export

internal/cmd/apihub/apis/get.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ var GetCmd = &cobra.Command{
3737
},
3838
}
3939

40-
var apiID string
41-
var force bool
40+
var (
41+
apiID string
42+
force bool
43+
)
4244

4345
func init() {
4446
GetCmd.Flags().StringVarP(&apiID, "id", "i",

internal/cmd/apihub/attributes/attributes.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func init() {
3737
AttributeCmd.AddCommand(GetCmd)
3838
AttributeCmd.AddCommand(DelCmd)
3939
AttributeCmd.AddCommand(ListCmd)
40+
AttributeCmd.AddCommand(UpdateCmd)
4041

4142
_ = AttributeCmd.MarkFlagRequired("org")
4243
_ = AttributeCmd.MarkFlagRequired("region")
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package attributes
16+
17+
import (
18+
"internal/apiclient"
19+
"internal/client/hub"
20+
"os"
21+
22+
"github.com/spf13/cobra"
23+
)
24+
25+
// UpdateCmd to update a catalog items
26+
var UpdateCmd = &cobra.Command{
27+
Use: "update",
28+
Short: "Update an Attribute",
29+
Long: "Update an Attribute",
30+
Args: func(cmd *cobra.Command, args []string) (err error) {
31+
apiclient.SetRegion(region)
32+
return apiclient.SetApigeeOrg(org)
33+
},
34+
RunE: func(cmd *cobra.Command, args []string) (err error) {
35+
cmd.SilenceUsage = true
36+
var aValues []byte
37+
38+
if aValuesPath != "" {
39+
if aValues, err = os.ReadFile(aValuesPath); err != nil {
40+
return err
41+
}
42+
}
43+
_, err = hub.UpdateAttribute(attributeID, updateMask, displayName, description, scope, dataType, aValues, cardinality)
44+
return
45+
},
46+
}
47+
48+
var updateMask string
49+
50+
func init() {
51+
UpdateCmd.Flags().StringVarP(&attributeID, "id", "i",
52+
"", "Attribute ID")
53+
UpdateCmd.Flags().StringVarP(&updateMask, "mask", "m",
54+
"", "Update Mask")
55+
UpdateCmd.Flags().StringVarP(&displayName, "display-name", "d",
56+
"", "Attribute Display Name")
57+
UpdateCmd.Flags().StringVarP(&description, "description", "",
58+
"", "Attribute Description")
59+
UpdateCmd.Flags().StringVarP(&scope, "scope", "s",
60+
"", "Attribute scope")
61+
UpdateCmd.Flags().StringVarP(&dataType, "data-type", "",
62+
"", "Attribute data type")
63+
UpdateCmd.Flags().IntVarP(&cardinality, "cardinality", "c",
64+
1, "Attribute cardinality")
65+
UpdateCmd.Flags().StringVarP(&aValuesPath, "allowed-values", "",
66+
"", "Path to a file containing allowed values")
67+
68+
_ = UpdateCmd.MarkFlagRequired("id")
69+
_ = UpdateCmd.MarkFlagRequired("display-name")
70+
_ = UpdateCmd.MarkFlagRequired("scope")
71+
_ = UpdateCmd.MarkFlagRequired("data-type")
72+
_ = UpdateCmd.MarkFlagRequired("mask")
73+
}

internal/cmd/spaces/seteditor.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,3 @@ func init() {
5555
_ = SetEditorCmd.MarkFlagRequired("space")
5656
_ = SetEditorCmd.MarkFlagRequired("name")
5757
}
58-

internal/cmd/spaces/setviewer.go

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,43 @@
1515
package spaces
1616

1717
import (
18-
"internal/apiclient"
19-
"internal/client/spaces"
20-
"internal/clilog"
18+
"internal/apiclient"
19+
"internal/client/spaces"
20+
"internal/clilog"
2121

22-
"github.com/spf13/cobra"
22+
"github.com/spf13/cobra"
2323
)
2424

2525
// SetViewerCmd to set role on env
2626
var SetViewerCmd = &cobra.Command{
27-
Use: "setviewer",
28-
Short: "Set Space Content Viewer role for a member on a Space",
29-
Long: "Set Space Content Viewer role for a member on a Space",
30-
Args: func(cmd *cobra.Command, args []string) (err error) {
31-
apiclient.SetRegion(region)
32-
return apiclient.SetApigeeOrg(org)
33-
},
34-
RunE: func(cmd *cobra.Command, args []string) (err error) {
35-
cmd.SilenceUsage = true
36-
37-
err = spaces.SetIAM(space, memberName, "viewer", memberType)
38-
if err != nil {
39-
return err
40-
}
41-
clilog.Info.Printf("Member \"%s\" granted access to \"roles/apigee.spaceContentViewer\" role in space \"%s\"\n", memberName, space)
42-
return nil
43-
},
44-
Example: `Set Space Viewer role for user in a space: ` + GetExample(3),
27+
Use: "setviewer",
28+
Short: "Set Space Content Viewer role for a member on a Space",
29+
Long: "Set Space Content Viewer role for a member on a Space",
30+
Args: func(cmd *cobra.Command, args []string) (err error) {
31+
apiclient.SetRegion(region)
32+
return apiclient.SetApigeeOrg(org)
33+
},
34+
RunE: func(cmd *cobra.Command, args []string) (err error) {
35+
cmd.SilenceUsage = true
36+
37+
err = spaces.SetIAM(space, memberName, "viewer", memberType)
38+
if err != nil {
39+
return err
40+
}
41+
clilog.Info.Printf("Member \"%s\" granted access to \"roles/apigee.spaceContentViewer\" role in space \"%s\"\n", memberName, space)
42+
return nil
43+
},
44+
Example: `Set Space Viewer role for user in a space: ` + GetExample(3),
4545
}
4646

4747
func init() {
48-
SetViewerCmd.Flags().StringVarP(&space, "space", "",
49-
"", "Space name.")
50-
SetViewerCmd.Flags().StringVarP(&memberName, "name", "n",
51-
"", "Member Name, example Service Account Name")
52-
SetViewerCmd.Flags().StringVarP(&memberType, "member-type", "m",
53-
"serviceAccount", "memberType must be serviceAccount, user or group")
54-
55-
_ = SetViewerCmd.MarkFlagRequired("space")
56-
_ = SetViewerCmd.MarkFlagRequired("name")
48+
SetViewerCmd.Flags().StringVarP(&space, "space", "",
49+
"", "Space name.")
50+
SetViewerCmd.Flags().StringVarP(&memberName, "name", "n",
51+
"", "Member Name, example Service Account Name")
52+
SetViewerCmd.Flags().StringVarP(&memberType, "member-type", "m",
53+
"serviceAccount", "memberType must be serviceAccount, user or group")
54+
55+
_ = SetViewerCmd.MarkFlagRequired("space")
56+
_ = SetViewerCmd.MarkFlagRequired("name")
5757
}
58-

internal/cmd/spaces/testiam.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,4 @@ func init() {
5252
"proxies", "resource")
5353

5454
_ = TestIamCmd.MarkFlagRequired("space")
55-
5655
}

0 commit comments

Comments
 (0)