forked from aliyun/terraform-provider-alicloud
/
resource_alicloud_simple_application_server_snapshot.go
139 lines (131 loc) · 4.89 KB
/
resource_alicloud_simple_application_server_snapshot.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
package alicloud
import (
"fmt"
"log"
"regexp"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
util "github.com/alibabacloud-go/tea-utils/service"
"github.com/gunnerliu/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceAlicloudSimpleApplicationServerSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudSimpleApplicationServerSnapshotCreate,
Read: resourceAlicloudSimpleApplicationServerSnapshotRead,
Delete: resourceAlicloudSimpleApplicationServerSnapshotDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
},
Schema: map[string]*schema.Schema{
"disk_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"snapshot_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile("^[a-zA-Z\u4E00-\u9FA5][\u4E00-\u9FA5A-Za-z0-9:._-]{2,50}$"), "The name must be 2 to 50 characters in length. It must start with a letter and cannot start with `http://` or `https://`. It can contain letters, digits, colons (:), underscores (_), periods (.),and hyphens (-)."),
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAlicloudSimpleApplicationServerSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
action := "CreateSnapshot"
request := make(map[string]interface{})
conn, err := client.NewSwasClient()
if err != nil {
return WrapError(err)
}
request["DiskId"] = d.Get("disk_id")
request["RegionId"] = client.RegionId
request["SnapshotName"] = d.Get("snapshot_name")
request["ClientToken"] = buildClientToken("CreateSnapshot")
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-06-01"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_simple_application_server_snapshot", action, AlibabaCloudSdkGoERROR)
}
d.SetId(fmt.Sprint(response["SnapshotId"]))
swasOpenService := SwasOpenService{client}
stateConf := BuildStateConf([]string{}, []string{"Accomplished"}, d.Timeout(schema.TimeoutCreate), 5*time.Second, swasOpenService.SimpleApplicationServerSnapshotStateRefreshFunc(d.Id(), []string{}))
if _, err := stateConf.WaitForState(); err != nil {
return WrapErrorf(err, IdMsg, d.Id())
}
return resourceAlicloudSimpleApplicationServerSnapshotRead(d, meta)
}
func resourceAlicloudSimpleApplicationServerSnapshotRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
swasOpenService := SwasOpenService{client}
object, err := swasOpenService.DescribeSimpleApplicationServerSnapshot(d.Id())
if err != nil {
if NotFoundError(err) {
log.Printf("[DEBUG] Resource alicloud_simple_application_server_snapshot swasOpenService.DescribeSimpleApplicationServerSnapshot Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("disk_id", object["SourceDiskId"])
d.Set("snapshot_name", object["SnapshotName"])
d.Set("status", object["Status"])
return nil
}
func resourceAlicloudSimpleApplicationServerSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
action := "DeleteSnapshot"
var response map[string]interface{}
conn, err := client.NewSwasClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"SnapshotId": d.Id(),
}
request["RegionId"] = client.RegionId
request["ClientToken"] = buildClientToken("DeleteSnapshot")
runtime := util.RuntimeOptions{}
runtime.SetAutoretry(true)
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2020-06-01"), StringPointer("AK"), nil, request, &runtime)
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
return nil
}