forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_pvtz_zone.go
167 lines (141 loc) · 4.6 KB
/
resource_alicloud_pvtz_zone.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
package alicloud
import (
"fmt"
"time"
"runtime"
"github.com/aliyun/alibaba-cloud-sdk-go/services/pvtz"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudPvtzZone() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudPvtzZoneCreate,
Read: resourceAlicloudPvtzZoneRead,
Update: resourceAlicloudPvtzZoneUpdate,
Delete: resourceAlicloudPvtzZoneDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
},
"remark": {
Type: schema.TypeString,
Optional: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
"update_time": {
Type: schema.TypeString,
Computed: true,
},
"is_ptr": {
Type: schema.TypeBool,
Computed: true,
},
"record_count": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceAlicloudPvtzZoneCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
args := pvtz.CreateAddZoneRequest()
if v, ok := d.GetOk("name"); ok && v.(string) != "" {
args.ZoneName = v.(string)
}
// API AddZone has a throttling limitation 5qps which one use only can send 5 requests in one second.
invoker := PvtzInvoker()
var raw interface{}
if err := invoker.Run(func() error {
rsp, err := client.WithPvtzClient(func(pvtzClient *pvtz.Client) (interface{}, error) {
return pvtzClient.AddZone(args)
})
raw = rsp
return err
}); err != nil {
return BuildWrapError(args.GetActionName(), args.ZoneName, AlibabaCloudSdkGoERROR, err, "")
}
response, _ := raw.(*pvtz.AddZoneResponse)
if response == nil {
return BuildWrapError(args.GetActionName(), args.ZoneName, AlibabaCloudSdkGoERROR, fmt.Errorf("AddZone got a nil response: %#v", response), "")
}
d.SetId(response.ZoneId)
return resourceAlicloudPvtzZoneUpdate(d, meta)
}
func resourceAlicloudPvtzZoneRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
pvtzService := PvtzService{client}
response, err := pvtzService.DescribePvtzZoneInfo(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return err
}
d.Set("name", response.ZoneName)
d.Set("remark", response.Remark)
d.Set("creation_time", response.CreateTime)
d.Set("update_time", response.UpdateTime)
d.Set("is_ptr", response.IsPtr)
d.Set("record_count", response.RecordCount)
return nil
}
func resourceAlicloudPvtzZoneUpdate(d *schema.ResourceData, meta interface{}) error {
request := pvtz.CreateUpdateZoneRemarkRequest()
request.ZoneId = d.Id()
if d.HasChange("remark") {
request.Remark = d.Get("remark").(string)
client := meta.(*connectivity.AliyunClient)
invoker := PvtzInvoker()
err := invoker.Run(func() error {
_, err := client.WithPvtzClient(func(pvtzClient *pvtz.Client) (interface{}, error) {
return pvtzClient.UpdateZoneRemark(request)
})
return BuildWrapError(request.GetActionName(), d.Id(), AlibabaCloudSdkGoERROR, err, "")
})
if err != nil {
return err
}
}
return resourceAlicloudPvtzZoneRead(d, meta)
}
func resourceAlicloudPvtzZoneDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
pvtzService := PvtzService{client}
runtime.Caller(0)
request := pvtz.CreateDeleteZoneRequest()
request.ZoneId = d.Id()
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := client.WithPvtzClient(func(pvtzClient *pvtz.Client) (interface{}, error) {
return pvtzClient.DeleteZone(request)
})
if err != nil {
if IsExceptedErrors(err, []string{PvtzThrottlingUser, PvtzSystemBusy, ZoneVpcExists}) {
time.Sleep(time.Duration(2) * time.Second)
return resource.RetryableError(WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR))
}
if IsExceptedErrors(err, []string{ZoneNotExists, ZoneVpcNotExists}) {
return nil
}
if !IsExceptedErrors(err, []string{PvtzInternalError}) {
return resource.NonRetryableError(WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR))
}
}
if _, err := pvtzService.DescribePvtzZoneInfo(d.Id()); err != nil {
if NotFoundError(err) {
return nil
}
return resource.NonRetryableError(WrapError(err))
}
return resource.RetryableError(WrapErrorf(err, DeleteTimeoutMsg, d.Id(), request.GetActionName(), ProviderERROR))
})
}