diff --git a/CHANGELOG.md b/CHANGELOG.md index 8452e93..f0165f3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cosmic/resource_cosmic_network_acl.go b/cosmic/resource_cosmic_network_acl.go index 2123cfc..f61a09e 100644 --- a/cosmic/resource_cosmic_network_acl.go +++ b/cosmic/resource_cosmic_network_acl.go @@ -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, @@ -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{ @@ -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) diff --git a/cosmic/resource_cosmic_network_acl_test.go b/cosmic/resource_cosmic_network_acl_test.go index 5f76c40..554da30 100644 --- a/cosmic/resource_cosmic_network_acl_test.go +++ b/cosmic/resource_cosmic_network_acl_test.go @@ -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] @@ -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") } @@ -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 @@ -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" @@ -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, + ) +} diff --git a/website/docs/r/network_acl.html.markdown b/website/docs/r/network_acl.html.markdown index 9c241e1..7f59025 100644 --- a/website/docs/r/network_acl.html.markdown +++ b/website/docs/r/network_acl.html.markdown @@ -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.