forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AuthorizationManager {Enable,Disable}Methods
- Loading branch information
Showing
3 changed files
with
217 additions
and
1 deletion.
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
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) | ||
} |
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
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,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) | ||
} |