-
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
Conversation
The tags_helper/ are from VIC project |
d55c985
to
f772395
Compare
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.
Looking good @jiatongw
I haven't fully reviewed yet, but here are some initial comments. And a few general comments:
-
New files need the license header
-
Error messages should not be written to stdout (via
fmt.Printf
) - just return the error and add details usingfmt.Errorf
if needed. -
I think
tags.categories
should be renamedtags.category
-
We need to think about a different location for tags_helper - we want this client library to be usable outside of govc
govc/main.go
Outdated
@@ -20,6 +20,8 @@ import ( | |||
"os" | |||
|
|||
"github.com/vmware/govmomi/govc/cli" | |||
_ "github.com/vmware/govmomi/govc/tags/categories" | |||
_ "github.com/vmware/govmomi/govc/tags/tags_helper" |
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.
No need to import tags_helper here.
} | ||
|
||
func (cmd *create) Usage() string { | ||
return ` |
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.
Include a description on the first line of Usage here.
|
||
func (cmd *create) Usage() string { | ||
return ` | ||
Examples: |
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.
Indentation needs to follow the other examples. That is:
-
No spaces for before Examples:
-
Two spaces before govc ...
return flag.ErrHelp | ||
} | ||
|
||
name := f.Arg(0) |
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.
I think name
should be the only required argument. Flags for the others:
-description
, -type
and -multi
. The Register
method can be used to add the flags.
id, err := c.CreateCategoryIfNotExist(ctx, name, description, categoryType, value) | ||
if err != nil { | ||
|
||
fmt.Printf(err.Error()) |
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.
You can remove this line, the returned error below will be printed by the cli framework.
return err | ||
} | ||
|
||
govcUrl := "https://" + os.Getenv("GOVC_URL") |
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 GOVC_URL
env var may not be set. Instead use vc.URL()
and cmd.Userinfo()
return err | ||
} | ||
|
||
// SSO admin server has its own session manager, so the govc persisted session cookies cannot |
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.
You can remove the SSO related code here (lines 61-87).
} | ||
} | ||
|
||
if err = c.Login(context.TODO()); err != nil { |
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.
Use the ctx
var here instead of context.TODO()
return withClient(ctx, cmd.ClientFlag, func(c *tags.RestClient) error { | ||
categories, err := c.ListCategories(ctx) | ||
if err != nil { | ||
fmt.Printf(err.Error()) |
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.
Remove this line.
return err | ||
} | ||
|
||
fmt.Println(categories) |
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.
Here you can use cmd.WriteResult
instead, to support the -json
and -dump
flags. For an example, see:
Lines 176 to 187 in 7736cdc
type findResult []string | |
func (r findResult) Write(w io.Writer) error { | |
for i := range r { | |
fmt.Fprintln(w, r[i]) | |
} | |
return nil | |
} | |
func (r findResult) Dump() interface{} { | |
return []string(r) | |
} |
govc/tags/tags_helper/categories.go
Outdated
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
We need this package to be outside of the govc directory, as it will be consumed outside of govc. The rest APIs are referred to as vAPI
, so let's move the govc/tags/tags_helper
directory to vapi/tags
at the top-level of the repo.
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.
@jiatongw all the files named .DS_Store
need to be removed from this PR. You can add the file name to your ~/.gitignore to avoid adding them in the future.
govc/main.go
Outdated
@@ -72,6 +72,7 @@ import ( | |||
_ "github.com/vmware/govmomi/govc/session" | |||
_ "github.com/vmware/govmomi/govc/sso/service" | |||
_ "github.com/vmware/govmomi/govc/sso/user" | |||
_ "github.com/vmware/govmomi/govc/tags/categories" |
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
govc/tags/categories/create.go
Outdated
limitations under the License. | ||
*/ | ||
|
||
package tags |
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 and package should be named category
.
govc/tags/categories/create.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
The usage strings should all start with an upper case letter, Description ...
govc/tags/categories/get.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Use the ctx
var here instead of context.Background()
govc/tags/categories/ls.go
Outdated
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, you can just use:
tagsURL := vc.URL()
tagsURL.User = cmd.Userinfo()
govc/tags/categories/ls.go
Outdated
return err | ||
} | ||
|
||
c := tags.NewClient(URL, true, "") |
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.
Instead of hardcoding true
for the insecure param, you can use !cmd.IsSecure()
govc/tags/categories/ls.go
Outdated
return err | ||
} | ||
|
||
token := os.Getenv("GOVC_LOGIN_TOKEN") |
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 sso related code here through line 108 can be removed.
govc/tags/categories/ls.go
Outdated
if err = c.Login(ctx); err != nil { | ||
return err | ||
} | ||
|
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.
Add a TODO: here, we should have a defer c.Logout()
method here. Otherwise the commands will increase active sessions, for which vCenter has a maximum.
govc/tags/categories/create.go
Outdated
return nil | ||
} | ||
|
||
func (cmd *create) Usage() string { |
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.
This method should be named Description
. The Usage
method just returns the command arguments, in this case NAME
. Have a look at the output of tags.category.create -h
to see the formatted help text these methods contribute to.
Rename categories folder to category, as well as the package name. Add Description() and Usage() methods. Add Logout() in withClient() method to release resource. Format outputs.
90986c4
to
2508c9e
Compare
} | ||
tagsURL := vc.URL() | ||
tagsURL.User = cmd.Userinfo() | ||
|
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
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.
Nice work @jiatongw , lgtm
Tags management in govc has 3 steps: 1. Creating tag category. 2. Creating tag. 3.Attaching created tag. This PR is related with the first step.
The tags management issue can be found here #1139