-
Notifications
You must be signed in to change notification settings - Fork 1
/
simpleops.go
96 lines (84 loc) · 3.03 KB
/
simpleops.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package wsman
/*
Copyright 2015 Victor Lowther <[email protected]>
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.
*/
import (
"fmt"
"github.com/VictorLowther/simplexml/dom"
"github.com/VictorLowther/simplexml/search"
"path"
"strings"
)
// Invoke creates a wsman.Message that will invoke method on resource.
// After creating the Message, you need to add the appropriate selectors
// with msg.Selectors(), and the appropriate parameters with msg.Parameters()
func (c *Client) Invoke(resource, method string) *Message {
return c.NewMessage(resource + "/" + method).ResourceURI(resource)
}
func (m *Message) InvokeResponse() (*dom.Element, string, error) {
action, err := m.GHC("Action")
if err != nil {
return nil, "", err
}
method, resource := path.Split(strings.TrimSuffix(action, "Response"))
retbody := search.First(search.Tag(method+"_OUTPUT", resource), m.Body())
if retbody == nil {
return nil, "", fmt.Errorf("No %s_OUTPUT section in response", method)
}
returnValue := ""
retval := search.First(search.Tag("ReturnValue", resource), retbody.Children())
if retval == nil {
return retbody, "", fmt.Errorf("No ReturnValue in %s_OUTPUT", method)
}
returnValue = string(retval.Content)
return retbody, returnValue, nil
}
// Get creates a wsman.Message that will get an instance
// at the passed-in resource.
func (c *Client) Get(resource string) *Message {
return c.NewMessage(GET).ResourceURI(resource)
}
func (m *Message) GetItem() (*dom.Element, error) {
action, err := m.GHC("Action")
if err != nil {
return nil, err
}
if action != GET+"Response" {
return nil, fmt.Errorf("Not a GetResponse message")
}
if err != nil {
return nil, err
}
b := m.Body()
if len(b) == 0 {
return nil, fmt.Errorf("No SOAP body elements")
}
return b[0], nil
}
// Put creates a wsman.Message that will update the passed-in
// resource. The updated resource should be passed in as the
// only element in the Body of the messate.
func (c *Client) Put(resource string) *Message {
return c.NewMessage(PUT).ResourceURI(resource)
}
// Create creates a wsman.Message that will update the passed-in
// resource. The updated resource should be passed in as the
// only element in the Body of the messate.
func (c *Client) Create(resource string) *Message {
return c.NewMessage(CREATE).ResourceURI(resource)
}
// Delete creates a wsman.Message that will update the passed-in
// resource. The updated resource should be passed in as the
// only element in the Body of the messate.
func (c *Client) Delete(resource string) *Message {
return c.NewMessage(DELETE).ResourceURI(resource)
}