Skip to content

Commit 719f3ad

Browse files
committed
Scaffold the Azure RM Network Security Rule resource
1 parent f4e138f commit 719f3ad

9 files changed

Lines changed: 696 additions & 157 deletions
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package azurerm
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
func validateNetworkSecurityRuleProtocol(v interface{}, k string) (ws []string, errors []error) {
9+
value := strings.ToLower(v.(string))
10+
protocols := map[string]bool{
11+
"tcp": true,
12+
"udp": true,
13+
"*": true,
14+
}
15+
16+
if !protocols[value] {
17+
errors = append(errors, fmt.Errorf("Network Security Rule Protocol can only be Tcp, Udp or *"))
18+
}
19+
return
20+
}
21+
22+
func validateNetworkSecurityRuleAccess(v interface{}, k string) (ws []string, errors []error) {
23+
value := strings.ToLower(v.(string))
24+
accessTypes := map[string]bool{
25+
"allow": true,
26+
"deny": true,
27+
}
28+
29+
if !accessTypes[value] {
30+
errors = append(errors, fmt.Errorf("Network Security Rule Access can only be Allow or Deny"))
31+
}
32+
return
33+
}
34+
35+
func validateNetworkSecurityRuleDirection(v interface{}, k string) (ws []string, errors []error) {
36+
value := strings.ToLower(v.(string))
37+
directions := map[string]bool{
38+
"inbound": true,
39+
"outbound": true,
40+
}
41+
42+
if !directions[value] {
43+
errors = append(errors, fmt.Errorf("Network Security Rule Directions can only be Inbound or Outbound"))
44+
}
45+
return
46+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package azurerm
2+
3+
import "testing"
4+
5+
func TestResourceAzureRMNetworkSecurityRuleProtocol_validation(t *testing.T) {
6+
cases := []struct {
7+
Value string
8+
ErrCount int
9+
}{
10+
{
11+
Value: "Random",
12+
ErrCount: 1,
13+
},
14+
{
15+
Value: "tcp",
16+
ErrCount: 0,
17+
},
18+
{
19+
Value: "TCP",
20+
ErrCount: 0,
21+
},
22+
{
23+
Value: "*",
24+
ErrCount: 0,
25+
},
26+
{
27+
Value: "Udp",
28+
ErrCount: 0,
29+
},
30+
{
31+
Value: "Tcp",
32+
ErrCount: 0,
33+
},
34+
}
35+
36+
for _, tc := range cases {
37+
_, errors := validateNetworkSecurityRuleProtocol(tc.Value, "azurerm_network_security_rule")
38+
39+
if len(errors) != tc.ErrCount {
40+
t.Fatalf("Expected the Azure RM Network Security Rule protocol to trigger a validation error")
41+
}
42+
}
43+
}
44+
45+
func TestResourceAzureRMNetworkSecurityRuleAccess_validation(t *testing.T) {
46+
cases := []struct {
47+
Value string
48+
ErrCount int
49+
}{
50+
{
51+
Value: "Random",
52+
ErrCount: 1,
53+
},
54+
{
55+
Value: "Allow",
56+
ErrCount: 0,
57+
},
58+
{
59+
Value: "Deny",
60+
ErrCount: 0,
61+
},
62+
{
63+
Value: "ALLOW",
64+
ErrCount: 0,
65+
},
66+
{
67+
Value: "deny",
68+
ErrCount: 0,
69+
},
70+
}
71+
72+
for _, tc := range cases {
73+
_, errors := validateNetworkSecurityRuleAccess(tc.Value, "azurerm_network_security_rule")
74+
75+
if len(errors) != tc.ErrCount {
76+
t.Fatalf("Expected the Azure RM Network Security Rule access to trigger a validation error")
77+
}
78+
}
79+
}
80+
81+
func TestResourceAzureRMNetworkSecurityRuleDirection_validation(t *testing.T) {
82+
cases := []struct {
83+
Value string
84+
ErrCount int
85+
}{
86+
{
87+
Value: "Random",
88+
ErrCount: 1,
89+
},
90+
{
91+
Value: "Inbound",
92+
ErrCount: 0,
93+
},
94+
{
95+
Value: "Outbound",
96+
ErrCount: 0,
97+
},
98+
{
99+
Value: "INBOUND",
100+
ErrCount: 0,
101+
},
102+
{
103+
Value: "Inbound",
104+
ErrCount: 0,
105+
},
106+
}
107+
108+
for _, tc := range cases {
109+
_, errors := validateNetworkSecurityRuleDirection(tc.Value, "azurerm_network_security_rule")
110+
111+
if len(errors) != tc.ErrCount {
112+
t.Fatalf("Expected the Azure RM Network Security Rule direction to trigger a validation error")
113+
}
114+
}
115+
}

builtin/providers/azurerm/provider.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"net/http"
66
"strings"
77

8+
"github.com/hashicorp/terraform/helper/mutexkv"
89
"github.com/hashicorp/terraform/helper/schema"
910
"github.com/hashicorp/terraform/terraform"
1011
)
@@ -44,9 +45,9 @@ func Provider() terraform.ResourceProvider {
4445
"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
4546
"azurerm_availability_set": resourceArmAvailabilitySet(),
4647
"azurerm_network_security_group": resourceArmNetworkSecurityGroup(),
48+
"azurerm_network_security_rule": resourceArmNetworkSecurityRule(),
4749
"azurerm_public_ip": resourceArmPublicIp(),
4850
},
49-
5051
ConfigureFunc: providerConfigure,
5152
}
5253
}
@@ -110,3 +111,6 @@ func azureRMNormalizeLocation(location interface{}) string {
110111
input := location.(string)
111112
return strings.Replace(strings.ToLower(input), " ", "", -1)
112113
}
114+
115+
// armMutexKV is the instance of MutexKV for ARM resources
116+
var armMutexKV = mutexkv.NewMutexKV()

