-
Notifications
You must be signed in to change notification settings - Fork 649
/
data_source_ibm_project.go
604 lines (570 loc) · 20.8 KB
/
data_source_ibm_project.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// 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 DataSourceIbmProject() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIbmProjectRead,
Schema: map[string]*schema.Schema{
"project_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The unique project ID.",
},
"crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "An IBM Cloud resource name, which 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, matching the date and time format as specified by RFC 3339.",
},
"cumulative_needs_attention_view": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The cumulative list of needs attention items for a project. If the view is successfully retrieved, an array which could be empty is returned.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"event": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The event name.",
},
"event_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A unique ID for that individual event.",
},
"config_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A unique ID for the configuration.",
},
"config_version": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The version number of the configuration.",
},
},
},
},
"cumulative_needs_attention_view_error": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "True indicates that the fetch of the needs attention items failed. It only exists if there was an error while retrieving the cumulative needs attention view.",
},
"location": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The IBM Cloud location where a resource is deployed.",
},
"resource_group_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource group id where the project's data and tools are created.",
},
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The project status value.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A URL.",
},
"resource_group": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The resource group name where the project's data and tools are created.",
},
"event_notifications_crn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The CRN of the event notifications instance if one is connected to this project.",
},
"configs": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The project configurations. These configurations are only included in the response of creating a project if a configs array is specified in the request payload.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The ID of the configuration. If this parameter is empty, an ID is automatically created for the configuration.",
},
"version": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "The version of the configuration.",
},
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The state of the configuration.",
},
"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, matching the date and time format as specified by RFC 3339.",
},
"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, matching 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 name and description of a project configuration.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A project configuration description.",
},
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The configuration name. It is unique within the account across projects and regions.",
},
},
},
},
"project": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The project referenced by this resource.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique ID.",
},
"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, which uniquely identifies a resource.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A URL.",
},
},
},
},
"deployment_model": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The configuration type.",
},
"approved_version": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "approved_version",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "state",
},
"version": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "version",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "version",
},
},
},
},
"deployed_version": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "deployed_version",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"state": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "state",
},
"version": &schema.Schema{
Type: schema.TypeInt,
Computed: true,
Description: "version",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "version",
},
},
},
},
},
},
},
"environments": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The project environments. These environments are only included in the response if project environments were created on the project.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The environment id as a friendly name.",
},
"project": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The project referenced by this resource.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The unique ID.",
},
"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, which uniquely identifies a resource.",
},
"href": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A URL.",
},
},
},
},
"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, matching 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 used in the project collection.",
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 is unique within the account across projects and regions.",
},
},
},
},
},
},
},
"definition": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The definition of the project.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "The name of the project. It is unique within the account across regions.",
},
"destroy_on_delete": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "The policy that indicates whether the resources are destroyed or not when a project is deleted.",
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "A brief explanation of the project's use in the configuration of a deployable architecture. It is possible to create a project without providing a description.",
},
"monitoring_enabled": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
Description: "A boolean flag to enable project monitoring.",
},
},
},
},
},
}
}
func dataSourceIbmProjectRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
projectClient, err := meta.(conns.ClientSession).ProjectV1()
if err != nil {
return diag.FromErr(err)
}
getProjectOptions := &projectv1.GetProjectOptions{}
getProjectOptions.SetID(d.Get("project_id").(string))
project, response, err := projectClient.GetProjectWithContext(context, getProjectOptions)
if err != nil {
log.Printf("[DEBUG] GetProjectWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetProjectWithContext failed %s\n%s", err, response))
}
d.SetId(fmt.Sprintf("%s", *getProjectOptions.ID))
if err = d.Set("crn", project.Crn); err != nil {
return diag.FromErr(fmt.Errorf("Error setting crn: %s", err))
}
if err = d.Set("created_at", flex.DateTimeToString(project.CreatedAt)); err != nil {
return diag.FromErr(fmt.Errorf("Error setting created_at: %s", err))
}
cumulativeNeedsAttentionView := []map[string]interface{}{}
if project.CumulativeNeedsAttentionView != nil {
for _, modelItem := range project.CumulativeNeedsAttentionView {
modelMap, err := dataSourceIbmProjectCumulativeNeedsAttentionToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
cumulativeNeedsAttentionView = append(cumulativeNeedsAttentionView, modelMap)
}
}
if err = d.Set("cumulative_needs_attention_view", cumulativeNeedsAttentionView); err != nil {
return diag.FromErr(fmt.Errorf("Error setting cumulative_needs_attention_view %s", err))
}
if err = d.Set("cumulative_needs_attention_view_error", project.CumulativeNeedsAttentionViewError); err != nil {
return diag.FromErr(fmt.Errorf("Error setting cumulative_needs_attention_view_error: %s", err))
}
if err = d.Set("location", project.Location); err != nil {
return diag.FromErr(fmt.Errorf("Error setting location: %s", err))
}
if err = d.Set("resource_group_id", project.ResourceGroupID); err != nil {
return diag.FromErr(fmt.Errorf("Error setting resource_group_id: %s", err))
}
if err = d.Set("state", project.State); err != nil {
return diag.FromErr(fmt.Errorf("Error setting state: %s", err))
}
if err = d.Set("href", project.Href); err != nil {
return diag.FromErr(fmt.Errorf("Error setting href: %s", err))
}
if err = d.Set("resource_group", project.ResourceGroup); err != nil {
return diag.FromErr(fmt.Errorf("Error setting resource_group: %s", err))
}
if err = d.Set("event_notifications_crn", project.EventNotificationsCrn); err != nil {
return diag.FromErr(fmt.Errorf("Error setting event_notifications_crn: %s", err))
}
configs := []map[string]interface{}{}
if project.Configs != nil {
for _, modelItem := range project.Configs {
modelMap, err := dataSourceIbmProjectProjectConfigSummaryToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
configs = append(configs, modelMap)
}
}
if err = d.Set("configs", configs); err != nil {
return diag.FromErr(fmt.Errorf("Error setting configs %s", err))
}
environments := []map[string]interface{}{}
if project.Environments != nil {
for _, modelItem := range project.Environments {
modelMap, err := dataSourceIbmProjectProjectEnvironmentSummaryToMap(&modelItem)
if err != nil {
return diag.FromErr(err)
}
environments = append(environments, modelMap)
}
}
if err = d.Set("environments", environments); err != nil {
return diag.FromErr(fmt.Errorf("Error setting environments %s", err))
}
definition := []map[string]interface{}{}
if project.Definition != nil {
modelMap, err := dataSourceIbmProjectProjectDefinitionPropertiesToMap(project.Definition)
if err != nil {
return diag.FromErr(err)
}
definition = append(definition, modelMap)
}
if err = d.Set("definition", definition); err != nil {
return diag.FromErr(fmt.Errorf("Error setting definition %s", err))
}
return nil
}
func dataSourceIbmProjectCumulativeNeedsAttentionToMap(model *projectv1.CumulativeNeedsAttention) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Event != nil {
modelMap["event"] = model.Event
}
if model.EventID != nil {
modelMap["event_id"] = model.EventID
}
if model.ConfigID != nil {
modelMap["config_id"] = model.ConfigID
}
if model.ConfigVersion != nil {
modelMap["config_version"] = flex.IntValue(model.ConfigVersion)
}
return modelMap, nil
}
func dataSourceIbmProjectProjectConfigSummaryToMap(model *projectv1.ProjectConfigSummary) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.ApprovedVersion != nil {
approvedVersionMap, err := dataSourceIbmProjectProjectConfigVersionSummaryToMap(model.ApprovedVersion)
if err != nil {
return modelMap, err
}
modelMap["approved_version"] = []map[string]interface{}{approvedVersionMap}
}
if model.DeployedVersion != nil {
deployedVersionMap, err := dataSourceIbmProjectProjectConfigVersionSummaryToMap(model.DeployedVersion)
if err != nil {
return modelMap, err
}
modelMap["deployed_version"] = []map[string]interface{}{deployedVersionMap}
}
modelMap["id"] = model.ID
modelMap["version"] = flex.IntValue(model.Version)
modelMap["state"] = model.State
modelMap["created_at"] = model.CreatedAt.String()
modelMap["modified_at"] = model.ModifiedAt.String()
modelMap["href"] = model.Href
definitionMap, err := dataSourceIbmProjectProjectConfigSummaryDefinitionToMap(model.Definition)
if err != nil {
return modelMap, err
}
modelMap["definition"] = []map[string]interface{}{definitionMap}
projectMap, err := dataSourceIbmProjectProjectReferenceToMap(model.Project)
if err != nil {
return modelMap, err
}
modelMap["project"] = []map[string]interface{}{projectMap}
if model.DeploymentModel != nil {
modelMap["deployment_model"] = model.DeploymentModel
}
return modelMap, nil
}
func dataSourceIbmProjectProjectConfigVersionSummaryToMap(model *projectv1.ProjectConfigVersionSummary) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["state"] = model.State
modelMap["version"] = flex.IntValue(model.Version)
modelMap["href"] = model.Href
return modelMap, nil
}
func dataSourceIbmProjectProjectConfigSummaryDefinitionToMap(model *projectv1.ProjectConfigSummaryDefinition) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Description != nil {
modelMap["description"] = model.Description
}
if model.Name != nil {
modelMap["name"] = model.Name
}
return modelMap, nil
}
func dataSourceIbmProjectProjectReferenceToMap(model *projectv1.ProjectReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["id"] = model.ID
definitionMap, err := dataSourceIbmProjectProjectDefinitionReferenceToMap(model.Definition)
if err != nil {
return modelMap, err
}
modelMap["definition"] = []map[string]interface{}{definitionMap}
modelMap["crn"] = model.Crn
modelMap["href"] = model.Href
return modelMap, nil
}
func dataSourceIbmProjectProjectDefinitionReferenceToMap(model *projectv1.ProjectDefinitionReference) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["name"] = model.Name
return modelMap, nil
}
func dataSourceIbmProjectProjectEnvironmentSummaryToMap(model *projectv1.ProjectEnvironmentSummary) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["id"] = model.ID
projectMap, err := dataSourceIbmProjectProjectReferenceToMap(model.Project)
if err != nil {
return modelMap, err
}
modelMap["project"] = []map[string]interface{}{projectMap}
modelMap["created_at"] = model.CreatedAt.String()
modelMap["href"] = model.Href
definitionMap, err := dataSourceIbmProjectProjectEnvironmentSummaryDefinitionToMap(model.Definition)
if err != nil {
return modelMap, err
}
modelMap["definition"] = []map[string]interface{}{definitionMap}
return modelMap, nil
}
func dataSourceIbmProjectProjectEnvironmentSummaryDefinitionToMap(model *projectv1.ProjectEnvironmentSummaryDefinition) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.Description != nil {
modelMap["description"] = model.Description
}
modelMap["name"] = model.Name
return modelMap, nil
}
func dataSourceIbmProjectProjectDefinitionPropertiesToMap(model *projectv1.ProjectDefinitionProperties) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
modelMap["name"] = model.Name
modelMap["destroy_on_delete"] = model.DestroyOnDelete
modelMap["description"] = model.Description
modelMap["monitoring_enabled"] = model.MonitoringEnabled
return modelMap, nil
}