diff --git a/scaleway/provider.go b/scaleway/provider.go index aabcfe218..898cedf8a 100644 --- a/scaleway/provider.go +++ b/scaleway/provider.go @@ -191,6 +191,7 @@ func Provider() terraform.ResourceProvider { "scaleway_baremetal_server_beta": resourceScalewayBaremetalServerBeta(), "scaleway_bucket": resourceScalewayBucket(), "scaleway_instance_ip": resourceScalewayInstanceIP(), + "scaleway_instance_ip_reverse_dns": resourceScalewayInstanceIPReverseDns(), "scaleway_instance_volume": resourceScalewayInstanceVolume(), "scaleway_instance_security_group": resourceScalewayInstanceSecurityGroup(), "scaleway_instance_server": resourceScalewayInstanceServer(), diff --git a/scaleway/resource_instance_ip.go b/scaleway/resource_instance_ip.go index 8d4736054..b5c97def9 100644 --- a/scaleway/resource_instance_ip.go +++ b/scaleway/resource_instance_ip.go @@ -24,6 +24,8 @@ func resourceScalewayInstanceIP() *schema.Resource { "reverse": { Type: schema.TypeString, Optional: true, + Computed: true, + Deprecated: "Deprecated in favor of scaleway_instance_ip_reverse_dns resource", Description: "The reverse DNS for this IP", }, "server_id": { diff --git a/scaleway/resource_instance_ip_reverse_dns.go b/scaleway/resource_instance_ip_reverse_dns.go new file mode 100644 index 000000000..8635ae6d9 --- /dev/null +++ b/scaleway/resource_instance_ip_reverse_dns.go @@ -0,0 +1,126 @@ +package scaleway + +import ( + "github.com/hashicorp/terraform/helper/schema" + "github.com/scaleway/scaleway-sdk-go/api/instance/v1" +) + +func resourceScalewayInstanceIPReverseDns() *schema.Resource { + return &schema.Resource{ + Create: resourceScalewayInstanceIPReverseDnsCreate, + Read: resourceScalewayInstanceIPReverseDnsRead, + Update: resourceScalewayInstanceIPReverseDnsUpdate, + Delete: resourceScalewayInstanceIPReverseDnsDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + SchemaVersion: 0, + Schema: map[string]*schema.Schema{ + "ip_id": { + Type: schema.TypeString, + Required: true, + Description: "The IP ID or IP address", + }, + "reverse": { + Type: schema.TypeString, + Required: true, + Description: "The reverse DNS for this IP", + }, + "zone": zoneSchema(), + }, + } +} + +func resourceScalewayInstanceIPReverseDnsCreate(d *schema.ResourceData, m interface{}) error { + instanceAPI, zone, err := getInstanceAPIWithZone(d, m) + if err != nil { + return err + } + + res, err := instanceAPI.GetIP(&instance.GetIPRequest{ + IP: expandID(d.Get("ip_id")), + Zone: zone, + }) + if err != nil { + return err + } + d.SetId(newZonedId(zone, res.IP.ID)) + + // We do not create any resource. We only need to update the IP. + return resourceScalewayInstanceIPReverseDnsUpdate(d, m) +} + +func resourceScalewayInstanceIPReverseDnsRead(d *schema.ResourceData, m interface{}) error { + instanceAPI, zone, ID, err := getInstanceAPIWithZoneAndID(m, d.Id()) + if err != nil { + return err + } + + res, err := instanceAPI.GetIP(&instance.GetIPRequest{ + IP: ID, + Zone: zone, + }) + + if err != nil { + // We check for 403 because instance API returns 403 for a deleted IP + if is404Error(err) || is403Error(err) { + d.SetId("") + return nil + } + return err + } + + d.Set("zone", string(zone)) + d.Set("reverse", res.IP.Reverse) + return nil +} + +func resourceScalewayInstanceIPReverseDnsUpdate(d *schema.ResourceData, m interface{}) error { + instanceAPI, zone, ID, err := getInstanceAPIWithZoneAndID(m, d.Id()) + if err != nil { + return err + } + + if d.HasChange("reverse") { + l.Debugf("updating IP %q reverse to %q\n", d.Id(), d.Get("reverse")) + + updateReverseReq := &instance.UpdateIPRequest{ + Zone: zone, + IPID: ID, + } + + reverse := d.Get("reverse").(string) + if reverse == "" { + updateReverseReq.Reverse = &instance.NullableStringValue{Null: true} + } else { + updateReverseReq.Reverse = &instance.NullableStringValue{Value: reverse} + } + _, err = instanceAPI.UpdateIP(updateReverseReq) + if err != nil { + return err + } + } + + return resourceScalewayInstanceIPReverseDnsRead(d, m) +} + +func resourceScalewayInstanceIPReverseDnsDelete(d *schema.ResourceData, m interface{}) error { + instanceAPI, zone, ID, err := getInstanceAPIWithZoneAndID(m, d.Id()) + if err != nil { + return err + } + + // Unset the reverse dns on the IP + updateReverseReq := &instance.UpdateIPRequest{ + Zone: zone, + IPID: ID, + Reverse: &instance.NullableStringValue{Null: true}, + } + _, err = instanceAPI.UpdateIP(updateReverseReq) + if err != nil { + return err + } + + d.SetId("") + return nil +} diff --git a/scaleway/resource_instance_ip_reverse_dns_test.go b/scaleway/resource_instance_ip_reverse_dns_test.go new file mode 100644 index 000000000..7a4441980 --- /dev/null +++ b/scaleway/resource_instance_ip_reverse_dns_test.go @@ -0,0 +1,38 @@ +package scaleway + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccScalewayInstanceReverseDnsIP(t *testing.T) { + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckScalewayInstanceIPDestroy, + Steps: []resource.TestStep{ + { + Config: ` + resource "scaleway_instance_ip" "ip" {} + resource "scaleway_instance_ip_reverse_dns" "base" { + ip_id = scaleway_instance_ip.ip.id + reverse = "www.google.fr" + } + `, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("scaleway_instance_ip_reverse_dns.base", "reverse", "www.google.fr"), + ), + }, + { + Config: ` + resource "scaleway_instance_ip" "ip" {} + `, + Taint: []string{"scaleway_instance_ip.ip"}, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("scaleway_instance_ip.ip", "reverse", ""), + ), + }, + }, + }) +} diff --git a/website/docs/r/instance_ip.html.markdown b/website/docs/r/instance_ip.html.markdown index 0f4776351..a996aa903 100644 --- a/website/docs/r/instance_ip.html.markdown +++ b/website/docs/r/instance_ip.html.markdown @@ -19,7 +19,7 @@ resource "scaleway_instance_ip" "server_ip" {} The following arguments are supported: -- `reverse` - (Optional) The reverse DNS for this IP. +- `reverse` - (Deprecated) Please use the `scaleway_instance_ip_reverse_dns` resource instead. - `server_id` - (Optional) The ID of the server you want to attach this resource to. - `zone` - (Defaults to [provider](../index.html#zone) `zone`) The [zone](../guides/regions_and_zones.html#zones) in which the IP should be reserved. - `organization_id` - (Defaults to [provider](../index.html#organization_id) `organization_id`) The ID of the organization the IP is associated with. diff --git a/website/docs/r/instance_ip_reverse_dns.html.markdown b/website/docs/r/instance_ip_reverse_dns.html.markdown new file mode 100644 index 000000000..8e2f3e115 --- /dev/null +++ b/website/docs/r/instance_ip_reverse_dns.html.markdown @@ -0,0 +1,42 @@ +--- +layout: "scaleway" +page_title: "Scaleway: scaleway_instance_ip_reverse_dns" +description: |- + Manages Scaleway Compute Instance IPs' reverse DNS. +--- + +# scaleway_instance_ip_reverse_dns + +Manages Scaleway Compute Instance IPs Reverse DNS. + +## Example Usage + +```hcl +resource "scaleway_instance_ip" "server_ip" {} +resource "scaleway_instance_ip_reverse_dns" "reverse" { + ip_id = scaleway_instance_ip.server_ip.id + reverse = "www.scaleway.com" +} +``` + +## Arguments Reference + +The following arguments are supported: + +- `ip_id` - (Required) The IP ID +- `reverse` - (Required) The reverse DNS for this IP. +- `zone` - (Defaults to [provider](../index.html#zone) `zone`) The [zone](../guides/regions_and_zones.html#zones) in which the IP should be reserved. + +## Attributes Reference + +In addition to all above arguments, the following attributes are exported: + +- `id` - The ID of the IP. + +## Import + +IPs reverse DNS can be imported using the `{zone}/{id}`, e.g. + +```bash +$ terraform import scaleway_instance_ip_reverse_dns.reverse fr-par-1/11111111-1111-1111-1111-111111111111 +```