From 50dca1a0b6843664f5577c96d11e29cb7ff32af9 Mon Sep 17 00:00:00 2001 From: Patrik Cyvoct Date: Tue, 14 Apr 2020 20:18:56 +0200 Subject: [PATCH] fix Signed-off-by: Patrik Cyvoct --- scaleway/resource_lb_beta.go | 28 ++-------- scaleway/resource_lb_beta_test.go | 80 +--------------------------- scaleway/resource_lb_ip_beta_test.go | 35 ++++++++++++ website/docs/r/lb_beta.html.markdown | 40 +++++++++++++- 4 files changed, 80 insertions(+), 103 deletions(-) diff --git a/scaleway/resource_lb_beta.go b/scaleway/resource_lb_beta.go index 889947f27..e2dba508a 100644 --- a/scaleway/resource_lb_beta.go +++ b/scaleway/resource_lb_beta.go @@ -37,14 +37,9 @@ func resourceScalewayLbBeta() *schema.Resource { Optional: true, Description: "Array of tags to associate with the load-balancer", }, - "release_ip_on_deletion": { - Type: schema.TypeBool, - Computed: true, - }, "ip_id": { Type: schema.TypeString, - Optional: true, - Computed: true, + Required: true, Description: "The load-balance public IP ID", ForceNew: true, DiffSuppressFunc: diffSuppressFuncLocality, @@ -68,18 +63,12 @@ func resourceScalewayLbBetaCreate(d *schema.ResourceData, m interface{}) error { createReq := &lb.CreateLbRequest{ Region: region, + IPID: scw.StringPtr(expandID(d.Get("ip_id").(string))), OrganizationID: d.Get("organization_id").(string), Name: expandOrGenerateString(d.Get("name"), "lb"), Type: d.Get("type").(string), } - if ipID, ok := d.GetOkExists("ip_id"); ok { - createReq.IPID = scw.StringPtr(expandID(ipID.(string))) - _ = d.Set("release_ip_on_deletion", false) - } else { - _ = d.Set("release_ip_on_deletion", true) - } - if raw, ok := d.GetOk("tags"); ok { for _, tag := range raw.([]interface{}) { createReq.Tags = append(createReq.Tags, tag.(string)) @@ -123,12 +112,6 @@ func resourceScalewayLbBetaRead(d *schema.ResourceData, m interface{}) error { return err } - if _, ok := d.GetOkExists("ip_id"); ok { - _ = d.Set("release_ip_on_deletion", false) - } else { - _ = d.Set("release_ip_on_deletion", true) - } - _ = d.Set("name", res.Name) _ = d.Set("region", string(region)) _ = d.Set("organization_id", res.OrganizationID) @@ -171,10 +154,9 @@ func resourceScalewayLbBetaDelete(d *schema.ResourceData, m interface{}) error { } err = lbAPI.DeleteLb(&lb.DeleteLbRequest{ - Region: region, - LbID: ID, - // This parameter will probably be breaking change when ip pre reservation will exist. - ReleaseIP: d.Get("release_ip_on_deletion").(bool), + Region: region, + LbID: ID, + ReleaseIP: false, }) if err != nil && !is404Error(err) { diff --git a/scaleway/resource_lb_beta_test.go b/scaleway/resource_lb_beta_test.go index 192811aae..63e5d3332 100644 --- a/scaleway/resource_lb_beta_test.go +++ b/scaleway/resource_lb_beta_test.go @@ -43,47 +43,6 @@ func testSweepLB(region string) error { return nil } -func TestAccScalewayLbBeta(t *testing.T) { - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckScalewayLbBetaDestroy, - Steps: []resource.TestStep{ - { - Config: ` - resource scaleway_lb_beta lb01 { - name = "test-lb" - type = "lb-s" - } - `, - Check: resource.ComposeTestCheckFunc( - testAccCheckScalewayLbBetaExists("scaleway_lb_beta.lb01"), - resource.TestCheckResourceAttr("scaleway_lb_beta.lb01", "name", "test-lb"), - testCheckResourceAttrUUID("scaleway_lb_beta.lb01", "ip_id"), - testCheckResourceAttrIPv4("scaleway_lb_beta.lb01", "ip_address"), - ), - }, - { - Config: ` - resource scaleway_lb_beta lb01 { - name = "test-lb" - type = "lb-s" - tags = ["tag1", "tag2"] - } - `, - Check: resource.ComposeTestCheckFunc( - testAccCheckScalewayLbBetaExists("scaleway_lb_beta.lb01"), - resource.TestCheckResourceAttr("scaleway_lb_beta.lb01", "name", "test-lb"), - resource.TestCheckResourceAttr("scaleway_lb_beta.lb01", "tags.0", "tag1"), - resource.TestCheckResourceAttr("scaleway_lb_beta.lb01", "tags.1", "tag2"), - testCheckResourceAttrUUID("scaleway_lb_beta.lb01", "ip_id"), - testCheckResourceAttrIPv4("scaleway_lb_beta.lb01", "ip_address"), - ), - }, - }, - }) -} - func TestAccScalewayLbAndIPBeta(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -108,62 +67,25 @@ func TestAccScalewayLbAndIPBeta(t *testing.T) { testCheckResourceAttrUUID("scaleway_lb_beta.lb01", "ip_id"), testCheckResourceAttrIPv4("scaleway_lb_beta.lb01", "ip_address"), resource.TestCheckResourceAttrPair("scaleway_lb_beta.lb01", "ip_id", "scaleway_lb_ip_beta.ip01", "id"), - resource.TestCheckResourceAttr("scaleway_lb_beta.lb01", "release_ip_on_deletion", "false"), ), }, { Config: ` resource scaleway_lb_ip_beta ip01 { - reverse = "reverse.com" # force the refresh of the IP } `, Check: resource.ComposeTestCheckFunc( testAccCheckScalewayLbIPBetaExists("scaleway_lb_ip_beta.ip01"), - resource.TestCheckResourceAttr("scaleway_lb_ip_beta.ip01", "lb_id", ""), - ), - }, - }, - }) -} - -func TestAccScalewayLbThenIPBeta(t *testing.T) { - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckScalewayLbBetaDestroy, - Steps: []resource.TestStep{ - { - Config: ` - resource scaleway_lb_beta lb01 { - name = "test-lb" - type = "lb-s" - } - `, - Check: resource.ComposeTestCheckFunc( - testAccCheckScalewayLbBetaExists("scaleway_lb_beta.lb01"), - resource.TestCheckResourceAttr("scaleway_lb_beta.lb01", "name", "test-lb"), - testCheckResourceAttrUUID("scaleway_lb_beta.lb01", "ip_id"), - testCheckResourceAttrIPv4("scaleway_lb_beta.lb01", "ip_address"), - resource.TestCheckResourceAttr("scaleway_lb_beta.lb01", "release_ip_on_deletion", "true"), ), }, { Config: ` resource scaleway_lb_ip_beta ip01 { } - - resource scaleway_lb_beta lb01 { - ip_id = scaleway_lb_ip_beta.ip01.id - name = "test-lb" - type = "lb-s" - } `, Check: resource.ComposeTestCheckFunc( - testAccCheckScalewayLbBetaExists("scaleway_lb_beta.lb01"), testAccCheckScalewayLbIPBetaExists("scaleway_lb_ip_beta.ip01"), - resource.TestCheckResourceAttrPair("scaleway_lb_beta.lb01", "ip_id", "scaleway_lb_ip_beta.ip01", "id"), - resource.TestCheckResourceAttr("scaleway_lb_beta.lb01", "release_ip_on_deletion", "false"), - resource.TestCheckResourceAttrPair("scaleway_lb_beta.lb01", "ip_id", "scaleway_lb_ip_beta.ip01", "id"), + resource.TestCheckResourceAttr("scaleway_lb_ip_beta.ip01", "lb_id", ""), ), }, }, diff --git a/scaleway/resource_lb_ip_beta_test.go b/scaleway/resource_lb_ip_beta_test.go index 615dfd399..55c4a98a2 100644 --- a/scaleway/resource_lb_ip_beta_test.go +++ b/scaleway/resource_lb_ip_beta_test.go @@ -7,8 +7,43 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/terraform" "github.com/scaleway/scaleway-sdk-go/api/lb/v1" + "github.com/scaleway/scaleway-sdk-go/scw" ) +func init() { + resource.AddTestSweepers("scaleway_lb_ip_beta", &resource.Sweeper{ + Name: "scaleway_lb_ip_beta", + F: testSweepLBIP, + }) +} + +func testSweepLBIP(region string) error { + scwClient, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("error getting client in sweeper: %s", err) + } + lbAPI := lb.NewAPI(scwClient) + + l.Debugf("sweeper: destroying the lb ips in (%s)", region) + listIPs, err := lbAPI.ListIPs(&lb.ListIPsRequest{}, scw.WithAllPages()) + if err != nil { + return fmt.Errorf("error listing lb ips in (%s) in sweeper: %s", region, err) + } + + for _, ip := range listIPs.IPs { + if ip.LbID == nil { + err := lbAPI.ReleaseIP(&lb.ReleaseIPRequest{ + IPID: ip.ID, + }) + if err != nil { + return fmt.Errorf("error deleting lb ip in sweeper: %s", err) + } + } + } + + return nil +} + func TestAccScalewayLbIPBeta(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, diff --git a/website/docs/r/lb_beta.html.markdown b/website/docs/r/lb_beta.html.markdown index fbfb08448..091c3b0ca 100644 --- a/website/docs/r/lb_beta.html.markdown +++ b/website/docs/r/lb_beta.html.markdown @@ -30,7 +30,7 @@ resource "scaleway_lb_beta" "base" { The following arguments are supported: -- `ip_id` - (Required) The ID of the associated IP. +- `ip_id` - (Required) The ID of the associated IP. See below. ~> **Important:** Updates to `ip_id` will recreate the load-balancer. @@ -53,6 +53,42 @@ In addition to all arguments above, the following attributes are exported: - `id` - The ID of the load-balancer. - `ip_address` - The load-balance public IP Address +## IP ID + +Since v1.15.0, `ip_id` is a required field. This means that now a separate `scaleway_lb_ip_beta` is required. +When importing, the IP needs to be imported as well as the LB. +When upgrading to v1.15.0, you will need to create a new `scaleway_lb_ip_beta` resource and import it. + +For instance, if you had the following: +```hcl +resource "scaleway_lb_beta" "base" { + region = "fr-par" + type = "LB-S" +} +``` + +You will need to update it to: +```hcl +resource "scaleway_lb_ip_beta" "ip" { +} + +resource "scaleway_lb_beta" "base" { + ip_id = scaleway_lb_ip_beta.ip.id + region = "fr-par" + type = "LB-S" +} +``` + +And before running `terraform apply` you will need to import the IP with: +```bash +$ terraform import scaleway_lb_ip_beta.ip fr-par/11111111-1111-1111-1111-111111111111 +``` + +The IP ID can either be found in the console, or you can run: +```bash +$ terraform state show scaleway_lb_beta.base +``` +and look for `ip_id`. ## Import @@ -61,3 +97,5 @@ Load-Balancer can be imported using the `{region}/{id}`, e.g. ```bash $ terraform import scaleway_lb_beta.lb01 fr-par/11111111-1111-1111-1111-111111111111 ``` + +Be aware that you will also need to import the `scaleway_lb_ip_beta` resource.