Skip to content

Commit cdbf0d7

Browse files
author
Peter McAtominey
committed
provider/azurerm: add servicebus_topic resource
TF_ACC=1 go test ./builtin/providers/azurerm -v -run TestAccAzureRMServiceBusTopic -timeout 120m === RUN TestAccAzureRMServiceBusTopic_importBasic --- PASS: TestAccAzureRMServiceBusTopic_importBasic (328.72s) === RUN TestAccAzureRMServiceBusTopic_basic --- PASS: TestAccAzureRMServiceBusTopic_basic (331.04s) === RUN TestAccAzureRMServiceBusTopic_update --- PASS: TestAccAzureRMServiceBusTopic_update (348.69s) PASS ok github.com/hashicorp/terraform/builtin/providers/azurerm 1008.588s
1 parent 028f96d commit cdbf0d7

7 files changed

Lines changed: 533 additions & 0 deletions

File tree

builtin/providers/azurerm/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type ArmClient struct {
6969
trafficManagerEndpointsClient trafficmanager.EndpointsClient
7070

7171
serviceBusNamespacesClient servicebus.NamespacesClient
72+
serviceBusTopicsClient servicebus.TopicsClient
7273
}
7374

7475
func withRequestLogging() autorest.SendDecorator {
@@ -352,6 +353,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
352353
sbnc.Sender = autorest.CreateSender(withRequestLogging())
353354
client.serviceBusNamespacesClient = sbnc
354355

356+
sbtc := servicebus.NewTopicsClient(c.SubscriptionID)
357+
setUserAgent(&sbtc.Client)
358+
sbtc.Authorizer = spt
359+
sbtc.Sender = autorest.CreateSender(withRequestLogging())
360+
client.serviceBusTopicsClient = sbtc
361+
355362
return &client, nil
356363
}
357364

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform/helper/acctest"
8+
"github.com/hashicorp/terraform/helper/resource"
9+
)
10+
11+
func TestAccAzureRMServiceBusTopic_importBasic(t *testing.T) {
12+
resourceName := "azurerm_servicebus_topic.test"
13+
14+
ri := acctest.RandInt()
15+
config := fmt.Sprintf(testAccAzureRMServiceBusTopic_basic, ri, ri, ri)
16+
17+
resource.Test(t, resource.TestCase{
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testCheckAzureRMServiceBusTopicDestroy,
21+
Steps: []resource.TestStep{
22+
resource.TestStep{
23+
Config: config,
24+
},
25+
26+
resource.TestStep{
27+
ResourceName: resourceName,
28+
ImportState: true,
29+
ImportStateVerify: true,
30+
},
31+
},
32+
})
33+
}

builtin/providers/azurerm/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func Provider() terraform.ResourceProvider {
5757
"azurerm_route": resourceArmRoute(),
5858
"azurerm_route_table": resourceArmRouteTable(),
5959
"azurerm_servicebus_namespace": resourceArmServiceBusNamespace(),
60+
"azurerm_servicebus_topic": resourceArmServiceBusTopic(),
6061
"azurerm_storage_account": resourceArmStorageAccount(),
6162
"azurerm_storage_blob": resourceArmStorageBlob(),
6263
"azurerm_storage_container": resourceArmStorageContainer(),
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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 resourceArmServiceBusTopic() *schema.Resource {
13+
return &schema.Resource{
14+
Create: resourceArmServiceBusTopicCreate,
15+
Read: resourceArmServiceBusTopicRead,
16+
Update: resourceArmServiceBusTopicCreate,
17+
Delete: resourceArmServiceBusTopicDelete,
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+
"location": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
ForceNew: true,
39+
StateFunc: azureRMNormalizeLocation,
40+
},
41+
42+
"resource_group_name": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
ForceNew: true,
46+
},
47+
48+
"auto_delete_on_idle": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
Computed: true,
52+
},
53+
54+
"default_message_ttl": {
55+
Type: schema.TypeString,
56+
Optional: true,
57+
Computed: true,
58+
},
59+
60+
"duplicate_detection_history_time_window": {
61+
Type: schema.TypeString,
62+
Optional: true,
63+
},
64+
65+
"enable_batched_operations": {
66+
Type: schema.TypeBool,
67+
Optional: true,
68+
},
69+
70+
"enable_express": {
71+
Type: schema.TypeBool,
72+
Optional: true,
73+
},
74+
75+
"enable_filtering_messages_before_publishing": {
76+
Type: schema.TypeBool,
77+
Optional: true,
78+
},
79+
80+
"enable_partitioning": {
81+
Type: schema.TypeBool,
82+
Optional: true,
83+
},
84+
85+
"max_size_in_megabytes": {
86+
Type: schema.TypeInt,
87+
Optional: true,
88+
Computed: true,
89+
},
90+
91+
"requires_duplicate_detection": {
92+
Type: schema.TypeBool,
93+
Optional: true,
94+
},
95+
96+
"support_ordering": {
97+
Type: schema.TypeBool,
98+
Optional: true,
99+
},
100+
},
101+
}
102+
}
103+
104+
func resourceArmServiceBusTopicCreate(d *schema.ResourceData, meta interface{}) error {
105+
client := meta.(*ArmClient).serviceBusTopicsClient
106+
log.Printf("[INFO] preparing arguments for Azure ARM ServiceBus Topic creation.")
107+
108+
name := d.Get("name").(string)
109+
namespaceName := d.Get("namespace_name").(string)
110+
location := d.Get("location").(string)
111+
resGroup := d.Get("resource_group_name").(string)
112+
113+
parameters := servicebus.TopicCreateOrUpdateParameters{
114+
Name: &name,
115+
Location: &location,
116+
Properties: &servicebus.TopicProperties{},
117+
}
118+
119+
if autoDeleteOnIdle := d.Get("auto_delete_on_idle").(string); autoDeleteOnIdle != "" {
120+
parameters.Properties.AutoDeleteOnIdle = &autoDeleteOnIdle
121+
}
122+
123+
if defaultTTL := d.Get("default_message_ttl").(string); defaultTTL != "" {
124+
parameters.Properties.DefaultMessageTimeToLive = &defaultTTL
125+
}
126+
127+
if duplicateWindow := d.Get("duplicate_detection_history_time_window").(string); duplicateWindow != "" {
128+
parameters.Properties.DuplicateDetectionHistoryTimeWindow = &duplicateWindow
129+
}
130+
131+
enableBatchedOps := d.Get("enable_batched_operations").(bool)
132+
enableExpress := d.Get("enable_express").(bool)
133+
enableFiltering := d.Get("enable_filtering_messages_before_publishing").(bool)
134+
enablePartitioning := d.Get("enable_partitioning").(bool)
135+
maxSize := int64(d.Get("max_size_in_megabytes").(int))
136+
requiresDuplicateDetection := d.Get("requires_duplicate_detection").(bool)
137+
supportOrdering := d.Get("support_ordering").(bool)
138+
139+
parameters.Properties.EnableBatchedOperations = &enableBatchedOps
140+
parameters.Properties.EnableExpress = &enableExpress
141+
parameters.Properties.FilteringMessagesBeforePublishing = &enableFiltering
142+
parameters.Properties.EnablePartitioning = &enablePartitioning
143+
parameters.Properties.MaxSizeInMegabytes = &maxSize
144+
parameters.Properties.RequiresDuplicateDetection = &requiresDuplicateDetection
145+
parameters.Properties.SupportOrdering = &supportOrdering
146+
147+
_, err := client.CreateOrUpdate(resGroup, namespaceName, name, parameters)
148+
if err != nil {
149+
return err
150+
}
151+
152+
read, err := client.Get(resGroup, namespaceName, name)
153+
if err != nil {
154+
return err
155+
}
156+
if read.ID == nil {
157+
return fmt.Errorf("Cannot read ServiceBus Topic %s (resource group %s) ID", name, resGroup)
158+
}
159+
160+
d.SetId(*read.ID)
161+
162+
return resourceArmServiceBusTopicRead(d, meta)
163+
}
164+
165+
func resourceArmServiceBusTopicRead(d *schema.ResourceData, meta interface{}) error {
166+
client := meta.(*ArmClient).serviceBusTopicsClient
167+
168+
id, err := parseAzureResourceID(d.Id())
169+
if err != nil {
170+
return err
171+
}
172+
resGroup := id.ResourceGroup
173+
namespaceName := id.Path["namespaces"]
174+
name := id.Path["topics"]
175+
176+
resp, err := client.Get(resGroup, namespaceName, name)
177+
if err != nil {
178+
return fmt.Errorf("Error making Read request on Azure ServiceBus Topic %s: %s", name, err)
179+
}
180+
if resp.StatusCode == http.StatusNotFound {
181+
d.SetId("")
182+
return nil
183+
}
184+
185+
d.Set("name", resp.Name)
186+
d.Set("resource_group_name", resGroup)
187+
d.Set("namespace_name", namespaceName)
188+
d.Set("location", azureRMNormalizeLocation(*resp.Location))
189+
190+
props := resp.Properties
191+
d.Set("auto_delete_on_idle", props.AutoDeleteOnIdle)
192+
d.Set("default_message_ttl", props.DefaultMessageTimeToLive)
193+
194+
if props.DuplicateDetectionHistoryTimeWindow != nil && *props.DuplicateDetectionHistoryTimeWindow != "" {
195+
d.Set("duplicate_detection_history_time_window", props.DuplicateDetectionHistoryTimeWindow)
196+
}
197+
198+
d.Set("enable_batched_operations", props.EnableBatchedOperations)
199+
d.Set("enable_express", props.EnableExpress)
200+
d.Set("enable_filtering_messages_before_publishing", props.FilteringMessagesBeforePublishing)
201+
d.Set("enable_partitioning", props.EnablePartitioning)
202+
d.Set("max_size_in_megabytes", int(*props.MaxSizeInMegabytes))
203+
d.Set("requires_duplicate_detection", props.RequiresDuplicateDetection)
204+
d.Set("support_ordering", props.SupportOrdering)
205+
206+
return nil
207+
}
208+
209+
func resourceArmServiceBusTopicDelete(d *schema.ResourceData, meta interface{}) error {
210+
client := meta.(*ArmClient).serviceBusTopicsClient
211+
212+
id, err := parseAzureResourceID(d.Id())
213+
if err != nil {
214+
return err
215+
}
216+
resGroup := id.ResourceGroup
217+
namespaceName := id.Path["namespaces"]
218+
name := id.Path["topics"]
219+
220+
_, err = client.Delete(resGroup, namespaceName, name)
221+
222+
return err
223+
}

0 commit comments

Comments
 (0)