Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reverting the change to make Nested security rules from List to Set. … #893

Merged
merged 3 commits into from
Mar 1, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions azurerm/resource_arm_network_security_group.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package azurerm

import (
"bytes"
"fmt"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2017-09-01/network"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -33,7 +35,7 @@ func resourceArmNetworkSecurityGroup() *schema.Resource {
"resource_group_name": resourceGroupNameSchema(),

"security_rule": {
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Expand Down Expand Up @@ -103,6 +105,7 @@ func resourceArmNetworkSecurityGroup() *schema.Resource {
},
},
},
Set: resourceArmNetworkSecurityGroupRuleHash,
},

"tags": tagsSchema(),
Expand Down Expand Up @@ -216,8 +219,23 @@ func resourceArmNetworkSecurityGroupDelete(d *schema.ResourceData, meta interfac
return err
}

func flattenNetworkSecurityRules(rules *[]network.SecurityRule) []interface{} {
result := make([]interface{}, 0)
func resourceArmNetworkSecurityGroupRuleHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%s-", m["protocol"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["source_port_range"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["destination_port_range"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["source_address_prefix"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["destination_address_prefix"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["access"].(string)))
buf.WriteString(fmt.Sprintf("%d-", m["priority"].(int)))
buf.WriteString(fmt.Sprintf("%s-", m["direction"].(string)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since all items are contained within the Set's hash function, we actually don't need to specify this function at all - so can we remove this method? the default set hash function will include all values in the set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. removed this function.


return hashcode.String(buf.String())
}

func flattenNetworkSecurityRules(rules *[]network.SecurityRule) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(*rules))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there's a crash here is rules is nil - can we make this:

result := make([]map[string]interface{}, 0)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it.


if rules != nil {
for _, rule := range *rules {
Expand Down Expand Up @@ -255,8 +273,8 @@ func flattenNetworkSecurityRules(rules *[]network.SecurityRule) []interface{} {
}

func expandAzureRmSecurityRules(d *schema.ResourceData) ([]network.SecurityRule, error) {
sgRules := d.Get("security_rule").([]interface{})
rules := make([]network.SecurityRule, 0)
sgRules := d.Get("security_rule").(*schema.Set).List()
rules := make([]network.SecurityRule, 0, len(sgRules))

for _, sgRaw := range sgRules {
data := sgRaw.(map[string]interface{})
Expand Down