Skip to content

Commit

Permalink
Merge pull request #207 from Charliekenney23/feat/instance-ip
Browse files Browse the repository at this point in the history
add instance_ip resource
  • Loading branch information
0xch4z committed Jan 27, 2021
2 parents e11a039 + c574b9e commit b1730e2
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 1.14.2 (January 27, 2021)

FEATURES:

* **New Resource** `linode_instance_ip`

## 1.14.1 (January 22, 2021)

ENHANCEMENTS:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
github.com/bflad/tfproviderlint v0.21.0
github.com/golangci/golangci-lint v1.24.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.0
github.com/linode/linodego v0.24.3
github.com/linode/linodego v0.24.4
github.com/linode/linodego/k8s v0.0.0-20200831124119-58d5d5bb7947
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -439,10 +439,8 @@ github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/linode/linodego v0.20.1/go.mod h1:XOWXRHjqeU2uPS84tKLgfWIfTlv3TYzCS0io4GOQzEI=
github.com/linode/linodego v0.24.2 h1:wj7DO4/8zGEJm6HtGp03L1HLATzmffkwEoifiKIFi0c=
github.com/linode/linodego v0.24.2/go.mod h1:GSBKPpjoQfxEfryoCRcgkuUOCuVtGHWhzI8OMdycNTE=
github.com/linode/linodego v0.24.3 h1:9U/I66zouHIU1tZCFgYK/U4IPJj9ShVu4WqgdGcXcyw=
github.com/linode/linodego v0.24.3/go.mod h1:GSBKPpjoQfxEfryoCRcgkuUOCuVtGHWhzI8OMdycNTE=
github.com/linode/linodego v0.24.4 h1:ufOYwv1vqfGSs6M2KmJWLgFyj+6Ousi356lPltwtuEk=
github.com/linode/linodego v0.24.4/go.mod h1:GSBKPpjoQfxEfryoCRcgkuUOCuVtGHWhzI8OMdycNTE=
github.com/linode/linodego/k8s v0.0.0-20200831124119-58d5d5bb7947 h1:e+tpC7AIiEgfYGEDq9Rjtdybq+V10S6OXzWjeGV/CEk=
github.com/linode/linodego/k8s v0.0.0-20200831124119-58d5d5bb7947/go.mod h1:MWI0tFyaJqRpirMv0VO7CGYT4V3IhHvml2rs/DlRQmY=
github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
Expand Down
1 change: 1 addition & 0 deletions linode/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func Provider() *schema.Provider {
"linode_firewall": resourceLinodeFirewall(),
"linode_image": resourceLinodeImage(),
"linode_instance": resourceLinodeInstance(),
"linode_instance_ip": resourceLinodeInstanceIP(),
"linode_lke_cluster": resourceLinodeLKECluster(),
"linode_nodebalancer": resourceLinodeNodeBalancer(),
"linode_nodebalancer_config": resourceLinodeNodeBalancerConfig(),
Expand Down
146 changes: 146 additions & 0 deletions linode/resource_linode_instance_ip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package linode

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/linode/linodego"
)

func resourceLinodeInstanceIP() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLinodeInstanceIPCreate,
ReadContext: resourceLinodeInstanceIPRead,
UpdateContext: resourceLinodeInstanceIPUpdate,
DeleteContext: resourceLinodeInstanceIPDelete,

Schema: map[string]*schema.Schema{
"linode_id": {
Type: schema.TypeInt,
Description: "The ID of the Linode to allocate an IPv4 address for.",
Required: true,
ForceNew: true,
},
"public": {
Type: schema.TypeBool,
Description: "Whether the IPv4 address is public or private.",
Default: true,
Optional: true,
ForceNew: true,
},

"address": {
Type: schema.TypeString,
Description: "The resulting IPv4 address.",
Computed: true,
},
"gateway": {
Type: schema.TypeString,
Description: "The default gateway for this address",
Computed: true,
},
"prefix": {
Type: schema.TypeInt,
Description: "The number of bits set in the subnet mask.",
Computed: true,
},
"rdns": {
Type: schema.TypeString,
Description: "The reverse DNS assigned to this address.",
Optional: true,
Computed: true,
},
"region": {
Type: schema.TypeString,
Description: "The region this IP resides in.",
Computed: true,
},
"subnet_mask": {
Type: schema.TypeString,
Description: "The mask that separates host bits from network bits for this address.",
Computed: true,
},
"type": {
Type: schema.TypeString,
Description: "The type of IP address.",
Computed: true,
},
},
}
}

func resourceLinodeInstanceIPRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderMeta).Client

address := d.Id()
linodeID := d.Get("linode_id").(int)
ip, err := client.GetInstanceIPAddress(ctx, linodeID, address)
if err != nil {
diag.Errorf("failed to get instance (%d) ip: %s", linodeID, err)
}

d.Set("address", ip.Address)
d.Set("gateway", ip.Gateway)
d.Set("prefix", ip.Prefix)
d.Set("rdns", ip.RDNS)
d.Set("region", ip.Region)
d.Set("subnet_mask", ip.SubnetMask)
d.Set("type", ip.Type)
return nil
}

func resourceLinodeInstanceIPCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderMeta).Client

