Skip to content

Commit

Permalink
categories cmd full version
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiatong Wang committed Jun 6, 2018
1 parent a49dc2c commit d55c985
Show file tree
Hide file tree
Showing 10 changed files with 850 additions and 0 deletions.
2 changes: 2 additions & 0 deletions govc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

_ "github.com/vmware/govmomi/govc/about"
_ "github.com/vmware/govmomi/govc/cluster"
Expand Down
Binary file removed govc/tags/.DS_Store
Binary file not shown.
Binary file removed govc/tags/categories/.DS_Store
Binary file not shown.
67 changes: 67 additions & 0 deletions govc/tags/categories/categories_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tags

import (
"context"
"flag"
"fmt"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/tags/tags_helper"
)

type create struct {
*flags.ClientFlag
}

func init() {
cli.Register("tags.categories.create", &create{})
}

func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
}

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 `
Examples:
govc tags.categories.create 'name' 'description' 'categoryType' 'multiValue(true/false)'`
}

func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 4 {
return flag.ErrHelp
}

name := f.Arg(0)
description := f.Arg(1)
categoryType := f.Arg(2)
multiValue := f.Arg(3)
value := false
if multiValue == "true" {
value = true
}

return withClient(ctx, cmd.ClientFlag, func(c *tags.RestClient) error {

id, err := c.CreateCategoryIfNotExist(ctx, name, description, categoryType, value)
if err != nil {

fmt.Printf(err.Error())
return err
}

fmt.Printf("Create category success id %s\n", *id)

return nil

})
}
57 changes: 57 additions & 0 deletions govc/tags/categories/categories_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tags

import (
"context"
"flag"
"fmt"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/tags/tags_helper"
)

type delete struct {
*flags.ClientFlag
}

func init() {
cli.Register("tags.categories.delete", &delete{})
}

func (cmd *delete) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
}

func (cmd *delete) Process(ctx context.Context) error {
if err := cmd.ClientFlag.Process(ctx); err != nil {
return err
}
return nil
}

func (cmd *delete) Usage() string {
return `
Examples:
govc tags.categories.delete 'id'`
}

func (cmd *delete) 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 {

err := c.DeleteCategory(ctx, categoryID)
if err != nil {

fmt.Printf(err.Error())
return err
}
return nil

})
}
57 changes: 57 additions & 0 deletions govc/tags/categories/categories_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package tags

import (
"context"
"flag"
"fmt"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/tags/tags_helper"
)

type getbyname struct {
*flags.ClientFlag
}

func init() {
cli.Register("tags.categories.getbyname", &getbyname{})
}

func (cmd *getbyname) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
}

func (cmd *getbyname) Process(ctx context.Context) error {
if err := cmd.ClientFlag.Process(ctx); err != nil {
return err
}
return nil
}

func (cmd *getbyname) Usage() string {
return `
Examples:
govc tags.categories.getbyname 'name'`
}

func (cmd *getbyname) 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 {

categories, err := c.GetCategoriesByName(context.Background(), name)
if err != nil {
fmt.Printf(err.Error())
return nil
}
fmt.Println(categories)
return nil

})
}
109 changes: 109 additions & 0 deletions govc/tags/categories/categories_ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package tags

import (
"context"
"flag"
"fmt"
"net/url"
"os"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/tags/tags_helper"
"github.com/vmware/govmomi/sts"
"github.com/vmware/govmomi/vim25/soap"
)

type ls struct {
*flags.ClientFlag
}

func init() {
cli.Register("tags.categories.ls", &ls{})
}

func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.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 `
Examples:
govc tags.categories.ls`
}

func withClient(ctx context.Context, cmd *flags.ClientFlag, f func(*tags.RestClient) error) error {
vc, err := cmd.Client()
if err != nil {
return err
}

govcUrl := "https://" + os.Getenv("GOVC_URL")

URL, err := url.Parse(govcUrl)
if err != nil {
return err
}

c := tags.NewClient(URL, true, "")
if err != nil {
return err
}

// SSO admin server has its own session manager, so the govc persisted session cookies cannot
// be used to authenticate. There is no SSO token persistence in govc yet, so just use an env
// var for now. If no GOVC_LOGIN_TOKEN is set, issue a new token.
token := os.Getenv("GOVC_LOGIN_TOKEN")
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(context.TODO()); err != nil {
return err
}

return f(c)
}

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 {
fmt.Printf(err.Error())
return err
}

fmt.Println(categories)
return nil

})
}
84 changes: 84 additions & 0 deletions govc/tags/categories/categories_update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package tags

import (
"context"
"flag"
"fmt"
"strings"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/tags/tags_helper"
)

type update struct {
*flags.ClientFlag
}

func init() {
cli.Register("tags.categories.update", &update{})
}

func (cmd *update) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
}

func (cmd *update) Process(ctx context.Context) error {
if err := cmd.ClientFlag.Process(ctx); err != nil {
return err
}
return nil
}

func (cmd *update) Usage() string {
return `
Examples:
govc tags.categories.create 'name' 'description' 'categoryType' 'multiValue(true/false)'`
}

func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 5 {
return flag.ErrHelp
}
id := f.Arg(0)

name := f.Arg(1)
description := f.Arg(2)
categoryType := f.Arg(3)
multiValue := f.Arg(4)

category := new(tags.CategoryUpdateSpec)

if categoryType == "" {
category.UpdateSpec = tags.CategoryUpdate{
// AssociableTypes: categoryType,
Cardinality: multiValue,
Description: description,
Name: name,
}

} else {
types := strings.Split(categoryType, ",")
category.UpdateSpec = tags.CategoryUpdate{
AssociableTypes: types,
Cardinality: multiValue,
Description: description,
Name: name,
}

}

return withClient(ctx, cmd.ClientFlag, func(c *tags.RestClient) error {

err := c.UpdateCategory(ctx, id, category)

if err != nil {

fmt.Printf(err.Error())
return err
}
return nil

})
}
Loading

0 comments on commit d55c985

Please sign in to comment.