Skip to content

Commit

Permalink
Update floating ip and instance placement (#55)
Browse files Browse the repository at this point in the history
* add polling to FloatingIPResource Create Function

* remove default placement
  • Loading branch information
Theophilus Okwugwuni authored Feb 19, 2024
1 parent c200462 commit 2be4d79
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
1 change: 0 additions & 1 deletion docs/resources/instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ resource "genesiscloud_instance" "example" {
- The string length must be at least 16.
- `placement_option` (String) The placement option identifier in which instances are physically located relative to each other within a zone.
- If the value of this attribute changes, Terraform will destroy and recreate the resource.
- Sets the default value "AUTO" if the attribute is not set.
- `security_group_ids` (Set of String) The security groups of the instance. If not provided will be set to the default security group.
- `ssh_key_ids` (Set of String) The ssh keys of the instance.
- If the value of this attribute changes, Terraform will destroy and recreate the resource.
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/genesiscloud/terraform-provider-genesiscloud
go 1.20

require (
github.com/genesiscloud/genesiscloud-go v1.0.5
github.com/genesiscloud/genesiscloud-go v1.0.6
github.com/hashicorp/go-retryablehttp v0.7.5
github.com/hashicorp/terraform-plugin-docs v0.18.0
github.com/hashicorp/terraform-plugin-framework v1.5.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/genesiscloud/genesiscloud-go v1.0.5 h1:9L1yci9ZzTqEzDKDmLv+AXIA5LZxzATs6MtbIV14Mu4=
github.com/genesiscloud/genesiscloud-go v1.0.5/go.mod h1:OAMjSCejQTC4BBLWXleegT/fg+X46SZ6t/vW9RPtXWg=
github.com/genesiscloud/genesiscloud-go v1.0.6 h1:31c6gltkSLB/dflEv/KUEq3q2RSLS7CiptmdfOCpxRk=
github.com/genesiscloud/genesiscloud-go v1.0.6/go.mod h1:OAMjSCejQTC4BBLWXleegT/fg+X46SZ6t/vW9RPtXWg=
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 h1:+zs/tPmkDkHx3U66DAb0lQFJrpS6731Oaa12ikc+DiI=
github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU=
github.com/go-git/go-git/v5 v5.10.1 h1:tu8/D8i+TWxgKpzQ3Vc43e+kkhXqtsZCKI/egajKnxk=
Expand Down
61 changes: 54 additions & 7 deletions internal/provider/floating_ip_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ func (r *FloatingIPResource) Create(ctx context.Context, req resource.CreateRequ

body.Name = data.Name.ValueString()
body.Region = genesiscloud.Region(data.Region.ValueString())
body.Description = data.Description.ValueStringPointer()

// TODO: add this back when we support ipv6
// body.Version = &genesiscloud.CreateFloatingIPJSONBodyVersion(data.Version.ValueString())
body.Description = pointer(data.Description.ValueString())

response, err := r.client.CreateFloatingIPWithResponse(ctx, body)
if err != nil {
Expand All @@ -160,7 +157,7 @@ func (r *FloatingIPResource) Create(ctx context.Context, req resource.CreateRequ
return
}

resp.Diagnostics.Append(data.PopulateFromClientResponse(ctx, floatingIPResponse.FloatingIp)...)
resp.Diagnostics.Append(data.PopulateFromClientResponse(ctx, &floatingIPResponse.FloatingIp)...)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -169,6 +166,56 @@ func (r *FloatingIPResource) Create(ctx context.Context, req resource.CreateRequ

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

floatingIPId := floatingIPResponse.FloatingIp.Id

for {
err := r.client.PollingWait(ctx)
if err != nil {
resp.Diagnostics.AddError("Polling Error", generateErrorMessage("polling floatingIP", err))
return
}

tflog.Trace(ctx, "polling a floatingIP resource")

response, err := r.client.GetFloatingIPWithResponse(ctx, floatingIPId)
if err != nil {
resp.Diagnostics.AddError("Client Error", generateErrorMessage("polling floatingIP", err))
return
}

floatingIPResponse := response.JSON200
if floatingIPResponse == nil {
resp.Diagnostics.AddError("Client Error", generateClientErrorMessage("polling floatingIP", ErrorResponse{
Body: response.Body,
HTTPResponse: response.HTTPResponse,
Error: response.JSONDefault,
}))
return
}

status := floatingIPResponse.FloatingIp.Status
if status == "created" || status == "error" {
resp.Diagnostics.Append(data.PopulateFromClientResponse(ctx, &floatingIPResponse.FloatingIp)...)
if resp.Diagnostics.HasError() {
return
}

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

if status == "error" {
resp.Diagnostics.AddError("Provisioning Error", generateErrorMessage("polling floatingIP", ErrResourceInErrorState))
}
return
}
}
}

func (r *FloatingIPResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
Expand Down Expand Up @@ -205,7 +252,7 @@ func (r *FloatingIPResource) Read(ctx context.Context, req resource.ReadRequest,
return
}

resp.Diagnostics.Append(data.PopulateFromClientResponse(ctx, floatingIPResponse.FloatingIp)...)
resp.Diagnostics.Append(data.PopulateFromClientResponse(ctx, &floatingIPResponse.FloatingIp)...)
if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -255,7 +302,7 @@ func (r *FloatingIPResource) Update(ctx context.Context, req resource.UpdateRequ
return
}

resp.Diagnostics.Append(data.PopulateFromClientResponse(ctx, floatingIPResponse.FloatingIp)...)
resp.Diagnostics.Append(data.PopulateFromClientResponse(ctx, &floatingIPResponse.FloatingIp)...)
if resp.Diagnostics.HasError() {
return
}
Expand Down
2 changes: 0 additions & 2 deletions internal/provider/instance_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/genesiscloud/genesiscloud-go"
"github.com/genesiscloud/terraform-provider-genesiscloud/internal/defaultplanmodifier"
"github.com/genesiscloud/terraform-provider-genesiscloud/internal/resourceenhancer"
"github.com/hashicorp/terraform-plugin-framework-timeouts/resource/timeouts"
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
Expand Down Expand Up @@ -128,7 +127,6 @@ func (r *InstanceResource) Schema(ctx context.Context, req resource.SchemaReques
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplace(),
defaultplanmodifier.String("AUTO"),
},
Validators: []validator.String{},
}),
Expand Down

0 comments on commit 2be4d79

Please sign in to comment.