forked from aliyun/terraform-provider-alicloud
/
resource_alicloud_mns_topic.go
117 lines (105 loc) · 3.57 KB
/
resource_alicloud_mns_topic.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
package alicloud
import (
ali_mns "github.com/aliyun/aliyun-mns-go-sdk"
"github.com/gunnerliu/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
)
func resourceAlicloudMNSTopic() *schema.Resource {
return &schema.Resource{
Create: resourceAlicloudMNSTopicCreate,
Read: resourceAlicloudMNSTopicRead,
Update: resourceAlicloudMNSTopicUpdate,
Delete: resourceAlicloudMNSTopicDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(3, 256),
},
"maximum_message_size": {
Type: schema.TypeInt,
Optional: true,
Default: 65536,
ValidateFunc: validation.IntBetween(1024, 65536),
},
"logging_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceAlicloudMNSTopicCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
name := d.Get("name").(string)
maximumMessageSize := d.Get("maximum_message_size").(int)
loggingEnabled := d.Get("logging_enabled").(bool)
raw, err := client.WithMnsTopicManager(func(topicManager ali_mns.AliTopicManager) (interface{}, error) {
return nil, topicManager.CreateTopic(name, int32(maximumMessageSize), loggingEnabled)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_mns_topic", "CreateTopic", AliMnsERROR)
}
addDebug("CreateTopic", raw)
d.SetId(name)
return resourceAlicloudMNSTopicRead(d, meta)
}
func resourceAlicloudMNSTopicRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
mnsService := MnsService{client}
object, err := mnsService.DescribeMnsTopic(d.Id())
if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapError(err)
}
d.Set("name", object.TopicName)
d.Set("maximum_message_size", object.MaxMessageSize)
d.Set("logging_enabled", object.LoggingEnabled)
return nil
}
func resourceAlicloudMNSTopicUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
attributeUpdate := false
maximumMessageSize := d.Get("maximum_message_size").(int)
loggingEnabled := d.Get("logging_enabled").(bool)
if d.HasChange("maximum_message_size") {
attributeUpdate = true
}
if d.HasChange("logging_enabled") {
attributeUpdate = true
}
if attributeUpdate {
raw, err := client.WithMnsTopicManager(func(topicManager ali_mns.AliTopicManager) (interface{}, error) {
return nil, topicManager.SetTopicAttributes(d.Id(), int32(maximumMessageSize), loggingEnabled)
})
if err != nil {
return WrapErrorf(err, DefaultErrorMsg, d.Id(), "SetTopicAttributes", AliMnsERROR)
}
addDebug("SetTopicAttributes", raw)
}
return resourceAlicloudMNSTopicRead(d, meta)
}
func resourceAlicloudMNSTopicDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
mnsService := MnsService{client}
raw, err := client.WithMnsTopicManager(func(topicManager ali_mns.AliTopicManager) (interface{}, error) {
return nil, topicManager.DeleteTopic(d.Id())
})
if err != nil {
if mnsService.TopicNotExistFunc(err) {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), "DeleteTopic", AliMnsERROR)
}
addDebug("DeleteTopic", raw)
return WrapError(mnsService.WaitForMnsTopic(d.Id(), Deleted, DefaultTimeout))
}