Skip to content

Commit 3ce7140

Browse files
authored
Merge pull request #3707 from dougm/issue-3706
api: Add optional CategoryID,TagID fields for category and tag creation
2 parents b154c78 + 87e9b3e commit 3ce7140

File tree

7 files changed

+56
-62
lines changed

7 files changed

+56
-62
lines changed

cli/tags/category/create.go

100755100644
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
/*
2-
Copyright (c) 2018 VMware, Inc. All Rights Reserved.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package category
186

@@ -39,6 +27,7 @@ func init() {
3927
func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
4028
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
4129
cmd.ClientFlag.Register(ctx, f)
30+
f.StringVar(&cmd.cat.CategoryID, "id", "", "Category ID")
4231
f.StringVar(&cmd.cat.Description, "d", "", "Description")
4332
f.Var((*kinds)(&cmd.cat.AssociableTypes), "t", "Object types")
4433
f.BoolVar(&cmd.multi, "m", false, "Allow multiple tags per object")

cli/tags/create.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
/*
2-
Copyright (c) 2018 VMware, Inc. All Rights Reserved.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package tags
186

@@ -38,6 +26,7 @@ func init() {
3826
func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
3927
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
4028
cmd.ClientFlag.Register(ctx, f)
29+
f.StringVar(&cmd.tag.TagID, "id", "", "Tag ID")
4130
f.StringVar(&cmd.tag.CategoryID, "c", "", "Category name")
4231
f.StringVar(&cmd.tag.Description, "d", "", "Description of tag")
4332
}

govc/USAGE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6117,6 +6117,7 @@ Examples:
61176117
61186118
Options:
61196119
-d= Description
6120+
-id= Category ID
61206121
-m=false Allow multiple tags per object
61216122
-t=[] Object types
61226123
```
@@ -6205,6 +6206,7 @@ Examples:
62056206
Options:
62066207
-c= Category name
62076208
-d= Description of tag
6209+
-id= Tag ID
62086210
```
62096211

62106212
## tags.detach

govc/test/tags.bats

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,16 @@ load test_helper
5151

5252
run govc tags.category.rm "$update_name"
5353
assert_success
54+
55+
run govc tags.category.create -id "invalid" custom-id-cat
56+
assert_failure
57+
58+
id="urn:vmomi:InventoryServiceCategory:$(new_id):GLOBAL"
59+
run govc tags.category.create -id "$id" custom-id-cat
60+
assert_success
61+
62+
run govc tags.category.info "$id"
63+
assert_success
5464
}
5565

5666
@test "tags" {
@@ -117,6 +127,16 @@ load test_helper
117127

118128
run govc tags.info enoent
119129
assert_failure # does not exist
130+
131+
run govc tags.category.create -c "$category_name" -id "invalid" custom-id-tag
132+
assert_failure
133+
134+
id="urn:vmomi:InventoryServiceTag:$(new_id):GLOBAL"
135+
run govc tags.create -c "$category_name" -id "$id" custom-id-tag
136+
assert_success
137+
138+
run govc tags.info "$id"
139+
assert_success
120140
}
121141

122142
@test "tags.association" {

vapi/simulator/simulator.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,13 @@ func (s *handler) category(w http.ResponseWriter, r *http.Request) {
595595
return
596596
}
597597
}
598-
id := newID("Category")
598+
id := spec.Category.CategoryID
599+
if id == "" {
600+
id = newID("Category")
601+
} else if !strings.HasPrefix(id, "urn:vmomi:InventoryServiceCategory:") {
602+
BadRequest(w, "com.vmware.vapi.std.errors.invalid_argument")
603+
return
604+
}
599605
spec.Category.ID = id
600606
s.Category[id] = &spec.Category
601607
OK(w, id)
@@ -668,7 +674,13 @@ func (s *handler) tag(w http.ResponseWriter, r *http.Request) {
668674
return
669675
}
670676
}
671-
id := newID("Tag")
677+
id := spec.Tag.TagID
678+
if id == "" {
679+
id = newID("Tag")
680+
} else if !strings.HasPrefix(id, "urn:vmomi:InventoryServiceTag:") {
681+
BadRequest(w, "com.vmware.vapi.std.errors.invalid_argument")
682+
return
683+
}
672684
spec.Tag.ID = id
673685
s.Tag[id] = &spec.Tag
674686
s.Association[id] = make(map[internal.AssociatedObject]bool)

vapi/tags/categories.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
/*
2-
Copyright (c) 2018 VMware, Inc. All Rights Reserved.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package tags
186

@@ -34,6 +22,7 @@ type Category struct {
3422
Cardinality string `json:"cardinality,omitempty"`
3523
AssociableTypes []string `json:"associable_types,omitempty"`
3624
UsedBy []string `json:"used_by,omitempty"`
25+
CategoryID string `json:"category_id,omitempty"`
3726
}
3827

3928
func (c *Category) hasType(kind string) bool {
@@ -74,6 +63,7 @@ func (c *Manager) CreateCategory(ctx context.Context, category *Category) (strin
7463
Description string `json:"description"`
7564
Cardinality string `json:"cardinality"`
7665
AssociableTypes []string `json:"associable_types"`
66+
CategoryID string `json:"category_id,omitempty"`
7767
}
7868
spec := struct {
7969
Category create `json:"create_spec"`
@@ -83,6 +73,7 @@ func (c *Manager) CreateCategory(ctx context.Context, category *Category) (strin
8373
Description: category.Description,
8474
Cardinality: category.Cardinality,
8575
AssociableTypes: category.AssociableTypes,
76+
CategoryID: category.CategoryID,
8677
},
8778
}
8879
if spec.Category.AssociableTypes == nil {

vapi/tags/tags.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
1-
/*
2-
Copyright (c) 2018 VMware, Inc. All Rights Reserved.
3-
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
7-
8-
http://www.apache.org/licenses/LICENSE-2.0
9-
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
15-
*/
1+
// © Broadcom. All Rights Reserved.
2+
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
3+
// SPDX-License-Identifier: Apache-2.0
164

175
package tags
186

@@ -50,6 +38,7 @@ type Tag struct {
5038
Name string `json:"name,omitempty"`
5139
CategoryID string `json:"category_id,omitempty"`
5240
UsedBy []string `json:"used_by,omitempty"`
41+
TagID string `json:"tag_id,omitempty"`
5342
}
5443

5544
// Patch merges updates from the given src.
@@ -73,6 +62,7 @@ func (c *Manager) CreateTag(ctx context.Context, tag *Tag) (string, error) {
7362
Name string `json:"name"`
7463
Description string `json:"description"`
7564
CategoryID string `json:"category_id"`
65+
TagID string `json:"tag_id,omitempty"`
7666
}
7767
spec := struct {
7868
Tag create `json:"create_spec"`
@@ -81,6 +71,7 @@ func (c *Manager) CreateTag(ctx context.Context, tag *Tag) (string, error) {
8171
Name: tag.Name,
8272
Description: tag.Description,
8373
CategoryID: tag.CategoryID,
74+
TagID: tag.TagID,
8475
},
8576
}
8677
if isName(tag.CategoryID) {

0 commit comments

Comments
 (0)