This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_opentelekomcloud_smn_subscription_v2.go
135 lines (119 loc) · 3.74 KB
/
resource_opentelekomcloud_smn_subscription_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
package opentelekomcloud
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/smn/v2/subscriptions"
)
func resourceSubscription() *schema.Resource {
return &schema.Resource{
Create: resourceSubscriptionCreate,
Read: resourceSubscriptionRead,
Delete: resourceSubscriptionDelete,
Schema: map[string]*schema.Schema{
"topic_urn": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"endpoint": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"protocol": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
return ValidateStringList(v, k, []string{"email", "sms", "http", "https"})
},
},
"remark": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"subscription_urn": {
Type: schema.TypeString,
Computed: true,
},
"owner": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}
func resourceSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.SmnV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud smn client: %s", err)
}
topicUrn := d.Get("topic_urn").(string)
createOpts := subscriptions.CreateOps{
Endpoint: d.Get("endpoint").(string),
Protocol: d.Get("protocol").(string),
Remark: d.Get("remark").(string),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
subscription, err := subscriptions.Create(client, createOpts, topicUrn).Extract()
if err != nil {
return fmt.Errorf("Error getting subscription from result: %s", err)
}
log.Printf("[DEBUG] Create : subscription.SubscriptionUrn %s", subscription.SubscriptionUrn)
if subscription.SubscriptionUrn != "" {
d.SetId(subscription.SubscriptionUrn)
d.Set("subscription_urn", subscription.SubscriptionUrn)
return resourceSubscriptionRead(d, meta)
}
return fmt.Errorf("Unexpected conversion error in resourceSubscriptionCreate.")
}
func resourceSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.SmnV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud smn client: %s", err)
}
log.Printf("[DEBUG] Deleting subscription %s", d.Id())
id := d.Id()
result := subscriptions.Delete(client, id)
if result.Err != nil {
return err
}
log.Printf("[DEBUG] Successfully deleted subscription %s", id)
return nil
}
func resourceSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
client, err := config.SmnV2Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("Error creating OpenTelekomCloud smn client: %s", err)
}
log.Printf("[DEBUG] Getting subscription %s", d.Id())
id := d.Id()
subscriptionslist, err := subscriptions.List(client).Extract()
if err != nil {
return fmt.Errorf("Error Get subscriptionslist: %s", err)
}
log.Printf("[DEBUG] list : subscriptionslist %#v", subscriptionslist)
for _, subscription := range subscriptionslist {
if subscription.SubscriptionUrn == id {
log.Printf("[DEBUG] subscription: %#v", subscription)
d.Set("topic_urn", subscription.TopicUrn)
d.Set("endpoint", subscription.Endpoint)
d.Set("protocol", subscription.Protocol)
d.Set("subscription_urn", subscription.SubscriptionUrn)
d.Set("owner", subscription.Owner)
d.Set("remark", subscription.Remark)
d.Set("status", subscription.Status)
}
}
log.Printf("[DEBUG] Successfully get subscription %s", id)
return nil
}