-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
provider/azurerm: Add support for servicebus namespaces (#8195)
* add dep for servicebus client from azure-sdk-for-node * add servicebus namespaces support * add docs for servicebus_namespaces * add Microsoft.ServiceBus to providers list
- Loading branch information
Showing
13 changed files
with
3,669 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
builtin/providers/azurerm/resource_arm_servicebus_namespace.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/servicebus" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
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, | ||
}, | ||
|
||
"location": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
StateFunc: azureRMNormalizeLocation, | ||
}, | ||
|
||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"sku": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateServiceBusNamespaceSku, | ||
}, | ||
|
||
"capacity": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
ForceNew: true, | ||
Default: 1, | ||
ValidateFunc: validateServiceBusNamespaceCapacity, | ||
}, | ||
|
||
"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), | ||
} | ||
|
||
_, err := namespaceClient.CreateOrUpdate(resGroup, name, parameters, make(chan struct{})) | ||
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) | ||
if resp.StatusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return nil | ||
} | ||
if err != nil { | ||
return fmt.Errorf("Error making Read request on Azure ServiceBus Namespace %s: %s", name, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("sku", strings.ToLower(string(resp.Sku.Name))) | ||
d.Set("capacity", resp.Sku.Capacity) | ||
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"] | ||
|
||
resp, err := namespaceClient.Delete(resGroup, name, make(chan struct{})) | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
return fmt.Errorf("Error issuing Azure ARM delete request of ServiceBus Namespace'%s': %s", name, err) | ||
} | ||
|
||
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 | ||
} |
162 changes: 162 additions & 0 deletions
162
builtin/providers/azurerm/resource_arm_servicebus_namespace_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAzureRMServiceBusNamespaceCapacity_validation(t *testing.T) { | ||
cases := []struct { | ||
Value int | ||
ErrCount int | ||
}{ | ||
{ | ||
Value: 17, | ||
ErrCount: 1, | ||
}, | ||
{ | ||
Value: 1, | ||
ErrCount: 0, | ||
}, | ||
{ | ||
Value: 2, | ||
ErrCount: 0, | ||
}, | ||
{ | ||
Value: 4, | ||
ErrCount: 0, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
_, errors := validateServiceBusNamespaceCapacity(tc.Value, "azurerm_servicebus_namespace") | ||
|
||
if len(errors) != tc.ErrCount { | ||
t.Fatalf("Expected the Azure RM ServiceBus Namespace Capacity to trigger a validation error") | ||
} | ||
} | ||
} | ||
|
||
func TestAccAzureRMServiceBusNamespaceSku_validation(t *testing.T) { | ||
cases := []struct { | ||
Value string | ||
ErrCount int | ||
}{ | ||
{ | ||
Value: "Basic", | ||
ErrCount: 0, | ||
}, | ||
{ | ||
Value: "Standard", | ||
ErrCount: 0, | ||
}, | ||
{ | ||
Value: "Premium", | ||
ErrCount: 0, | ||
}, | ||
{ | ||
Value: "Random", | ||
ErrCount: 1, | ||
}, | ||
} | ||
|
||
for _, tc := range cases { | ||
_, errors := validateServiceBusNamespaceSku(tc.Value, "azurerm_servicebus_namespace") | ||
|
||
if len(errors) != tc.ErrCount { | ||
t.Fatalf("Expected the Azure RM ServiceBus Namespace Sku to trigger a validation error") | ||
} | ||
} | ||
} | ||
|
||
func TestAccAzureRMServiceBusNamespace_basic(t *testing.T) { | ||
|
||
ri := acctest.RandInt() | ||
config := fmt.Sprintf(testAccAzureRMServiceBusNamespace_basic, ri, ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMServiceBusNamespaceDestroy, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMServiceBusNamespaceExists("azurerm_servicebus_namespace.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMServiceBusNamespaceDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*ArmClient).serviceBusNamespacesClient | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_servicebus_namespace" { | ||
continue | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
|
||
resp, err := conn.Get(resourceGroup, name) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
return fmt.Errorf("ServiceBus Namespace still exists:\n%#v", resp.Properties) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testCheckAzureRMServiceBusNamespaceExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// Ensure we have enough information in state to look up in API | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
namespaceName := rs.Primary.Attributes["name"] | ||
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
if !hasResourceGroup { | ||
return fmt.Errorf("Bad: no resource group found in state for public ip: %s", namespaceName) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*ArmClient).serviceBusNamespacesClient | ||
|
||
resp, err := conn.Get(resourceGroup, namespaceName) | ||
if err != nil { | ||
return fmt.Errorf("Bad: Get on serviceBusNamespacesClient: %s", err) | ||
} | ||
|
||
if resp.StatusCode == http.StatusNotFound { | ||
return fmt.Errorf("Bad: Public IP %q (resource group: %q) does not exist", namespaceName, resourceGroup) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
var testAccAzureRMServiceBusNamespace_basic = ` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestrg-%d" | ||
location = "West US" | ||
} | ||
resource "azurerm_servicebus_namespace" "test" { | ||
name = "acctestservicebusnamespace-%d" | ||
location = "West US" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
sku = "basic" | ||
} | ||
` |
Oops, something went wrong.