forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_dns_group.go
119 lines (102 loc) · 3.22 KB
/
resource_alicloud_dns_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
package alicloud
import (
"fmt"
"time"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/dns"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudDnsGroup() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudDnsGroupCreate,
Read: resourceAlicloudDnsGroupRead,
Update: resourceAlicloudDnsGroupUpdate,
Delete: resourceAlicloudDnsGroupDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}
func resourceAlicloudDnsGroupCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
args := &dns.AddDomainGroupArgs{
GroupName: d.Get("name").(string),
}
raw, err := client.WithDnsClient(func(dnsClient *dns.Client) (interface{}, error) {
return dnsClient.AddDomainGroup(args)
})
if err != nil {
return fmt.Errorf("AddDomainGroup got a error: %#v", err)
}
response, _ := raw.(*dns.AddDomainGroupResponse)
d.SetId(response.GroupId)
d.Set("name", response.GroupName)
return resourceAlicloudDnsGroupUpdate(d, meta)
}
func resourceAlicloudDnsGroupUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
d.Partial(true)
args := &dns.UpdateDomainGroupArgs{
GroupId: d.Id(),
}
if d.HasChange("name") && !d.IsNewResource() {
d.SetPartial("name")
args.GroupName = d.Get("name").(string)
_, err := client.WithDnsClient(func(dnsClient *dns.Client) (interface{}, error) {
return dnsClient.UpdateDomainGroup(args)
})
if err != nil {
return fmt.Errorf("UpdateDomainGroup got an error: %#v", err)
}
}
d.Partial(false)
return resourceAlicloudDnsGroupRead(d, meta)
}
func resourceAlicloudDnsGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
args := &dns.DescribeDomainGroupsArgs{
KeyWord: d.Get("name").(string),
}
raw, err := client.WithDnsClient(func(dnsClient *dns.Client) (interface{}, error) {
return dnsClient.DescribeDomainGroups(args)
})
if err != nil {
return err
}
groups, _ := raw.([]dns.DomainGroupType)
if groups == nil || len(groups) <= 0 {
return fmt.Errorf("No domain groups found.")
}
for _, v := range groups {
if v.GroupName == d.Get("name").(string) {
d.Set("name", v.GroupName)
return nil
}
}
d.SetId("")
return nil
}
func resourceAlicloudDnsGroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
args := &dns.DeleteDomainGroupArgs{
GroupId: d.Id(),
}
return resource.Retry(5*time.Minute, func() *resource.RetryError {
_, err := client.WithDnsClient(func(dnsClient *dns.Client) (interface{}, error) {
return dnsClient.DeleteDomainGroup(args)
})
if err != nil {
e, _ := err.(*common.Error)
if e.ErrorResponse.Code == FobiddenNotEmptyGroup {
return resource.RetryableError(fmt.Errorf("The domain group can’t be deleted because it is not empty - trying again after it empty."))
}
return resource.NonRetryableError(fmt.Errorf("Error deleting group %s: %#v", d.Id(), err))
}
return nil
})
}