-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_schematics_policies.go
283 lines (263 loc) · 9.06 KB
/
data_source_ibm_schematics_policies.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
// 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 DataSourceIbmSchematicsPolicies() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmSchematicsPoliciesRead,
Schema: map[string]*schema.Schema{
"policy_kind": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Policy kind or categories for managing and deriving policy decision * `agent_assignment_policy` Agent assignment policy for job execution.",
},
"total_count": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The total number of policy records.",
},
"limit": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The number of policy records returned.",
},
"offset": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The skipped number of policy records.",
},
"policies": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The list of Schematics policies.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of Schematics customization policy.",
},
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The system generated Policy Id.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The policy CRN.",
},
"account": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The Account id.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The description of Schematics customization policy.",
},
"resource_group": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Resource-group name for the Policy. By default, Policy will be created in Default Resource Group.",
},
"tags": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "Tags for the Schematics customization policy.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"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.",
},
"state": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "User defined status of the Schematics object.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "User-defined states * `draft` Object can be modified; can be used by Jobs run by the author, during execution * `live` Object can be modified; can be used by Jobs during execution * `locked` Object cannot be modified; can be used by Jobs during execution * `disable` Object can be modified. cannot be used by Jobs during execution.",
},
"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.",
},
},
},
},
"policy_kind": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Policy kind or categories for managing and deriving policy decision * `agent_assignment_policy` Agent assignment policy for job execution.",
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The policy creation time.",
},
"created_by": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The user who created the Policy.",
},
"updated_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The policy updation time.",
},
},
},
},
},
}
}
func dataSourceIbmSchematicsPoliciesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
schematicsClient, err := meta.(conns.ClientSession).SchematicsV1()
if err != nil {
return diag.FromErr(err)
}
listPolicyOptions := &schematicsv1.ListPolicyOptions{}
policyList, response, err := schematicsClient.ListPolicyWithContext(context, listPolicyOptions)
if err != nil {
log.Printf("[DEBUG] ListPolicyWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("ListPolicyWithContext failed %s\n%s", err, response))
}
// Use the provided filter argument and construct a new list with only the requested resource(s)
var matchPolicies []schematicsv1.PolicyLite
var policyKind string
var suppliedFilter bool
if v, ok := d.GetOk("policy_kind"); ok {
policyKind = v.(string)
suppliedFilter = true
for _, data := range policyList.Policies {
if data.PolicyKind != nil {
if *data.PolicyKind == policyKind {
matchPolicies = append(matchPolicies, data)
}
}
}
} else {
matchPolicies = policyList.Policies
}
policyList.Policies = matchPolicies
if suppliedFilter {
if len(policyList.Policies) == 0 {
log.Printf("[DEBUG] no Policies found with policyKind %s\n", policyKind)
// return diag.FromErr(fmt.Errorf("no Policies found with policyKind %s", policyKind))
}
d.SetId(policyKind)
} else {
d.SetId(dataSourceIbmSchematicsPoliciesID(d))
}
if err = d.Set("total_count", flex.IntValue(policyList.TotalCount)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting total_count: %s", err))
}
if err = d.Set("limit", flex.IntValue(policyList.Limit)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting limit: %s", err))
}
if err = d.Set("offset", flex.IntValue(policyList.Offset)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting offset: %s", err))
}
policies := []map[string]interface{}{}
if policyList.Policies != nil {
for _, modelItem := range policyList.Policies {
modelMap, err := dataSourceIbmSchematicsPoliciesPolicyLiteToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
policies = append(policies, modelMap)
}
}
if err = d.Set("policies", policies); err != nil {
return diag.FromErr(fmt.Errorf("Error setting policies %s", err))
}
return nil
}
// dataSourceIbmSchematicsPoliciesID returns a reasonable ID for the list.
func dataSourceIbmSchematicsPoliciesID(d *schema.ResourceData) string {
return time.Now().UTC().String()
}
func dataSourceIbmSchematicsPoliciesPolicyLiteToMap(model *schematicsv1.PolicyLite) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Name != nil {
modelMap["name"] = *model.Name
}
if model.ID != nil {
modelMap["id"] = *model.ID
}
if model.Crn != nil {
modelMap["crn"] = *model.Crn
}
if model.Account != nil {
modelMap["account"] = *model.Account
}
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.Location != nil {
modelMap["location"] = *model.Location
}
if model.State != nil {
stateMap, err := dataSourceIbmSchematicsPoliciesUserStateToMap(model.State)
if err != nil {
return modelMap, err
}
modelMap["state"] = []map[string]interface{}{stateMap}
}
if model.PolicyKind != nil {
modelMap["policy_kind"] = *model.PolicyKind
}
if model.CreatedAt != nil {
modelMap["created_at"] = model.CreatedAt.String()
}
if model.CreatedBy != nil {
modelMap["created_by"] = *model.CreatedBy
}
if model.UpdatedAt != nil {
modelMap["updated_at"] = model.UpdatedAt.String()
}
return modelMap, nil
}
func dataSourceIbmSchematicsPoliciesUserStateToMap(model *schematicsv1.UserState) (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
}