Skip to content

Commit 8580cc4

Browse files
tombuildsstuffstack72
authored andcommitted
provider/azurerm: EventHub Authorization Rules (hashicorp#10971)
* Adding a missing property to the Consumer Groups doc * Support for Event Hub Authorization Rules * Documentation for Authorization Rules * Missed a comment * Fixing the `no authorisation rule` state * Making the documentation around the Permissions more explicit / updating the import url * Fixing up the tests * Clearing up the docs * Fixing the linting * Moving the validation inside of the expand
1 parent c430751 commit 8580cc4

7 files changed

Lines changed: 725 additions & 3 deletions
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package azurerm
2+
3+
import (
4+
"testing"
5+
6+
"fmt"
7+
8+
"github.com/hashicorp/terraform/helper/acctest"
9+
"github.com/hashicorp/terraform/helper/resource"
10+
)
11+
12+
func TestAccAzureRMEventHubAuthorizationRule_importListen(t *testing.T) {
13+
resourceName := "azurerm_eventhub_authorization_rule.test"
14+
15+
ri := acctest.RandInt()
16+
config := fmt.Sprintf(testAccAzureRMEventHubAuthorizationRule_listen, ri, ri, ri, ri)
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
22+
Steps: []resource.TestStep{
23+
{
24+
Config: config,
25+
},
26+
27+
{
28+
ResourceName: resourceName,
29+
ImportState: true,
30+
ImportStateVerify: true,
31+
},
32+
},
33+
})
34+
}
35+
36+
func TestAccAzureRMEventHubAuthorizationRule_importSend(t *testing.T) {
37+
resourceName := "azurerm_eventhub_authorization_rule.test"
38+
39+
ri := acctest.RandInt()
40+
config := fmt.Sprintf(testAccAzureRMEventHubAuthorizationRule_send, ri, ri, ri, ri)
41+
42+
resource.Test(t, resource.TestCase{
43+
PreCheck: func() { testAccPreCheck(t) },
44+
Providers: testAccProviders,
45+
CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
46+
Steps: []resource.TestStep{
47+
{
48+
Config: config,
49+
},
50+
51+
{
52+
ResourceName: resourceName,
53+
ImportState: true,
54+
ImportStateVerify: true,
55+
},
56+
},
57+
})
58+
}
59+
60+
func TestAccAzureRMEventHubAuthorizationRule_importReadWrite(t *testing.T) {
61+
resourceName := "azurerm_eventhub_authorization_rule.test"
62+
63+
ri := acctest.RandInt()
64+
config := fmt.Sprintf(testAccAzureRMEventHubAuthorizationRule_readwrite, ri, ri, ri, ri)
65+
66+
resource.Test(t, resource.TestCase{
67+
PreCheck: func() { testAccPreCheck(t) },
68+
Providers: testAccProviders,
69+
CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
70+
Steps: []resource.TestStep{
71+
{
72+
Config: config,
73+
},
74+
75+
{
76+
ResourceName: resourceName,
77+
ImportState: true,
78+
ImportStateVerify: true,
79+
},
80+
},
81+
})
82+
}
83+
84+
func TestAccAzureRMEventHubAuthorizationRule_importManage(t *testing.T) {
85+
resourceName := "azurerm_eventhub_authorization_rule.test"
86+
87+
ri := acctest.RandInt()
88+
config := fmt.Sprintf(testAccAzureRMEventHubAuthorizationRule_manage, ri, ri, ri, ri)
89+
90+
resource.Test(t, resource.TestCase{
91+
PreCheck: func() { testAccPreCheck(t) },
92+
Providers: testAccProviders,
93+
CheckDestroy: testCheckAzureRMEventHubAuthorizationRuleDestroy,
94+
Steps: []resource.TestStep{
95+
{
96+
Config: config,
97+
},
98+
99+
{
100+
ResourceName: resourceName,
101+
ImportState: true,
102+
ImportStateVerify: true,
103+
},
104+
},
105+
})
106+
}

