Skip to content
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 8 commits into from
Jun 13, 2018
Merged

Tags Categories cmd available #1150

merged 8 commits into from
Jun 13, 2018

Conversation

jiatongw
Copy link

@jiatongw jiatongw commented Jun 5, 2018

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

@jiatongw
Copy link
Author

jiatongw commented Jun 5, 2018

The tags_helper/ are from VIC project

@jiatongw jiatongw force-pushed the master branch 3 times, most recently from d55c985 to f772395 Compare June 6, 2018 22:03
Copy link
Member

@dougm dougm left a 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 using fmt.Errorf if needed.

  • I think tags.categories should be renamed tags.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"
Copy link
Member

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 `
Copy link
Member

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:
Copy link
Member

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)
Copy link
Member

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())
Copy link
Member

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")
Copy link
Member

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
Copy link
Member

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 {
Copy link
Member

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())
Copy link
Member

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)
Copy link
Member

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:

govmomi/govc/object/find.go

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)
}

// See the License for the specific language governing permissions and
// limitations under the License.

package tags
Copy link
Member

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.

Copy link
Member

@dougm dougm left a 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"
Copy link
Member

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

limitations under the License.
*/

package tags
Copy link
Member

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.

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")
Copy link
Member

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 ...

name := f.Arg(0)

return withClient(ctx, cmd.ClientFlag, func(c *tags.RestClient) error {
categoriesName, err := c.GetCategoriesByName(context.Background(), name)
Copy link
Member

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()

if err != nil {
return err
}
usrDecode, err := url.QueryUnescape(cmd.Userinfo().String())
Copy link
Member

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()

return err
}

c := tags.NewClient(URL, true, "")
Copy link
Member

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()

return err
}

token := os.Getenv("GOVC_LOGIN_TOKEN")
Copy link
Member

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.

if err = c.Login(ctx); err != nil {
return err
}

Copy link
Member

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.

return nil
}

func (cmd *create) Usage() string {
Copy link
Member

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.
@jiatongw jiatongw force-pushed the master branch 3 times, most recently from 90986c4 to 2508c9e Compare June 12, 2018 21:54
@jiatongw
Copy link
Author

@dougm Updated PR starts from 6211bca

}
tagsURL := vc.URL()
tagsURL.User = cmd.Userinfo()

Copy link
Author

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

Copy link
Member

@dougm dougm left a 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

@dougm dougm merged commit 1ddfb01 into vmware:master Jun 13, 2018
@dougm dougm added this to the 2018.09 milestone Sep 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants