Skip to content

Commit

Permalink
feat(instance): create new resource instance_ip_reverse_dns (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
jerome-quere authored Nov 13, 2019
1 parent 556fb6f commit 2d91e15
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 1 deletion.
1 change: 1 addition & 0 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 2 additions & 0 deletions scaleway/resource_instance_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
126 changes: 126 additions & 0 deletions scaleway/resource_instance_ip_reverse_dns.go
Original file line number Diff line number Diff line change
@@ -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
}
38 changes: 38 additions & 0 deletions scaleway/resource_instance_ip_reverse_dns_test.go
Original file line number Diff line number Diff line change
@@ -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", ""),
),
},
},
})
}
2 changes: 1 addition & 1 deletion website/docs/r/instance_ip.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
42 changes: 42 additions & 0 deletions website/docs/r/instance_ip_reverse_dns.html.markdown
Original file line number Diff line number Diff line change
@@ -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
```

0 comments on commit 2d91e15

Please sign in to comment.