Skip to content

Commit

Permalink
Tags Categories cmd available (#1150)
Browse files Browse the repository at this point in the history
Add tags.category create, get, ls, rm and update commands
  • Loading branch information
jiatongw authored and dougm committed Jun 13, 2018
1 parent f5c88d2 commit 1ddfb01
Show file tree
Hide file tree
Showing 8 changed files with 917 additions and 0 deletions.
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/category"
_ "github.com/vmware/govmomi/govc/task"
_ "github.com/vmware/govmomi/govc/vapp"
_ "github.com/vmware/govmomi/govc/version"
Expand Down
86 changes: 86 additions & 0 deletions govc/tags/category/create.go
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
})

}
90 changes: 90 additions & 0 deletions govc/tags/category/get.go
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
})
}
104 changes: 104 additions & 0 deletions govc/tags/category/ls.go
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

})

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

0 comments on commit 1ddfb01

Please sign in to comment.