-
Notifications
You must be signed in to change notification settings - Fork 640
/
data_source_ibm_scc_instance_settings.go
163 lines (148 loc) · 5.03 KB
/
data_source_ibm_scc_instance_settings.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
// Copyright IBM Corp. 2023 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package scc
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/go-sdk-core/v5/core"
"github.com/IBM/scc-go-sdk/v5/securityandcompliancecenterapiv3"
)
func DataSourceIbmSccInstanceSettings() *schema.Resource {
return AddSchemaData(&schema.Resource{
ReadContext: dataSourceIbmSccInstanceSettingsRead,
Schema: map[string]*schema.Schema{
"event_notifications": {
Type: schema.TypeList,
Computed: true,
Description: "The Event Notifications settings.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_crn": {
Type: schema.TypeString,
Computed: true,
Description: "The Event Notifications instance CRN.",
},
"updated_on": {
Type: schema.TypeString,
Computed: true,
Description: "The date when the Event Notifications connection was updated.",
},
"source_id": {
Type: schema.TypeString,
Computed: true,
Description: "The connected Security and Compliance Center instance CRN.",
},
},
},
},
"object_storage": {
Type: schema.TypeList,
Computed: true,
Description: "The Cloud Object Storage settings.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"instance_crn": {
Type: schema.TypeString,
Computed: true,
Description: "The connected Cloud Object Storage instance CRN.",
},
"bucket": {
Type: schema.TypeString,
Computed: true,
Description: "The connected Cloud Object Storage bucket name.",
},
"bucket_location": {
Type: schema.TypeString,
Computed: true,
Description: "The connected Cloud Object Storage bucket location.",
},
"bucket_endpoint": {
Type: schema.TypeString,
Computed: true,
Description: "The connected Cloud Object Storage bucket endpoint.",
},
"updated_on": {
Type: schema.TypeString,
Computed: true,
Description: "The date when the bucket connection was updated.",
},
},
},
},
},
})
}
func dataSourceIbmSccInstanceSettingsRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
adminClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3()
if err != nil {
return diag.FromErr(err)
}
getSettingsOptions := &securityandcompliancecenterapiv3.GetSettingsOptions{}
getSettingsOptions.SetInstanceID(d.Get("instance_id").(string))
settings, response, err := adminClient.GetSettingsWithContext(context, getSettingsOptions)
service_url := adminClient.GetServiceURL()
d.SetId(service_url)
if err != nil {
if response != nil && response.StatusCode == 404 {
d.SetId("")
return nil
}
log.Printf("[DEBUG] GetSettingsWithContext failed %s\n%s", err, response)
return diag.FromErr(fmt.Errorf("GetSettingsWithContext failed %s\n%s", err, response))
}
if !core.IsNil(settings.EventNotifications) {
eventNotificationsMap, err := dataSourceIbmSccInstanceSettingsEventNotificationsToMap(settings.EventNotifications)
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("event_notifications", []map[string]interface{}{eventNotificationsMap}); err != nil {
return diag.FromErr(fmt.Errorf("Error setting event_notifications: %s", err))
}
}
if !core.IsNil(settings.ObjectStorage) {
objectStorageMap, err := dataSourceIbmSccInstanceSettingsObjectStorageToMap(settings.ObjectStorage)
if err != nil {
return diag.FromErr(err)
}
if err = d.Set("object_storage", []map[string]interface{}{objectStorageMap}); err != nil {
return diag.FromErr(fmt.Errorf("Error setting object_storage: %s", err))
}
}
return nil
}
func dataSourceIbmSccInstanceSettingsEventNotificationsToMap(model *securityandcompliancecenterapiv3.EventNotifications) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.InstanceCrn != nil {
modelMap["instance_crn"] = model.InstanceCrn
}
if model.UpdatedOn != nil {
modelMap["updated_on"] = model.UpdatedOn.String()
}
if model.SourceID != nil {
modelMap["source_id"] = model.SourceID
}
return modelMap, nil
}
func dataSourceIbmSccInstanceSettingsObjectStorageToMap(model *securityandcompliancecenterapiv3.ObjectStorage) (map[string]interface{}, error) {
modelMap := make(map[string]interface{})
if model.InstanceCrn != nil {
modelMap["instance_crn"] = model.InstanceCrn
}
if model.Bucket != nil {
modelMap["bucket"] = model.Bucket
}
if model.BucketLocation != nil {
modelMap["bucket_location"] = model.BucketLocation
}
if model.BucketEndpoint != nil {
modelMap["bucket_endpoint"] = model.BucketEndpoint
}
if model.UpdatedOn != nil {
modelMap["updated_on"] = model.UpdatedOn.String()
}
return modelMap, nil
}