builtin/providers/azurerm/provider.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,10 @@ func Provider() terraform.ResourceProvider {
5656
"azurerm_cdn_profile": resourceArmCdnProfile(),
5757
"azurerm_container_registry": resourceArmContainerRegistry(),
5858

59-
"azurerm_eventhub": resourceArmEventHub(),
60-
"azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(),
61-
"azurerm_eventhub_namespace": resourceArmEventHubNamespace(),
59+
"azurerm_eventhub": resourceArmEventHub(),
60+
"azurerm_eventhub_authorization_rule": resourceArmEventHubAuthorizationRule(),
61+
"azurerm_eventhub_consumer_group": resourceArmEventHubConsumerGroup(),
62+
"azurerm_eventhub_namespace": resourceArmEventHubNamespace(),
6263

6364
"azurerm_lb": resourceArmLoadBalancer(),
6465
"azurerm_lb_backend_address_pool": resourceArmLoadBalancerBackendAddressPool(),
Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"net/http"
8+
9+
"github.com/Azure/azure-sdk-for-go/arm/eventhub"
10+
"github.com/hashicorp/terraform/helper/schema"
11+
)
12+
13+
func resourceArmEventHubAuthorizationRule() *schema.Resource {
14+
return &schema.Resource{
15+
Create: resourceArmEventHubAuthorizationRuleCreateUpdate,
16+
Read: resourceArmEventHubAuthorizationRuleRead,
17+
Update: resourceArmEventHubAuthorizationRuleCreateUpdate,
18+
Delete: resourceArmEventHubAuthorizationRuleDelete,
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+
"namespace_name": {
31+
Type: schema.TypeString,
32+
Required: true,
33+
ForceNew: true,
34+
},
35+
36+
"eventhub_name": {
37+
Type: schema.TypeString,
38+
Required: true,
39+
ForceNew: true,
40+
},
41+
42+
"resource_group_name": {
43+
Type: schema.TypeString,
44+
Required: true,
45+
ForceNew: true,
46+
},
47+
48+
"location": {
49+
Type: schema.TypeString,
50+
Required: true,
51+
ForceNew: true,
52+
},
53+
54+
"listen": {
55+
Type: schema.TypeBool,
56+
Optional: true,
57+
Default: false,
58+
},
59+
60+
"send": {
61+
Type: schema.TypeBool,
62+
Optional: true,
63+
Default: false,
64+
},
65+
66+
"manage": {
67+
Type: schema.TypeBool,
68+
Optional: true,
69+
Default: false,
70+
},
71+
72+
"primary_key": {
73+
Type: schema.TypeString,
74+
Computed: true,
75+
},
76+
77+
"primary_connection_string": {
78+
Type: schema.TypeString,
79+
Computed: true,
80+
},
81+
82+
"secondary_key": {
83+
Type: schema.TypeString,
84+
Computed: true,
85+
},
86+
87+
"secondary_connection_string": {
88+
Type: schema.TypeString,
89+
Computed: true,
90+
},
91+
},
92+
}
93+
}
94+
95+
func resourceArmEventHubAuthorizationRuleCreateUpdate(d *schema.ResourceData, meta interface{}) error {
96+
client := meta.(*ArmClient).eventHubClient
97+
log.Printf("[INFO] preparing arguments for Azure ARM EventHub Authorization Rule creation.")
98+
99+
name := d.Get("name").(string)
100+
namespaceName := d.Get("namespace_name").(string)
101+
eventHubName := d.Get("eventhub_name").(string)
102+
location := d.Get("location").(string)
103+
resGroup := d.Get("resource_group_name").(string)
104+
105+
rights, err := expandEventHubAuthorizationRuleAccessRights(d)
106+
if err != nil {
107+
return err
108+
}
109+
110+
parameters := eventhub.SharedAccessAuthorizationRuleCreateOrUpdateParameters{
111+
Name: &name,
112+
Location: &location,
113+
SharedAccessAuthorizationRuleProperties: &eventhub.SharedAccessAuthorizationRuleProperties{
114+
Rights: rights,
115+
},
116+
}
117+
118+
_, err = client.CreateOrUpdateAuthorizationRule(resGroup, namespaceName, eventHubName, name, parameters)
119+
if err != nil {
120+
return err
121+
}
122+
123+
read, err := client.GetAuthorizationRule(resGroup, namespaceName, eventHubName, name)
124+
if err != nil {
125+
return err
126+
}
127+
128+
if read.ID == nil {
129+
return fmt.Errorf("Cannot read EventHub Authorization Rule %s (resource group %s) ID", name, resGroup)
130+
}
131+
132+
d.SetId(*read.ID)
133+
134+
return resourceArmEventHubAuthorizationRuleRead(d, meta)
135+
}
136+
137+
func resourceArmEventHubAuthorizationRuleRead(d *schema.ResourceData, meta interface{}) error {
138+
client := meta.(*ArmClient).eventHubClient
139+
140+
id, err := parseAzureResourceID(d.Id())
141+
if err != nil {
142+
return err
143+
}
144+
resGroup := id.ResourceGroup
145+
namespaceName := id.Path["namespaces"]
146+
eventHubName := id.Path["eventhubs"]
147+
name := id.Path["authorizationRules"]
148+
149+
resp, err := client.GetAuthorizationRule(resGroup, namespaceName, eventHubName, name)
150+
if err != nil {
151+
return fmt.Errorf("Error making Read request on Azure EventHub Authorization Rule %s: %s", name, err)
152+
}
153+
if resp.StatusCode == http.StatusNotFound {
154+
d.SetId("")
155+
return nil
156+
}
157+
158+
keysResp, err := client.ListKeys(resGroup, namespaceName, eventHubName, name)
159+
if err != nil {
160+
return fmt.Errorf("Error making Read request on Azure EventHub Authorization Rule List Keys %s: %s", name, err)
161+
}
162+
163+
d.Set("name", name)
164+
d.Set("eventhub_name", eventHubName)
165+
d.Set("namespace_name", namespaceName)
166+
d.Set("resource_group_name", resGroup)
167+
d.Set("location", azureRMNormalizeLocation(*resp.Location))
168+
169+
flattenEventHubAuthorizationRuleAccessRights(d, resp)
170+
171+
d.Set("primary_key", keysResp.PrimaryKey)
172+
d.Set("primary_connection_string", keysResp.PrimaryConnectionString)
173+
d.Set("secondary_key", keysResp.SecondaryKey)
174+
d.Set("secondary_connection_string", keysResp.SecondaryConnectionString)
175+
176+
return nil
177+
}
178+
179+
func resourceArmEventHubAuthorizationRuleDelete(d *schema.ResourceData, meta interface{}) error {
180+
eventhubClient := meta.(*ArmClient).eventHubClient
181+
182+
id, err := parseAzureResourceID(d.Id())
183+
if err != nil {
184+
return err
185+
}
186+
resGroup := id.ResourceGroup
187+
namespaceName := id.Path["namespaces"]
188+
eventHubName := id.Path["eventhubs"]
189+
name := id.Path["authorizationRules"]
190+
191+
resp, err := eventhubClient.DeleteAuthorizationRule(resGroup, namespaceName, eventHubName, name)
192+
193+
if resp.StatusCode != http.StatusOK {
194+
return fmt.Errorf("Error issuing Azure ARM delete request of EventHub Authorization Rule '%s': %s", name, err)
195+
}
196+
197+
return nil
198+
}
199+
200+
func expandEventHubAuthorizationRuleAccessRights(d *schema.ResourceData) (*[]eventhub.AccessRights, error) {
201+
canSend := d.Get("send").(bool)
202+
canListen := d.Get("listen").(bool)
203+
canManage := d.Get("manage").(bool)
204+
rights := []eventhub.AccessRights{}
205+
if canListen {
206+
rights = append(rights, eventhub.Listen)
207+
}
208+
209+
if canSend {
210+
rights = append(rights, eventhub.Send)
211+
}
212+
213+
if canManage {
214+
rights = append(rights, eventhub.Manage)
215+
}
216+
217+
if len(rights) == 0 {
218+
return nil, fmt.Errorf("At least one Authorization Rule State must be enabled (e.g. Listen/Manage/Send)")
219+
}
220+
221+
if canManage && !(canListen && canSend) {
222+
return nil, fmt.Errorf("In order to enable the 'Manage' Authorization Rule - both the 'Listen' and 'Send' rules must be enabled")
223+
}
224+
225+
return &rights, nil
226+
}
227+
228+
func flattenEventHubAuthorizationRuleAccessRights(d *schema.ResourceData, resp eventhub.SharedAccessAuthorizationRuleResource) {
229+
230+
var canListen = false
231+
var canSend = false
232+
var canManage = false
233+
234+
for _, right := range *resp.Rights {
235+
switch right {
236+
case eventhub.Listen:
237+
canListen = true
238+
case eventhub.Send:
239+
canSend = true
240+
case eventhub.Manage:
241+
canManage = true
242+
default:
243+
log.Printf("[DEBUG] Unknown Authorization Rule Right '%s'", right)
244+
}
245+
}
246+
247+
d.Set("listen", canListen)
248+
d.Set("send", canSend)
249+
d.Set("manage", canManage)
250+
}

0 commit comments

Comments
 (0)