-
Notifications
You must be signed in to change notification settings - Fork 645
/
data_source_ibm_schematics_agents.go
363 lines (341 loc) · 11.4 KB
/
data_source_ibm_schematics_agents.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package schematics
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/schematics-go-sdk/schematicsv1"
)
func DataSourceIbmSchematicsAgents() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmSchematicsAgentsRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The name of the agent (must be unique, for an account).",
},
"total_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The total number of records.",
},
"limit": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of records returned.",
},
"offset": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The skipped number of records.",
},
"agents": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The list of agents in the account.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the agent (must be unique, for an account).",
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Agent description.",
},
"resource_group": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource-group name for the agent. By default, Agent will be registered in Default Resource Group.",
},
"tags": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Tags for the agent.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"agent_location": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The location where agent is deployed in the user environment.",
},
"location": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "List of locations supported by IBM Cloud Schematics service. While creating your workspace or action, choose the right region, since it cannot be changed. Note, this does not limit the location of the IBM Cloud resources, provisioned using Schematics.",
},
"profile_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IAM trusted profile id, used by the Agent instance.",
},
"agent_crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Agent crn, obtained from the Schematics Agent deployment configuration.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Agent registration id.",
},
"registered_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Agent registration date-time.",
},
"registered_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The email address of an user who registered the Agent.",
},
"updated_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Agent registration updation time.",
},
"updated_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Email address of user who updated the Agent registration.",
},
"user_state": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "User defined status of the agent.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "User-defined states * `enable` Agent is enabled by the user. * `disable` Agent is disbaled by the user.",
},
"set_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Name of the User who set the state of the Object.",
},
"set_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "When the User who set the state of the Object.",
},
},
},
},
"connection_state": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Connection status of the agent.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Agent Connection Status * `Connected` When Schematics is able to connect to the agent. * `Disconnected` When Schematics is able not connect to the agent.",
},
"checked_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "When the connection state is modified.",
},
},
},
},
"system_state": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Computed state of the agent.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Agent Status.",
},
"message": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Agent status message.",
},
},
},
},
},
},
},
},
}
}
func dataSourceIbmSchematicsAgentsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schematicsClient, err := meta.(conns.ClientSession).SchematicsV1()
if err != nil {
return diag.FromErr(err)
}
listAgentOptions := &schematicsv1.ListAgentOptions{}
agentList, response, err := schematicsClient.ListAgentWithContext(context, listAgentOptions)
if err != nil {
log.Printf("[DEBUG] ListAgentWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListAgentWithContext failed %s\n%s", err, response))
}
// Use the provided filter argument and construct a new list with only the requested resource(s)
var matchAgents []schematicsv1.Agent
var name string
var suppliedFilter bool
if v, ok := d.GetOk("name"); ok {
name = v.(string)
suppliedFilter = true
for _, data := range agentList.Agents {
if *data.Name == name {
matchAgents = append(matchAgents, data)
}
}
} else {
matchAgents = agentList.Agents
}
agentList.Agents = matchAgents
if suppliedFilter {
if len(agentList.Agents) == 0 {
log.Printf("[DEBUG] no Agents found with name %s\n", name)
// return diag.FromErr(fmt.Errorf("no Agents found with name %s", name))
}
d.SetId(name)
} else {
d.SetId(dataSourceIbmSchematicsAgentsID(d))
}
if err = d.Set("total_count", flex.IntValue(agentList.TotalCount)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting total_count: %s", err))
}
if err = d.Set("limit", flex.IntValue(agentList.Limit)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting limit: %s", err))
}
if err = d.Set("offset", flex.IntValue(agentList.Offset)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting offset: %s", err))
}
agents := []map[string]interface{}{}
if agentList.Agents != nil {
for _, modelItem := range agentList.Agents {
modelMap, err := dataSourceIbmSchematicsAgentsAgentToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
agents = append(agents, modelMap)
}
}
if err = d.Set("agents", agents); err != nil {
return diag.FromErr(fmt.Errorf("Error setting agents %s", err))
}
return nil
}
// dataSourceIbmSchematicsAgentsID returns a reasonable ID for the list.
func dataSourceIbmSchematicsAgentsID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIbmSchematicsAgentsAgentToMap(model *schematicsv1.Agent) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Name != nil {
modelMap["name"] = *model.Name
}
if model.Description != nil {
modelMap["description"] = *model.Description
}
if model.ResourceGroup != nil {
modelMap["resource_group"] = *model.ResourceGroup
}
if model.Tags != nil {
modelMap["tags"] = model.Tags
}
if model.AgentLocation != nil {
modelMap["agent_location"] = *model.AgentLocation
}
if model.Location != nil {
modelMap["location"] = *model.Location
}
if model.ProfileID != nil {
modelMap["profile_id"] = *model.ProfileID
}
if model.AgentCrn != nil {
modelMap["agent_crn"] = *model.AgentCrn
}
if model.ID != nil {
modelMap["id"] = *model.ID
}
if model.RegisteredAt != nil {
modelMap["registered_at"] = model.RegisteredAt.String()
}
if model.RegisteredBy != nil {
modelMap["registered_by"] = *model.RegisteredBy
}
if model.UpdatedAt != nil {
modelMap["updated_at"] = model.UpdatedAt.String()
}
if model.UpdatedBy != nil {
modelMap["updated_by"] = *model.UpdatedBy
}
if model.UserState != nil {
userStateMap, err := dataSourceIbmSchematicsAgentsAgentUserStateToMap(model.UserState)
if err != nil {
return modelMap, err
}
modelMap["user_state"] = []map[string]interface{}{userStateMap}
}
if model.ConnectionState != nil {
connectionStateMap, err := dataSourceIbmSchematicsAgentsConnectionStateToMap(model.ConnectionState)
if err != nil {
return modelMap, err
}
modelMap["connection_state"] = []map[string]interface{}{connectionStateMap}
}
if model.SystemState != nil {
systemStateMap, err := dataSourceIbmSchematicsAgentsAgentSystemStateToMap(model.SystemState)
if err != nil {
return modelMap, err
}
modelMap["system_state"] = []map[string]interface{}{systemStateMap}
}
return modelMap, nil
}
func dataSourceIbmSchematicsAgentsAgentUserStateToMap(model *schematicsv1.AgentUserState) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.State != nil {
modelMap["state"] = *model.State
}
if model.SetBy != nil {
modelMap["set_by"] = *model.SetBy
}
if model.SetAt != nil {
modelMap["set_at"] = model.SetAt.String()
}
return modelMap, nil
}
func dataSourceIbmSchematicsAgentsConnectionStateToMap(model *schematicsv1.ConnectionState) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.State != nil {
modelMap["state"] = *model.State
}
if model.CheckedAt != nil {
modelMap["checked_at"] = model.CheckedAt.String()
}
return modelMap, nil
}
func dataSourceIbmSchematicsAgentsAgentSystemStateToMap(model *schematicsv1.AgentSystemState) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.State != nil {
modelMap["state"] = *model.State
}
if model.Message != nil {
modelMap["message"] = *model.Message
}
return modelMap, nil
}