This repository has been archived by the owner on Sep 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
resource_flexibleengine_smn_topic_v2.go
143 lines (119 loc) · 3.5 KB
/
resource_flexibleengine_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
package flexibleengine
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"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,
Computed: true,
},
"push_policy": {
Type: schema.TypeInt,
Computed: true,
},
"update_time": {
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Type: schema.TypeString,
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 FlexibleEngine 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)
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 FlexibleEngine 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 FlexibleEngine 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
}
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 FlexibleEngine 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)
}
log.Printf("[DEBUG] Update : topic.TopicUrn: %s", id)
return resourceTopicRead(d, meta)
}