-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_project_environment.go
334 lines (311 loc) · 11.9 KB
/
data_source_ibm_project_environment.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
// Copyright IBM Corp. 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package project
import (
"context"
"fmt"
"log"
"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/project-go-sdk/projectv1"
)
func DataSourceIbmProjectEnvironment() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmProjectEnvironmentRead,
Schema: map[string]*schema.Schema{
"project_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The unique project ID.",
},
"project_environment_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The environment ID.",
},
"project": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The project that is referenced by this resource.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique ID.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A URL.",
},
"definition": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The definition of the project reference.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the project.",
},
},
},
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "An IBM Cloud resource name that uniquely identifies a resource.",
},
},
},
},
"created_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A date and time value in the format YYYY-MM-DDTHH:mm:ssZ or YYYY-MM-DDTHH:mm:ss.sssZ to match the date and time format as specified by RFC 3339.",
},
"target_account": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The target account ID derived from the authentication block values. The target account exists only if the environment currently has an authorization block.",
},
"modified_at": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A date and time value in the format YYYY-MM-DDTHH:mm:ssZ or YYYY-MM-DDTHH:mm:ss.sssZ to match the date and time format as specified by RFC 3339.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A URL.",
},
"definition": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The environment definition.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The description of the environment.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the environment. It's unique within the account across projects and regions.",
},
"authorizations": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The authorization details. You can authorize by using a trusted profile or an API key in Secrets Manager.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"trusted_profile_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The trusted profile ID.",
},
"method": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The authorization method. You can authorize by using a trusted profile or an API key in Secrets Manager.",
},
"api_key": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "The IBM Cloud API Key. It can be either raw or pulled from the catalog via a `CRN` or `JSON` blob.",
},
},
},
},
"inputs": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
Description: "The input variables that are used for configuration definition and environment.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"compliance_profile": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The profile that is required for compliance.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique ID for the compliance profile.",
},
"instance_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A unique ID for the instance of a compliance profile.",
},
"instance_location": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The location of the compliance instance.",
},
"attachment_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A unique ID for the attachment to a compliance profile.",
},
"profile_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the compliance profile.",
},
},
},
},
},
},
},
},
}
}
func dataSourceIbmProjectEnvironmentRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
projectClient, err := meta.(conns.ClientSession).ProjectV1()
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_project_environment", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
getProjectEnvironmentOptions := &projectv1.GetProjectEnvironmentOptions{}
getProjectEnvironmentOptions.SetProjectID(d.Get("project_id").(string))
getProjectEnvironmentOptions.SetID(d.Get("project_environment_id").(string))
environment, _, err := projectClient.GetProjectEnvironmentWithContext(context, getProjectEnvironmentOptions)
if err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("GetProjectEnvironmentWithContext failed: %s", err.Error()), "(Data) ibm_project_environment", "read")
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
return tfErr.GetDiag()
}
d.SetId(fmt.Sprintf("%s/%s", *getProjectEnvironmentOptions.ProjectID, *getProjectEnvironmentOptions.ID))
project := []map[string]interface{}{}
if environment.Project != nil {
modelMap, err := dataSourceIbmProjectEnvironmentProjectReferenceToMap(environment.Project)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_project_environment", "read")
return tfErr.GetDiag()
}
project = append(project, modelMap)
}
if err = d.Set("project", project); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting project: %s", err), "(Data) ibm_project_environment", "read")
return tfErr.GetDiag()
}
if err = d.Set("created_at", flex.DateTimeToString(environment.CreatedAt)); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting created_at: %s", err), "(Data) ibm_project_environment", "read")
return tfErr.GetDiag()
}
if err = d.Set("target_account", environment.TargetAccount); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting target_account: %s", err), "(Data) ibm_project_environment", "read")
return tfErr.GetDiag()
}
if err = d.Set("modified_at", flex.DateTimeToString(environment.ModifiedAt)); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting modified_at: %s", err), "(Data) ibm_project_environment", "read")
return tfErr.GetDiag()
}
if err = d.Set("href", environment.Href); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting href: %s", err), "(Data) ibm_project_environment", "read")
return tfErr.GetDiag()
}
definition := []map[string]interface{}{}
if environment.Definition != nil {
modelMap, err := dataSourceIbmProjectEnvironmentEnvironmentDefinitionRequiredPropertiesResponseToMap(environment.Definition)
if err != nil {
tfErr := flex.TerraformErrorf(err, err.Error(), "(Data) ibm_project_environment", "read")
return tfErr.GetDiag()
}
definition = append(definition, modelMap)
}
if err = d.Set("definition", definition); err != nil {
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Error setting definition: %s", err), "(Data) ibm_project_environment", "read")
return tfErr.GetDiag()
}
return nil
}
func dataSourceIbmProjectEnvironmentProjectReferenceToMap(model *projectv1.ProjectReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["id"] = model.ID
modelMap["href"] = model.Href
definitionMap, err := dataSourceIbmProjectEnvironmentProjectDefinitionReferenceToMap(model.Definition)
if err != nil {
return modelMap, err
}
modelMap["definition"] = []map[string]interface{}{definitionMap}
modelMap["crn"] = model.Crn
return modelMap, nil
}
func dataSourceIbmProjectEnvironmentProjectDefinitionReferenceToMap(model *projectv1.ProjectDefinitionReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["name"] = model.Name
return modelMap, nil
}
func dataSourceIbmProjectEnvironmentEnvironmentDefinitionRequiredPropertiesResponseToMap(model *projectv1.EnvironmentDefinitionRequiredPropertiesResponse) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["description"] = model.Description
modelMap["name"] = model.Name
if model.Authorizations != nil {
authorizationsMap, err := dataSourceIbmProjectEnvironmentProjectConfigAuthToMap(model.Authorizations)
if err != nil {
return modelMap, err
}
modelMap["authorizations"] = []map[string]interface{}{authorizationsMap}
}
if model.Inputs != nil {
inputs := make(map[string]interface{})
for k, v := range model.Inputs {
inputs[k] = fmt.Sprintf("%v", v)
}
modelMap["inputs"] = inputs
}
if model.ComplianceProfile != nil {
complianceProfileMap, err := dataSourceIbmProjectEnvironmentProjectComplianceProfileToMap(model.ComplianceProfile)
if err != nil {
return modelMap, err
}
modelMap["compliance_profile"] = []map[string]interface{}{complianceProfileMap}
}
return modelMap, nil
}
func dataSourceIbmProjectEnvironmentProjectConfigAuthToMap(model *projectv1.ProjectConfigAuth) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.TrustedProfileID != nil {
modelMap["trusted_profile_id"] = model.TrustedProfileID
}
if model.Method != nil {
modelMap["method"] = model.Method
}
if model.ApiKey != nil {
modelMap["api_key"] = model.ApiKey
}
return modelMap, nil
}
func dataSourceIbmProjectEnvironmentProjectComplianceProfileToMap(model *projectv1.ProjectComplianceProfile) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ID != nil {
modelMap["id"] = model.ID
}
if model.InstanceID != nil {
modelMap["instance_id"] = model.InstanceID
}
if model.InstanceLocation != nil {
modelMap["instance_location"] = model.InstanceLocation
}
if model.AttachmentID != nil {
modelMap["attachment_id"] = model.AttachmentID
}
if model.ProfileName != nil {
modelMap["profile_name"] = model.ProfileName
}
return modelMap, nil
}