-
Notifications
You must be signed in to change notification settings - Fork 912
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
Changes from 6 commits
0476915
f4f8ae6
6b1ea2b
4d8b799
ec5f540
6211bca
44ba72e
f033846
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
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 tags | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The directory and package should be named |
||
|
||
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The usage strings should all start with an upper case letter, |
||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method should be named |
||
return ` Create tag category. This command will output the ID you just created. | ||
|
||
Examples: | ||
govc tags.category.create -d "description" -t "categoryType" -m(optional, 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 | ||
|
||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
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 tags | ||
|
||
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 | ||
} | ||
|
||
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 `Get tags by tags' name. Will return empty if category name not exist. | ||
Examples: | ||
govc tags.category.get NAME` | ||
} | ||
|
||
type getName []tags.Category | ||
|
||
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(context.Background(), name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the |
||
if err != nil { | ||
return err | ||
} | ||
result := getName(categoriesName) | ||
cmd.WriteResult(result) | ||
return nil | ||
|
||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
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 tags | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
"os" | ||
|
||
"github.com/vmware/govmomi/govc/cli" | ||
"github.com/vmware/govmomi/govc/flags" | ||
"github.com/vmware/govmomi/sts" | ||
"github.com/vmware/govmomi/vapi/tags" | ||
"github.com/vmware/govmomi/vim25/soap" | ||
) | ||
|
||
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) Usage() 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 | ||
} | ||
usrDecode, err := url.QueryUnescape(cmd.Userinfo().String()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead, you can just use: tagsURL := vc.URL()
tagsURL.User = cmd.Userinfo() |
||
if err != nil { | ||
return err | ||
} | ||
|
||
govcUrl := "https://" + usrDecode + "@" + vc.URL().Hostname() | ||
|
||
URL, err := url.Parse(govcUrl) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c := tags.NewClient(URL, true, "") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of hardcoding |
||
if err != nil { | ||
return err | ||
} | ||
|
||
token := os.Getenv("GOVC_LOGIN_TOKEN") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The sso related code here through line 108 can be removed. |
||
header := soap.Header{ | ||
Security: &sts.Signer{ | ||
Certificate: vc.Certificate(), | ||
Token: token, | ||
}, | ||
} | ||
|
||
if token == "" { | ||
tokens, cerr := sts.NewClient(ctx, vc) | ||
if cerr != nil { | ||
return cerr | ||
} | ||
|
||
req := sts.TokenRequest{ | ||
Certificate: vc.Certificate(), | ||
Userinfo: cmd.Userinfo(), | ||
} | ||
|
||
header.Security, cerr = tokens.Issue(ctx, req) | ||
if cerr != nil { | ||
return cerr | ||
} | ||
} | ||
|
||
if err = c.Login(ctx); err != nil { | ||
return err | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a TODO: here, we should have a |
||
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 | ||
|
||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
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 tags | ||
|
||
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 `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) | ||
|
||
}) | ||
} |
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.
The directory should also be renamed to
category