Skip to content

Commit

Permalink
Merge pull request #29 from ks6088ts/enhancement/issue-28_handle-auth
Browse files Browse the repository at this point in the history
explicitly handles authentication method other than authKey
  • Loading branch information
ks6088ts authored Oct 24, 2022
2 parents 200f5b3 + 73176bd commit 8ba6a74
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions internal/conns/conns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
34 changes: 34 additions & 0 deletions internal/conns/conns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
}
}

0 comments on commit 8ba6a74

Please sign in to comment.