forked from aliyun/terraform-provider-alicloud
/
resource_alicloud_dfs_access_group.go
174 lines (167 loc) · 5.48 KB
/
resource_alicloud_dfs_access_group.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
package alicloud
import (
"fmt"
"log"
"time"
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"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceAlicloudDfsAccessGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudDfsAccessGroupCreate,
Read: resourceAlicloudDfsAccessGroupRead,
Update: resourceAlicloudDfsAccessGroupUpdate,
Delete: resourceAlicloudDfsAccessGroupDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"access_group_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"description": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringLenBetween(1, 100),
},
"network_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{"VPC"}, false),
},
},
}
}
func resourceAlicloudDfsAccessGroupCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
var response map[string]interface{}
action := "CreateAccessGroup"
request := make(map[string]interface{})
conn, err := client.NewAlidfsClient()
if err != nil {
return WrapError(err)
}
request["AccessGroupName"] = d.Get("access_group_name")
if v, ok := d.GetOk("description"); ok {
request["Description"] = v
}
request["NetworkType"] = d.Get("network_type")
request["InputRegionId"] = client.RegionId
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("2018-06-20"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
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_dfs_access_group", action, AlibabaCloudSdkGoERROR)
}
d.SetId(fmt.Sprint(response["AccessGroupId"]))
return resourceAlicloudDfsAccessGroupRead(d, meta)
}
func resourceAlicloudDfsAccessGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
dfsService := DfsService{client}
object, err := dfsService.DescribeDfsAccessGroup(d.Id())
if err != nil {
if NotFoundError(err) {
log.Printf("[DEBUG] Resource alicloud_dfs_access_group dfsService.DescribeDfsAccessGroup Failed!!! %s", err)
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("access_group_name", object["AccessGroupName"])
d.Set("description", object["Description"])
d.Set("network_type", object["NetworkType"])
return nil
}
func resourceAlicloudDfsAccessGroupUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
conn, err := client.NewAlidfsClient()
if err != nil {
return WrapError(err)
}
var response map[string]interface{}
update := false
request := map[string]interface{}{
"AccessGroupId": d.Id(),
}
request["InputRegionId"] = client.RegionId
if d.HasChange("access_group_name") {
update = true
request["AccessGroupName"] = d.Get("access_group_name")
}
if d.HasChange("description") {
update = true
if v, ok := d.GetOk("description"); ok {
request["Description"] = v
}
}
if update {
action := "ModifyAccessGroup"
wait := incrementalWait(3*time.Second, 3*time.Second)
err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
response, err = conn.DoRequest(StringPointer(action), nil, StringPointer("POST"), StringPointer("2018-06-20"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
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 resourceAlicloudDfsAccessGroupRead(d, meta)
}
func resourceAlicloudDfsAccessGroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
action := "DeleteAccessGroup"
var response map[string]interface{}
conn, err := client.NewAlidfsClient()
if err != nil {
return WrapError(err)
}
request := map[string]interface{}{
"AccessGroupId": d.Id(),
}
request["InputRegionId"] = client.RegionId
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("2018-06-20"), StringPointer("AK"), nil, request, &util.RuntimeOptions{})
if err != nil {
if NeedRetry(err) {
wait()
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
addDebug(action, response, request)
if err != nil {
if IsExpectedErrors(err, []string{"InvalidParameter.AccessGroupNotFound"}) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
}
return nil
}