Skip to content

Commit

Permalink
Feat: add a retry mechanism for getSshKeyByName (#849)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerHeber authored May 12, 2024
1 parent cbf73c4 commit a76836d
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 16 deletions.
36 changes: 20 additions & 16 deletions env0/data_sshkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package env0
import (
"context"
"fmt"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -62,26 +64,28 @@ func dataSshKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{
func getSshKeyByName(name interface{}, meta interface{}) (*client.SshKey, error) {
apiClient := meta.(client.ApiClientInterface)

sshKeys, err := apiClient.SshKeys()
if err != nil {
return nil, err
}
return backoff.RetryWithData(func() (*client.SshKey, error) {
sshKeys, err := apiClient.SshKeys()
if err != nil {
return nil, err
}

var sshKeysByName []client.SshKey
for _, candidate := range sshKeys {
if candidate.Name == name {
sshKeysByName = append(sshKeysByName, candidate)
var sshKeysByName []client.SshKey
for _, candidate := range sshKeys {
if candidate.Name == name {
sshKeysByName = append(sshKeysByName, candidate)
}
}
}

if len(sshKeysByName) > 1 {
return nil, fmt.Errorf("found multiple ssh keys with name: %s. Use id instead or make sure ssh key names are unique %v", name, sshKeysByName)
}
if len(sshKeysByName) == 0 {
return nil, fmt.Errorf("ssh key with name %v not found", name)
}
if len(sshKeysByName) > 1 {
return nil, backoff.Permanent(fmt.Errorf("found multiple ssh keys with name: %s. Use id instead or make sure ssh key names are unique %v", name, sshKeysByName))
}
if len(sshKeysByName) == 0 {
return nil, fmt.Errorf("ssh key with name %v not found", name)
}

return &sshKeysByName[0], nil
return &sshKeysByName[0], nil
}, backoff.NewExponentialBackOff(backoff.WithMaxElapsedTime(time.Minute*1), backoff.WithMaxInterval(time.Second*10)))
}

func getSshKeyById(id interface{}, meta interface{}) (*client.SshKey, error) {
Expand Down
42 changes: 42 additions & 0 deletions env0/data_sshkey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ package env0

import (
"encoding/json"
"errors"
"regexp"
"testing"

"github.com/env0/terraform-provider-env0/client"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"go.uber.org/mock/gomock"
)

func TestUnitSshKeyDataSourceById(t *testing.T) {
Expand Down Expand Up @@ -70,3 +72,43 @@ func testUnitSshKeyDataSource(t *testing.T, byKey string) {
mock.EXPECT().SshKeys().AnyTimes().Return([]client.SshKey{sshKey}, nil)
})
}

func TestUnitSshKeyDataSourceRetryByName(t *testing.T) {
resourceType := "env0_ssh_key"
resourceName := "test"
accessor := dataSourceAccessor(resourceType, resourceName)

sshKey := client.SshKey{
Id: "id0",
Name: "name0",
Value: "Key🔑",
}

otherSshKey := client.SshKey{
Id: "id1",
Name: "name1",
Value: "value1",
}

testCase := resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{
"name": sshKey.Name,
}),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(accessor, "id", sshKey.Id),
resource.TestCheckResourceAttr(accessor, "name", sshKey.Name),
),
},
},
}

runUnitTest(t, testCase, func(mock *client.MockApiClientInterface) {
gomock.InOrder(
mock.EXPECT().SshKeys().Times(1).Return(nil, errors.New("error")),
mock.EXPECT().SshKeys().Times(1).Return([]client.SshKey{otherSshKey}, nil),
mock.EXPECT().SshKeys().AnyTimes().Return([]client.SshKey{sshKey, otherSshKey}, nil),
)
})
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21
require (
github.com/Masterminds/semver/v3 v3.2.1
github.com/adhocore/gronx v1.6.6
github.com/cenkalti/backoff/v4 v4.3.0
github.com/go-resty/resty/v2 v2.11.0
github.com/google/uuid v1.5.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kB
github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA=
github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
Expand Down

0 comments on commit a76836d

Please sign in to comment.