-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
resource_arm_servicebus_subscription.go
204 lines (167 loc) · 5.63 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
package azurerm
import (
"fmt"
"log"
"github.com/Azure/azure-sdk-for-go/services/servicebus/mgmt/2017-04-01/servicebus"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
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": deprecatedLocationSchema(),
"resource_group_name": resourceGroupNameSchema(),
"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_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,
},
// TODO: remove in the next major version
"dead_lettering_on_filter_evaluation_exceptions": {
Type: schema.TypeBool,
Optional: true,
Deprecated: "This field has been deprecated by Azure",
},
},
}
}
func resourceArmServiceBusSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).serviceBusSubscriptionsClient
ctx := meta.(*ArmClient).StopContext
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)
resourceGroup := d.Get("resource_group_name").(string)
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 := servicebus.SBSubscription{
SBSubscriptionProperties: &servicebus.SBSubscriptionProperties{
DeadLetteringOnMessageExpiration: &deadLetteringExpiration,
EnableBatchedOperations: &enableBatchedOps,
MaxDeliveryCount: &maxDeliveryCount,
RequiresSession: &requiresSession,
},
}
if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" {
parameters.SBSubscriptionProperties.AutoDeleteOnIdle = &autoDeleteOnIdle
}
if lockDuration := d.Get("lock_duration").(string); lockDuration != "" {
parameters.SBSubscriptionProperties.LockDuration = &lockDuration
}
_, err := client.CreateOrUpdate(ctx, resourceGroup, namespaceName, topicName, name, parameters)
if err != nil {
return err
}
read, err := client.Get(ctx, resourceGroup, 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, resourceGroup)
}
d.SetId(*read.ID)
return resourceArmServiceBusSubscriptionRead(d, meta)
}
func resourceArmServiceBusSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).serviceBusSubscriptionsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
namespaceName := id.Path["namespaces"]
topicName := id.Path["topics"]
name := id.Path["subscriptions"]
resp, err := client.Get(ctx, resourceGroup, namespaceName, topicName, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on Azure ServiceBus Subscription %q (Resource Group %q): %+v", name, resourceGroup, err)
}
d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("namespace_name", namespaceName)
d.Set("topic_name", topicName)
if props := resp.SBSubscriptionProperties; props != nil {
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_message_expiration", props.DeadLetteringOnMessageExpiration)
d.Set("enable_batched_operations", props.EnableBatchedOperations)
d.Set("requires_session", props.RequiresSession)
if count := props.MaxDeliveryCount; count != nil {
d.Set("max_delivery_count", int(*count))
}
}
return nil
}
func resourceArmServiceBusSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).serviceBusSubscriptionsClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
namespaceName := id.Path["namespaces"]
topicName := id.Path["topics"]
name := id.Path["subscriptions"]
_, err = client.Delete(ctx, resourceGroup, namespaceName, topicName, name)
return err
}