-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
387 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package scaleway | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
lb "github.com/scaleway/scaleway-sdk-go/api/lb/v1" | ||
) | ||
|
||
func resourceScalewayLbIPBeta() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceScalewayLbIPBetaCreate, | ||
Read: resourceScalewayLbIPBetaRead, | ||
Update: resourceScalewayLbIPBetaUpdate, | ||
Delete: resourceScalewayLbIPBetaDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
SchemaVersion: 0, | ||
Schema: map[string]*schema.Schema{ | ||
"reverse": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Computed: true, | ||
Description: "The reverse domain name for this IP", | ||
}, | ||
"region": regionSchema(), | ||
"organization_id": organizationIDSchema(), | ||
// Computed | ||
"ip_address": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The load-balance public IP address", | ||
}, | ||
"lb_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The ID of the loadbalancer attached to this IP, if any", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceScalewayLbIPBetaCreate(d *schema.ResourceData, m interface{}) error { | ||
lbAPI, region, err := lbAPIWithRegion(d, m) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
createReq := &lb.CreateIPRequest{ | ||
Region: region, | ||
OrganizationID: d.Get("organization_id").(string), | ||
Reverse: expandStringPtr(d.Get("reverse")), | ||
} | ||
|
||
res, err := lbAPI.CreateIP(createReq) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(newRegionalId(region, res.ID)) | ||
|
||
return resourceScalewayLbIPBetaRead(d, m) | ||
} | ||
|
||
func resourceScalewayLbIPBetaRead(d *schema.ResourceData, m interface{}) error { | ||
lbAPI, region, ID, err := lbAPIWithRegionAndID(m, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
res, err := lbAPI.GetIP(&lb.GetIPRequest{ | ||
Region: region, | ||
IPID: ID, | ||
}) | ||
|
||
if err != nil { | ||
if is404Error(err) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return err | ||
} | ||
|
||
_ = d.Set("region", string(region)) | ||
_ = d.Set("organization_id", res.OrganizationID) | ||
_ = d.Set("ip_id", res.ID) | ||
_ = d.Set("ip_address", res.IPAddress) | ||
_ = d.Set("reverse", res.Reverse) | ||
_ = d.Set("lb_ip", flattenStringPtr(res.LbID)) | ||
|
||
return nil | ||
} | ||
|
||
func resourceScalewayLbIPBetaUpdate(d *schema.ResourceData, m interface{}) error { | ||
lbAPI, region, ID, err := lbAPIWithRegionAndID(m, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if d.HasChange("reverse") { | ||
req := &lb.UpdateIPRequest{ | ||
Region: region, | ||
IPID: ID, | ||
Reverse: expandStringPtr(d.Get("reverse")), | ||
} | ||
|
||
_, err = lbAPI.UpdateIP(req) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return resourceScalewayLbIPBetaRead(d, m) | ||
} | ||
|
||
func resourceScalewayLbIPBetaDelete(d *schema.ResourceData, m interface{}) error { | ||
lbAPI, region, ID, err := lbAPIWithRegionAndID(m, d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = lbAPI.ReleaseIP(&lb.ReleaseIPRequest{ | ||
Region: region, | ||
IPID: ID, | ||
}) | ||
|
||
if err != nil && !is404Error(err) { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package scaleway | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"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) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckScalewayLbIPBetaDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: ` | ||
resource scaleway_lb_ip_beta ip01 { | ||
} | ||
`, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckScalewayLbIPBetaExists("scaleway_lb_ip_beta.ip01"), | ||
testCheckResourceAttrIPv4("scaleway_lb_ip_beta.ip01", "ip_address"), | ||
resource.TestCheckResourceAttrSet("scaleway_lb_ip_beta.ip01", "reverse"), | ||
), | ||
}, | ||
{ | ||
Config: ` | ||
resource scaleway_lb_ip_beta ip01 { | ||
reverse = "myreverse.com" | ||
} | ||
`, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckScalewayLbIPBetaExists("scaleway_lb_ip_beta.ip01"), | ||
testCheckResourceAttrIPv4("scaleway_lb_ip_beta.ip01", "ip_address"), | ||
resource.TestCheckResourceAttr("scaleway_lb_ip_beta.ip01", "reverse", "myreverse.com"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckScalewayLbIPBetaExists(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("resource not found: %s", n) | ||
} | ||
|
||
lbAPI, region, ID, err := lbAPIWithRegionAndID(testAccProvider.Meta(), rs.Primary.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = lbAPI.GetIP(&lb.GetIPRequest{ | ||
IPID: ID, | ||
Region: region, | ||
}) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckScalewayLbIPBetaDestroy(s *terraform.State) error { | ||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "scaleway_lb_ip_beta" { | ||
continue | ||
} | ||
|
||
lbAPI, region, ID, err := lbAPIWithRegionAndID(testAccProvider.Meta(), rs.Primary.ID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
_, err = lbAPI.GetIP(&lb.GetIPRequest{ | ||
Region: region, | ||
IPID: ID, | ||
}) | ||
|
||
// If no error resource still exist | ||
if err == nil { | ||
return fmt.Errorf("IP (%s) still exists", rs.Primary.ID) | ||
} | ||
|
||
// Unexpected api error we return it | ||
if !is404Error(err) { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.