-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathresource_multi_action_group.go
203 lines (173 loc) · 5.08 KB
/
resource_multi_action_group.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package bigfix
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceMultiActionGroup() *schema.Resource {
return &schema.Resource{
Create: resourceMultiActionGroupCreate,
Read: resourceMultiActionGroupRead,
Delete: resourceMultiActionGroupDelete,
Schema: map[string]*schema.Schema{
// required values
"input_file_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"target_computer_name": {
Type: schema.TypeString,
Description: "Computer name of target computer",
Optional: true,
Computed: true,
},
"target_computer_id": {
Type: schema.TypeString,
Description: "Computer ID of target computer",
Sensitive: true,
Required: true,
ForceNew: true,
},
"site_name": {
Type: schema.TypeSet,
Description: "name of site to get relevant fixlets",
Required: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
// computed values
"title": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"relevance": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"state": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
// resourceFixletSetToState : Function to set some resource properties
func resourceMAGSetToState(d *schema.ResourceData, action *Action, state string) {
d.Set("title", action.MultipleActionGroup.Title)
d.Set("relevance", action.MultipleActionGroup.Relevance)
d.Set("state", state)
}
func resourceMultiActionGroupCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(Config)
inputFile := d.Get("input_file_name").(string)
targetComputerID := d.Get("target_computer_id").(string)
site := []string{}
siteSet := d.Get("site_name").(*schema.Set)
for _, v := range siteSet.List() {
site = append(site, v.(string))
}
//get name of computer
computerName := getNameOfComputer(config, targetComputerID)
// get list of members of MAG
var memberListStruct []SourcedMemberAction
memberListStruct, err := SetSourcedMemberList(config, site, targetComputerID)
if err != nil {
return err
}
// get values from XML file
file, err := ioutil.ReadFile(inputFile)
if err != nil {
log.Printf("[DEBUG] Error while reading XML file : %v\n", err)
return fmt.Errorf("error while reading file :{ %s }", err)
}
log.Println("[DEBUG] input file data : ", string(file))
// set file data into struct
var magStruct MAGFile
err1 := xml.Unmarshal(file, &magStruct)
if err1 != nil {
log.Printf("[DEBUG] Error while parsing XML file : %v\n", err1)
return fmt.Errorf("error while parsing XML file:{ %s }", err1)
}
// create buffer using file content and list data
buff := ParseMAGXMLMarshal(magStruct, targetComputerID, site, memberListStruct)
url := CreateMAGAPI(config.ServerIP, config.Port)
response, err := config.BfxConnection(POST, url, bytes.NewReader(buff))
if err != nil {
return (err)
}
// Check whether action is created
var responseStruct ActionCreationResponse
if response != nil {
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("[DEBUG] Error reading response body : %v\n", err)
return err
}
xml.Unmarshal([]byte(data), &responseStruct)
if responseStruct.Action.ID != "" {
log.Println("[DEBUG] MAG created successfully !")
}
magID := responseStruct.Action.ID
// get status of action
actionStatus := GetActionStatus(config, magID)
log.Printf("[DEBUG] Action ID is : %s ", magID)
d.Set("title", responseStruct.Action.Name)
d.Set("target_computer_name", computerName)
d.Set("state", actionStatus)
d.SetId(magID)
return nil
}
return err
}
func resourceMultiActionGroupRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(Config)
log.Printf("[DEBUG] Read Action %s", d.Id())
id := d.Id()
// get status of action
actionStatus := GetActionStatus(config, id)
// Get URL for Action Details using ID
url := GetActionDetailAPI(config.ServerIP, config.Port, id)
response, err := config.BfxConnection(GET, url, nil)
if err != nil {
return err
}
// Get action
var actionDetailStruct Action
if response != nil {
defer response.Body.Close()
data, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Printf("[DEBUG] Error reading response body : %v\n", err)
return err
}
xml.Unmarshal([]byte(data), &actionDetailStruct)
resourceMAGSetToState(d, &actionDetailStruct, actionStatus)
return nil
}
return err
}
func resourceMultiActionGroupDelete(d *schema.ResourceData, meta interface{}) error {
resourceMultiActionGroupRead(d, meta)
if d.Id() == "" {
log.Println("[ERROR] Action not found")
return fmt.Errorf("Action not found")
}
config := meta.(Config)
deleted, err := DeleteAction(config, d.Id())
if deleted == true {
log.Printf("[DEBUG] MAG having id [%s] deleted successfully \n", d.Id())
return nil
}
log.Printf("[DEBUG] Error while deleting a MAG from bigfix : %v\n", err)
return err
}