-
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.
feat(instance): create new resource instance_ip_reverse_dns (#315)
- Loading branch information
1 parent
556fb6f
commit 2d91e15
Showing
6 changed files
with
210 additions
and
1 deletion.
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
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 | ||
} |
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,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", ""), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
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,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 | ||
``` |