Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Patrik Cyvoct <[email protected]>
  • Loading branch information
Sh4d1 committed Apr 15, 2020
1 parent 760e5a3 commit 50dca1a
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 103 deletions.
28 changes: 5 additions & 23 deletions scaleway/resource_lb_beta.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
80 changes: 1 addition & 79 deletions scaleway/resource_lb_beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand All @@ -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", ""),
),
},
},
Expand Down
35 changes: 35 additions & 0 deletions scaleway/resource_lb_ip_beta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down
40 changes: 39 additions & 1 deletion website/docs/r/lb_beta.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand All @@ -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.

0 comments on commit 50dca1a

Please sign in to comment.