-
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.
Signed-off-by: Patrik Cyvoct <[email protected]>
- Loading branch information
Showing
7 changed files
with
351 additions
and
7 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,137 @@ | ||
package scaleway | ||
|
||
import ( | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
lb "github.com/scaleway/scaleway-sdk-go/api/lb/v1" | ||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
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), | ||
} | ||
if reverse, ok := d.GetOk("reverse"); ok { | ||
createReq.Reverse = scw.StringPtr(reverse.(string)) | ||
} | ||
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) | ||
if res.LbID != nil { | ||
_ = d.Set("lb_id", *res.LbID) | ||
} else { | ||
_ = d.Set("lb_id", "") | ||
} | ||
|
||
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: scw.StringPtr(d.Get("reverse").(string)), | ||
} | ||
|
||
_, 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,98 @@ | ||
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" | ||
) | ||
|
||
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 | ||
} |
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
Oops, something went wrong.