Skip to content

Commit

Permalink
improve tag value association error handling (#73)
Browse files Browse the repository at this point in the history
* improve tag value association error handling

* fix syntax

* handle edge case where tag value exists in state but was removed from torque by api

* textual issue
  • Loading branch information
amiros89 authored Oct 1, 2024
1 parent ac8685c commit 1d3d8f2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,18 @@ func (r *TorqueTagBlueprintValueAssociationResource) Create(ctx context.Context,

err := r.client.CreateBlueprintTagValue(data.SpaceName.ValueString(), data.TagName.ValueString(),
data.TagValue.ValueString(), data.RepositoryName.ValueString(), data.BlueprintName.ValueString())
if err != nil && strings.Contains(err.Error(), "422") {
new_err := r.client.SetBlueprintTagValue(data.SpaceName.ValueString(), data.TagName.ValueString(), data.TagValue.ValueString(), data.RepositoryName.ValueString(), data.BlueprintName.ValueString())
if new_err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create tag value in blueprint, got error: %s", err))
return
}
} else {
if err != nil {
if err != nil {
if strings.Contains(err.Error(), "422") {
new_err := r.client.SetBlueprintTagValue(data.SpaceName.ValueString(), data.TagName.ValueString(), data.TagValue.ValueString(), data.RepositoryName.ValueString(), data.BlueprintName.ValueString())
if new_err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to set tag value in blueprint, got error: %s", err))
return
}
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create tag value in blueprint, got error: %s", err))
return
}
}

tflog.Trace(ctx, "Resource Created Successful!")

// Save data into Terraform state.
Expand All @@ -127,6 +126,10 @@ func (r *TorqueTagBlueprintValueAssociationResource) Read(ctx context.Context, r
return
}
tag, err := r.client.GetBlueprintTag(data.SpaceName.ValueString(), data.TagName.ValueString(), data.RepositoryName.ValueString(), data.BlueprintName.ValueString())
if tag == (client.NameValuePair{}) {
resp.State.RemoveResource(ctx)
return
}
if err != nil {
resp.Diagnostics.AddError(
"Error Reading tag details",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,14 @@ func (r *TorqueTagSpaceValueAssociationResource) Create(ctx context.Context, req
}

err := r.client.CreateSpaceTagValue(data.SpaceName.ValueString(), data.TagName.ValueString(), data.TagValue.ValueString())
if err != nil && strings.Contains(err.Error(), "422") {
new_err := r.client.SetSpaceTagValue(data.SpaceName.ValueString(), data.TagName.ValueString(), data.TagValue.ValueString())
if new_err != nil {
if err != nil {
if strings.Contains(err.Error(), "422") {
newErr := r.client.SetSpaceTagValue(data.SpaceName.ValueString(), data.TagName.ValueString(), data.TagValue.ValueString())
if newErr != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to set tag value in space, got error: %s", newErr))
return
}
} else {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create tag value in space, got error: %s", err))
return
}
Expand All @@ -110,10 +115,14 @@ func (r *TorqueTagSpaceValueAssociationResource) Read(ctx context.Context, req r
return
}
tag, err := r.client.GetSpaceTag(data.SpaceName.ValueString(), data.TagName.ValueString())
if tag == (client.NameValuePair{}) {
resp.State.RemoveResource(ctx)
return
}
if err != nil {
resp.Diagnostics.AddError(
"Error Reading tag details",
"Could not read space tag name "+data.TagName.ValueString()+": "+err.Error(),
"Could not read space tag value of "+data.TagName.ValueString()+": "+err.Error(),
)
return
}
Expand Down

0 comments on commit 1d3d8f2

Please sign in to comment.