Skip to content

Commit

Permalink
NACT-427 Errors when reading incarnations are ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
mottetm-roche committed Aug 9, 2024
1 parent 4a1cbc8 commit 6711849
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 80 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ on:

permissions:
contents: read

jobs:
# Ensure project builds before running testing matrix
build:
Expand All @@ -23,12 +23,13 @@ jobs:
with:
go-version-file: 'go.mod'
cache: true
- run: go mod download
- run: go build -v .
- run: go mod tidy
- name: Run linters
uses: golangci/golangci-lint-action@639cd343e1d3b897ff35927a75193d57cfcba299 # v3.6.0
uses: golangci/golangci-lint-action@v6 # v3.6.0
with:
version: latest
skip-go-installation: true
- run: go build .

generate:
name: Generate
Expand Down
13 changes: 0 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.20
require (
github.com/deepmap/oapi-codegen v1.13.4
github.com/hashicorp/go-retryablehttp v0.7.4
github.com/hashicorp/terraform-plugin-docs v0.16.0
github.com/hashicorp/terraform-plugin-framework v1.3.4
github.com/hashicorp/terraform-plugin-framework-validators v0.11.0
github.com/hashicorp/terraform-plugin-go v0.18.0
Expand All @@ -19,15 +18,10 @@ require (
)

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/ProtonMail/go-crypto v0.0.0-20230217124315-7d5c6f04bbb8 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apapsch/go-jsonmerge/v2 v2.0.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/bytedance/sonic v1.10.0-rc3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
github.com/chenzhuoyu/iasm v0.9.0 // indirect
Expand Down Expand Up @@ -62,7 +56,6 @@ require (
github.com/hashicorp/terraform-registry-address v0.2.1 // indirect
github.com/hashicorp/terraform-svchost v0.1.1 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/kr/pretty v0.3.1 // indirect
Expand All @@ -71,7 +64,6 @@ require (
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mitchellh/cli v1.1.5 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
Expand All @@ -82,10 +74,6 @@ require (
github.com/oklog/run v1.0.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.9 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
Expand All @@ -96,7 +84,6 @@ require (
github.com/zclconf/go-cty v1.13.2 // indirect
golang.org/x/arch v0.4.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
Expand Down
55 changes: 0 additions & 55 deletions go.sum

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions internal/provider/data_incarnation.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,15 @@ func (ds *incarnationDataSource) Read(ctx context.Context, req datasource.ReadRe

id := data.Id.ValueString()

inc := getIncarnation(
var inc Incarnation
var diags diag.Diagnostics
inc, diags = getIncarnation(
ctx,
ds.client,
resp.Diagnostics,
IncarnationId(id),
data.WaitForMRStatus,
)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
Expand All @@ -204,7 +206,6 @@ func (ds *incarnationDataSource) Read(ctx context.Context, req datasource.ReadRe
data.MergeRequestStatus = types.StringValue(*inc.MergeRequestStatus)
}

var diags diag.Diagnostics
templateData := map[string]string{}
for key, value := range inc.TemplateData {
switch value := value.(type) {
Expand Down
3 changes: 1 addition & 2 deletions internal/provider/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ import (
func getIncarnation(
ctx context.Context,
client FoxopsClient,
diags diag.Diagnostics,
id IncarnationId,
waitForStatus *waitForStatusMRModel,
) (inc Incarnation) {
) (inc Incarnation, diags diag.Diagnostics) {
var err error
if waitForStatus == nil {
tflog.Info(ctx, "fetching the incarnation", map[string]interface{}{"id": id})
Expand Down
11 changes: 8 additions & 3 deletions internal/provider/resource_incarnation.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,16 @@ func (r *incarnationResource) Read(ctx context.Context, req resource.ReadRequest

id := data.Id.ValueString()

inc := getIncarnation(
var inc Incarnation
var diags diag.Diagnostics
inc, diags = getIncarnation(
ctx,
r.client,
resp.Diagnostics,
IncarnationId(id),
data.WaitForMRStatus,
)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}
Expand Down Expand Up @@ -249,7 +252,9 @@ func (r *incarnationResource) Update(ctx context.Context, req resource.UpdateReq
return
}

inc = getIncarnation(ctx, r.client, resp.Diagnostics, inc.Id, data.WaitForMRStatus)
var diags diag.Diagnostics
inc, diags = getIncarnation(ctx, r.client, inc.Id, data.WaitForMRStatus)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
Expand Down
90 changes: 90 additions & 0 deletions internal/provider/resource_incarnation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package provider_test

import (
"context"
"errors"
"fmt"
"reflect"
"regexp"
"testing"

"github.com/Roche/terraform-provider-foxops/internal/helpers"
Expand Down Expand Up @@ -322,3 +324,91 @@ func TestIncarnationResourceChanges(t *testing.T) {
)
}
}

func TestAccEdgeClusterResource_ClientErrorShouldNotDeleteState(t *testing.T) {
setup := newTestProviderSetup(t)

incarnation := provider.Incarnation{
Id: provider.IncarnationId("1234"),
IncarnationRepository: "inc/repo",
TemplateRepository: "template/repo",
TemplateRepositoryVersion: "template/repo/version",
TargetDirectory: ".",
CommitSha: "12345678",
CommitUrl: "template/repo/commit",
MergeRequestId: helpers.Addr("1234"),
MergeRequestStatus: helpers.Addr("merged"),
MergeRequestUrl: helpers.Addr("inc/repo/mr!1234"),
TemplateData: map[string]interface{}{
"hello": "World!",
},
}

req := provider.CreateIncarnationRequest{
IncarnationRepository: incarnation.IncarnationRepository,
TargetDirectory: &incarnation.TargetDirectory,
TemplateRepository: incarnation.TemplateRepository,
UpdateIncarnationRequest: provider.UpdateIncarnationRequest{
TemplateData: incarnation.TemplateData,
TemplateRepositoryVersion: incarnation.TemplateRepositoryVersion,
},
}

setup.client.EXPECT().
CreateIncarnation(gomock.Any(), req).
Return(incarnation, nil)

setup.client.EXPECT().
GetIncarnation(gomock.Any(), incarnation.Id).
Return(incarnation, nil)

setup.client.EXPECT().
GetIncarnation(gomock.Any(), incarnation.Id).
Return(provider.Incarnation{}, errors.New("test"))

setup.client.EXPECT().
DeleteIncarnation(gomock.Any(), incarnation.Id).
Return(nil)

hello, ok := incarnation.TemplateData["hello"].(string)
require.True(t, ok)
resource.Test(t, resource.TestCase{
IsUnitTest: true,
ProtoV6ProviderFactories: setup.testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: incarnationResourceConfigFactory("test", req),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("foxops_incarnation.test", "id", string(incarnation.Id)),
resource.TestCheckResourceAttr("foxops_incarnation.test", "incarnation_repository", incarnation.IncarnationRepository),
resource.TestCheckResourceAttr("foxops_incarnation.test", "template_repository", incarnation.TemplateRepository),
resource.TestCheckResourceAttr("foxops_incarnation.test", "template_repository_version", incarnation.TemplateRepositoryVersion),
resource.TestCheckResourceAttr("foxops_incarnation.test", "target_directory", incarnation.TargetDirectory),
resource.TestCheckResourceAttr("foxops_incarnation.test", "template_data.hello", hello),
resource.TestCheckResourceAttr("foxops_incarnation.test", "commit_sha", incarnation.CommitSha),
resource.TestCheckResourceAttr("foxops_incarnation.test", "commit_url", incarnation.CommitUrl),
resource.TestCheckResourceAttr("foxops_incarnation.test", "merge_request_id", *incarnation.MergeRequestId),
resource.TestCheckResourceAttr("foxops_incarnation.test", "merge_request_status", *incarnation.MergeRequestStatus),
resource.TestCheckResourceAttr("foxops_incarnation.test", "merge_request_url", *incarnation.MergeRequestUrl),
),
},
{
RefreshState: true,
ExpectError: regexp.MustCompile("failed to retrieve incarnation"),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("foxops_incarnation.test", "id", string(incarnation.Id)),
resource.TestCheckResourceAttr("foxops_incarnation.test", "incarnation_repository", incarnation.IncarnationRepository),
resource.TestCheckResourceAttr("foxops_incarnation.test", "template_repository", incarnation.TemplateRepository),
resource.TestCheckResourceAttr("foxops_incarnation.test", "template_repository_version", incarnation.TemplateRepositoryVersion),
resource.TestCheckResourceAttr("foxops_incarnation.test", "target_directory", incarnation.TargetDirectory),
resource.TestCheckResourceAttr("foxops_incarnation.test", "template_data.hello", hello),
resource.TestCheckResourceAttr("foxops_incarnation.test", "commit_sha", incarnation.CommitSha),
resource.TestCheckResourceAttr("foxops_incarnation.test", "commit_url", incarnation.CommitUrl),
resource.TestCheckResourceAttr("foxops_incarnation.test", "merge_request_id", *incarnation.MergeRequestId),
resource.TestCheckResourceAttr("foxops_incarnation.test", "merge_request_status", *incarnation.MergeRequestStatus),
resource.TestCheckResourceAttr("foxops_incarnation.test", "merge_request_url", *incarnation.MergeRequestUrl),
),
},
},
})
}

0 comments on commit 6711849

Please sign in to comment.