Skip to content

Commit

Permalink
Merge pull request #60 from MissionCriticalCloud/support_renaming_net…
Browse files Browse the repository at this point in the history
…work_acls

Support renaming network ACL properties
  • Loading branch information
shoekstra authored Apr 19, 2020
2 parents 7f1d2f2 + df33464 commit 7639e56
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## Unreleased

- Add ability to rename `cosmic_network_acl`'s `name` and `description` fields
- Add `client_timeout` and `server_timeout` fields to `cosmic_loadbalancer_rule`
- Fix bug where changing `cosmic_loadbalancer_rule` private or public ports did not recreate the resource
- Fix bug where changing `cosmic_loadbalancer_rule` protocol did not recreate the resource
Expand Down
32 changes: 30 additions & 2 deletions cosmic/resource_cosmic_network_acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func resourceCosmicNetworkACL() *schema.Resource {
return &schema.Resource{
Create: resourceCosmicNetworkACLCreate,
Read: resourceCosmicNetworkACLRead,
Update: resourceCosmicNetworkACLUpdate,
Delete: resourceCosmicNetworkACLDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand All @@ -22,14 +23,12 @@ func resourceCosmicNetworkACL() *schema.Resource {
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"description": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},

"vpc_id": &schema.Schema{
Expand Down Expand Up @@ -90,6 +89,35 @@ func resourceCosmicNetworkACLRead(d *schema.ResourceData, meta interface{}) erro
return nil
}

func resourceCosmicNetworkACLUpdate(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cosmic.CosmicClient)
name := d.Get("name").(string)

// Create a new parameter struct
p := cs.NetworkACL.NewUpdateNetworkACLListParams(d.Id())

// Check if the name or description is changed
if d.HasChange("name") || d.HasChange("description") {
p.SetName(name)

// Compute/set the display text
description := d.Get("description").(string)
if description == "" {
description = name
}
p.SetDescription(description)
}

// Update the network ACL
_, err := cs.NetworkACL.UpdateNetworkACLList(p)
if err != nil {
return fmt.Errorf(
"Error updating network ACL %s: %s", name, err)
}

return resourceCosmicNetworkACLRead(d, meta)
}

func resourceCosmicNetworkACLDelete(d *schema.ResourceData, meta interface{}) error {
cs := meta.(*cosmic.CosmicClient)

Expand Down
81 changes: 62 additions & 19 deletions cosmic/resource_cosmic_network_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,52 @@ func TestAccCosmicNetworkACL_basic(t *testing.T) {
t.Skip("This test requires an existing VPC offering (set it by exporting COSMIC_VPC_OFFERING)")
}

var id string
var acl cosmic.NetworkACLList

createAttributes := &testAccCheckCosmicNetworkACLExpectedAttributes{
Name: "terraform-acl",
Description: "terraform-acl-text",
}

updateAttributes := &testAccCheckCosmicNetworkACLExpectedAttributes{
Name: "terraform-acl-updated",
Description: "terraform-acl-text-updated",
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCosmicNetworkACLDestroy,
Steps: []resource.TestStep{
{
Config: testAccCosmicNetworkACL_basic,
Check: resource.ComposeTestCheckFunc(
testAccCheckCosmicNetworkACLExists(
"cosmic_network_acl.foo", &acl),
testAccCheckCosmicNetworkACLBasicAttributes(&acl),
Config: testAccCosmicNetworkACL_basic(createAttributes),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCosmicNetworkACLExists("cosmic_network_acl.foo", &id, &acl),
testAccCheckCosmicNetworkACLBasicAttributes(&acl, createAttributes),
resource.TestCheckResourceAttr(
"cosmic_network_acl.foo", "name", createAttributes.Name),
resource.TestCheckResourceAttr(
"cosmic_network_acl.foo", "description", createAttributes.Description),
),
},

{
Config: testAccCosmicNetworkACL_basic(updateAttributes),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckCosmicNetworkACLExists("cosmic_network_acl.foo", &id, &acl),
testAccCheckCosmicNetworkACLBasicAttributes(&acl, updateAttributes),
resource.TestCheckResourceAttr(
"cosmic_network_acl.foo", "name", updateAttributes.Name),
resource.TestCheckResourceAttr(
"cosmic_network_acl.foo", "description", updateAttributes.Description),
),
},
},
})
}

