Skip to content

Commit

Permalink
Add helper methods for getting/setting state/plan data (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
danafallon authored Aug 28, 2024
1 parent db634f6 commit 39e11d3
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 84 deletions.
61 changes: 33 additions & 28 deletions internal/provider/deployment_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
Expand All @@ -19,6 +20,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

// Ensure provider defined types fully satisfy framework interfaces.
Expand Down Expand Up @@ -176,11 +178,26 @@ func (r *DeploymentResource) Configure(ctx context.Context, req resource.Configu
r.client = client
}

func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Read Terraform plan data into the model
func (r *DeploymentResource) GetUUIDFromState(ctx context.Context, state tfsdk.State, diagnostics diag.Diagnostics) (string, bool) {
var stateData tfmodels.Deployment
diagnostics.Append(state.Get(ctx, &stateData)...)
return stateData.UUID.ValueString(), diagnostics.HasError()
}

func (r *DeploymentResource) GetPlanData(ctx context.Context, plan tfsdk.Plan, diagnostics diag.Diagnostics) (tfmodels.Deployment, bool) {
var planData tfmodels.Deployment
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
if resp.Diagnostics.HasError() {
diagnostics.Append(plan.Get(ctx, &planData)...)
return planData, diagnostics.HasError()
}

func (r *DeploymentResource) SetStateData(ctx context.Context, state *tfsdk.State, diagnostics diag.Diagnostics, deployment artieclient.Deployment) {
// Translate API response type into Terraform model and save it into state
diagnostics.Append(state.Set(ctx, tfmodels.DeploymentFromAPIModel(deployment))...)
}

func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
planData, hasError := r.GetPlanData(ctx, req.Plan, resp.Diagnostics)
if hasError {
return
}

Expand Down Expand Up @@ -214,44 +231,36 @@ func (r *DeploymentResource) Create(ctx context.Context, req resource.CreateRequ
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.DeploymentFromAPIModel(updatedDeployment))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, updatedDeployment)
}

func (r *DeploymentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Read Terraform prior state data into the model
var stateData tfmodels.Deployment
resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)
if resp.Diagnostics.HasError() {
deploymentUUID, hasError := r.GetUUIDFromState(ctx, req.State, resp.Diagnostics)
if hasError {
return
}

deployment, err := r.client.Deployments().Get(ctx, stateData.UUID.ValueString())
deployment, err := r.client.Deployments().Get(ctx, deploymentUUID)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Deployment", err.Error())
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.DeploymentFromAPIModel(deployment))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, deployment)
}

func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Read Terraform plan data into the model
var planData tfmodels.Deployment
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
if resp.Diagnostics.HasError() {
planData, hasError := r.GetPlanData(ctx, req.Plan, resp.Diagnostics)
if hasError {
return
}

// Validate source & destination config before updating the deployment
baseDeployment := planData.ToAPIBaseModel()
if err := r.client.Deployments().ValidateSource(ctx, baseDeployment); err != nil {
resp.Diagnostics.AddError("Unable to Update Deployment", err.Error())
return
}

if err := r.client.Deployments().ValidateDestination(ctx, baseDeployment); err != nil {
resp.Diagnostics.AddError("Unable to Update Deployment", err.Error())
return
Expand All @@ -263,20 +272,16 @@ func (r *DeploymentResource) Update(ctx context.Context, req resource.UpdateRequ
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.DeploymentFromAPIModel(updatedDeployment))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, updatedDeployment)
}

