2016-08-15 19:00:00 +02:00
|
|
|
package azurerm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/Azure/azure-sdk-for-go/arm/servicebus"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
)
|
|
|
|
|
2016-10-05 23:12:28 +02:00
|
|
|
// Default Authorization Rule/Policy created by Azure, used to populate the
|
|
|
|
// default connection strings and keys
|
|
|
|
var serviceBusNamespaceDefaultAuthorizationRule = "RootManageSharedAccessKey"
|
|
|
|
|
2016-08-15 19:00:00 +02:00
|
|
|
func resourceArmServiceBusNamespace() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceArmServiceBusNamespaceCreate,
|
|
|
|
Read: resourceArmServiceBusNamespaceRead,
|
|
|
|
Update: resourceArmServiceBusNamespaceCreate,
|
|
|
|
Delete: resourceArmServiceBusNamespaceDelete,
|
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-11-29 16:54:00 +01:00
|
|
|
"location": locationSchema(),
|
2016-08-15 19:00:00 +02:00
|
|
|
|
|
|
|
"resource_group_name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"sku": {
|
2017-06-01 13:46:36 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
ValidateFunc: validateServiceBusNamespaceSku,
|
|
|
|
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
|
2016-08-15 19:00:00 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
"capacity": {
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Default: 1,
|
|
|
|
ValidateFunc: validateServiceBusNamespaceCapacity,
|
|
|
|
},
|
|
|
|
|
2016-10-05 23:12:28 +02:00
|
|
|
"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,
|
|
|
|
},
|
|
|
|
|
2016-08-15 19:00:00 +02:00
|
|
|
"tags": tagsSchema(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmServiceBusNamespaceCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
client := meta.(*ArmClient)
|
|
|
|
namespaceClient := client.serviceBusNamespacesClient
|
|
|
|
log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus 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{})
|
|
|
|
|
|
|
|
parameters := servicebus.NamespaceCreateOrUpdateParameters{
|
|
|
|
Location: &location,
|
|
|
|
Sku: &servicebus.Sku{
|
|
|
|
Name: servicebus.SkuName(sku),
|
|
|
|
Tier: servicebus.SkuTier(sku),
|
|
|
|
Capacity: &capacity,
|
|
|
|
},
|
|
|
|
Tags: expandTags(tags),
|
|
|
|
}
|
|
|
|
|
2017-06-01 11:18:22 +02:00
|
|
|
_, error := namespaceClient.CreateOrUpdate(resGroup, name, parameters, make(chan struct{}))
|
|
|
|
err := <-error
|
2016-08-15 19:00:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
read, err := namespaceClient.Get(resGroup, name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if read.ID == nil {
|
|
|
|
return fmt.Errorf("Cannot read ServiceBus Namespace %s (resource group %s) ID", name, resGroup)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(*read.ID)
|
|
|
|
|
|
|
|
return resourceArmServiceBusNamespaceRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmServiceBusNamespaceRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
namespaceClient := meta.(*ArmClient).serviceBusNamespacesClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["namespaces"]
|
|
|
|
|
|
|
|
resp, err := namespaceClient.Get(resGroup, name)
|
provider/azurerm: Reordering the checks after an Azure API Get
We are receiving suggestions of a panic as follows:
```
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference
2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f]
2016/09/01 07:21:55 [DEBUG] plugin: terraform:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0)
```
This is because the code is as follows:
```
resp, err := client.Get(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
```
When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first
```
resp, err := client.Get(resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
```
2016-09-01 16:31:42 +02:00
|
|
|
if err != nil {
|
2017-03-31 21:24:40 +02:00
|
|
|
return fmt.Errorf("Error making Read request on Azure ServiceBus Namespace %s: %+v", name, err)
|
provider/azurerm: Reordering the checks after an Azure API Get
We are receiving suggestions of a panic as follows:
```
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic: runtime error: invalid memory address or nil pointer dereference
2016/09/01 07:21:55 [DEBUG] plugin: terraform: [signal SIGSEGV: segmentation violation code=0x1 addr=0x10 pc=0xa3170f]
2016/09/01 07:21:55 [DEBUG] plugin: terraform:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: goroutine 114 [running]:
2016/09/01 07:21:55 [DEBUG] plugin: terraform: panic(0x27f4e60, 0xc4200100e0)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/go/src/runtime/panic.go:500 +0x1a1
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/builtin/providers/azurerm.resourceArmVirtualMachineRead(0xc4206d8060, 0x2995620, 0xc4204d0000, 0x0, 0x17)
2016/09/01 07:21:55 [DEBUG] plugin: terraform: /opt/gopath/src/github.com/hashicorp/terraform/builtin/providers/azurerm/resource_arm_virtual_machine.go:488 +0x1ff
2016/09/01 07:21:55 [DEBUG] plugin: terraform: github.com/hashicorp/terraform/helper/schema.(*Resource).Refresh(0xc420017a40, 0xc42040c780, 0x2995620, 0xc4204d0000, 0xc42019c990, 0x1, 0x0)
```
This is because the code is as follows:
```
resp, err := client.Get(resGroup, vnetName, name)
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
```
When a request throws an error, the response object isn't valid. Therefore, we need to flip that code to check the error first
```
resp, err := client.Get(resGroup, vnetName, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure virtual network peering %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}
```
2016-09-01 16:31:42 +02:00
|
|
|
}
|
2016-08-15 19:00:00 +02:00
|
|
|
if resp.StatusCode == http.StatusNotFound {
|
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("name", resp.Name)
|
2016-11-22 13:13:11 +01:00
|
|
|
d.Set("resource_group_name", resGroup)
|
|
|
|
d.Set("location", azureRMNormalizeLocation(*resp.Location))
|
2016-08-15 19:00:00 +02:00
|
|
|
d.Set("sku", strings.ToLower(string(resp.Sku.Name)))
|
|
|
|
d.Set("capacity", resp.Sku.Capacity)
|
2016-10-05 23:12:28 +02:00
|
|
|
|
|
|
|
keys, err := namespaceClient.ListKeys(resGroup, name, serviceBusNamespaceDefaultAuthorizationRule)
|
|
|
|
if err != nil {
|
2017-03-31 21:24:40 +02:00
|
|
|
log.Printf("[ERROR] Unable to List default keys for Namespace %s: %+v", name, err)
|
2016-10-05 23:12:28 +02:00
|
|
|
} 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)
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:00:00 +02:00
|
|
|
flattenAndSetTags(d, resp.Tags)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceArmServiceBusNamespaceDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
namespaceClient := meta.(*ArmClient).serviceBusNamespacesClient
|
|
|
|
|
|
|
|
id, err := parseAzureResourceID(d.Id())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
resGroup := id.ResourceGroup
|
|
|
|
name := id.Path["namespaces"]
|
|
|
|
|
2017-06-01 11:18:22 +02:00
|
|
|
deleteResp, error := namespaceClient.Delete(resGroup, name, make(chan struct{}))
|
|
|
|
resp := <-deleteResp
|
|
|
|
err = <-error
|
2016-08-15 19:00:00 +02:00
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusNotFound {
|
2017-03-31 21:24:40 +02:00
|
|
|
return fmt.Errorf("Error issuing Azure ARM delete request of ServiceBus Namespace'%s': %+v", name, err)
|
2016-08-15 19:00:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateServiceBusNamespaceSku(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := strings.ToLower(v.(string))
|
|
|
|
skus := map[string]bool{
|
|
|
|
"basic": true,
|
|
|
|
"standard": true,
|
|
|
|
"premium": true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !skus[value] {
|
|
|
|
errors = append(errors, fmt.Errorf("ServiceBus Namespace SKU can only be Basic, Standard or Premium"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateServiceBusNamespaceCapacity(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(int)
|
|
|
|
capacities := map[int]bool{
|
|
|
|
1: true,
|
|
|
|
2: true,
|
|
|
|
4: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
if !capacities[value] {
|
|
|
|
errors = append(errors, fmt.Errorf("ServiceBus Namespace Capacity can only be 1, 2 or 4"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|