linodeID := d.Get("linode_id").(int)
private := d.Get("public").(bool)
ip, err := client.AddInstanceIPAddress(ctx, linodeID, private)
if err != nil {
diag.Errorf("failed to create instance (%d) ip: %s", linodeID, err)
}

rdns := d.Get("rdns").(string)
if rdns != "" {
if _, err := client.UpdateIPAddress(ctx, ip.Address, linodego.IPAddressUpdateOptions{
RDNS: &rdns,
}); err != nil {
return diag.Errorf("failed to set RDNS for instance (%d) ip (%s): %s", linodeID, ip.Address, err)
}
}

d.SetId(ip.Address)
return resourceLinodeInstanceIPRead(ctx, d, meta)
}

func resourceLinodeInstanceIPUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderMeta).Client

address := d.Id()
linodeID := d.Get("linode_id").(int)
rdns := d.Get("rdns").(string)
if d.HasChange("rdns") {
updateOptions := linodego.IPAddressUpdateOptions{}
if rdns != "" {
updateOptions.RDNS = &rdns
}

if _, err := client.UpdateIPAddress(ctx, address, linodego.IPAddressUpdateOptions{
RDNS: &rdns,
}); err != nil {
return diag.Errorf("failed to update RDNS for instance (%d) ip (%s): %s", linodeID, address, err)
}
}
return nil
}

func resourceLinodeInstanceIPDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*ProviderMeta).Client

address := d.Id()
linodeID := d.Get("linode_id").(int)
if err := client.DeleteInstanceIPAddress(ctx, linodeID, address); err != nil {
return diag.Errorf("failed to delete instance (%d) ip (%s): %s", linodeID, address, err)
}
return nil
}
61 changes: 61 additions & 0 deletions linode/resource_linode_instance_ip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package linode

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const testInstanceIPResName = "linode_instance_ip.test"

func TestAccLinodeInstanceIP_basic(t *testing.T) {
t.Parallel()

name := acctest.RandomWithPrefix("tf_test")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLinodeInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckLinodeInstanceIPBasic(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet(testInstanceIPResName, "address"),
resource.TestCheckResourceAttrSet(testInstanceIPResName, "gateway"),
resource.TestCheckResourceAttrSet(testInstanceIPResName, "prefix"),
resource.TestCheckResourceAttrSet(testInstanceIPResName, "rdns"),
resource.TestCheckResourceAttrSet(testInstanceIPResName, "subnet_mask"),
resource.TestCheckResourceAttr(testInstanceIPResName, "region", "us-east"),
resource.TestCheckResourceAttr(testInstanceIPResName, "type", "ipv4"),
),
},
},
})
}

func testAccCheckLinodeInstanceIPInstance(label string) string {
return fmt.Sprintf(`
resource "linode_instance" "%[1]s" {
label = "%[1]s"
group = "tf_test"
type = "g6-nanode-1"
region = "us-east"
disk {
label = "disk"
image = "linode/alpine3.11"
root_pass = "b4d_p4s5"
authorized_keys = ["%[2]s"]
size = 3000
}
}`, label, publicKeyMaterial)
}

func testAccCheckLinodeInstanceIPBasic(label string) string {
return testAccCheckLinodeInstanceIPInstance(label) + fmt.Sprintf(`
resource "linode_instance_ip" "test" {
linode_id = linode_instance.%s.id
public = true
}`, label)
}
55 changes: 55 additions & 0 deletions website/docs/r/instance_ip.html.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
layout: "linode"
page_title: "Linode: linode_instance_ip"
sidebar_current: "docs-linode-instance-ip"
description: |-
Manages a Linode instance IP.
---

# linode\_instance\_ip

~> **NOTICE:** You may need to contact support to increase your instance IP limit before you can allocate additional IPs.

Manages a Linode instance IP.

## Example Usage

```terraform
resource "linode_instance" "foo" {
image = "linode/alpine3.12"
label = "foobar-test"
type = "g6-nanode-1"
region = "us-east"
}
resource "linode_instance_ip" "foo" {
linode_id = linode_instance.foo.id
public = true
}
```

## Argument Reference

The following arguments are supported:

* `linode_id` - (Required) The ID of the Linode to allocate an IPv4 address for.

* `public` - (Optional) Whether the IPv4 address is public or private. Defaults to true.

* `rdns` - (Optional) The reverse DNS assigned to this address.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `gateway` - The default gateway for this address

* `prefix` - The number of bits set in the subnet mask.

* `address` - The resulting IPv4 address.

* `region` - The region this IP resides in.

* `subnet_mask` - The mask that separates host bits from network bits for this address.

* `type` - The type of IP address.
3 changes: 3 additions & 0 deletions website/linode.erb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@
<li<%= sidebar_current("docs-linode-resource-instance") %>>
<a href="/docs/providers/linode/r/instance.html">linode_instance</a>
</li>
<li<%= sidebar_current("docs-linode-resource-instance-ip") %>>
<a href="/docs/providers/linode/r/instance_ip.html">linode_instance</a>
</li>
<li<%= sidebar_current("docs-linode-resource-lke-cluster") %>>
<a href="/docs/providers/linode/r/lke_cluster.html">linode_lke_cluster</a>
</li>
Expand Down

0 comments on commit b1730e2

Please sign in to comment.