Skip to content

Commit

Permalink
fix: Only update attributes during Read if the value has actually cha…
Browse files Browse the repository at this point in the history
…nged, to avoid showing plan changes

Issue #2
  • Loading branch information
MatthewJohn committed Jan 30, 2024
1 parent f9e0916 commit fcb74ec
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
37 changes: 23 additions & 14 deletions internal/provider/module_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,25 @@ func (r *ModuleResource) Read(ctx context.Context, req resource.ReadRequest, res
return
}

// Update attributes
data.GitProviderID = types.Int64Value(module.GitProviderID)
data.RepoBaseUrlTemplate = types.StringValue(module.RepoBaseUrlTemplate)
data.RepoCloneUrlTemplate = types.StringValue(module.RepoCloneUrlTemplate)
data.RepoBrowseUrlTemplate = types.StringValue(module.RepoBrowseUrlTemplate)
data.GitTagFormat = types.StringValue(module.GitTagFormat)
data.GitPath = types.StringValue(module.GitPath)
// Generate ID
data.ID = types.StringValue(fmt.Sprintf("%s/%s/%s", data.Namespace.ValueString(), data.Name.ValueString(), data.Provider.ValueString()))
// Update attributes, if they've modified
if data.GitProviderID.ValueInt64() != module.GitProviderID {
data.GitProviderID = types.Int64Value(module.GitProviderID)
}
if data.RepoBaseUrlTemplate.ValueString() != module.RepoBaseUrlTemplate {
data.RepoBaseUrlTemplate = types.StringValue(module.RepoBaseUrlTemplate)
}
if data.RepoCloneUrlTemplate.ValueString() != module.RepoCloneUrlTemplate {
data.RepoCloneUrlTemplate = types.StringValue(module.RepoCloneUrlTemplate)
}
if data.RepoBrowseUrlTemplate.ValueString() != module.RepoBrowseUrlTemplate {
data.RepoBrowseUrlTemplate = types.StringValue(module.RepoBrowseUrlTemplate)
}
if data.GitTagFormat.ValueString() != module.GitTagFormat {
data.GitTagFormat = types.StringValue(module.GitTagFormat)
}
if data.GitPath.ValueString() != module.GitPath {
data.GitPath = types.StringValue(module.GitPath)
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand All @@ -227,9 +237,6 @@ func (r *ModuleResource) Update(ctx context.Context, req resource.UpdateRequest,
diags = req.State.GetAttribute(ctx, path.Root("provider_name"), &provider)
resp.Diagnostics.Append(diags...)

// Initally set ID to original name, in case update fails
data.ID = types.StringValue(fmt.Sprintf("%s/%s/%s", namespace.ValueString(), name.ValueString(), provider.ValueString()))

// Only provide namespace, name and provider, if one of the attributes
// has been changed
var newNamespace string
Expand All @@ -241,7 +248,7 @@ func (r *ModuleResource) Update(ctx context.Context, req resource.UpdateRequest,
newProvider = data.Provider.ValueString()
}

id, err := r.client.UpdateModule(
newId, err := r.client.UpdateModule(
namespace.ValueString(),
name.ValueString(),
provider.ValueString(),
Expand All @@ -265,7 +272,9 @@ func (r *ModuleResource) Update(ctx context.Context, req resource.UpdateRequest,
}

// Update ID attribute
data.ID = types.StringValue(id)
if newId != "" {
data.ID = types.StringValue(newId)
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
11 changes: 7 additions & 4 deletions internal/provider/namespace_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ func (r *NamespaceResource) Read(ctx context.Context, req resource.ReadRequest,
}

// Update attributes
data.ID = data.Name
data.DisplayName = types.StringValue(namespace.DisplayName)
if data.DisplayName.ValueString() != namespace.DisplayName {
data.DisplayName = types.StringValue(namespace.DisplayName)
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down Expand Up @@ -162,8 +163,10 @@ func (r *NamespaceResource) Update(ctx context.Context, req resource.UpdateReque
return
}

// Update ID attribute
data.ID = data.Name
// Update ID attribute, if name has changed
if name != data.Name {
data.ID = data.Name
}

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
Expand Down
3 changes: 1 addition & 2 deletions internal/terrareg/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,12 @@ func (c *TerraregClient) UpdateModule(namespace string, name string, provider st

// Ignore namespace/name/provider fields if they have not been set
var dataToSend interface{}
var newId string
var newId string = ""
if config.Namespace != "" && config.Name != "" && config.Provider != "" {
dataToSend = config
newId = fmt.Sprintf("%s/%s/%s", config.Namespace, config.Name, config.Provider)
} else {
dataToSend = config.ModuleModel
newId = fmt.Sprintf("%s/%s/%s", namespace, name, provider)
}

res, err := c.makeRequest(url, "POST", dataToSend)
Expand Down

0 comments on commit fcb74ec

Please sign in to comment.