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_eip.go
executable file
·142 lines (117 loc) · 3.78 KB
/
resource_jdcloud_eip.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
package jdcloud
import (
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jdcloud-api/jdcloud-sdk-go/services/charge/models"
"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/apis"
"github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/client"
vpcModels "github.com/jdcloud-api/jdcloud-sdk-go/services/vpc/models"
"time"
)
func resourceJDCloudEIP() *schema.Resource {
return &schema.Resource{
Create: resourceJDCloudEIPCreate,
Read: resourceJDCloudEIPRead,
Delete: resourceJDCloudEIPDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"eip_provider": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"bandwidth_mbps": &schema.Schema{
Type: schema.TypeInt,
Required: true,
ForceNew: true,
},
"elastic_ip_address": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
},
}
}
func resourceJDCloudEIPCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*JDCloudConfig)
elasticIpSpec := vpcModels.ElasticIpSpec{
BandwidthMbps: d.Get("bandwidth_mbps").(int),
Provider: d.Get("eip_provider").(string),
ChargeSpec: &models.ChargeSpec{},
}
vpcClient := client.NewVpcClient(config.Credential)
req := apis.NewCreateElasticIpsRequest(config.Region, MAX_EIP_COUNT, &elasticIpSpec)
if _, ok := d.GetOk("elastic_ip_address"); ok {
req.ElasticIpAddress = GetStringAddr(d, "elastic_ip_address")
}
err := resource.Retry(20*time.Second, func() *resource.RetryError {
resp, err := vpcClient.CreateElasticIps(req)
if err == nil && resp.Error.Code == REQUEST_COMPLETED {
d.SetId(resp.Result.ElasticIpIds[0])
return nil
}
if connectionError(err) {
return resource.RetryableError(formatConnectionErrorMessage())
} else {
return resource.NonRetryableError(formatErrorMessage(resp.Error, err))
}
})
if err != nil {
return err
}
return resourceJDCloudEIPRead(d, meta)
}
func resourceJDCloudEIPRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*JDCloudConfig)
req := apis.NewDescribeElasticIpRequest(config.Region, d.Id())
vpcClient := client.NewVpcClient(config.Credential)
return resource.Retry(time.Minute, func() *resource.RetryError {
resp, err := vpcClient.DescribeElasticIp(req)
if err == nil && resp.Error.Code == REQUEST_COMPLETED {
d.Set("elastic_ip_address", resp.Result.ElasticIp.ElasticIpAddress)
d.Set("bandwidth_mbps", resp.Result.ElasticIp.BandwidthMbps)
d.Set("eip_provider", resp.Result.ElasticIp.Provider)
return nil
}
if resp.Error.Code == RESOURCE_NOT_FOUND {
d.SetId("")
return nil
}
if connectionError(err) {
return resource.RetryableError(formatConnectionErrorMessage())
} else {
return resource.NonRetryableError(formatErrorMessage(resp.Error, err))
}
})
}
func resourceJDCloudEIPDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*JDCloudConfig)
elasticIpId := d.Id()
rq := apis.NewDeleteElasticIpRequest(config.Region, elasticIpId)
vpcClient := client.NewVpcClient(config.Credential)
return resource.Retry(20*time.Second, func() *resource.RetryError {
resp, err := vpcClient.DeleteElasticIp(rq)
if err == nil && resp.Error.Code == REQUEST_COMPLETED {
d.SetId("")
return nil
}
if resp.Error.Code == RESOURCE_NOT_FOUND {
d.SetId("")
return nil
}
if connectionError(err) {
return resource.RetryableError(formatConnectionErrorMessage())
} else {
return resource.NonRetryableError(formatErrorMessage(resp.Error, err))
}
})
}
/*
Log:
Its been tested that you can not tell if an EIP has been detached or not
Since there is no info on "status" about EIP
*/