This repository has been archived by the owner on Dec 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
resource_jdcloud_subnet.go
executable file
·155 lines (122 loc) · 3.91 KB
/
resource_jdcloud_subnet.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
package jdcloud
import (
"fmt"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/apis"
"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/client"
"time"
)
func resourceJDCloudSubnet() *schema.Resource {
return &schema.Resource{
Create: resourceSubnetCreate,
Read: resourceSubnetRead,
Update: resourceSubnetUpdate,
Delete: resourceSubnetDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"vpc_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"cidr_block": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"subnet_name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceSubnetCreate(d *schema.ResourceData, m interface{}) error {
config := m.(*JDCloudConfig)
conn := client.NewVpcClient(config.Credential)
req := apis.NewCreateSubnetRequest(config.Region,
d.Get("vpc_id").(string),
d.Get("subnet_name").(string),
d.Get("cidr_block").(string))
if _, ok := d.GetOk("description"); ok {
req.Description = GetStringAddr(d, "description")
}
e := resource.Retry(20*time.Second, func() *resource.RetryError {
resp, err := conn.CreateSubnet(req)
if err == nil && resp.Error.Code == REQUEST_COMPLETED {
d.SetId(resp.Result.SubnetId)
return nil
}
if connectionError(err) {
return resource.RetryableError(formatConnectionErrorMessage())
} else {
return resource.NonRetryableError(formatErrorMessage(resp.Error, err))
}
})
if e != nil {
return e
}
return resourceSubnetRead(d, m)
}
func resourceSubnetRead(d *schema.ResourceData, m interface{}) error {
config := m.(*JDCloudConfig)
subnetClient := client.NewVpcClient(config.Credential)
req := apis.NewDescribeSubnetRequest(config.Region, d.Id())
resp, err := subnetClient.DescribeSubnet(req)
if err != nil {
return fmt.Errorf("[ERROR] resourceSubnetRead failed %s ", err.Error())
}
if resp.Error.Code == RESOURCE_NOT_FOUND {
d.SetId("")
return nil
}
if resp.Error.Code != REQUEST_COMPLETED {
return fmt.Errorf("[ERROR] resourceSubnetRead failed code:%d staus:%s message:%s ", resp.Error.Code, resp.Error.Status, resp.Error.Message)
}
d.Set("subnet_name", resp.Result.Subnet.SubnetName)
d.Set("description", resp.Result.Subnet.Description)
d.Set("cidr_block", resp.Result.Subnet.AddressPrefix)
d.Set("vpc_id", resp.Result.Subnet.VpcId)
return nil
}
func resourceSubnetUpdate(d *schema.ResourceData, m interface{}) error {
config := m.(*JDCloudConfig)
subnetClient := client.NewVpcClient(config.Credential)
if d.HasChange("subnet_name") || d.HasChange("description") {
req := apis.NewModifySubnetRequestWithAllParams(
config.Region,
d.Id(),
GetStringAddr(d, "subnet_name"),
GetStringAddr(d, "description"),
)
resp, err := subnetClient.ModifySubnet(req)
if err != nil {
return fmt.Errorf("[ERROR] resourceSubnetUpdate failed %s ", err.Error())
}
if resp.Error.Code != REQUEST_COMPLETED {
return fmt.Errorf("[ERROR] resourceSubnetUpdate failed code:%d staus:%s message:%s ", resp.Error.Code, resp.Error.Status, resp.Error.Message)
}
}
return resourceSubnetRead(d, m)
}
func resourceSubnetDelete(d *schema.ResourceData, m interface{}) error {
config := m.(*JDCloudConfig)
subnetClient := client.NewVpcClient(config.Credential)
req := apis.NewDeleteSubnetRequest(config.Region, d.Id())
resp, err := subnetClient.DeleteSubnet(req)
if err != nil {
return fmt.Errorf("[ERROR] resourceSubnetDelete failed %s ", err.Error())
}
if resp.Error.Code != REQUEST_COMPLETED {
return fmt.Errorf("[ERROR] resourceSubnetDelete failed code:%d staus:%s message:%s ", resp.Error.Code, resp.Error.Status, resp.Error.Message)
}
d.SetId("")
return nil
}