forked from huaweicloud/terraform-provider-huaweicloud
/
resource_huaweicloud_smn_topic_v2.go
164 lines (139 loc) · 3.88 KB
/
resource_huaweicloud_smn_topic_v2.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
package huaweicloud
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/huaweicloud/golangsdk"
"github.com/huaweicloud/golangsdk/openstack/smn/v2/topics"
)
func resourceTopic() *schema.Resource {
return &schema.Resource{
Create: resourceTopicCreate,
Read: resourceTopicRead,
Delete: resourceTopicDelete,
Update: resourceTopicUpdate,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: false,
},
"topic_urn": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"push_policy": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
},
"update_time": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"create_time": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
},
}
}
func resourceTopicCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.SmnV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud smn client: %s", err)
}
createOpts := topics.CreateOps{
Name: d.Get("name").(string),
DisplayName: d.Get("display_name").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
topic, err := topics.Create(client, createOpts).Extract()
if err != nil {
return fmt.Errorf("Error getting topic from result: %s", err)
}
log.Printf("[DEBUG] Create : topic.TopicUrn %s", topic.TopicUrn)
if topic.TopicUrn != "" {
d.SetId(topic.TopicUrn)
time.Sleep(120 * time.Second)
return resourceTopicRead(d, meta)
}
return fmt.Errorf("Unexpected conversion error in resourceTopicCreate.")
}
func resourceTopicRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.SmnV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud smn client: %s", err)
}
topicUrn := d.Id()
topicGet, err := topics.Get(client, topicUrn).ExtractGet()
if err != nil {
return CheckDeleted(d, err, "topic")
}
log.Printf("[DEBUG] Retrieved topic %s: %#v", topicUrn, topicGet)
d.Set("topic_urn", topicGet.TopicUrn)
d.Set("display_name", topicGet.DisplayName)
d.Set("name", topicGet.Name)
d.Set("push_policy", topicGet.PushPolicy)
d.Set("update_time", topicGet.UpdateTime)
d.Set("create_time", topicGet.CreateTime)
return nil
}
func resourceTopicDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.SmnV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud smn client: %s", err)
}
log.Printf("[DEBUG] Deleting topic %s", d.Id())
id := d.Id()
result := topics.Delete(client, id)
if result.Err != nil {
return err
}
for {
_, err = topics.Get(client, id).ExtractGet()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
break
}
}
}
log.Printf("[DEBUG] Successfully deleted topic %s", id)
return nil
}
func resourceTopicUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.SmnV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating HuaweiCloud smn client: %s", err)
}
log.Printf("[DEBUG] Updating topic %s", d.Id())
id := d.Id()
var updateOpts topics.UpdateOps
if d.HasChange("display_name") {
updateOpts.DisplayName = d.Get("display_name").(string)
}
_, err = topics.Update(client, updateOpts, id).Extract()
if err != nil {
return fmt.Errorf("Error updating topic from result: %s", err)
}
time.Sleep(120 * time.Second)
log.Printf("[DEBUG] Update : topic.TopicUrn: %s", id)
return resourceTopicRead(d, meta)
}