Remove deleted resources and datasources from Terraform state on Read (all remaining services) (#346)
* Remove deleted resources and datasources from state on Read * Simplify code * Fix function description Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com> * Fix function description Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com> * Fix whitespace --------- Co-authored-by: Diogo Ferrão <diogo.ferrao@freiheit.com>
This commit is contained in:
parent
464884cabe
commit
66d6ec2bd0
33 changed files with 262 additions and 40 deletions
|
|
@ -128,9 +128,14 @@ func (r *credentialsGroupDataSource) Read(ctx context.Context, req datasource.Re
|
|||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "credentials_group_id", credentialsGroupId)
|
||||
|
||||
err := readCredentialsGroups(ctx, &model, r.client)
|
||||
found, err := readCredentialsGroups(ctx, &model, r.client)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credentialsGroup", fmt.Sprintf("getting credential group from list of credentials groups: %v", err))
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credentials group", fmt.Sprintf("getting credential group from list of credentials groups: %v", err))
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
resp.State.RemoveResource(ctx)
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credentials group", "Credentials group not found")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -198,11 +198,15 @@ func (r *credentialsGroupResource) Read(ctx context.Context, req resource.ReadRe
|
|||
ctx = tflog.SetField(ctx, "project_id", projectId)
|
||||
ctx = tflog.SetField(ctx, "credentials_group_id", credentialsGroupId)
|
||||
|
||||
err := readCredentialsGroups(ctx, &model, r.client)
|
||||
found, err := readCredentialsGroups(ctx, &model, r.client)
|
||||
if err != nil {
|
||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading credentialsGroup", fmt.Sprintf("getting credential group from list of credentials groups: %v", err))
|
||||
return
|
||||
}
|
||||
if !found {
|
||||
resp.State.RemoveResource(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// Set refreshed state
|
||||
diags = resp.State.Set(ctx, model)
|
||||
|
|
@ -318,21 +322,22 @@ func enableProject(ctx context.Context, model *Model, client objectStorageClient
|
|||
}
|
||||
|
||||
// readCredentialsGroups gets all the existing credentials groups for the specified project,
|
||||
// finds the credentials group that is being read and updates the state. If the credentials group cannot be found, it throws an error
|
||||
func readCredentialsGroups(ctx context.Context, model *Model, client objectStorageClient) error {
|
||||
// finds the credentials group that is being read and updates the state.
|
||||
// Returns True if the credential was found, False otherwise.
|
||||
func readCredentialsGroups(ctx context.Context, model *Model, client objectStorageClient) (bool, error) {
|
||||
found := false
|
||||
|
||||
if model.CredentialsGroupId.ValueString() == "" && model.Name.ValueString() == "" {
|
||||
return fmt.Errorf("missing configuration: either name or credentials group id must be provided")
|
||||
return found, fmt.Errorf("missing configuration: either name or credentials group id must be provided")
|
||||
}
|
||||
|
||||
credentialsGroupsResp, err := client.ListCredentialsGroupsExecute(ctx, model.ProjectId.ValueString())
|
||||
if err != nil {
|
||||
return fmt.Errorf("getting credentials groups: %w", err)
|
||||
return found, fmt.Errorf("getting credentials groups: %w", err)
|
||||
}
|
||||
|
||||
if credentialsGroupsResp == nil {
|
||||
return fmt.Errorf("nil response from GET credentials groups")
|
||||
return found, fmt.Errorf("nil response from GET credentials groups")
|
||||
}
|
||||
|
||||
for _, credentialsGroup := range *credentialsGroupsResp.CredentialsGroups {
|
||||
|
|
@ -342,14 +347,10 @@ func readCredentialsGroups(ctx context.Context, model *Model, client objectStora
|
|||
found = true
|
||||
err = mapCredentialsGroup(credentialsGroup, model)
|
||||
if err != nil {
|
||||
return err
|
||||
return found, err
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("credentials group could not be found")
|
||||
}
|
||||
|
||||
return nil
|
||||
return found, nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -162,7 +162,8 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
tests := []struct {
|
||||
description string
|
||||
mockedResp *objectstorage.ListCredentialsGroupsResponse
|
||||
expected Model
|
||||
expectedModel Model
|
||||
expectedFound bool
|
||||
getCredentialsGroupsFails bool
|
||||
isValid bool
|
||||
}{
|
||||
|
|
@ -185,6 +186,7 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
CredentialsGroupId: types.StringValue("cid"),
|
||||
URN: types.StringNull(),
|
||||
},
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
},
|
||||
|
|
@ -211,6 +213,7 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
CredentialsGroupId: types.StringValue("cid"),
|
||||
URN: types.StringValue("urn"),
|
||||
},
|
||||
true,
|
||||
false,
|
||||
true,
|
||||
},
|
||||
|
|
@ -222,6 +225,7 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
Model{},
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_credentials_groups",
|
||||
|
|
@ -231,6 +235,7 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
Model{},
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"nil_response",
|
||||
|
|
@ -238,6 +243,7 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
Model{},
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"non_matching_credentials_group",
|
||||
|
|
@ -253,6 +259,7 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
Model{},
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"error_response",
|
||||
|
|
@ -266,6 +273,7 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
},
|
||||
},
|
||||
Model{},
|
||||
false,
|
||||
true,
|
||||
false,
|
||||
},
|
||||
|
|
@ -277,10 +285,10 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
listCredentialsGroupsResp: tt.mockedResp,
|
||||
}
|
||||
model := &Model{
|
||||
ProjectId: tt.expected.ProjectId,
|
||||
CredentialsGroupId: tt.expected.CredentialsGroupId,
|
||||
ProjectId: tt.expectedModel.ProjectId,
|
||||
CredentialsGroupId: tt.expectedModel.CredentialsGroupId,
|
||||
}
|
||||
err := readCredentialsGroups(context.Background(), model, client)
|
||||
found, err := readCredentialsGroups(context.Background(), model, client)
|
||||
if !tt.isValid && err == nil {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
|
|
@ -288,10 +296,14 @@ func TestReadCredentialsGroups(t *testing.T) {
|
|||
t.Fatalf("Should not have failed: %v", err)
|
||||
}
|
||||
if tt.isValid {
|
||||
diff := cmp.Diff(model, &tt.expected)
|
||||
diff := cmp.Diff(model, &tt.expectedModel)
|
||||
if diff != "" {
|
||||
t.Fatalf("Data does not match: %s", diff)
|
||||
}
|
||||
|
||||
if found != tt.expectedFound {
|
||||
t.Fatalf("Found does not match")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue