-
Notifications
You must be signed in to change notification settings - Fork 920
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tags Categories cmd available (#1150)
Add tags.category create, get, ls, rm and update commands
- Loading branch information
Showing
8 changed files
with
917 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
Copyright (c) 2018 VMware, Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package category | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
|
||
"github.com/vmware/govmomi/govc/cli" | ||
"github.com/vmware/govmomi/govc/flags" | ||
"github.com/vmware/govmomi/vapi/tags" | ||
) | ||
|
||
type create struct { | ||
*flags.ClientFlag | ||
description string | ||
types string | ||
multi bool | ||
} | ||
|
||
func init() { | ||
cli.Register("tags.category.create", &create{}) | ||
} | ||
|
||
func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) { | ||
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) | ||
cmd.ClientFlag.Register(ctx, f) | ||
f.StringVar(&cmd.description, "d", "", "Description of category") | ||
f.StringVar(&cmd.types, "t", "", "Associable_types of category") | ||
f.BoolVar(&cmd.multi, "m", false, "Cardinality of category") | ||
} | ||
|
||
func (cmd *create) Process(ctx context.Context) error { | ||
if err := cmd.ClientFlag.Process(ctx); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (cmd *create) Usage() string { | ||
return "NAME" | ||
} | ||
|
||
func (cmd *create) Description() string { | ||
return ` Create tag category. | ||
This command will output the ID you just created. | ||
Examples: | ||
govc tags.category.create -d "description" -t "categoryType" -m(if set, the cardinality will be true) NAME` | ||
} | ||
|
||
func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error { | ||
if f.NArg() != 1 { | ||
return flag.ErrHelp | ||
} | ||
|
||
name := f.Arg(0) | ||
|
||
return withClient(ctx, cmd.ClientFlag, func(c *tags.RestClient) error { | ||
|
||
id, err := c.CreateCategoryIfNotExist(ctx, name, cmd.description, cmd.types, cmd.multi) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println(*id) | ||
return nil | ||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright (c) 2018 VMware, Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package category | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/vmware/govmomi/govc/cli" | ||
"github.com/vmware/govmomi/govc/flags" | ||
"github.com/vmware/govmomi/vapi/tags" | ||
) | ||
|
||
type get struct { | ||
*flags.ClientFlag | ||
*flags.OutputFlag | ||
} | ||
|
||
type getName []tags.Category | ||
|
||
func init() { | ||
cli.Register("tags.category.get", &get{}) | ||
} | ||
|
||
func (cmd *get) Register(ctx context.Context, f *flag.FlagSet) { | ||
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) | ||
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) | ||
cmd.ClientFlag.Register(ctx, f) | ||
cmd.OutputFlag.Register(ctx, f) | ||
} | ||
|
||
func (cmd *get) Process(ctx context.Context) error { | ||
if err := cmd.ClientFlag.Process(ctx); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (cmd *get) Usage() string { | ||
return "NAME" | ||
} | ||
|
||
func (cmd *get) Description() string { | ||
return `Get category by name. | ||
Will return empty if category name doesn't exist. | ||
Examples: | ||
govc tags.category.get NAME` | ||
} | ||
|
||
func (r getName) Write(w io.Writer) error { | ||
for i := range r { | ||
fmt.Fprintln(w, r[i]) | ||
} | ||
return nil | ||
} | ||
|
||
func (cmd *get) Run(ctx context.Context, f *flag.FlagSet) error { | ||
if f.NArg() != 1 { | ||
return flag.ErrHelp | ||
} | ||
name := f.Arg(0) | ||
|
||
return withClient(ctx, cmd.ClientFlag, func(c *tags.RestClient) error { | ||
categoriesName, err := c.GetCategoriesByName(ctx, name) | ||
if err != nil { | ||
return err | ||
} | ||
result := getName(categoriesName) | ||
cmd.WriteResult(result) | ||
return nil | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
Copyright (c) 2018 VMware, Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package category | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/vmware/govmomi/govc/cli" | ||
"github.com/vmware/govmomi/govc/flags" | ||
"github.com/vmware/govmomi/vapi/tags" | ||
) | ||
|
||
type ls struct { | ||
*flags.ClientFlag | ||
*flags.OutputFlag | ||
} | ||
|
||
func init() { | ||
cli.Register("tags.category.ls", &ls{}) | ||
} | ||
|
||
func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) { | ||
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) | ||
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) | ||
cmd.ClientFlag.Register(ctx, f) | ||
cmd.OutputFlag.Register(ctx, f) | ||
} | ||
|
||
func (cmd *ls) Process(ctx context.Context) error { | ||
if err := cmd.ClientFlag.Process(ctx); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (cmd *ls) Description() string { | ||
return `List all categories. | ||
Examples: | ||
govc tags.category.ls` | ||
} | ||
|
||
func withClient(ctx context.Context, cmd *flags.ClientFlag, f func(*tags.RestClient) error) error { | ||
vc, err := cmd.Client() | ||
if err != nil { | ||
return err | ||
} | ||
tagsURL := vc.URL() | ||
tagsURL.User = cmd.Userinfo() | ||
|
||
c := tags.NewClient(tagsURL, !cmd.IsSecure(), "") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err = c.Login(ctx); err != nil { | ||
return err | ||
} | ||
defer c.Logout(ctx) | ||
|
||
return f(c) | ||
} | ||
|
||
type getResult []string | ||
|
||
func (r getResult) Write(w io.Writer) error { | ||
for i := range r { | ||
fmt.Fprintln(w, r[i]) | ||
} | ||
return nil | ||
} | ||
|
||
func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error { | ||
|
||
return withClient(ctx, cmd.ClientFlag, func(c *tags.RestClient) error { | ||
categories, err := c.ListCategories(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
result := getResult(categories) | ||
cmd.WriteResult(result) | ||
return nil | ||
|
||
}) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright (c) 2018 VMware, Inc. All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package category | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
|
||
"github.com/vmware/govmomi/govc/cli" | ||
"github.com/vmware/govmomi/govc/flags" | ||
"github.com/vmware/govmomi/vapi/tags" | ||
) | ||
|
||
type rm struct { | ||
*flags.ClientFlag | ||
} | ||
|
||
func init() { | ||
cli.Register("tags.category.rm", &rm{}) | ||
} | ||
|
||
func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) { | ||
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) | ||
cmd.ClientFlag.Register(ctx, f) | ||
} | ||
|
||
func (cmd *rm) Process(ctx context.Context) error { | ||
if err := cmd.ClientFlag.Process(ctx); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func (cmd *rm) Usage() string { | ||
return "ID" | ||
} | ||
|
||
func (cmd *rm) Description() string { | ||
return `Delete category. | ||
Examples: | ||
govc tags.category.rm ID` | ||
} | ||
|
||
func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error { | ||
if f.NArg() != 1 { | ||
return flag.ErrHelp | ||
} | ||
categoryID := f.Arg(0) | ||
|
||
return withClient(ctx, cmd.ClientFlag, func(c *tags.RestClient) error { | ||
return c.DeleteCategory(ctx, categoryID) | ||
}) | ||
} |
Oops, something went wrong.