github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/azurerm/resource_arm_servicebus_namespace.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 "strings" 8 9 "github.com/Azure/azure-sdk-for-go/arm/servicebus" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceArmServiceBusNamespace() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceArmServiceBusNamespaceCreate, 16 Read: resourceArmServiceBusNamespaceRead, 17 Update: resourceArmServiceBusNamespaceCreate, 18 Delete: resourceArmServiceBusNamespaceDelete, 19 Importer: &schema.ResourceImporter{ 20 State: schema.ImportStatePassthrough, 21 }, 22 23 Schema: map[string]*schema.Schema{ 24 "name": { 25 Type: schema.TypeString, 26 Required: true, 27 ForceNew: true, 28 }, 29 30 "location": { 31 Type: schema.TypeString, 32 Required: true, 33 ForceNew: true, 34 StateFunc: azureRMNormalizeLocation, 35 }, 36 37 "resource_group_name": { 38 Type: schema.TypeString, 39 Required: true, 40 ForceNew: true, 41 }, 42 43 "sku": { 44 Type: schema.TypeString, 45 Required: true, 46 ForceNew: true, 47 ValidateFunc: validateServiceBusNamespaceSku, 48 }, 49 50 "capacity": { 51 Type: schema.TypeInt, 52 Optional: true, 53 ForceNew: true, 54 Default: 1, 55 ValidateFunc: validateServiceBusNamespaceCapacity, 56 }, 57 58 "tags": tagsSchema(), 59 }, 60 } 61 } 62 63 func resourceArmServiceBusNamespaceCreate(d *schema.ResourceData, meta interface{}) error { 64 client := meta.(*ArmClient) 65 namespaceClient := client.serviceBusNamespacesClient 66 log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Namespace creation.") 67 68 name := d.Get("name").(string) 69 location := d.Get("location").(string) 70 resGroup := d.Get("resource_group_name").(string) 71 sku := d.Get("sku").(string) 72 capacity := int32(d.Get("capacity").(int)) 73 tags := d.Get("tags").(map[string]interface{}) 74 75 parameters := servicebus.NamespaceCreateOrUpdateParameters{ 76 Location: &location, 77 Sku: &servicebus.Sku{ 78 Name: servicebus.SkuName(sku), 79 Tier: servicebus.SkuTier(sku), 80 Capacity: &capacity, 81 }, 82 Tags: expandTags(tags), 83 } 84 85 _, err := namespaceClient.CreateOrUpdate(resGroup, name, parameters, make(chan struct{})) 86 if err != nil { 87 return err 88 } 89 90 read, err := namespaceClient.Get(resGroup, name) 91 if err != nil { 92 return err 93 } 94 95 if read.ID == nil { 96 return fmt.Errorf("Cannot read ServiceBus Namespace %s (resource group %s) ID", name, resGroup) 97 } 98 99 d.SetId(*read.ID) 100 101 return resourceArmServiceBusNamespaceRead(d, meta) 102 } 103 104 func resourceArmServiceBusNamespaceRead(d *schema.ResourceData, meta interface{}) error { 105 namespaceClient := meta.(*ArmClient).serviceBusNamespacesClient 106 107 id, err := parseAzureResourceID(d.Id()) 108 if err != nil { 109 return err 110 } 111 resGroup := id.ResourceGroup 112 name := id.Path["namespaces"] 113 114 resp, err := namespaceClient.Get(resGroup, name) 115 if err != nil { 116 return fmt.Errorf("Error making Read request on Azure ServiceBus Namespace %s: %s", name, err) 117 } 118 if resp.StatusCode == http.StatusNotFound { 119 d.SetId("") 120 return nil 121 } 122 123 d.Set("name", resp.Name) 124 d.Set("sku", strings.ToLower(string(resp.Sku.Name))) 125 d.Set("capacity", resp.Sku.Capacity) 126 flattenAndSetTags(d, resp.Tags) 127 128 return nil 129 } 130 131 func resourceArmServiceBusNamespaceDelete(d *schema.ResourceData, meta interface{}) error { 132 namespaceClient := meta.(*ArmClient).serviceBusNamespacesClient 133 134 id, err := parseAzureResourceID(d.Id()) 135 if err != nil { 136 return err 137 } 138 resGroup := id.ResourceGroup 139 name := id.Path["namespaces"] 140 141 resp, err := namespaceClient.Delete(resGroup, name, make(chan struct{})) 142 143 if resp.StatusCode != http.StatusNotFound { 144 return fmt.Errorf("Error issuing Azure ARM delete request of ServiceBus Namespace'%s': %s", name, err) 145 } 146 147 return nil 148 } 149 150 func validateServiceBusNamespaceSku(v interface{}, k string) (ws []string, errors []error) { 151 value := strings.ToLower(v.(string)) 152 skus := map[string]bool{ 153 "basic": true, 154 "standard": true, 155 "premium": true, 156 } 157 158 if !skus[value] { 159 errors = append(errors, fmt.Errorf("ServiceBus Namespace SKU can only be Basic, Standard or Premium")) 160 } 161 return 162 } 163 164 func validateServiceBusNamespaceCapacity(v interface{}, k string) (ws []string, errors []error) { 165 value := v.(int) 166 capacities := map[int]bool{ 167 1: true, 168 2: true, 169 4: true, 170 } 171 172 if !capacities[value] { 173 errors = append(errors, fmt.Errorf("ServiceBus Namespace Capacity can only be 1, 2 or 4")) 174 } 175 return 176 }