This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 27
/
resource_alicloud_ons_instance.go
190 lines (159 loc) · 5.28 KB
/
resource_alicloud_ons_instance.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
package alicloud
import (
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ons"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudOnsInstance() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudOnsInstanceCreate,
Read: resourceAlicloudOnsInstanceRead,
Update: resourceAlicloudOnsInstanceUpdate,
Delete: resourceAlicloudOnsInstanceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(3, 64),
},
"remark": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(0, 128),
},
// Computed Values
"instance_type": {
Type: schema.TypeInt,
Computed: true,
},
"instance_status": {
Type: schema.TypeInt,
Computed: true,
},
"release_time": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAlicloudOnsInstanceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
onsService := OnsService{client}
request := ons.CreateOnsInstanceCreateRequest()
request.RegionId = client.RegionId
request.InstanceName = d.Get("name").(string)
if v, ok := d.GetOk("remark"); ok {
request.Remark = v.(string)
}
var response *ons.OnsInstanceCreateResponse
err := resource.Retry(5*time.Minute, func() *resource.RetryError {
raw, err := onsService.client.WithOnsClient(func(onsClient *ons.Client) (interface{}, error) {
return onsClient.OnsInstanceCreate(request)
})
if err != nil {
if IsExpectedErrors(err, []string{ThrottlingUser}) {
time.Sleep(10 * time.Second)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
response, _ = raw.(*ons.OnsInstanceCreateResponse)
return nil
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_ons_instance", request.GetActionName(), AlibabaCloudSdkGoERROR)
}
d.SetId(response.Data.InstanceId)
return resourceAlicloudOnsInstanceRead(d, meta)
}
func resourceAlicloudOnsInstanceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
onsService := OnsService{client}
response, err := onsService.DescribeOnsInstance(d.Id())
if err != nil {
// Handle exceptions
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("name", response.InstanceBaseInfo.InstanceName)
d.Set("instance_type", response.InstanceBaseInfo.InstanceType)
d.Set("instance_status", response.InstanceBaseInfo.InstanceStatus)
d.Set("release_time", time.Unix(int64(response.InstanceBaseInfo.ReleaseTime)/1000, 0).Format("2006-01-02 03:04:05"))
return nil
}
func resourceAlicloudOnsInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
onsService := OnsService{client}
attributeUpdate := false
request := ons.CreateOnsInstanceUpdateRequest()
request.RegionId = client.RegionId
request.InstanceId = d.Id()
if d.HasChange("name") {
var name string
if v, ok := d.GetOk("name"); ok {
name = v.(string)
}
request.InstanceName = name
attributeUpdate = true
}
if d.HasChange("remark") {
var remark string
if v, ok := d.GetOk("remark"); ok {
remark = v.(string)
}
request.Remark = remark
attributeUpdate = true
}
if attributeUpdate {
raw, err := onsService.client.WithOnsClient(func(onsClient *ons.Client) (interface{}, error) {
return onsClient.OnsInstanceUpdate(request)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
}
return resourceAlicloudOnsInstanceRead(d, meta)
}
func resourceAlicloudOnsInstanceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
onsService := OnsService{client}
request := ons.CreateOnsInstanceDeleteRequest()
request.RegionId = client.RegionId
request.InstanceId = d.Id()
err := resource.Retry(3*time.Minute, func() *resource.RetryError {
raw, err := onsService.client.WithOnsClient(func(onsClient *ons.Client) (interface{}, error) {
return onsClient.OnsInstanceDelete(request)
})
if err != nil {
if IsExpectedErrors(err, []string{"INSTANCE_NOT_EMPTY"}) {
return resource.RetryableError(err)
}
if IsExpectedErrors(err, []string{ThrottlingUser}) {
time.Sleep(10 * time.Second)
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
addDebug(request.GetActionName(), raw, request.RpcRequest, request)
return nil
})
if err != nil {
if IsExpectedErrors(err, []string{"INSTANCE_NOT_FOUND"}) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), request.GetActionName(), AlibabaCloudSdkGoERROR)
}
return WrapError(onsService.WaitForOnsInstance(d.Id(), Deleted, DefaultTimeoutMedium))
}