/
data_source_edgecenter_securitygroup.go
268 lines (241 loc) · 7.25 KB
/
data_source_edgecenter_securitygroup.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
package edgecenter
import (
"context"
"encoding/json"
"fmt"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
edgecloudV2 "github.com/Edge-Center/edgecentercloud-go/v2"
utilV2 "github.com/Edge-Center/edgecentercloud-go/v2/util"
)
func dataSourceSecurityGroup() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSecurityGroupRead,
Description: "Represent SecurityGroups(Firewall)",
Schema: map[string]*schema.Schema{
"project_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"project_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the project. Either 'project_id' or 'project_name' must be specified.",
ExactlyOneOf: []string{"project_id", "project_name"},
},
"region_id": {
Type: schema.TypeInt,
Optional: true,
Description: "The uuid of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"region_name": {
Type: schema.TypeString,
Optional: true,
Description: "The name of the region. Either 'region_id' or 'region_name' must be specified.",
ExactlyOneOf: []string{"region_id", "region_name"},
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the security group.",
},
"metadata_k": {
Type: schema.TypeString,
Optional: true,
Description: "Filtration query opts (only key).",
},
"metadata_kv": {
Type: schema.TypeMap,
Optional: true,
Description: `Filtration query opts, for example, {offset = "10", limit = "10"}`,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "A detailed description of the security group.",
},
"metadata_read_only": {
Type: schema.TypeList,
Computed: true,
Description: `A list of read-only metadata items, e.g. tags.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Computed: true,
},
"value": {
Type: schema.TypeString,
Computed: true,
},
"read_only": {
Type: schema.TypeBool,
Computed: true,
},
},
},
},
"security_group_rules": {
Type: schema.TypeSet,
Computed: true,
Description: "Firewall rules control what inbound(ingress) and outbound(egress) traffic is allowed to enter or leave a Instance. At least one 'egress' rule should be set",
Set: secGroupUniqueID,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"direction": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("Available value is '%s', '%s'", edgecloudV2.SGRuleDirectionIngress, edgecloudV2.SGRuleDirectionEgress),
},
"ethertype": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("Available value is '%s', '%s'", edgecloudV2.EtherTypeIPv4, edgecloudV2.EtherTypeIPv6),
},
"protocol": {
Type: schema.TypeString,
Computed: true,
Description: fmt.Sprintf("Available value is %s", strings.Join(utilV2.SecurityGroupRuleProtocol("").StringList(), ",")),
},
"port_range_min": {
Type: schema.TypeInt,
Computed: true,
},
"port_range_max": {
Type: schema.TypeInt,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"remote_ip_prefix": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceSecurityGroupRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start SecurityGroup reading")
var diags diag.Diagnostics
config := m.(*Config)
clientV2 := config.CloudClient
regionID, projectID, err := GetRegionIDandProjectID(ctx, clientV2, d)
if err != nil {
return diag.FromErr(err)
}
clientV2.Region = regionID
clientV2.Project = projectID
name := d.Get("name").(string)
metaOpts := &edgecloudV2.SecurityGroupListOptions{}
if metadataK, ok := d.GetOk("metadata_k"); ok {
metaOpts.MetadataK = metadataK.(string)
}
if metadataRaw, ok := d.GetOk("metadata_kv"); ok {
typedMetadataKV := make(map[string]string, len(metadataRaw.(map[string]interface{})))
for k, v := range metadataRaw.(map[string]interface{}) {
typedMetadataKV[k] = v.(string)
}
typedMetadataKVJson, err := json.Marshal(typedMetadataKV)
if err != nil {
return diag.FromErr(err)
}
metaOpts.MetadataKV = string(typedMetadataKVJson)
}
sgs, _, err := clientV2.SecurityGroups.List(ctx, metaOpts)
if err != nil {
return diag.FromErr(err)
}
var found bool
var sg edgecloudV2.SecurityGroup
for _, s := range sgs {
if s.Name == name {
sg = s
found = true
break
}
}
if !found {
return diag.Errorf("security group with name %s not found", name)
}
d.SetId(sg.ID)
d.Set("project_id", sg.ProjectID)
d.Set("region_id", sg.RegionID)
d.Set("name", sg.Name)
d.Set("description", sg.Description)
metadataReadOnly := make([]map[string]interface{}, 0, len(sg.Metadata))
if len(sg.Metadata) > 0 {
for _, metadataItem := range sg.Metadata {
metadataReadOnly = append(metadataReadOnly, map[string]interface{}{
"key": metadataItem.Key,
"value": metadataItem.Value,
"read_only": metadataItem.ReadOnly,
})
}
}
if err := d.Set("metadata_read_only", metadataReadOnly); err != nil {
return diag.FromErr(err)
}
newSgRules := make([]interface{}, len(sg.SecurityGroupRules))
for i, sgr := range sg.SecurityGroupRules {
r := make(map[string]interface{})
r["id"] = sgr.ID
r["direction"] = string(sgr.Direction)
r["ethertype"] = ""
if sgr.EtherType != nil {
r["ethertype"] = string(*sgr.EtherType)
}
r["protocol"] = edgecloudV2.SGRuleProtocolANY
if sgr.Protocol != nil {
r["protocol"] = string(*sgr.Protocol)
}
r["port_range_max"] = 65535
if sgr.PortRangeMax != nil {
r["port_range_max"] = *sgr.PortRangeMax
}
r["port_range_min"] = 1
if sgr.PortRangeMin != nil {
r["port_range_min"] = *sgr.PortRangeMin
}
r["description"] = ""
if sgr.Description != nil {
r["description"] = *sgr.Description
}
r["remote_ip_prefix"] = ""
if sgr.RemoteIPPrefix != nil {
r["remote_ip_prefix"] = *sgr.RemoteIPPrefix
}
r["updated_at"] = sgr.UpdatedAt
r["created_at"] = sgr.CreatedAt
newSgRules[i] = r
}
if err := d.Set("security_group_rules", schema.NewSet(secGroupUniqueID, newSgRules)); err != nil {
return diag.FromErr(err)
}
log.Println("[DEBUG] Finish SecurityGroup reading")
return diags
}