Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ListCaches does not need NextToken, update tests, update comments #10

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 10 additions & 17 deletions internal/provider/cache_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,25 +201,18 @@ func (r *CacheResource) ImportState(ctx context.Context, req resource.ImportStat
}

func findCache(ctx context.Context, client momento.CacheClient, name string) (bool, error) {
token := ""
for {
resp, err := client.ListCaches(ctx, &momento.ListCachesRequest{NextToken: token})
if err != nil {
return false, err
}
if r, ok := resp.(*responses.ListCachesSuccess); ok {
for _, cacheInfo := range r.Caches() {
if cacheInfo.Name() == name {
return true, nil
}
}
token = r.NextToken()
if token == "" {
break
resp, err := client.ListCaches(ctx, &momento.ListCachesRequest{})
if err != nil {
return false, err
}
if r, ok := resp.(*responses.ListCachesSuccess); ok {
for _, cacheInfo := range r.Caches() {
if cacheInfo.Name() == name {
return true, nil
}
} else {
return false, fmt.Errorf("unexpected response type %T", resp)
}
} else {
return false, fmt.Errorf("unexpected response type %T", resp)
}
return false, nil
}
55 changes: 40 additions & 15 deletions internal/provider/cache_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,63 @@ import (

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/plancheck"
)

func TestAccCacheResource(t *testing.T) {
rName1 := acctest.RandStringFromCharSet(8, acctest.CharSetAlphaNum)
rName2 := acctest.RandStringFromCharSet(8, acctest.CharSetAlphaNum)
func TestCreateCacheResource(t *testing.T) {
cacheName1 := "terraform-provider-momento-test-" + acctest.RandString(8)
cacheName2 := "terraform-provider-momento-test-" + acctest.RandString(8)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
// Each TestStep represents one `terraform apply`
Steps: []resource.TestStep{
// Create and Read testing
// Create and Read one cache
{
Config: testAccCacheResourceConfig(rName1),
Config: testAccCacheResourceConfig(cacheName1),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("momento_cache.test", "Create"),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("momento_cache.test", "name", rName1),
resource.TestCheckResourceAttr("momento_cache.test", "id", rName1),
resource.TestCheckResourceAttr("momento_cache.test", "name", cacheName1),
resource.TestCheckResourceAttr("momento_cache.test", "id", cacheName1),
),
},
// ImportState testing
// Creating a cache should be idempotent (no new cache should be created on this second call)
{
ResourceName: "momento_cache.test",
ImportState: true,
ImportStateVerify: true,
Config: testAccCacheResourceConfig(cacheName1),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("momento_cache.test", "NoOp"),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("momento_cache.test", "name", cacheName1),
resource.TestCheckResourceAttr("momento_cache.test", "id", cacheName1),
),
},
// Update and Read testing
// Updating the config with new cache name should destroy the old cache and create a new one
{
Config: testAccCacheResourceConfig(rName2),
Config: testAccCacheResourceConfig(cacheName2),
ConfigPlanChecks: resource.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectResourceAction("momento_cache.test", "DestroyBeforeCreate"),
},
},
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("momento_cache.test", "name", rName2),
resource.TestCheckResourceAttr("momento_cache.test", "id", rName2),
resource.TestCheckResourceAttr("momento_cache.test", "name", cacheName2),
resource.TestCheckResourceAttr("momento_cache.test", "id", cacheName2),
),
},
// Test ImportState method (imports existing resources)
{
ResourceName: "momento_cache.test",
ImportState: true,
ImportStateVerify: true,
},
// Delete testing automatically occurs in TestCase
},
})
Expand Down
25 changes: 9 additions & 16 deletions internal/provider/caches_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,23 +125,16 @@ func (d *CachesDataSource) Read(ctx context.Context, req datasource.ReadRequest,

func listCaches(ctx context.Context, client momento.CacheClient) ([]string, error) {
var caches []string
token := ""
for {
resp, err := client.ListCaches(ctx, &momento.ListCachesRequest{NextToken: token})
if err != nil {
return nil, err
}
if r, ok := resp.(*responses.ListCachesSuccess); ok {
for _, cacheInfo := range r.Caches() {
caches = append(caches, cacheInfo.Name())
}
token = r.NextToken()
if token == "" {
break
}
} else {
return nil, fmt.Errorf("unexpected response type %T", resp)
resp, err := client.ListCaches(ctx, &momento.ListCachesRequest{})
if err != nil {
return nil, err
}
if r, ok := resp.(*responses.ListCachesSuccess); ok {
for _, cacheInfo := range r.Caches() {
caches = append(caches, cacheInfo.Name())
}
} else {
return nil, fmt.Errorf("unexpected response type %T", resp)
}
return caches, nil
}
10 changes: 5 additions & 5 deletions internal/provider/caches_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ import (
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccCachesDataSource(t *testing.T) {
rName := acctest.RandStringFromCharSet(8, acctest.CharSetAlphaNum)
func TestListCachesDataSource(t *testing.T) {
cacheName := "terraform-provider-momento-test-" + acctest.RandString(8)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
// Create at least one cache to test the data source
{
Config: testAccCacheResourceConfig(rName),
Config: testAccCacheResourceConfig(cacheName),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("momento_cache.test", "name", rName),
resource.TestCheckResourceAttr("momento_cache.test", "id", rName),
resource.TestCheckResourceAttr("momento_cache.test", "name", cacheName),
resource.TestCheckResourceAttr("momento_cache.test", "id", cacheName),
),
},
// Read testing
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (p *MomentoProvider) Configure(ctx context.Context, req provider.ConfigureR
credProvider, err := auth.NewStringMomentoTokenProvider(authToken)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Momento Token Provider",
"Unable to Create Momento Credential Provider",
"An unexpected error occurred when creating the Momento API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"Momento Client Error: "+err.Error(),
Expand All @@ -121,7 +121,7 @@ func (p *MomentoProvider) Configure(ctx context.Context, req provider.ConfigureR
return
}

// Make the HashiCups client available during DataSource and Resource
// Make the Momento client available during DataSource and Resource
// type Configure methods.
resp.DataSourceData = client
resp.ResourceData = client
Expand Down
Loading