github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/azurerm/resource_arm_servicebus_subscription.go (about) 1 package azurerm 2 3 import ( 4 "fmt" 5 "log" 6 "net/http" 7 8 "github.com/Azure/azure-sdk-for-go/arm/servicebus" 9 "github.com/hashicorp/terraform/helper/schema" 10 ) 11 12 func resourceArmServiceBusSubscription() *schema.Resource { 13 return &schema.Resource{ 14 Create: resourceArmServiceBusSubscriptionCreate, 15 Read: resourceArmServiceBusSubscriptionRead, 16 Update: resourceArmServiceBusSubscriptionCreate, 17 Delete: resourceArmServiceBusSubscriptionDelete, 18 Importer: &schema.ResourceImporter{ 19 State: schema.ImportStatePassthrough, 20 }, 21 22 Schema: map[string]*schema.Schema{ 23 "name": { 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 29 "namespace_name": { 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 35 "topic_name": { 36 Type: schema.TypeString, 37 Required: true, 38 ForceNew: true, 39 }, 40 41 "location": locationSchema(), 42 43 "resource_group_name": { 44 Type: schema.TypeString, 45 Required: true, 46 ForceNew: true, 47 }, 48 49 "auto_delete_on_idle": { 50 Type: schema.TypeString, 51 Optional: true, 52 Computed: true, 53 }, 54 55 "default_message_ttl": { 56 Type: schema.TypeString, 57 Optional: true, 58 Computed: true, 59 }, 60 61 "lock_duration": { 62 Type: schema.TypeString, 63 Optional: true, 64 Computed: true, 65 }, 66 67 "dead_lettering_on_filter_evaluation_exceptions": { 68 Type: schema.TypeBool, 69 Optional: true, 70 }, 71 72 "dead_lettering_on_message_expiration": { 73 Type: schema.TypeBool, 74 Optional: true, 75 }, 76 77 "enable_batched_operations": { 78 Type: schema.TypeBool, 79 Optional: true, 80 }, 81 82 "max_delivery_count": { 83 Type: schema.TypeInt, 84 Required: true, 85 }, 86 87 "requires_session": { 88 Type: schema.TypeBool, 89 Optional: true, 90 // cannot be modified 91 ForceNew: true, 92 }, 93 }, 94 } 95 } 96 97 func resourceArmServiceBusSubscriptionCreate(d *schema.ResourceData, meta interface{}) error { 98 client := meta.(*ArmClient).serviceBusSubscriptionsClient 99 log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Subscription creation.") 100 101 name := d.Get("name").(string) 102 topicName := d.Get("topic_name").(string) 103 namespaceName := d.Get("namespace_name").(string) 104 location := d.Get("location").(string) 105 resGroup := d.Get("resource_group_name").(string) 106 107 parameters := servicebus.SubscriptionCreateOrUpdateParameters{ 108 Location: &location, 109 SubscriptionProperties: &servicebus.SubscriptionProperties{}, 110 } 111 112 if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" { 113 parameters.SubscriptionProperties.AutoDeleteOnIdle = &autoDeleteOnIdle 114 } 115 116 if lockDuration := d.Get("lock_duration").(string); lockDuration != "" { 117 parameters.SubscriptionProperties.LockDuration = &lockDuration 118 } 119 120 deadLetteringFilterExceptions := d.Get("dead_lettering_on_filter_evaluation_exceptions").(bool) 121 deadLetteringExpiration := d.Get("dead_lettering_on_message_expiration").(bool) 122 enableBatchedOps := d.Get("enable_batched_operations").(bool) 123 maxDeliveryCount := int32(d.Get("max_delivery_count").(int)) 124 requiresSession := d.Get("requires_session").(bool) 125 126 parameters.SubscriptionProperties.DeadLetteringOnFilterEvaluationExceptions = &deadLetteringFilterExceptions 127 parameters.SubscriptionProperties.DeadLetteringOnMessageExpiration = &deadLetteringExpiration 128 parameters.SubscriptionProperties.EnableBatchedOperations = &enableBatchedOps 129 parameters.SubscriptionProperties.MaxDeliveryCount = &maxDeliveryCount 130 parameters.SubscriptionProperties.RequiresSession = &requiresSession 131 132 _, err := client.CreateOrUpdate(resGroup, namespaceName, topicName, name, parameters) 133 if err != nil { 134 return err 135 } 136 137 read, err := client.Get(resGroup, namespaceName, topicName, name) 138 if err != nil { 139 return err 140 } 141 if read.ID == nil { 142 return fmt.Errorf("Cannot read ServiceBus Subscription %s (resource group %s) ID", name, resGroup) 143 } 144 145 d.SetId(*read.ID) 146 147 return resourceArmServiceBusSubscriptionRead(d, meta) 148 } 149 150 func resourceArmServiceBusSubscriptionRead(d *schema.ResourceData, meta interface{}) error { 151 client := meta.(*ArmClient).serviceBusSubscriptionsClient 152 153 id, err := parseAzureResourceID(d.Id()) 154 if err != nil { 155 return err 156 } 157 resGroup := id.ResourceGroup 158 namespaceName := id.Path["namespaces"] 159 topicName := id.Path["topics"] 160 name := id.Path["subscriptions"] 161 162 log.Printf("[INFO] subscriptionID: %s, args: %s, %s, %s, %s", d.Id(), resGroup, namespaceName, topicName, name) 163 164 resp, err := client.Get(resGroup, namespaceName, topicName, name) 165 if err != nil { 166 return fmt.Errorf("Error making Read request on Azure ServiceBus Subscription %s: %+v", name, err) 167 } 168 if resp.StatusCode == http.StatusNotFound { 169 d.SetId("") 170 return nil 171 } 172 173 d.Set("name", resp.Name) 174 d.Set("resource_group_name", resGroup) 175 d.Set("namespace_name", namespaceName) 176 d.Set("topic_name", topicName) 177 d.Set("location", azureRMNormalizeLocation(*resp.Location)) 178 179 props := resp.SubscriptionProperties 180 d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle) 181 d.Set("default_message_ttl", props.DefaultMessageTimeToLive) 182 d.Set("lock_duration", props.LockDuration) 183 d.Set("dead_lettering_on_filter_evaluation_exceptions", props.DeadLetteringOnFilterEvaluationExceptions) 184 d.Set("dead_lettering_on_message_expiration", props.DeadLetteringOnMessageExpiration) 185 d.Set("enable_batched_operations", props.EnableBatchedOperations) 186 d.Set("max_delivery_count", int(*props.MaxDeliveryCount)) 187 d.Set("requires_session", props.RequiresSession) 188 189 return nil 190 } 191 192 func resourceArmServiceBusSubscriptionDelete(d *schema.ResourceData, meta interface{}) error { 193 client := meta.(*ArmClient).serviceBusSubscriptionsClient 194 195 id, err := parseAzureResourceID(d.Id()) 196 if err != nil { 197 return err 198 } 199 resGroup := id.ResourceGroup 200 namespaceName := id.Path["namespaces"] 201 topicName := id.Path["topics"] 202 name := id.Path["subscriptions"] 203 204 _, err = client.Delete(resGroup, namespaceName, topicName, name) 205 206 return err 207 }