builtin/providers/azurerm/resource_arm_network_security_group.go

Lines changed: 29 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"net/http"
88
"time"
99

10-
"strings"
11-
1210
"github.com/Azure/azure-sdk-for-go/arm/network"
1311
"github.com/hashicorp/terraform/helper/hashcode"
1412
"github.com/hashicorp/terraform/helper/resource"
@@ -132,7 +130,7 @@ func resourceArmNetworkSecurityGroupCreate(d *schema.ResourceData, meta interfac
132130
location := d.Get("location").(string)
133131
resGroup := d.Get("resource_group_name").(string)
134132

135-
sgRules, sgErr := expandAzureRmSecurityGroupRules(d)
133+
sgRules, sgErr := expandAzureRmSecurityRules(d)
136134
if sgErr != nil {
137135
return fmt.Errorf("Error Building list of Network Security Group Rules: %s", sgErr)
138136
}
@@ -185,6 +183,10 @@ func resourceArmNetworkSecurityGroupRead(d *schema.ResourceData, meta interface{
185183
return fmt.Errorf("Error making Read request on Azure Network Security Group %s: %s", name, err)
186184
}
187185

186+
if resp.Properties.SecurityRules != nil {
187+
d.Set("security_rule", flattenNetworkSecurityRules(resp.Properties.SecurityRules))
188+
}
189+
188190
return nil
189191
}
190192

@@ -229,7 +231,30 @@ func securityGroupStateRefreshFunc(client *ArmClient, resourceGroupName string,
229231
}
230232
}
231233

232-
func expandAzureRmSecurityGroupRules(d *schema.ResourceData) ([]network.SecurityRule, error) {
234+
func flattenNetworkSecurityRules(rules *[]network.SecurityRule) []map[string]interface{} {
235+
result := make([]map[string]interface{}, 0, len(*rules))
236+
for _, rule := range *rules {
237+
sgRule := make(map[string]interface{})
238+
sgRule["name"] = *rule.Name
239+
sgRule["destination_address_prefix"] = *rule.Properties.DestinationAddressPrefix
240+
sgRule["destination_port_range"] = *rule.Properties.DestinationPortRange
241+
sgRule["source_address_prefix"] = *rule.Properties.SourceAddressPrefix
242+
sgRule["source_port_range"] = *rule.Properties.SourcePortRange
243+
sgRule["priority"] = int(*rule.Properties.Priority)
244+
sgRule["access"] = rule.Properties.Access
245+
sgRule["direction"] = rule.Properties.Direction
246+
sgRule["protocol"] = rule.Properties.Protocol
247+
248+
if rule.Properties.Description != nil {
249+
sgRule["description"] = *rule.Properties.Description
250+
}
251+
252+
result = append(result, sgRule)
253+
}
254+
return result
255+
}
256+
257+
func expandAzureRmSecurityRules(d *schema.ResourceData) ([]network.SecurityRule, error) {
233258
sgRules := d.Get("security_rule").(*schema.Set).List()
234259
rules := make([]network.SecurityRule, 0, len(sgRules))
235260

@@ -268,43 +293,3 @@ func expandAzureRmSecurityGroupRules(d *schema.ResourceData) ([]network.Security
268293

269294
return rules, nil
270295
}
271-
272-
func validateNetworkSecurityRuleProtocol(v interface{}, k string) (ws []string, errors []error) {
273-
value := strings.ToLower(v.(string))
274-
viewTypes := map[string]bool{
275-
"tcp": true,
276-
"udp": true,
277-
"*": true,
278-
}
279-
280-
if !viewTypes[value] {
281-
errors = append(errors, fmt.Errorf("Network Security Rule Protocol can only be Tcp, Udp or *"))
282-
}
283-
return
284-
}
285-
286-
func validateNetworkSecurityRuleAccess(v interface{}, k string) (ws []string, errors []error) {
287-
value := strings.ToLower(v.(string))
288-
viewTypes := map[string]bool{
289-
"allow": true,
290-
"deny": true,
291-
}
292-
293-
if !viewTypes[value] {
294-
errors = append(errors, fmt.Errorf("Network Security Rule Access can only be Allow or Deny"))
295-
}
296-
return
297-
}
298-
299-
func validateNetworkSecurityRuleDirection(v interface{}, k string) (ws []string, errors []error) {
300-
value := strings.ToLower(v.(string))
301-
viewTypes := map[string]bool{
302-
"inbound": true,
303-
"outbound": true,
304-
}
305-
306-
if !viewTypes[value] {
307-
errors = append(errors, fmt.Errorf("Network Security Rule Directions can only be Inbound or Outbound"))
308-
}
309-
return
310-
}

0 commit comments

Comments
 (0)