-
Notifications
You must be signed in to change notification settings - Fork 912
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jiatong Wang
committed
Jun 6, 2018
1 parent
a49dc2c
commit d55c985
Showing
10 changed files
with
850 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
}) | ||
} |
Oops, something went wrong.