forked from hashicorp/terraform-provider-azurerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_arm_eventhub_namespace.go
253 lines (208 loc) · 7.38 KB
/
resource_arm_eventhub_namespace.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package azurerm
import (
"context"
"fmt"
"log"
"strconv"
"time"
"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/response"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
// Default Authorization Rule/Policy created by Azure, used to populate the
// default connection strings and keys
var eventHubNamespaceDefaultAuthorizationRule = "RootManageSharedAccessKey"
func resourceArmEventHubNamespace() *schema.Resource {
return &schema.Resource{
Create: resourceArmEventHubNamespaceCreate,
Read: resourceArmEventHubNamespaceRead,
Update: resourceArmEventHubNamespaceCreate,
Delete: resourceArmEventHubNamespaceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"location": locationSchema(),
"resource_group_name": resourceGroupNameSchema(),
"sku": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(eventhub.Basic),
string(eventhub.Standard),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},
"capacity": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validation.IntBetween(1, 20),
},
"auto_inflate_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"maximum_throughput_units": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validation.IntBetween(1, 20),
},
"default_primary_connection_string": {
Type: schema.TypeString,
Computed: true,
},
"default_secondary_connection_string": {
Type: schema.TypeString,
Computed: true,
},
"default_primary_key": {
Type: schema.TypeString,
Computed: true,
},
"default_secondary_key": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmEventHubNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).eventHubNamespacesClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[INFO] preparing arguments for AzureRM EventHub Namespace creation.")
name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
sku := d.Get("sku").(string)
capacity := int32(d.Get("capacity").(int))
tags := d.Get("tags").(map[string]interface{})
autoInflateEnabled := d.Get("auto_inflate_enabled").(bool)
parameters := eventhub.EHNamespace{
Location: &location,
Sku: &eventhub.Sku{
Name: eventhub.SkuName(sku),
Tier: eventhub.SkuTier(sku),
Capacity: &capacity,
},
EHNamespaceProperties: &eventhub.EHNamespaceProperties{
IsAutoInflateEnabled: utils.Bool(autoInflateEnabled),
},
Tags: expandTags(tags),
}
if v, ok := d.GetOk("maximum_throughput_units"); ok {
maximumThroughputUnits := v.(int)
parameters.EHNamespaceProperties.MaximumThroughputUnits = utils.Int32(int32(maximumThroughputUnits))
}
future, err := client.CreateOrUpdate(ctx, resGroup, name, parameters)
if err != nil {
return err
}
err = future.WaitForCompletion(ctx, client.Client)
if err != nil {
return fmt.Errorf("Error creating eventhub namespace: %+v", err)
}
read, err := client.Get(ctx, resGroup, name)
if err != nil {
return err
}
if read.ID == nil {
return fmt.Errorf("Cannot read EventHub Namespace %q (resource group %q) ID", name, resGroup)
}
d.SetId(*read.ID)
return resourceArmEventHubNamespaceRead(d, meta)
}
func resourceArmEventHubNamespaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).eventHubNamespacesClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["namespaces"]
resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on EventHub Namespace %q: %+v", name, err)
}
d.Set("name", resp.Name)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
d.Set("resource_group_name", resGroup)
d.Set("sku", string(resp.Sku.Name))
d.Set("capacity", resp.Sku.Capacity)
keys, err := client.ListKeys(ctx, resGroup, name, eventHubNamespaceDefaultAuthorizationRule)
if err != nil {
log.Printf("[WARN] Unable to List default keys for EventHub Namespace %q: %+v", name, err)
} else {
d.Set("default_primary_connection_string", keys.PrimaryConnectionString)
d.Set("default_secondary_connection_string", keys.SecondaryConnectionString)
d.Set("default_primary_key", keys.PrimaryKey)
d.Set("default_secondary_key", keys.SecondaryKey)
}
if props := resp.EHNamespaceProperties; props != nil {
d.Set("auto_inflate_enabled", props.IsAutoInflateEnabled)
d.Set("maximum_throughput_units", int(*props.MaximumThroughputUnits))
}
flattenAndSetTags(d, resp.Tags)
return nil
}
func resourceArmEventHubNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).eventHubNamespacesClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["namespaces"]
future, err := client.Delete(ctx, resGroup, name)
if err != nil {
if response.WasNotFound(future.Response()) {
return nil
}
return fmt.Errorf("Error issuing delete request of EventHub Namespace %q (Resource Group %q): %+v", name, resGroup, err)
}
return waitForEventHubNamespaceToBeDeleted(ctx, client, resGroup, name)
}
func waitForEventHubNamespaceToBeDeleted(ctx context.Context, client eventhub.NamespacesClient, resourceGroup, name string) error {
// we can't use the Waiter here since the API returns a 200 once it's deleted which is considered a polling status code..
log.Printf("[DEBUG] Waiting for EventHub Namespace (%q in Resource Group %q) to be deleted", name, resourceGroup)
stateConf := &resource.StateChangeConf{
Pending: []string{"200"},
Target: []string{"404"},
Refresh: eventHubNamespaceStateStatusCodeRefreshFunc(ctx, client, resourceGroup, name),
Timeout: 40 * time.Minute,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf("Error waiting for EventHub NameSpace (%q in Resource Group %q) to be deleted: %+v", name, resourceGroup, err)
}
return nil
}
func eventHubNamespaceStateStatusCodeRefreshFunc(ctx context.Context, client eventhub.NamespacesClient, resourceGroup, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
res, err := client.Get(ctx, resourceGroup, name)
log.Printf("Retrieving EventHub Namespace %q (Resource Group %q) returned Status %d", resourceGroup, name, res.StatusCode)
if err != nil {
if utils.ResponseWasNotFound(res.Response) {
return res, strconv.Itoa(res.StatusCode), nil
}
return nil, "", fmt.Errorf("Error polling for the status of the EventHub Namespace %q (RG: %q): %+v", name, resourceGroup, err)
}
return res, strconv.Itoa(res.StatusCode), nil
}
}