Skip to content

Commit

Permalink
Add AuthorizationManager {Enable,Disable}Methods
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed Apr 27, 2017
1 parent 5072cda commit c7f718b
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 1 deletion.
104 changes: 104 additions & 0 deletions govc/object/method.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Copyright (c) 2017 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 object

import (
"context"
"flag"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/object"
)

type method struct {
*flags.DatacenterFlag

name string
reason string
source string
enable bool
}

func init() {
cli.Register("object.method", &method{})
}

func (cmd *method) Register(ctx context.Context, f *flag.FlagSet) {
cmd.DatacenterFlag, ctx = flags.NewDatacenterFlag(ctx)
cmd.DatacenterFlag.Register(ctx, f)

f.StringVar(&cmd.name, "name", "", "Method name")
f.StringVar(&cmd.reason, "reason", "", "Reason for disabling method")
f.StringVar(&cmd.source, "source", "govc", "Source ID")
f.BoolVar(&cmd.enable, "enable", true, "Enable method")
}

func (cmd *method) Usage() string {
return "PATH..."
}

func (cmd *method) Description() string {
return `Enable or disable methods for managed objects.
Examples:
govc object.method -name Destroy_Task -enable=false /dc1/vm/foo
govc object.collect /dc1/vm/foo disabledMethod | grep --color Destroy_Task
govc object.method -name Destroy_Task -enable /dc1/vm/foo`
}

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

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

if cmd.name == "" {
return flag.ErrHelp
}

c, err := cmd.Client()
if err != nil {
return err
}

objs, err := cmd.ManagedObjects(ctx, f.Args())
if err != nil {
return err
}

m := object.NewAuthorizationManager(c)

if cmd.enable {
return m.EnableMethods(ctx, objs, []string{cmd.name}, cmd.source)
}

method := []object.DisabledMethodRequest{
{
Method: cmd.name,
Reason: cmd.reason,
},
}

return m.DisableMethods(ctx, objs, method, cmd.source)
}
28 changes: 27 additions & 1 deletion govc/test/object.bats
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,31 @@ load test_helper
assert_output "$folder/$vm"

run govc find "$folder" -name "$vm"
assert_output "$folder/$vm"
}

@test "object.method" {
vcsim_env

vm=$(govc find vm -type m | head -1)

run govc object.method -enable=false -name NoSuchMethod "$vm"
assert_failure

run govc object.method -enable=false -name Destroy_Task enoent
assert_failure

run govc object.collect -s "$vm" disabledMethod
! assert_matches "Destroy_Task" "$output"

run govc object.method -enable=false -name Destroy_Task "$vm"
assert_success

run govc object.collect -s "$vm" disabledMethod
assert_matches "Destroy_Task" "$output"

run govc object.method -enable -name Destroy_Task "$vm"
assert_success

run govc object.collect -s "$vm" disabledMethod
! assert_matches "Destroy_Task" "$output"
}
86 changes: 86 additions & 0 deletions object/authorization_manager_internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
Copyright (c) 2017 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 object

import (
"context"

"github.com/vmware/govmomi/vim25/soap"
"github.com/vmware/govmomi/vim25/types"
)

type DisabledMethodRequest struct {
Method string `xml:"method"`
Reason string `xml:"reasonId"`
}

type disableMethodsRequest struct {
This types.ManagedObjectReference `xml:"_this"`
Entity []types.ManagedObjectReference `xml:"entity"`
Method []DisabledMethodRequest `xml:"method"`
Source string `xml:"sourceId"`
Scope bool `xml:"sessionScope,omitempty"`
}

type disableMethodsBody struct {
Req *disableMethodsRequest `xml:"urn:internalvim25 DisableMethods,omitempty"`
Res interface{} `xml:"urn:vim25 DisableMethodsResponse,omitempty"`
Err *soap.Fault `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault,omitempty"`
}

func (b *disableMethodsBody) Fault() *soap.Fault { return b.Err }

func (m AuthorizationManager) DisableMethods(ctx context.Context, entity []types.ManagedObjectReference, method []DisabledMethodRequest, source string) error {
var reqBody, resBody disableMethodsBody

reqBody.Req = &disableMethodsRequest{
This: m.Reference(),
Entity: entity,
Method: method,
Source: source,
}

return m.Client().RoundTrip(ctx, &reqBody, &resBody)
}

type enableMethodsRequest struct {
This types.ManagedObjectReference `xml:"_this"`
Entity []types.ManagedObjectReference `xml:"entity"`
Method []string `xml:"method"`
Source string `xml:"sourceId"`
}

type enableMethodsBody struct {
Req *enableMethodsRequest `xml:"urn:internalvim25 EnableMethods,omitempty"`
Res interface{} `xml:"urn:vim25 EnableMethodsResponse,omitempty"`
Err *soap.Fault `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault,omitempty"`
}

func (b *enableMethodsBody) Fault() *soap.Fault { return b.Err }

func (m AuthorizationManager) EnableMethods(ctx context.Context, entity []types.ManagedObjectReference, method []string, source string) error {
var reqBody, resBody enableMethodsBody

reqBody.Req = &enableMethodsRequest{
This: m.Reference(),
Entity: entity,
Method: method,
Source: source,
}

return m.Client().RoundTrip(ctx, &reqBody, &resBody)
}

0 comments on commit c7f718b

Please sign in to comment.