Skip to content

Commit

Permalink
Merge pull request #40 from ans-group/instance-custom-ip
Browse files Browse the repository at this point in the history
Allow IP to be specified for instances
  • Loading branch information
0x4c6565 authored Dec 2, 2022
2 parents 4a2f2be + 2227167 commit c390101
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
1 change: 1 addition & 0 deletions docs/resources/ecloud_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ resource "ecloud_instance" "instance-1" {
- `volume_group_id`: ID of the volumegroup to attach to the instance.
- `host_group_id`: ID of the dedicated host group to move the instance to. Cannot be used with `resource_tier_id`
- `resource_tier_id`: ID of the public resource tier to move the instance to. Cannot be used with `host_group_id`
- `ip_address`: DHCP IP address to allocate to instance


**Note on Floating IPs**
Expand Down
38 changes: 22 additions & 16 deletions ecloud/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ func resourceInstance() *schema.Resource {
Computed: true,
},
"floating_ip_id": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"requires_floating_ip"},
DiffSuppressFunc: func(k, oldValue, newValue string, d *schema.ResourceData) bool {
if d.Get("requires_floating_ip").(bool) {
Expand All @@ -120,10 +120,10 @@ func resourceInstance() *schema.Resource {
},
},
"requires_floating_ip": {
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
Type: schema.TypeBool,
Optional: true,
Default: false,
ForceNew: true,
ConflictsWith: []string{"floating_ip_id"},
},
"data_volume_ids": {
Expand All @@ -141,20 +141,25 @@ func resourceInstance() *schema.Resource {
ForceNew: true,
},
"host_group_id": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ConflictsWith: []string{"resource_tier_id"},
},
"resource_tier_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"host_group_id"},
},
"volume_group_id": {
Type: schema.TypeString,
Optional: true,
},
"ip_address": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
Expand All @@ -179,6 +184,7 @@ func resourceInstanceCreate(d *schema.ResourceData, meta interface{}) error {
RequiresFloatingIP: d.Get("requires_floating_ip").(bool),
HostGroupID: d.Get("host_group_id").(string),
ResourceTierID: d.Get("resource_tier_id").(string),
CustomIPAddress: connection.IPAddress(d.Get("ip_address").(string)),
SSHKeyPairIDs: expandSshKeyPairIds(d.Get("ssh_keypair_ids").(*schema.Set).List()),
}
log.Printf("[DEBUG] Created CreateInstanceRequest: %+v", createReq)
Expand Down Expand Up @@ -387,12 +393,12 @@ func resourceInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
if len(oldFip) < 1 {
return fmt.Errorf("invalid floating ip ID: %s", oldFip)
}

unlock := lock.LockResource(oldFip)
defer unlock()

log.Printf("[DEBUG] Unassigning floating IP with ID [%s]", oldFip)

//unassign floating ip but don't delete as it may be managed by another resource
taskID, err := service.UnassignFloatingIP(oldFip)
if err != nil {
Expand All @@ -403,7 +409,7 @@ func resourceInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
return fmt.Errorf("Error unassigning floating ip with ID [%s]: %s", oldFip, err)
}
}

_, err = waitForResourceState(
ecloudservice.TaskStatusComplete.String(),
TaskStatusRefreshFunc(service, taskID),
Expand All @@ -414,15 +420,15 @@ func resourceInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
}

//unset floating ip
d.Set("floating_ip_id", "")
d.Set("floating_ip_id", "")
}

if oldFip == "" && newFip != "" {
// lock the fip
if len(newFip) < 1 {
return fmt.Errorf("invalid floating ip ID: %s", newFip)
}

unlock := lock.LockResource(newFip)
defer unlock()

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/ukfast/terraform-provider-ecloud
go 1.18

require (
github.com/ans-group/sdk-go v1.16.0
github.com/ans-group/sdk-go v1.16.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.16.0
github.com/stretchr/testify v1.8.1
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki
github.com/anmitsu/go-shlex v0.0.0-20161002113705-648efa622239/go.mod h1:2FmKhYUyUczH0OGQWaF5ceTx0UBShxjsH6f8oGKYe2c=
github.com/ans-group/go-durationstring v1.2.0 h1:UJIuQATkp0t1rBvZsHRwki33YHV9E+Ulro+3NbMB7MM=
github.com/ans-group/go-durationstring v1.2.0/go.mod h1:QGF9Mdpq9058QXaut8r55QWu6lcHX6i/GvF1PZVkV6o=
github.com/ans-group/sdk-go v1.16.0 h1:hHJhKaQzP3Kqa/fmrkgxR4HXLuarAYfMs4gVw52o3JQ=
github.com/ans-group/sdk-go v1.16.0/go.mod h1:Wfbz7OFd8SFIJy5Jun6AsrBnoUv9tp6H1DJhaKU01es=
github.com/ans-group/sdk-go v1.16.1 h1:6yjfc4BC1f16Ln9legoMhjlux+wX6ucRN4AhHhIuV6I=
github.com/ans-group/sdk-go v1.16.1/go.mod h1:Wfbz7OFd8SFIJy5Jun6AsrBnoUv9tp6H1DJhaKU01es=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
Expand Down

0 comments on commit c390101

Please sign in to comment.