/
resource_huaweicloud_vpc_sub_network_interface.go
280 lines (247 loc) · 9.5 KB
/
resource_huaweicloud_vpc_sub_network_interface.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
package vpc
import (
"context"
"fmt"
"net/http"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/jmespath/go-jmespath"
"github.com/chnsz/golangsdk"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils"
)
// @API VPC POST /v3/{project_id}/vpc/sub-network-interfaces
// @API VPC GET /v3/{project_id}/vpc/sub-network-interfaces/{sub_network_interface_id}
// @API VPC PUT /v3/{project_id}/vpc/sub-network-interfaces/{sub_network_interface_id}
// @API VPC DELETE /v3/{project_id}/vpc/sub-network-interfaces/{sub_network_interface_id}
func ResourceSubNetworkInterface() *schema.Resource {
return &schema.Resource{
CreateContext: resourceSubNetworkInterfaceCreate,
ReadContext: resourceSubNetworkInterfaceRead,
UpdateContext: resourceSubNetworkInterfaceUpdate,
DeleteContext: resourceSubNetworkInterfaceDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"subnet_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"parent_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"security_group_ids": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"vlan_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"ipv6_enable": {
Type: schema.TypeBool,
Optional: true,
},
"ipv6_ip_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"mac_address": {
Type: schema.TypeString,
Computed: true,
},
"parent_device_id": {
Type: schema.TypeString,
Computed: true,
},
"vpc_id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func buildCreateSubNetworkInterfaceBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"sub_network_interface": map[string]interface{}{
"virsubnet_id": d.Get("subnet_id"),
"parent_id": d.Get("parent_id"),
"security_groups": utils.ValueIngoreEmpty(d.Get("security_group_ids")),
"description": utils.ValueIngoreEmpty(d.Get("description")),
"vlan_id": utils.ValueIngoreEmpty(d.Get("vlan_id")),
"private_ip_address": utils.ValueIngoreEmpty(d.Get("ip_address")),
"ipv6_enable": utils.ValueIngoreEmpty(d.Get("ipv6_enable")),
"ipv6_ip_address": utils.ValueIngoreEmpty(d.Get("ipv6_ip_address")),
},
}
return bodyParams
}
func resourceSubNetworkInterfaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("vpcv3", region)
if err != nil {
return diag.Errorf("error creating VPC v3 client: %s", err)
}
createSubNetworkInterfaceHttpUrl := "vpc/sub-network-interfaces"
createSubNetworkInterfacePath := client.ResourceBaseURL() + createSubNetworkInterfaceHttpUrl
createSubNetworkInterfaceOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
201,
},
}
createSubNetworkInterfaceOpt.JSONBody = utils.RemoveNil(buildCreateSubNetworkInterfaceBodyParams(d))
createSubNetworkInterfaceResp, err := client.Request("POST", createSubNetworkInterfacePath, &createSubNetworkInterfaceOpt)
if err != nil {
return diag.Errorf("error creating supplementary network interface: %s", err)
}
createcreateSubNetworkInterfaceRespBody, err := utils.FlattenResponse(createSubNetworkInterfaceResp)
if err != nil {
return diag.FromErr(err)
}
id, err := jmespath.Search("sub_network_interface.id", createcreateSubNetworkInterfaceRespBody)
if err != nil || id == nil {
return diag.Errorf("error creating supplementary network interface: %s is not found in API response", id)
}
d.SetId(id.(string))
return resourceSubNetworkInterfaceRead(ctx, d, meta)
}
func getSubNetworkInterfaceInfo(d *schema.ResourceData, meta interface{}) (*http.Response, error) {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("vpcv3", region)
if err != nil {
return nil, fmt.Errorf("error creating VPC v3 client: %s", err)
}
getSubNetworkInterfaceHttpUrl := "vpc/sub-network-interfaces/" + d.Id()
getSubNetworkInterfacePath := client.ResourceBaseURL() + getSubNetworkInterfaceHttpUrl
getSubNetworkInterfaceOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
resp, err := client.Request("GET", getSubNetworkInterfacePath, &getSubNetworkInterfaceOpt)
if err != nil {
return nil, err
}
return resp, nil
}
func resourceSubNetworkInterfaceRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
getSubNetworkInterfaceResp, err := getSubNetworkInterfaceInfo(d, meta)
if err != nil {
return common.CheckDeletedDiag(d, err, "VPC supplementary network interface")
}
getSubNetworkInterfaceRespBody, err := utils.FlattenResponse(getSubNetworkInterfaceResp)
if err != nil {
return diag.FromErr(err)
}
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
mErr := multierror.Append(
nil,
d.Set("region", region),
d.Set("subnet_id", utils.PathSearch("sub_network_interface.virsubnet_id", getSubNetworkInterfaceRespBody, nil)),
d.Set("parent_id", utils.PathSearch("sub_network_interface.parent_id", getSubNetworkInterfaceRespBody, nil)),
d.Set("security_group_ids", utils.PathSearch("sub_network_interface.security_groups", getSubNetworkInterfaceRespBody, nil)),
d.Set("description", utils.PathSearch("sub_network_interface.description", getSubNetworkInterfaceRespBody, nil)),
d.Set("vlan_id", fmt.Sprintf("%v", utils.PathSearch("sub_network_interface.vlan_id", getSubNetworkInterfaceRespBody, nil))),
d.Set("ip_address", utils.PathSearch("sub_network_interface.private_ip_address", getSubNetworkInterfaceRespBody, nil)),
d.Set("ipv6_enable", utils.PathSearch("sub_network_interface.ipv6_enable", getSubNetworkInterfaceRespBody, nil)),
d.Set("ipv6_ip_address", utils.PathSearch("sub_network_interface.ipv6_ip_address", getSubNetworkInterfaceRespBody, nil)),
d.Set("mac_address", utils.PathSearch("sub_network_interface.mac_address", getSubNetworkInterfaceRespBody, nil)),
d.Set("parent_device_id", utils.PathSearch("sub_network_interface.parent_device_id", getSubNetworkInterfaceRespBody, nil)),
d.Set("vpc_id", utils.PathSearch("sub_network_interface.vpc_id", getSubNetworkInterfaceRespBody, nil)),
d.Set("status", utils.PathSearch("sub_network_interface.state", getSubNetworkInterfaceRespBody, nil)),
d.Set("created_at", utils.PathSearch("sub_network_interface.created_at", getSubNetworkInterfaceRespBody, nil)),
)
return diag.FromErr(mErr.ErrorOrNil())
}
func buildUpdateSubNetworkInterfaceBodyParams(d *schema.ResourceData) map[string]interface{} {
bodyParams := map[string]interface{}{
"sub_network_interface": map[string]interface{}{
"description": d.Get("description"),
"security_groups": utils.ValueIngoreEmpty(d.Get("security_group_ids")),
},
}
return bodyParams
}
func resourceSubNetworkInterfaceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("vpcv3", region)
if err != nil {
return diag.Errorf("error creating VPC v3 Client: %s", err)
}
if d.HasChanges("security_group_ids", "description") {
updateSubNetworkInterfaceHttpUrl := "vpc/sub-network-interfaces/" + d.Id()
updateSubNetworkInterfacePath := client.ResourceBaseURL() + updateSubNetworkInterfaceHttpUrl
updateSubNetworkInterfaceOpts := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
200,
},
}
updateSubNetworkInterfaceOpts.JSONBody = utils.RemoveNil(buildUpdateSubNetworkInterfaceBodyParams(d))
_, err = client.Request("PUT", updateSubNetworkInterfacePath, &updateSubNetworkInterfaceOpts)
if err != nil {
return diag.Errorf("error updating VPC supplementary network interface: %s", err)
}
}
return resourceSubNetworkInterfaceRead(ctx, d, meta)
}
func resourceSubNetworkInterfaceDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.NewServiceClient("vpcv3", region)
if err != nil {
return diag.Errorf("error creating VPC v3 Client: %s", err)
}
deleteSubNetworkInterfaceHttpUrl := "vpc/sub-network-interfaces/" + d.Id()
deleteSubNetworkInterfacePath := client.ResourceBaseURL() + deleteSubNetworkInterfaceHttpUrl
deleteSubNetworkInterfaceOpt := golangsdk.RequestOpts{
KeepResponseBody: true,
OkCodes: []int{
204,
},
}
_, err = client.Request("DELETE", deleteSubNetworkInterfacePath, &deleteSubNetworkInterfaceOpt)
if err != nil {
return diag.Errorf("error deleting supplementary network interface: %s", err)
}
return nil
}