func (r *DeploymentResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Read Terraform prior state data into the model
var stateData tfmodels.Deployment
resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)
if resp.Diagnostics.HasError() {
deploymentUUID, hasError := r.GetUUIDFromState(ctx, req.State, resp.Diagnostics)
if hasError {
return
}

if err := r.client.Deployments().Delete(ctx, stateData.UUID.ValueString()); err != nil {
if err := r.client.Deployments().Delete(ctx, deploymentUUID); err != nil {
resp.Diagnostics.AddError("Unable to Delete Deployment", err.Error())
}
}
Expand Down
62 changes: 33 additions & 29 deletions internal/provider/destination_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"terraform-provider-artie/internal/provider/tfmodels"

"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

// Ensure provider defined types fully satisfy framework interfaces.
Expand Down Expand Up @@ -104,11 +106,26 @@ func (r *DestinationResource) Configure(ctx context.Context, req resource.Config
r.client = client
}

func (r *DestinationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Read Terraform plan data into the model
func (r *DestinationResource) GetUUIDFromState(ctx context.Context, state tfsdk.State, diagnostics diag.Diagnostics) (string, bool) {
var stateData tfmodels.Destination
diagnostics.Append(state.Get(ctx, &stateData)...)
return stateData.UUID.ValueString(), diagnostics.HasError()
}

func (r *DestinationResource) GetPlanData(ctx context.Context, plan tfsdk.Plan, diagnostics diag.Diagnostics) (tfmodels.Destination, bool) {
var planData tfmodels.Destination
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
if resp.Diagnostics.HasError() {
diagnostics.Append(plan.Get(ctx, &planData)...)
return planData, diagnostics.HasError()
}

func (r *DestinationResource) SetStateData(ctx context.Context, state *tfsdk.State, diagnostics diag.Diagnostics, destination artieclient.Destination) {
// Translate API response type into Terraform model and save it into state
diagnostics.Append(state.Set(ctx, tfmodels.DestinationFromAPIModel(destination))...)
}

func (r *DestinationResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
planData, hasError := r.GetPlanData(ctx, req.Plan, resp.Diagnostics)
if hasError {
return
}

Expand All @@ -124,35 +141,27 @@ func (r *DestinationResource) Create(ctx context.Context, req resource.CreateReq
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.DestinationFromAPIModel(destination))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, destination)
}

func (r *DestinationResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Read Terraform prior state data into the model
var stateData tfmodels.Destination
resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)
if resp.Diagnostics.HasError() {
destinationUUID, hasError := r.GetUUIDFromState(ctx, req.State, resp.Diagnostics)
if hasError {
return
}

destinationResp, err := r.client.Destinations().Get(ctx, stateData.UUID.ValueString())
destination, err := r.client.Destinations().Get(ctx, destinationUUID)
if err != nil {
resp.Diagnostics.AddError("Unable to Read Destination", err.Error())
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.DestinationFromAPIModel(destinationResp))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, destination)
}

func (r *DestinationResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Read Terraform plan data into the model
var planData tfmodels.Destination
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
if resp.Diagnostics.HasError() {
planData, hasError := r.GetPlanData(ctx, req.Plan, resp.Diagnostics)
if hasError {
return
}

Expand All @@ -161,27 +170,22 @@ func (r *DestinationResource) Update(ctx context.Context, req resource.UpdateReq
return
}

fullDestination := planData.ToAPIModel()
updatedDestination, err := r.client.Destinations().Update(ctx, fullDestination)
updatedDestination, err := r.client.Destinations().Update(ctx, planData.ToAPIModel())
if err != nil {
resp.Diagnostics.AddError("Unable to Update Destination", err.Error())
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.DestinationFromAPIModel(updatedDestination))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, updatedDestination)
}

func (r *DestinationResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Read Terraform prior state data into the model
var stateData tfmodels.Destination
resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)
if resp.Diagnostics.HasError() {
destinationUUID, hasError := r.GetUUIDFromState(ctx, req.State, resp.Diagnostics)
if hasError {
return
}

if err := r.client.Destinations().Delete(ctx, stateData.UUID.ValueString()); err != nil {
if err := r.client.Destinations().Delete(ctx, destinationUUID); err != nil {
resp.Diagnostics.AddError("Unable to Delete Destination", err.Error())
return
}
Expand Down
59 changes: 32 additions & 27 deletions internal/provider/ssh_tunnel_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (
"terraform-provider-artie/internal/provider/tfmodels"

"github.com/hashicorp/terraform-plugin-framework-validators/int32validator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)

// Ensure provider defined types fully satisfy framework interfaces.
Expand Down Expand Up @@ -83,11 +85,26 @@ func (r *SSHTunnelResource) Configure(ctx context.Context, req resource.Configur
r.client = client
}

func (r *SSHTunnelResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
// Read Terraform plan data into the model
func (r *SSHTunnelResource) GetUUIDFromState(ctx context.Context, state tfsdk.State, diagnostics diag.Diagnostics) (string, bool) {
var stateData tfmodels.SSHTunnel
diagnostics.Append(state.Get(ctx, &stateData)...)
return stateData.UUID.ValueString(), diagnostics.HasError()
}

func (r *SSHTunnelResource) GetPlanData(ctx context.Context, plan tfsdk.Plan, diagnostics diag.Diagnostics) (tfmodels.SSHTunnel, bool) {
var planData tfmodels.SSHTunnel
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
if resp.Diagnostics.HasError() {
diagnostics.Append(plan.Get(ctx, &planData)...)
return planData, diagnostics.HasError()
}

func (r *SSHTunnelResource) SetStateData(ctx context.Context, state *tfsdk.State, diagnostics diag.Diagnostics, sshTunnel artieclient.SSHTunnel) {
// Translate API response type into Terraform model and save it into state
diagnostics.Append(state.Set(ctx, tfmodels.SSHTunnelFromAPIModel(sshTunnel))...)
}

func (r *SSHTunnelResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
planData, hasError := r.GetPlanData(ctx, req.Plan, resp.Diagnostics)
if hasError {
return
}

Expand All @@ -97,35 +114,27 @@ func (r *SSHTunnelResource) Create(ctx context.Context, req resource.CreateReque
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.SSHTunnelFromAPIModel(sshTunnel))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, sshTunnel)
}

func (r *SSHTunnelResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
// Read Terraform prior state data into the model
var stateData tfmodels.SSHTunnel
resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)
if resp.Diagnostics.HasError() {
tunnelUUID, hasError := r.GetUUIDFromState(ctx, req.State, resp.Diagnostics)
if hasError {
return
}

sshTunnel, err := r.client.SSHTunnels().Get(ctx, stateData.UUID.ValueString())
sshTunnel, err := r.client.SSHTunnels().Get(ctx, tunnelUUID)
if err != nil {
resp.Diagnostics.AddError("Unable to Read SSH Tunnel", err.Error())
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.SSHTunnelFromAPIModel(sshTunnel))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, sshTunnel)
}

func (r *SSHTunnelResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Read Terraform plan data into the model
var planData tfmodels.SSHTunnel
resp.Diagnostics.Append(req.Plan.Get(ctx, &planData)...)
if resp.Diagnostics.HasError() {
planData, hasError := r.GetPlanData(ctx, req.Plan, resp.Diagnostics)
if hasError {
return
}

Expand All @@ -135,20 +144,16 @@ func (r *SSHTunnelResource) Update(ctx context.Context, req resource.UpdateReque
return
}

// Translate API response into Terraform model and save it into state
diagnostics := resp.State.Set(ctx, tfmodels.SSHTunnelFromAPIModel(sshTunnel))
resp.Diagnostics.Append(diagnostics...)
r.SetStateData(ctx, &resp.State, resp.Diagnostics, sshTunnel)
}

func (r *SSHTunnelResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
// Read Terraform prior state data into the model
var stateData tfmodels.SSHTunnel
resp.Diagnostics.Append(req.State.Get(ctx, &stateData)...)
if resp.Diagnostics.HasError() {
tunnelUUID, hasError := r.GetUUIDFromState(ctx, req.State, resp.Diagnostics)
if hasError {
return
}

if err := r.client.SSHTunnels().Delete(ctx, stateData.UUID.ValueString()); err != nil {
if err := r.client.SSHTunnels().Delete(ctx, tunnelUUID); err != nil {
resp.Diagnostics.AddError("Unable to Delete SSH Tunnel", err.Error())
}
}
Expand Down

0 comments on commit 39e11d3

Please sign in to comment.