|
7 | 7 | "net/http" |
8 | 8 | "time" |
9 | 9 |
|
10 | | - "strings" |
11 | | - |
12 | 10 | "github.com/Azure/azure-sdk-for-go/arm/network" |
13 | 11 | "github.com/hashicorp/terraform/helper/hashcode" |
14 | 12 | "github.com/hashicorp/terraform/helper/resource" |
@@ -132,7 +130,7 @@ func resourceArmNetworkSecurityGroupCreate(d *schema.ResourceData, meta interfac |
132 | 130 | location := d.Get("location").(string) |
133 | 131 | resGroup := d.Get("resource_group_name").(string) |
134 | 132 |
|
135 | | - sgRules, sgErr := expandAzureRmSecurityGroupRules(d) |
| 133 | + sgRules, sgErr := expandAzureRmSecurityRules(d) |
136 | 134 | if sgErr != nil { |
137 | 135 | return fmt.Errorf("Error Building list of Network Security Group Rules: %s", sgErr) |
138 | 136 | } |
@@ -185,6 +183,10 @@ func resourceArmNetworkSecurityGroupRead(d *schema.ResourceData, meta interface{ |
185 | 183 | return fmt.Errorf("Error making Read request on Azure Network Security Group %s: %s", name, err) |
186 | 184 | } |
187 | 185 |
|
| 186 | + if resp.Properties.SecurityRules != nil { |
| 187 | + d.Set("security_rule", flattenNetworkSecurityRules(resp.Properties.SecurityRules)) |
| 188 | + } |
| 189 | + |
188 | 190 | return nil |
189 | 191 | } |
190 | 192 |
|
@@ -229,7 +231,30 @@ func securityGroupStateRefreshFunc(client *ArmClient, resourceGroupName string, |
229 | 231 | } |
230 | 232 | } |
231 | 233 |
|
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) { |
233 | 258 | sgRules := d.Get("security_rule").(*schema.Set).List() |
234 | 259 | rules := make([]network.SecurityRule, 0, len(sgRules)) |
235 | 260 |
|
@@ -268,43 +293,3 @@ func expandAzureRmSecurityGroupRules(d *schema.ResourceData) ([]network.Security |
268 | 293 |
|
269 | 294 | return rules, nil |
270 | 295 | } |
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