func testAccCheckCosmicNetworkACLExists(n string, acl *cosmic.NetworkACLList) resource.TestCheckFunc {
func testAccCheckCosmicNetworkACLExists(n string, id *string, acl *cosmic.NetworkACLList) resource.TestCheckFunc {
return func(s *terraform.State) error {

rs, ok := s.RootModule().Resources[n]
Expand All @@ -45,13 +71,21 @@ func testAccCheckCosmicNetworkACLExists(n string, acl *cosmic.NetworkACLList) re
return fmt.Errorf("No network ACL ID is set")
}

if id != nil {
if *id != "" && *id != rs.Primary.ID {
return fmt.Errorf("Resource ID has changed")
}

*id = rs.Primary.ID
}

cs := testAccProvider.Meta().(*cosmic.CosmicClient)
acllist, _, err := cs.NetworkACL.GetNetworkACLListByID(rs.Primary.ID)
acllist, count, err := cs.NetworkACL.GetNetworkACLListByID(rs.Primary.ID)
if err != nil {
return err
}

if acllist.Id != rs.Primary.ID {
if count == 0 {
return fmt.Errorf("Network ACL not found")
}

Expand All @@ -61,15 +95,20 @@ func testAccCheckCosmicNetworkACLExists(n string, acl *cosmic.NetworkACLList) re
}
}

func testAccCheckCosmicNetworkACLBasicAttributes(acl *cosmic.NetworkACLList) resource.TestCheckFunc {
type testAccCheckCosmicNetworkACLExpectedAttributes struct {
Description string
Name string
}

func testAccCheckCosmicNetworkACLBasicAttributes(acl *cosmic.NetworkACLList, want *testAccCheckCosmicNetworkACLExpectedAttributes) resource.TestCheckFunc {
return func(s *terraform.State) error {

if acl.Name != "terraform-acl" {
return fmt.Errorf("Bad name: %s", acl.Name)
if acl.Name != want.Name {
return fmt.Errorf("Bad name: got %s; want %s", acl.Name, want.Name)
}

if acl.Description != "terraform-acl-text" {
return fmt.Errorf("Bad description: %s", acl.Description)
if acl.Description != want.Description {
return fmt.Errorf("Bad name: got %s; want %s", acl.Description, want.Description)
}

return nil
Expand Down Expand Up @@ -97,7 +136,8 @@ func testAccCheckCosmicNetworkACLDestroy(s *terraform.State) error {
return nil
}

var testAccCosmicNetworkACL_basic = fmt.Sprintf(`
func testAccCosmicNetworkACL_basic(attr *testAccCheckCosmicNetworkACLExpectedAttributes) string {
return fmt.Sprintf(`
resource "cosmic_vpc" "foo" {
name = "terraform-vpc"
display_text = "terraform-vpc"
Expand All @@ -108,10 +148,13 @@ resource "cosmic_vpc" "foo" {
}
resource "cosmic_network_acl" "foo" {
name = "terraform-acl"
description = "terraform-acl-text"
name = "%s"
description = "%s"
vpc_id = "${cosmic_vpc.foo.id}"
}`,
COSMIC_VPC_OFFERING,
COSMIC_ZONE,
)
COSMIC_VPC_OFFERING,
COSMIC_ZONE,
attr.Name,
attr.Description,
)
}
6 changes: 2 additions & 4 deletions website/docs/r/network_acl.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ resource "cosmic_network_acl" "default" {

The following arguments are supported:

* `name` - (Required) The name of the ACL. Changing this forces a new resource
to be created.
* `name` - (Required) The name of the ACL.

* `description` - (Optional) The description of the ACL. Changing this forces a
new resource to be created.
* `description` - (Optional) The description of the ACL.

* `vpc_id` - (Required) The ID of the VPC to create this ACL for. Changing this
forces a new resource to be created.
Expand Down

0 comments on commit 7639e56

Please sign in to comment.