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

enhancement : added tag support for network ACL #2343

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
51 changes: 50 additions & 1 deletion ibm/resource_ibm_is_networkacls.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ package ibm

import (
"container/list"
"context"
"fmt"
"log"
"os"
"reflect"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"

"github.com/IBM/vpc-go-sdk/vpcclassicv1"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -39,6 +43,8 @@ const (
isNetworkACLRuleSourcePortMin = "source_port_min"
isNetworkACLVPC = "vpc"
isNetworkACLResourceGroup = "resource_group"
isNetworkACLTags = "tags"
isNetworkACLCRN = "crn"
)

func resourceIBMISNetworkACL() *schema.Resource {
Expand All @@ -55,6 +61,12 @@ func resourceIBMISNetworkACL() *schema.Resource {
Delete: schema.DefaultTimeout(10 * time.Minute),
},

CustomizeDiff: customdiff.Sequence(
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
return resourceTagsCustomizeDiff(diff)
},
),

Schema: map[string]*schema.Schema{
isNetworkACLName: {
Type: schema.TypeString,
Expand All @@ -76,6 +88,20 @@ func resourceIBMISNetworkACL() *schema.Resource {
Computed: true,
Description: "Resource group ID for the network ACL",
},
isNetworkACLTags: {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: resourceIBMVPCHash,
Description: "List of tags",
},

isNetworkACLCRN: {
Type: schema.TypeString,
Computed: true,
Description: "The crn of the resource",
},
ResourceControllerURL: {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -464,6 +490,15 @@ func nwaclCreate(d *schema.ResourceData, meta interface{}, name string) error {
if err != nil {
return err
}
v := os.Getenv("IC_ENV_TAGS")
if _, ok := d.GetOk(isNetworkACLTags); ok || v != "" {
oldList, newList := d.GetChange(isNetworkACLTags)
err = UpdateTagsUsingCRN(oldList, newList, meta, *nwacl.CRN)
if err != nil {
log.Printf(
"Error on create of resource network acl (%s) tags: %s", d.Id(), err)
}
}
return nil
}

Expand Down Expand Up @@ -619,7 +654,13 @@ func nwaclGet(d *schema.ResourceData, meta interface{}, id string) error {
d.Set(isNetworkACLResourceGroup, *nwacl.ResourceGroup.ID)
d.Set(ResourceGroupName, *nwacl.ResourceGroup.Name)
}

tags, err := GetTagsUsingCRN(meta, *nwacl.CRN)
if err != nil {
log.Printf(
"Error on get of resource network acl (%s) tags: %s", d.Id(), err)
}
d.Set(isNetworkACLTags, tags)
d.Set(isNetworkACLCRN, *nwacl.CRN)
rules := make([]interface{}, 0)
if len(nwacl.Rules) > 0 {
for _, rulex := range nwacl.Rules {
Expand Down Expand Up @@ -806,6 +847,14 @@ func nwaclUpdate(d *schema.ResourceData, meta interface{}, id, name string, hasC
return fmt.Errorf("Error Updating Network ACL(%s) : %s\n%s", id, err, response)
}
}
if d.HasChange(isNetworkACLTags) {
oldList, newList := d.GetChange(isNetworkACLTags)
err = UpdateTagsUsingCRN(oldList, newList, meta, d.Get(isNetworkACLCRN).(string))
if err != nil {
log.Printf(
"Error on update of resource network acl (%s) tags: %s", d.Id(), err)
}
}
if d.HasChange(isNetworkACLRules) {
err := validateInlineRules(rules)
if err != nil {
Expand Down
8 changes: 8 additions & 0 deletions ibm/resource_ibm_is_networkacls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func TestNetworkACLGen2(t *testing.T) {
"ibm_is_network_acl.isExampleACL", "name", "is-example-acl"),
resource.TestCheckResourceAttr(
"ibm_is_network_acl.isExampleACL", "rules.#", "2"),
resource.TestCheckResourceAttr(
"ibm_is_network_acl.isExampleACL", "tags.#", "2"),
),
},
},
Expand Down Expand Up @@ -133,8 +135,13 @@ func testAccCheckIBMISNetworkACLExists(n, nwACL string) resource.TestCheckFunc {

func testAccCheckIBMISNetworkACLConfig() string {
return fmt.Sprintf(`
resource "ibm_is_vpc" "testacc_vpc" {
name = "tf-nwacl-vpc"
}

resource "ibm_is_network_acl" "isExampleACL" {
name = "is-example-acl"
vpc = ibm_is_vpc.testacc_vpc.id
rules {
name = "outbound"
action = "allow"
Expand Down Expand Up @@ -175,6 +182,7 @@ func testAccCheckIBMISNetworkACLConfig1() string {

resource "ibm_is_network_acl" "isExampleACL" {
name = "is-example-acl"
tags = ["Tag1", "tag2"]
vpc = ibm_is_vpc.testacc_vpc.id
rules {
name = "outbound"
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/is_network_acl.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ Nested `rules` blocks have the following structure:
* `port_min` - (Optional, int) The lowest port in the range of ports to be matched; if unspecified, 1 is used.
* `source_port_max` - (Optional, int) The highest port in the range of ports to be matched; if unspecified, 65535 is used.
* `source_port_min` - (Optional, int) The lowest port in the range of ports to be matched; if unspecified, 1 is used.
* `tags` - (Optional, list(string)) Tags associated with the network ACL.


## Attribute Reference
Expand All @@ -156,6 +157,7 @@ Nested `rules` blocks have the following structure:
* `id` - The rule id.
* `ip_version` - The IP version of the rule.
* `subnets` - The subnets for the ACL rule.
* `crn` - The CRN of the network ACL.

## Import

Expand Down