-
Notifications
You must be signed in to change notification settings - Fork 919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Tags Categories cmd available #1150
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0476915
categories cmd full version
f4f8ae6
remove errors pkg
6b1ea2b
fixed printf bugs
4d8b799
tags category apis
ec5f540
delete useless file
6211bca
Add Description() method and and rename category package and files
44ba72e
Add Logout() method to RestCilent type as a receiver function
f033846
Delete .DS_Store files
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These two lines (vc.URL()) work in version 6.7, but will fail in version 6.5