From 88067cbf1a6fcb5ff078524e3a8113b13398db16 Mon Sep 17 00:00:00 2001 From: ks6088ts Date: Mon, 24 Oct 2022 20:05:21 +0900 Subject: [PATCH 1/2] explicitly handles authentication method other than authKey --- internal/conns/conns.go | 12 ++++++++++++ internal/conns/conns_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/internal/conns/conns.go b/internal/conns/conns.go index aa38cb0..aace092 100644 --- a/internal/conns/conns.go +++ b/internal/conns/conns.go @@ -63,6 +63,10 @@ func getProfile(profileName string) (*profile, error) { return nil, err } + if err := validateProfile(&p); err != nil { + return nil, err + } + // supply default values for older versions (which support 'jp' coverage type only) if p.CoverageType == "" { p.CoverageType = "jp" @@ -71,6 +75,14 @@ func getProfile(profileName string) (*profile, error) { return &p, nil } +func validateProfile(p *profile) error { + // validation logic: handles authentication methods other than auth key + if p.AuthKey == nil || p.AuthKeyId == nil { + return fmt.Errorf("authentication by auth key is only supported. please specify AuthKey and AuthKeyId in profile") + } + return nil +} + type SoracomClient struct { Client *soracom.APIClient AuthResponse *soracom.AuthResponse diff --git a/internal/conns/conns_test.go b/internal/conns/conns_test.go index 2cab543..f61e477 100644 --- a/internal/conns/conns_test.go +++ b/internal/conns/conns_test.go @@ -113,3 +113,37 @@ func TestGetProfile(t *testing.T) { t.Errorf("failed to get registerPaymentMethod") } } + +func TestValidateProfile(t *testing.T) { + tempAuthKeyId := "authKeyId" + tempAuthKey := "authKey" + + testCases := []struct { + name string + profile profile + hasError bool + }{ + { + name: "nominal scenarios with authKey specified in profile", + profile: profile{ + AuthKeyId: &tempAuthKeyId, + AuthKey: &tempAuthKey, + }, + hasError: false, + }, + { + name: "non-nominal scenarios without authKey in profile", + profile: profile{}, + hasError: true, + }, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + err := validateProfile(&testCase.profile) + if hasError := (err != nil); hasError != testCase.hasError { + t.Errorf("got %v, expected %v, err %v", hasError, testCase.hasError, err) + } + }) + } +} From 73176bd444fe3144f33c855b99c00e60b828841e Mon Sep 17 00:00:00 2001 From: ks6088ts Date: Mon, 24 Oct 2022 20:24:19 +0900 Subject: [PATCH 2/2] fixes flags for release --- .goreleaser.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.goreleaser.yml b/.goreleaser.yml index 4bb39e8..3a22400 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -14,7 +14,7 @@ builds: flags: - -trimpath ldflags: - - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + - '-s -w -X github.com/ks6088ts/terraform-provider-soracom/internal.Version={{.Version}} -X github.com/ks6088ts/terraform-provider-soracom/internal.Revision={{.Commit}}' goos: - freebsd - windows