-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #207 from Charliekenney23/feat/instance-ip
add instance_ip resource
- Loading branch information
Showing
8 changed files
with
275 additions
and
5 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
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,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 | ||
} |
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,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) | ||
} |
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,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. |
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