/
salt_api.go
168 lines (134 loc) · 4.04 KB
/
salt_api.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package plugins
import (
"encoding/json"
"fmt"
"github.com/WeBankPartners/wecube-plugins-saltstack/common/log"
)
var SaltApiActions = make(map[string]Action)
func init() {
SaltApiActions["call"] = new(SaltApiCallAction)
}
type SaltApiPlugin struct {
}
func (plugin *SaltApiPlugin) GetActionByName(actionName string) (Action, error) {
action, found := SaltApiActions[actionName]
if !found {
return nil, fmt.Errorf("SaltApi plugin,action = %s not found", actionName)
}
return action, nil
}
type SaltApiCallInputs struct {
Inputs []SaltApiCallInput `json:"inputs,omitempty"`
}
type SaltApiCallInput struct {
CallBackParameter
Guid string `json:"guid,omitempty"`
Client string `json:"client,omitempty"`
Target string `json:"target,omitempty"`
Function string `json:"function,omitempty"`
Args []string `json:"args,omitempty"`
}
type SaltApiCallOutputs struct {
Outputs []SaltApiCallOutput `json:"outputs,omitempty"`
}
type SaltApiCallOutput struct {
CallBackParameter
Result
Guid string `json:"guid,omitempty"`
Detail string `json:"detail,omitempty"`
}
type SaltApiCallAction struct {
Language string
}
func (action *SaltApiCallAction) SetAcceptLanguage(language string) {
action.Language = language
}
func (action *SaltApiCallAction) ReadParam(param interface{}) (interface{}, error) {
var inputs SaltApiCallInputs
err := UnmarshalJson(param, &inputs)
if err != nil {
return nil, err
}
return inputs, nil
}
func (action *SaltApiCallAction) CheckParam(input SaltApiCallInput) error {
return nil
}
type SaltApiCmdScriptResults struct {
Results []map[string]SaltApiCmdScriptResult `json:"return,omitempty"`
}
type SaltApiCmdScriptResult struct {
Pid int `json:"pid,omitempty"`
RetCode int `json:"retcode,omitempty"`
Stderr string `json:"stderr,omitempty"`
Stdout string `json:"stdout,omitempty"`
}
func parseSaltApiCmdScriptCallResult(jsonStr string) (*SaltApiCmdScriptResults, error) {
result := SaltApiCmdScriptResults{}
if err := json.Unmarshal([]byte(jsonStr), &result); err != nil {
return &result, err
}
if len(result.Results) == 0 {
return &result, fmt.Errorf("parseSaltApiCmdScriptCallResult,get %d result", len(result.Results))
}
return &result, nil
}
type SaltApiCmdRunResults struct {
Results []map[string]SaltApiCmdRunResult `json:"return,omitempty"`
}
type SaltApiCmdRunResult struct {
Jid string `json:"jid,omitempty"`
RetCode int `json:"retcode,omitempty"`
RetDetail string `json:"ret,omitempty"`
}
func parseSaltApiCmdRunCallResult(jsonStr string) (*SaltApiCmdRunResults, error) {
result := SaltApiCmdRunResults{}
if err := json.Unmarshal([]byte(jsonStr), &result); err != nil {
return &result, err
}
if len(result.Results) == 0 {
return &result, fmt.Errorf("Parse SaltApiCmdRunCallResult,get %d result ", len(result.Results))
}
return &result, nil
}
func (action *SaltApiCallAction) callSaltApiCall(input *SaltApiCallInput) (output SaltApiCallOutput, err error) {
defer func() {
output.Guid = input.Guid
output.CallBackParameter.Parameter = input.CallBackParameter.Parameter
if err == nil {
output.Result.Code = RESULT_CODE_SUCCESS
} else {
output.Result.Code = RESULT_CODE_ERROR
output.Result.Message = err.Error()
}
}()
err = action.CheckParam(*input)
if err != nil {
return output, err
}
request := SaltApiRequest{}
request.Client = input.Client
request.Function = input.Function
request.Target = input.Target
request.Args = input.Args
result, err := CallSaltApi("https://127.0.0.1:8080", request, action.Language)
if err != nil {
return output, err
}
output.Detail = result
return output, err
}
func (action *SaltApiCallAction) Do(input interface{}) (interface{}, error) {
files, _ := input.(SaltApiCallInputs)
outputs := SaltApiCallOutputs{}
var finalErr error
for _, file := range files.Inputs {
fileOutput, err := action.callSaltApiCall(&file)
if err != nil {
log.Logger.Error("Salt api call action", log.Error(err))
finalErr = err
}
outputs.Outputs = append(outputs.Outputs, fileOutput)
}
return &outputs, finalErr
}