From 344994a4122e7cb1cb3854a095f28ad7276c4011 Mon Sep 17 00:00:00 2001 From: Emond Papegaaij Date: Thu, 9 Nov 2023 10:06:04 +0100 Subject: [PATCH] #29: mark resources as removed when API returns 404 --- internal/provider/datasource-account.go | 2 +- internal/provider/datasource-certificate.go | 2 +- internal/provider/datasource-client.go | 2 +- internal/provider/datasource-directory.go | 2 +- internal/provider/datasource-group.go | 2 +- .../datasource-groupclassification.go | 2 +- .../provider/datasource-organizationalunit.go | 2 +- .../provider/datasource-serviceaccount.go | 2 +- internal/provider/datasource-system.go | 2 +- internal/provider/datasource-vaultrecord.go | 2 +- internal/provider/datasource-webhook.go | 2 +- internal/provider/full-helpers.go | 55 ++++++++++++++----- .../provider/resource-client_vaultrecord.go | 18 +++++- .../provider/resource-clientapplication.go | 10 +++- internal/provider/resource-group.go | 10 +++- .../provider/resource-group_vaultrecord.go | 18 +++++- internal/provider/resource-grouponsystem.go | 18 +++++- internal/provider/resource-serviceaccount.go | 10 +++- 18 files changed, 120 insertions(+), 41 deletions(-) diff --git a/internal/provider/datasource-account.go b/internal/provider/datasource-account.go index aea94e1..6c8e88b 100644 --- a/internal/provider/datasource-account.go +++ b/internal/provider/datasource-account.go @@ -87,7 +87,7 @@ func (d *accountDataSource) Read(ctx context.Context, req datasource.ReadRequest }, }) - tkh, diags := findFirst[keyhubmodels.AuthAccountable](ctx, wrapper, "account", &uuid, err) + tkh, diags := findFirst[keyhubmodels.AuthAccountable](ctx, wrapper, "account", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-certificate.go b/internal/provider/datasource-certificate.go index b6268bc..af7624b 100644 --- a/internal/provider/datasource-certificate.go +++ b/internal/provider/datasource-certificate.go @@ -87,7 +87,7 @@ func (d *certificateDataSource) Read(ctx context.Context, req datasource.ReadReq }, }) - tkh, diags := findFirst[keyhubmodels.CertificateCertificateable](ctx, wrapper, "certificate", &uuid, err) + tkh, diags := findFirst[keyhubmodels.CertificateCertificateable](ctx, wrapper, "certificate", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-client.go b/internal/provider/datasource-client.go index 1bd9c40..2b0af9f 100644 --- a/internal/provider/datasource-client.go +++ b/internal/provider/datasource-client.go @@ -87,7 +87,7 @@ func (d *clientDataSource) Read(ctx context.Context, req datasource.ReadRequest, }, }) - tkh, diags := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "client", &uuid, err) + tkh, diags := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "client", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-directory.go b/internal/provider/datasource-directory.go index 4e63a67..d958ce6 100644 --- a/internal/provider/datasource-directory.go +++ b/internal/provider/datasource-directory.go @@ -87,7 +87,7 @@ func (d *directoryDataSource) Read(ctx context.Context, req datasource.ReadReque }, }) - tkh, diags := findFirst[keyhubmodels.DirectoryAccountDirectoryable](ctx, wrapper, "directory", &uuid, err) + tkh, diags := findFirst[keyhubmodels.DirectoryAccountDirectoryable](ctx, wrapper, "directory", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-group.go b/internal/provider/datasource-group.go index 27d7235..9f934ab 100644 --- a/internal/provider/datasource-group.go +++ b/internal/provider/datasource-group.go @@ -87,7 +87,7 @@ func (d *groupDataSource) Read(ctx context.Context, req datasource.ReadRequest, }, }) - tkh, diags := findFirst[keyhubmodels.GroupGroupable](ctx, wrapper, "group", &uuid, err) + tkh, diags := findFirst[keyhubmodels.GroupGroupable](ctx, wrapper, "group", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-groupclassification.go b/internal/provider/datasource-groupclassification.go index cb2cbe0..1385653 100644 --- a/internal/provider/datasource-groupclassification.go +++ b/internal/provider/datasource-groupclassification.go @@ -87,7 +87,7 @@ func (d *groupclassificationDataSource) Read(ctx context.Context, req datasource }, }) - tkh, diags := findFirst[keyhubmodels.GroupGroupClassificationable](ctx, wrapper, "groupclassification", &uuid, err) + tkh, diags := findFirst[keyhubmodels.GroupGroupClassificationable](ctx, wrapper, "groupclassification", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-organizationalunit.go b/internal/provider/datasource-organizationalunit.go index 5356608..5dbc722 100644 --- a/internal/provider/datasource-organizationalunit.go +++ b/internal/provider/datasource-organizationalunit.go @@ -87,7 +87,7 @@ func (d *organizationalunitDataSource) Read(ctx context.Context, req datasource. }, }) - tkh, diags := findFirst[keyhubmodels.OrganizationOrganizationalUnitable](ctx, wrapper, "organizationalunit", &uuid, err) + tkh, diags := findFirst[keyhubmodels.OrganizationOrganizationalUnitable](ctx, wrapper, "organizationalunit", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-serviceaccount.go b/internal/provider/datasource-serviceaccount.go index a19cda4..f76d4bf 100644 --- a/internal/provider/datasource-serviceaccount.go +++ b/internal/provider/datasource-serviceaccount.go @@ -87,7 +87,7 @@ func (d *serviceaccountDataSource) Read(ctx context.Context, req datasource.Read }, }) - tkh, diags := findFirst[keyhubmodels.ServiceaccountServiceAccountable](ctx, wrapper, "serviceaccount", &uuid, err) + tkh, diags := findFirst[keyhubmodels.ServiceaccountServiceAccountable](ctx, wrapper, "serviceaccount", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-system.go b/internal/provider/datasource-system.go index aea0964..1f91ce8 100644 --- a/internal/provider/datasource-system.go +++ b/internal/provider/datasource-system.go @@ -87,7 +87,7 @@ func (d *systemDataSource) Read(ctx context.Context, req datasource.ReadRequest, }, }) - tkh, diags := findFirst[keyhubmodels.ProvisioningProvisionedSystemable](ctx, wrapper, "system", &uuid, err) + tkh, diags := findFirst[keyhubmodels.ProvisioningProvisionedSystemable](ctx, wrapper, "system", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-vaultrecord.go b/internal/provider/datasource-vaultrecord.go index bba4220..c436542 100644 --- a/internal/provider/datasource-vaultrecord.go +++ b/internal/provider/datasource-vaultrecord.go @@ -87,7 +87,7 @@ func (d *vaultrecordDataSource) Read(ctx context.Context, req datasource.ReadReq }, }) - tkh, diags := findFirst[keyhubmodels.VaultVaultRecordable](ctx, wrapper, "vaultrecord", &uuid, err) + tkh, diags := findFirst[keyhubmodels.VaultVaultRecordable](ctx, wrapper, "vaultrecord", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/datasource-webhook.go b/internal/provider/datasource-webhook.go index d409ada..2bf9fa6 100644 --- a/internal/provider/datasource-webhook.go +++ b/internal/provider/datasource-webhook.go @@ -87,7 +87,7 @@ func (d *webhookDataSource) Read(ctx context.Context, req datasource.ReadRequest }, }) - tkh, diags := findFirst[keyhubmodels.WebhookWebhookable](ctx, wrapper, "webhook", &uuid, err) + tkh, diags := findFirst[keyhubmodels.WebhookWebhookable](ctx, wrapper, "webhook", &uuid, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return diff --git a/internal/provider/full-helpers.go b/internal/provider/full-helpers.go index 03cf6c8..e49fa1d 100644 --- a/internal/provider/full-helpers.go +++ b/internal/provider/full-helpers.go @@ -195,7 +195,7 @@ func parseCast[T any, Z any](val basetypes.StringValue, parser func(string) (Z, return caster(parsed), diags } -func findFirst[T keyhubmodels.Linkableable](ctx context.Context, wrapper interface{ GetItems() []T }, name string, uuid *string, err error) (T, diag.Diagnostics) { +func findFirst[T keyhubmodels.Linkableable](ctx context.Context, wrapper interface{ GetItems() []T }, name string, uuid *string, notFoundIsNil bool, err error) (T, diag.Diagnostics) { var diags diag.Diagnostics var noVal T if err != nil { @@ -203,14 +203,23 @@ func findFirst[T keyhubmodels.Linkableable](ctx context.Context, wrapper interfa return noVal, diags } if len(wrapper.GetItems()) == 0 { - diags.AddError("Client Error", fmt.Sprintf("Unable to find %s with UUID %s", name, *uuid)) + if !notFoundIsNil { + diags.AddError("Client Error", fmt.Sprintf("Unable to find %s with UUID %s", name, *uuid)) + } return noVal, diags } return wrapper.GetItems()[0], nil - } func findGroupGroupPrimerByUUID(ctx context.Context, uuid *string) (keyhubmodels.GroupGroupPrimerable, diag.Diagnostics) { + return findGroupGroupPrimerByUUIDOptionallyNil(ctx, uuid, false) +} + +func findGroupGroupPrimerByUUIDOrNil(ctx context.Context, uuid *string) (keyhubmodels.GroupGroupPrimerable, diag.Diagnostics) { + return findGroupGroupPrimerByUUIDOptionallyNil(ctx, uuid, true) +} + +func findGroupGroupPrimerByUUIDOptionallyNil(ctx context.Context, uuid *string, notFoundIsNil bool) (keyhubmodels.GroupGroupPrimerable, diag.Diagnostics) { if uuid == nil || *uuid == "" { return nil, diag.Diagnostics{} } @@ -220,7 +229,7 @@ func findGroupGroupPrimerByUUID(ctx context.Context, uuid *string) (keyhubmodels Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.GroupGroupable](ctx, wrapper, "group", uuid, err) + ret, diag := findFirst[keyhubmodels.GroupGroupable](ctx, wrapper, "group", uuid, notFoundIsNil, err) if ret == nil { return ret, diag } @@ -242,7 +251,7 @@ func findDirectoryAccountDirectoryPrimerByUUID(ctx context.Context, uuid *string Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.DirectoryAccountDirectoryable](ctx, wrapper, "directory", uuid, err) + ret, diag := findFirst[keyhubmodels.DirectoryAccountDirectoryable](ctx, wrapper, "directory", uuid, false, err) if ret == nil { return ret, diag } @@ -264,7 +273,7 @@ func findOrganizationOrganizationalUnitPrimerByUUID(ctx context.Context, uuid *s Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.OrganizationOrganizationalUnitable](ctx, wrapper, "organizational unit", uuid, err) + ret, diag := findFirst[keyhubmodels.OrganizationOrganizationalUnitable](ctx, wrapper, "organizational unit", uuid, false, err) if ret == nil { return ret, diag } @@ -286,7 +295,7 @@ func findCertificateCertificatePrimerByUUID(ctx context.Context, uuid *string) ( Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.CertificateCertificateable](ctx, wrapper, "certificate", uuid, err) + ret, diag := findFirst[keyhubmodels.CertificateCertificateable](ctx, wrapper, "certificate", uuid, false, err) if ret == nil { return ret, diag } @@ -299,6 +308,14 @@ func findCertificateCertificatePrimerByUUID(ctx context.Context, uuid *string) ( } func findProvisioningProvisionedSystemPrimerByUUID(ctx context.Context, uuid *string) (keyhubmodels.ProvisioningProvisionedSystemPrimerable, diag.Diagnostics) { + return findProvisioningProvisionedSystemPrimerByUUIDOptionallyNil(ctx, uuid, false) +} + +func findProvisioningProvisionedSystemPrimerByUUIDOrNil(ctx context.Context, uuid *string) (keyhubmodels.ProvisioningProvisionedSystemPrimerable, diag.Diagnostics) { + return findProvisioningProvisionedSystemPrimerByUUIDOptionallyNil(ctx, uuid, true) +} + +func findProvisioningProvisionedSystemPrimerByUUIDOptionallyNil(ctx context.Context, uuid *string, notFoundIsNil bool) (keyhubmodels.ProvisioningProvisionedSystemPrimerable, diag.Diagnostics) { if uuid == nil || *uuid == "" { return nil, diag.Diagnostics{} } @@ -308,7 +325,7 @@ func findProvisioningProvisionedSystemPrimerByUUID(ctx context.Context, uuid *st Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.ProvisioningProvisionedSystemable](ctx, wrapper, "provisioned system", uuid, err) + ret, diag := findFirst[keyhubmodels.ProvisioningProvisionedSystemable](ctx, wrapper, "provisioned system", uuid, notFoundIsNil, err) if ret == nil { return ret, diag } @@ -330,7 +347,7 @@ func findGroupGroupClassificationPrimerByUUID(ctx context.Context, uuid *string) Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.GroupGroupClassificationable](ctx, wrapper, "group classification", uuid, err) + ret, diag := findFirst[keyhubmodels.GroupGroupClassificationable](ctx, wrapper, "group classification", uuid, false, err) if ret == nil { return ret, diag } @@ -343,6 +360,14 @@ func findGroupGroupClassificationPrimerByUUID(ctx context.Context, uuid *string) } func findClientClientApplicationPrimerByUUID(ctx context.Context, uuid *string) (keyhubmodels.ClientClientApplicationPrimerable, diag.Diagnostics) { + return findClientClientApplicationPrimerByUUIDOptionallyNil(ctx, uuid, false) +} + +func findClientClientApplicationPrimerByUUIDOrNil(ctx context.Context, uuid *string) (keyhubmodels.ClientClientApplicationPrimerable, diag.Diagnostics) { + return findClientClientApplicationPrimerByUUIDOptionallyNil(ctx, uuid, true) +} + +func findClientClientApplicationPrimerByUUIDOptionallyNil(ctx context.Context, uuid *string, notFoundIsNil bool) (keyhubmodels.ClientClientApplicationPrimerable, diag.Diagnostics) { if uuid == nil || *uuid == "" { return nil, diag.Diagnostics{} } @@ -352,7 +377,7 @@ func findClientClientApplicationPrimerByUUID(ctx context.Context, uuid *string) Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "client application", uuid, err) + ret, diag := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "client application", uuid, notFoundIsNil, err) if ret == nil { return ret, diag } @@ -374,7 +399,7 @@ func findClientOAuth2ClientByUUID(ctx context.Context, uuid *string) (keyhubmode Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "client application", uuid, err) + ret, diag := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "client application", uuid, false, err) if ret == nil { return nil, diag } @@ -395,7 +420,7 @@ func findClientLdapClientByUUID(ctx context.Context, uuid *string) (keyhubmodels Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "client application", uuid, err) + ret, diag := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "client application", uuid, false, err) if ret == nil { return nil, diag } @@ -429,7 +454,7 @@ func findAuthAccountByUUID(ctx context.Context, uuid *string) (keyhubmodels.Auth Uuid: []string{*uuid}, }, }) - return findFirst[keyhubmodels.AuthAccountable](ctx, wrapper, "account", uuid, err) + return findFirst[keyhubmodels.AuthAccountable](ctx, wrapper, "account", uuid, false, err) } func findServiceaccountServiceAccountPrimerByUUID(ctx context.Context, uuid *string) (keyhubmodels.ServiceaccountServiceAccountPrimerable, diag.Diagnostics) { @@ -442,7 +467,7 @@ func findServiceaccountServiceAccountPrimerByUUID(ctx context.Context, uuid *str Uuid: []string{*uuid}, }, }) - ret, diag := findFirst[keyhubmodels.ServiceaccountServiceAccountable](ctx, wrapper, "service account", uuid, err) + ret, diag := findFirst[keyhubmodels.ServiceaccountServiceAccountable](ctx, wrapper, "service account", uuid, false, err) if ret == nil { return ret, diag } @@ -477,7 +502,7 @@ func findVaultVaultRecordByUUID(ctx context.Context, uuid *string) (keyhubmodels Uuid: []string{*uuid}, }, }) - return findFirst[keyhubmodels.VaultVaultRecordable](ctx, wrapper, "vault record", uuid, err) + return findFirst[keyhubmodels.VaultVaultRecordable](ctx, wrapper, "vault record", uuid, false, err) } func errorReportToString(ctx context.Context, err error) string { diff --git a/internal/provider/resource-client_vaultrecord.go b/internal/provider/resource-client_vaultrecord.go index 54da346..c5a57ce 100644 --- a/internal/provider/resource-client_vaultrecord.go +++ b/internal/provider/resource-client_vaultrecord.go @@ -100,7 +100,7 @@ func (r *clientVaultrecordResource) Create(ctx context.Context, req resource.Cre Additional: collectAdditional(data), }, }) - tkh, diags := findFirst[keyhubmodels.VaultVaultRecordable](ctx, wrapper, "client_vaultrecord", nil, err) + tkh, diags := findFirst[keyhubmodels.VaultVaultRecordable](ctx, wrapper, "client_vaultrecord", nil, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return @@ -131,12 +131,18 @@ func (r *clientVaultrecordResource) Read(ctx context.Context, req resource.ReadR defer r.providerData.Mutex.RUnlock() ctx = context.WithValue(ctx, keyHubClientKey, r.providerData.Client) tflog.Info(ctx, "Reading client_vaultrecord from Topicus KeyHub") - tkhParent, diags := findClientClientApplicationPrimerByUUID(ctx, data.ClientApplicationUUID.ValueStringPointer()) + tkhParent, diags := findClientClientApplicationPrimerByUUIDOrNil(ctx, data.ClientApplicationUUID.ValueStringPointer()) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } + if tkhParent == nil { + tflog.Info(ctx, "Parent client_application not found, marking resource as removed") + resp.State.RemoveResource(ctx) + return + } + tkh, err := r.providerData.Client.Client().ByClientidInt64(*tkhParent.GetLinks()[0].GetId()).Vault().Record().ByRecordidInt64(getSelfLink(data.Links).ID.ValueInt64()).Get( ctx, &keyhubreq.ItemVaultRecordWithRecordItemRequestBuilderGetRequestConfiguration{ QueryParameters: &keyhubreq.ItemVaultRecordWithRecordItemRequestBuilderGetQueryParameters{ @@ -144,7 +150,13 @@ func (r *clientVaultrecordResource) Read(ctx context.Context, req resource.ReadR }, }) - if !isHttpStatusCodeOk(ctx, -1, err, &resp.Diagnostics) { + if !isHttpStatusCodeOk(ctx, 404, err, &resp.Diagnostics) { + return + } + // only 404 remains + if err != nil { + tflog.Info(ctx, "client_vaultrecord not found, marking resource as removed") + resp.State.RemoveResource(ctx) return } diff --git a/internal/provider/resource-clientapplication.go b/internal/provider/resource-clientapplication.go index 9bcb309..b581b15 100644 --- a/internal/provider/resource-clientapplication.go +++ b/internal/provider/resource-clientapplication.go @@ -94,7 +94,7 @@ func (r *clientapplicationResource) Create(ctx context.Context, req resource.Cre Additional: collectAdditional(data), }, }) - tkh, diags := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "clientapplication", nil, err) + tkh, diags := findFirst[keyhubmodels.ClientClientApplicationable](ctx, wrapper, "clientapplication", nil, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return @@ -131,7 +131,13 @@ func (r *clientapplicationResource) Read(ctx context.Context, req resource.ReadR }, }) - if !isHttpStatusCodeOk(ctx, -1, err, &resp.Diagnostics) { + if !isHttpStatusCodeOk(ctx, 404, err, &resp.Diagnostics) { + return + } + // only 404 remains + if err != nil { + tflog.Info(ctx, "clientapplication not found, marking resource as removed") + resp.State.RemoveResource(ctx) return } diff --git a/internal/provider/resource-group.go b/internal/provider/resource-group.go index f14a18f..14560c8 100644 --- a/internal/provider/resource-group.go +++ b/internal/provider/resource-group.go @@ -94,7 +94,7 @@ func (r *groupResource) Create(ctx context.Context, req resource.CreateRequest, Additional: collectAdditional(data), }, }) - tkh, diags := findFirst[keyhubmodels.GroupGroupable](ctx, wrapper, "group", nil, err) + tkh, diags := findFirst[keyhubmodels.GroupGroupable](ctx, wrapper, "group", nil, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return @@ -131,7 +131,13 @@ func (r *groupResource) Read(ctx context.Context, req resource.ReadRequest, resp }, }) - if !isHttpStatusCodeOk(ctx, -1, err, &resp.Diagnostics) { + if !isHttpStatusCodeOk(ctx, 404, err, &resp.Diagnostics) { + return + } + // only 404 remains + if err != nil { + tflog.Info(ctx, "group not found, marking resource as removed") + resp.State.RemoveResource(ctx) return } diff --git a/internal/provider/resource-group_vaultrecord.go b/internal/provider/resource-group_vaultrecord.go index b59cf36..cc7cec5 100644 --- a/internal/provider/resource-group_vaultrecord.go +++ b/internal/provider/resource-group_vaultrecord.go @@ -100,7 +100,7 @@ func (r *groupVaultrecordResource) Create(ctx context.Context, req resource.Crea Additional: collectAdditional(data), }, }) - tkh, diags := findFirst[keyhubmodels.VaultVaultRecordable](ctx, wrapper, "group_vaultrecord", nil, err) + tkh, diags := findFirst[keyhubmodels.VaultVaultRecordable](ctx, wrapper, "group_vaultrecord", nil, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return @@ -131,12 +131,18 @@ func (r *groupVaultrecordResource) Read(ctx context.Context, req resource.ReadRe defer r.providerData.Mutex.RUnlock() ctx = context.WithValue(ctx, keyHubClientKey, r.providerData.Client) tflog.Info(ctx, "Reading group_vaultrecord from Topicus KeyHub") - tkhParent, diags := findGroupGroupPrimerByUUID(ctx, data.GroupUUID.ValueStringPointer()) + tkhParent, diags := findGroupGroupPrimerByUUIDOrNil(ctx, data.GroupUUID.ValueStringPointer()) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } + if tkhParent == nil { + tflog.Info(ctx, "Parent group not found, marking resource as removed") + resp.State.RemoveResource(ctx) + return + } + tkh, err := r.providerData.Client.Group().ByGroupidInt64(*tkhParent.GetLinks()[0].GetId()).Vault().Record().ByRecordidInt64(getSelfLink(data.Links).ID.ValueInt64()).Get( ctx, &keyhubreq.ItemVaultRecordWithRecordItemRequestBuilderGetRequestConfiguration{ QueryParameters: &keyhubreq.ItemVaultRecordWithRecordItemRequestBuilderGetQueryParameters{ @@ -144,7 +150,13 @@ func (r *groupVaultrecordResource) Read(ctx context.Context, req resource.ReadRe }, }) - if !isHttpStatusCodeOk(ctx, -1, err, &resp.Diagnostics) { + if !isHttpStatusCodeOk(ctx, 404, err, &resp.Diagnostics) { + return + } + // only 404 remains + if err != nil { + tflog.Info(ctx, "group_vaultrecord not found, marking resource as removed") + resp.State.RemoveResource(ctx) return } diff --git a/internal/provider/resource-grouponsystem.go b/internal/provider/resource-grouponsystem.go index 655e0d2..687d6b5 100644 --- a/internal/provider/resource-grouponsystem.go +++ b/internal/provider/resource-grouponsystem.go @@ -100,7 +100,7 @@ func (r *grouponsystemResource) Create(ctx context.Context, req resource.CreateR Additional: collectAdditional(data), }, }) - tkh, diags := findFirst[keyhubmodels.ProvisioningGroupOnSystemable](ctx, wrapper, "grouponsystem", nil, err) + tkh, diags := findFirst[keyhubmodels.ProvisioningGroupOnSystemable](ctx, wrapper, "grouponsystem", nil, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return @@ -131,12 +131,18 @@ func (r *grouponsystemResource) Read(ctx context.Context, req resource.ReadReque defer r.providerData.Mutex.RUnlock() ctx = context.WithValue(ctx, keyHubClientKey, r.providerData.Client) tflog.Info(ctx, "Reading grouponsystem from Topicus KeyHub") - tkhParent, diags := findProvisioningProvisionedSystemPrimerByUUID(ctx, data.ProvisionedSystemUUID.ValueStringPointer()) + tkhParent, diags := findProvisioningProvisionedSystemPrimerByUUIDOrNil(ctx, data.ProvisionedSystemUUID.ValueStringPointer()) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return } + if tkhParent == nil { + tflog.Info(ctx, "Parent provisioned_system not found, marking resource as removed") + resp.State.RemoveResource(ctx) + return + } + tkh, err := r.providerData.Client.System().BySystemidInt64(*tkhParent.GetLinks()[0].GetId()).Group().ByGroupidInt64(getSelfLink(data.Links).ID.ValueInt64()).Get( ctx, &keyhubreq.ItemGroupWithGroupItemRequestBuilderGetRequestConfiguration{ QueryParameters: &keyhubreq.ItemGroupWithGroupItemRequestBuilderGetQueryParameters{ @@ -144,7 +150,13 @@ func (r *grouponsystemResource) Read(ctx context.Context, req resource.ReadReque }, }) - if !isHttpStatusCodeOk(ctx, -1, err, &resp.Diagnostics) { + if !isHttpStatusCodeOk(ctx, 404, err, &resp.Diagnostics) { + return + } + // only 404 remains + if err != nil { + tflog.Info(ctx, "grouponsystem not found, marking resource as removed") + resp.State.RemoveResource(ctx) return } diff --git a/internal/provider/resource-serviceaccount.go b/internal/provider/resource-serviceaccount.go index 5c862c2..a8847b6 100644 --- a/internal/provider/resource-serviceaccount.go +++ b/internal/provider/resource-serviceaccount.go @@ -94,7 +94,7 @@ func (r *serviceaccountResource) Create(ctx context.Context, req resource.Create Additional: collectAdditional(data), }, }) - tkh, diags := findFirst[keyhubmodels.ServiceaccountServiceAccountable](ctx, wrapper, "serviceaccount", nil, err) + tkh, diags := findFirst[keyhubmodels.ServiceaccountServiceAccountable](ctx, wrapper, "serviceaccount", nil, false, err) resp.Diagnostics.Append(diags...) if resp.Diagnostics.HasError() { return @@ -131,7 +131,13 @@ func (r *serviceaccountResource) Read(ctx context.Context, req resource.ReadRequ }, }) - if !isHttpStatusCodeOk(ctx, -1, err, &resp.Diagnostics) { + if !isHttpStatusCodeOk(ctx, 404, err, &resp.Diagnostics) { + return + } + // only 404 remains + if err != nil { + tflog.Info(ctx, "serviceaccount not found, marking resource as removed") + resp.State.RemoveResource(ctx) return }