forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 2
/
resource_arm_servicebus_subscription.go
212 lines (174 loc) · 5.82 KB
/
resource_arm_servicebus_subscription.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package azurerm
import (
"fmt"
"log"
"net/http"
"github.com/Azure/azure-sdk-for-go/arm/servicebus"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceArmServiceBusSubscription() *schema.Resource {
return &schema.Resource{
Create: resourceArmServiceBusSubscriptionCreate,
Read: resourceArmServiceBusSubscriptionRead,
Update: resourceArmServiceBusSubscriptionCreate,
Delete: resourceArmServiceBusSubscriptionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"namespace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"topic_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
StateFunc: azureRMNormalizeLocation,
},
"resource_group_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"auto_delete_on_idle": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"default_message_ttl": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"lock_duration": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"dead_lettering_on_filter_evaluation_exceptions": {
Type: schema.TypeBool,
Optional: true,
},
"dead_lettering_on_message_expiration": {
Type: schema.TypeBool,
Optional: true,
},
"enable_batched_operations": {
Type: schema.TypeBool,
Optional: true,
},
"max_delivery_count": {
Type: schema.TypeInt,
Required: true,
},
"requires_session": {
Type: schema.TypeBool,
Optional: true,
// cannot be modified
ForceNew: true,
},
},
}
}
func resourceArmServiceBusSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).serviceBusSubscriptionsClient
log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Subscription creation.")
name := d.Get("name").(string)
topicName := d.Get("topic_name").(string)
namespaceName := d.Get("namespace_name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
parameters := servicebus.SubscriptionCreateOrUpdateParameters{
Location: &location,
Properties: &servicebus.SubscriptionProperties{},
}
if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" {
parameters.Properties.AutoDeleteOnIdle = &autoDeleteOnIdle
}
if lockDuration := d.Get("lock_duration").(string); lockDuration != "" {
parameters.Properties.LockDuration = &lockDuration
}
deadLetteringFilterExceptions := d.Get("dead_lettering_on_filter_evaluation_exceptions").(bool)
deadLetteringExpiration := d.Get("dead_lettering_on_message_expiration").(bool)
enableBatchedOps := d.Get("enable_batched_operations").(bool)
maxDeliveryCount := int32(d.Get("max_delivery_count").(int))
requiresSession := d.Get("requires_session").(bool)
parameters.Properties.DeadLetteringOnFilterEvaluationExceptions = &deadLetteringFilterExceptions
parameters.Properties.DeadLetteringOnMessageExpiration = &deadLetteringExpiration
parameters.Properties.EnableBatchedOperations = &enableBatchedOps
parameters.Properties.MaxDeliveryCount = &maxDeliveryCount
parameters.Properties.RequiresSession = &requiresSession
_, err := client.CreateOrUpdate(resGroup, namespaceName, topicName, name, parameters)
if err != nil {
return err
}
read, err := client.Get(resGroup, namespaceName, topicName, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read ServiceBus Subscription %s (resource group %s) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmServiceBusSubscriptionRead(d, meta)
}
func resourceArmServiceBusSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).serviceBusSubscriptionsClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
namespaceName := id.Path["namespaces"]
topicName := id.Path["topics"]
name := id.Path["subscriptions"]
log.Printf("[INFO] subscriptionID: %s, args: %s, %s, %s, %s", d.Id(), resGroup, namespaceName, topicName, name)
resp, err := client.Get(resGroup, namespaceName, topicName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure ServiceBus Subscription %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("namespace_name", namespaceName)
d.Set("topic_name", topicName)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
props := resp.Properties
d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle)
d.Set("default_message_ttl", props.DefaultMessageTimeToLive)
d.Set("lock_duration", props.LockDuration)
d.Set("dead_lettering_on_filter_evaluation_exceptions", props.DeadLetteringOnFilterEvaluationExceptions)
d.Set("dead_lettering_on_message_expiration", props.DeadLetteringOnMessageExpiration)
d.Set("enable_batched_operations", props.EnableBatchedOperations)
d.Set("max_delivery_count", int(*props.MaxDeliveryCount))
d.Set("requires_session", props.RequiresSession)
return nil
}
func resourceArmServiceBusSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).serviceBusSubscriptionsClient
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
namespaceName := id.Path["namespaces"]
topicName := id.Path["topics"]
name := id.Path["subscriptions"]
_, err = client.Delete(resGroup, namespaceName, topicName, name)
return err
}