-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: new mongodbatlas_stream_instances data source (#1701)
- Loading branch information
1 parent
9a2c2fa
commit 71bcdb2
Showing
12 changed files
with
457 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package dsschema | ||
|
||
import ( | ||
"maps" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
) | ||
|
||
func PaginatedDSSchema(arguments, resultAttributes map[string]schema.Attribute) schema.Schema { | ||
result := schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"page_num": schema.Int64Attribute{ | ||
Optional: true, | ||
}, | ||
"items_per_page": schema.Int64Attribute{ | ||
Optional: true, | ||
}, | ||
"total_count": schema.Int64Attribute{ | ||
Computed: true, | ||
}, | ||
"results": schema.ListNestedAttribute{ | ||
Computed: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: resultAttributes, | ||
}, | ||
}, | ||
}, | ||
} | ||
maps.Copy(result.Attributes, arguments) | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package dsschema_test | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/dsschema" | ||
) | ||
|
||
func TestPaginatedDSSchema(t *testing.T) { | ||
expectedSchema := schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"project_id": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"page_num": schema.Int64Attribute{ | ||
Optional: true, | ||
}, | ||
"items_per_page": schema.Int64Attribute{ | ||
Optional: true, | ||
}, | ||
"total_count": schema.Int64Attribute{ | ||
Computed: true, | ||
}, | ||
"results": schema.ListNestedAttribute{ | ||
Computed: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"instance_name": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
resultSchema := dsschema.PaginatedDSSchema( | ||
map[string]schema.Attribute{ | ||
"project_id": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
map[string]schema.Attribute{ | ||
"id": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
"instance_name": schema.StringAttribute{ | ||
Computed: true, | ||
}, | ||
}) | ||
|
||
if !reflect.DeepEqual(resultSchema, expectedSchema) { | ||
t.Errorf("created schema does not matched expected") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
internal/service/streaminstance/data_source_stream_instances.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package streaminstance | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/datasource" | ||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/dsschema" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config" | ||
"go.mongodb.org/atlas-sdk/v20231115002/admin" | ||
) | ||
|
||
var _ datasource.DataSource = &streamInstancesDS{} | ||
var _ datasource.DataSourceWithConfigure = &streamInstancesDS{} | ||
|
||
func PluralDataSource() datasource.DataSource { | ||
return &streamInstancesDS{ | ||
DSCommon: config.DSCommon{ | ||
DataSourceName: fmt.Sprintf("%ss", streamInstanceName), | ||
}, | ||
} | ||
} | ||
|
||
type streamInstancesDS struct { | ||
config.DSCommon | ||
} | ||
|
||
type TFStreamInstancesModel struct { | ||
ID types.String `tfsdk:"id"` | ||
ProjectID types.String `tfsdk:"project_id"` | ||
Results []TFStreamInstanceModel `tfsdk:"results"` | ||
PageNum types.Int64 `tfsdk:"page_num"` | ||
ItemsPerPage types.Int64 `tfsdk:"items_per_page"` | ||
TotalCount types.Int64 `tfsdk:"total_count"` | ||
} | ||
|
||
func (d *streamInstancesDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) { | ||
resp.Schema = dsschema.PaginatedDSSchema( | ||
map[string]schema.Attribute{ | ||
"project_id": schema.StringAttribute{ | ||
Required: true, | ||
}, | ||
}, | ||
DSAttributes(false)) | ||
} | ||
|
||
func (d *streamInstancesDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { | ||
var streamInstancesConfig TFStreamInstancesModel | ||
resp.Diagnostics.Append(req.Config.Get(ctx, &streamInstancesConfig)...) | ||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
connV2 := d.Client.AtlasV2 | ||
projectID := streamInstancesConfig.ProjectID.ValueString() | ||
itemsPerPage := streamInstancesConfig.ItemsPerPage.ValueInt64Pointer() | ||
pageNum := streamInstancesConfig.PageNum.ValueInt64Pointer() | ||
apiResp, _, err := connV2.StreamsApi.ListStreamInstancesWithParams(ctx, &admin.ListStreamInstancesApiParams{ | ||
GroupId: projectID, | ||
ItemsPerPage: conversion.Int64PtrToIntPtr(itemsPerPage), | ||
PageNum: conversion.Int64PtrToIntPtr(pageNum), | ||
}).Execute() | ||
if err != nil { | ||
resp.Diagnostics.AddError("error fetching results", err.Error()) | ||
return | ||
} | ||
|
||
newStreamInstancesModel, diags := NewTFStreamInstances(ctx, &streamInstancesConfig, apiResp) | ||
if diags.HasError() { | ||
resp.Diagnostics.Append(diags...) | ||
return | ||
} | ||
resp.Diagnostics.Append(resp.State.Set(ctx, newStreamInstancesModel)...) | ||
} |
89 changes: 89 additions & 0 deletions
89
internal/service/streaminstance/data_source_stream_instances_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package streaminstance_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc" | ||
"go.mongodb.org/atlas-sdk/v20231115002/admin" | ||
) | ||
|
||
func TestAccStreamDSStreamInstances_basic(t *testing.T) { | ||
var ( | ||
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") | ||
projectName = acctest.RandomWithPrefix("test-acc-stream") | ||
instanceName = acctest.RandomWithPrefix("test-acc-name") | ||
dataSourceName = "data.mongodbatlas_stream_instances.test" | ||
) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acc.PreCheckBasic(t) }, | ||
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, | ||
CheckDestroy: checkDestroyStreamInstance, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: streamInstancesDataSourceConfig(orgID, projectName, instanceName, region, cloudProvider), | ||
Check: streamInstancesAttributeChecks(dataSourceName, nil, nil, 1), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccStreamDSStreamInstances_withPageConfig(t *testing.T) { | ||
var ( | ||
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID") | ||
projectName = acctest.RandomWithPrefix("test-acc-stream") | ||
instanceName = acctest.RandomWithPrefix("test-acc-name") | ||
dataSourceName = "data.mongodbatlas_stream_instances.test" | ||
) | ||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acc.PreCheckBasic(t) }, | ||
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories, | ||
CheckDestroy: checkDestroyStreamInstance, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: streamInstancesWithPageAttrDataSourceConfig(orgID, projectName, instanceName, region, cloudProvider), | ||
Check: streamInstancesAttributeChecks(dataSourceName, admin.PtrInt(2), admin.PtrInt(1), 0), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func streamInstancesDataSourceConfig(orgID, projectName, instanceName, region, cloudProvider string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "mongodbatlas_stream_instances" "test" { | ||
project_id = mongodbatlas_stream_instance.test.project_id | ||
} | ||
`, streamInstanceConfig(orgID, projectName, instanceName, region, cloudProvider)) | ||
} | ||
|
||
func streamInstancesWithPageAttrDataSourceConfig(orgID, projectName, instanceName, region, cloudProvider string) string { | ||
return fmt.Sprintf(` | ||
%s | ||
data "mongodbatlas_stream_instances" "test" { | ||
project_id = mongodbatlas_stream_instance.test.project_id | ||
page_num = 2 | ||
items_per_page = 1 | ||
} | ||
`, streamInstanceConfig(orgID, projectName, instanceName, region, cloudProvider)) | ||
} | ||
|
||
func streamInstancesAttributeChecks(resourceName string, pageNum, itemsPerPage *int, totalCount int) resource.TestCheckFunc { | ||
resourceChecks := []resource.TestCheckFunc{ | ||
resource.TestCheckResourceAttrSet(resourceName, "project_id"), | ||
resource.TestCheckResourceAttrSet(resourceName, "total_count"), | ||
resource.TestCheckResourceAttr(resourceName, "results.#", fmt.Sprint(totalCount)), | ||
} | ||
if pageNum != nil { | ||
resourceChecks = append(resourceChecks, resource.TestCheckResourceAttr(resourceName, "page_num", fmt.Sprint(*pageNum))) | ||
} | ||
if itemsPerPage != nil { | ||
resourceChecks = append(resourceChecks, resource.TestCheckResourceAttr(resourceName, "items_per_page", fmt.Sprint(*itemsPerPage))) | ||
} | ||
return resource.ComposeTestCheckFunc(resourceChecks...) | ||
} |
Oops, something went wrong.