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
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions govc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

_ "github.com/vmware/govmomi/govc/task"
_ "github.com/vmware/govmomi/govc/vapp"
_ "github.com/vmware/govmomi/govc/version"
Expand Down
81 changes: 81 additions & 0 deletions govc/tags/categories/create.go
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
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.


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

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

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

})
}
85 changes: 85 additions & 0 deletions govc/tags/categories/get.go
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)
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
}
result := getName(categoriesName)
cmd.WriteResult(result)
return nil

})
}
139 changes: 139 additions & 0 deletions govc/tags/categories/ls.go
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())
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()

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

if err != nil {
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.

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
}

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

})
}
66 changes: 66 additions & 0 deletions govc/tags/categories/rm.go
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)

})
}
Loading