Skip to content

Commit 9a2c2fa

Browse files
feat: new mongodbatlas_stream_instance data source (#1689)
* feat: stream_instance data source * minor fix in docs
1 parent ebe95d4 commit 9a2c2fa

8 files changed

+184
-24
lines changed

internal/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@ func (p *MongodbtlasProvider) DataSources(context.Context) []func() datasource.D
413413
atlasuser.DataSource,
414414
atlasuser.PluralDataSource,
415415
searchdeployment.DataSource,
416+
streaminstance.DataSource,
416417
}
417418
}
418419

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package streaminstance
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/datasource"
7+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
9+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
10+
)
11+
12+
var _ datasource.DataSource = &streamInstanceDS{}
13+
var _ datasource.DataSourceWithConfigure = &streamInstanceDS{}
14+
15+
func DataSource() datasource.DataSource {
16+
return &streamInstanceDS{
17+
DSCommon: config.DSCommon{
18+
DataSourceName: streamInstanceName,
19+
},
20+
}
21+
}
22+
23+
type streamInstanceDS struct {
24+
config.DSCommon
25+
}
26+
27+
func (d *streamInstanceDS) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
28+
resp.Schema = schema.Schema{
29+
Attributes: map[string]schema.Attribute{
30+
"id": schema.StringAttribute{
31+
Computed: true,
32+
},
33+
"instance_name": schema.StringAttribute{
34+
Required: true,
35+
},
36+
"project_id": schema.StringAttribute{
37+
Required: true,
38+
},
39+
"data_process_region": schema.SingleNestedAttribute{
40+
Computed: true,
41+
Attributes: map[string]schema.Attribute{
42+
"cloud_provider": schema.StringAttribute{
43+
Computed: true,
44+
},
45+
"region": schema.StringAttribute{
46+
Computed: true,
47+
},
48+
},
49+
},
50+
"hostnames": schema.ListAttribute{
51+
ElementType: types.StringType,
52+
Computed: true,
53+
},
54+
},
55+
}
56+
}
57+
58+
func (d *streamInstanceDS) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
59+
var streamInstanceConfig TFStreamInstanceModel
60+
resp.Diagnostics.Append(req.Config.Get(ctx, &streamInstanceConfig)...)
61+
if resp.Diagnostics.HasError() {
62+
return
63+
}
64+
65+
connV2 := d.Client.AtlasV2
66+
projectID := streamInstanceConfig.ProjectID.ValueString()
67+
instanceName := streamInstanceConfig.InstanceName.ValueString()
68+
apiResp, _, err := connV2.StreamsApi.GetStreamInstance(ctx, projectID, instanceName).Execute()
69+
if err != nil {
70+
resp.Diagnostics.AddError("error fetching resource", err.Error())
71+
return
72+
}
73+
74+
newStreamInstanceModel, diags := NewTFStreamInstance(ctx, apiResp)
75+
if diags.HasError() {
76+
resp.Diagnostics.Append(diags...)
77+
return
78+
}
79+
resp.Diagnostics.Append(resp.State.Set(ctx, newStreamInstanceModel)...)
80+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package streaminstance_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
11+
)
12+
13+
func TestAccStreamInstanceDS_basic(t *testing.T) {
14+
var (
15+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
16+
projectName = acctest.RandomWithPrefix("test-acc-stream")
17+
instanceName = acctest.RandomWithPrefix("test-acc-name")
18+
dataSourceName = "data.mongodbatlas_stream_instance.test"
19+
)
20+
resource.ParallelTest(t, resource.TestCase{
21+
PreCheck: func() { acc.PreCheckBasic(t) },
22+
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
23+
CheckDestroy: checkDestroyStreamInstance,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: streamInstanceDataSourceConfig(orgID, projectName, instanceName, region, cloudProvider),
27+
Check: streamInstanceAttributeChecks(dataSourceName, orgID, projectName, instanceName, region, cloudProvider),
28+
},
29+
},
30+
})
31+
}
32+
33+
func streamInstanceDataSourceConfig(orgID, projectName, instanceName, region, cloudProvider string) string {
34+
return fmt.Sprintf(`
35+
%s
36+
37+
data "mongodbatlas_stream_instance" "test" {
38+
project_id = mongodbatlas_stream_instance.test.project_id
39+
instance_name = mongodbatlas_stream_instance.test.instance_name
40+
}
41+
`, streamInstanceConfig(orgID, projectName, instanceName, region, cloudProvider))
42+
}

internal/service/streaminstance/model_stream_instance.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"go.mongodb.org/atlas-sdk/v20231115002/admin"
1010
)
1111

12-
func NewStreamInstanceCreateReq(ctx context.Context, plan *TFStreamInstanceRSModel) (*admin.StreamsTenant, diag.Diagnostics) {
12+
func NewStreamInstanceCreateReq(ctx context.Context, plan *TFStreamInstanceModel) (*admin.StreamsTenant, diag.Diagnostics) {
1313
dataProcessRegion := &TFInstanceProcessRegionSpecModel{}
1414
if diags := plan.DataProcessRegion.As(ctx, dataProcessRegion, basetypes.ObjectAsOptions{}); diags.HasError() {
1515
return nil, diags
@@ -24,7 +24,7 @@ func NewStreamInstanceCreateReq(ctx context.Context, plan *TFStreamInstanceRSMod
2424
}, nil
2525
}
2626

27-
func NewStreamInstanceUpdateReq(ctx context.Context, plan *TFStreamInstanceRSModel) (*admin.StreamsDataProcessRegion, diag.Diagnostics) {
27+
func NewStreamInstanceUpdateReq(ctx context.Context, plan *TFStreamInstanceModel) (*admin.StreamsDataProcessRegion, diag.Diagnostics) {
2828
dataProcessRegion := &TFInstanceProcessRegionSpecModel{}
2929
if diags := plan.DataProcessRegion.As(ctx, dataProcessRegion, basetypes.ObjectAsOptions{}); diags.HasError() {
3030
return nil, diags
@@ -35,7 +35,7 @@ func NewStreamInstanceUpdateReq(ctx context.Context, plan *TFStreamInstanceRSMod
3535
}, nil
3636
}
3737

38-
func NewTFStreamInstance(ctx context.Context, apiResp *admin.StreamsTenant) (*TFStreamInstanceRSModel, diag.Diagnostics) {
38+
func NewTFStreamInstance(ctx context.Context, apiResp *admin.StreamsTenant) (*TFStreamInstanceModel, diag.Diagnostics) {
3939
hostnames, diags := types.ListValueFrom(ctx, types.StringType, apiResp.Hostnames)
4040

4141
var dataProcessRegion = types.ObjectNull(ProcessRegionObjectType.AttrTypes)
@@ -51,7 +51,7 @@ func NewTFStreamInstance(ctx context.Context, apiResp *admin.StreamsTenant) (*TF
5151
return nil, diags
5252
}
5353

54-
return &TFStreamInstanceRSModel{
54+
return &TFStreamInstanceModel{
5555
ID: types.StringPointerValue(apiResp.Id),
5656
InstanceName: types.StringPointerValue(apiResp.Name),
5757
ProjectID: types.StringPointerValue(apiResp.GroupId),

internal/service/streaminstance/model_stream_instance_test.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212

1313
type sdkToTFModelTestCase struct {
1414
SDKResp *admin.StreamsTenant
15-
expectedTFModel *streaminstance.TFStreamInstanceRSModel
15+
expectedTFModel *streaminstance.TFStreamInstanceModel
1616
name string
1717
}
1818

@@ -40,7 +40,7 @@ func TestStreamInstanceSDKToTFModel(t *testing.T) {
4040
Hostnames: hostnames,
4141
Name: admin.PtrString(instanceName),
4242
},
43-
expectedTFModel: &streaminstance.TFStreamInstanceRSModel{
43+
expectedTFModel: &streaminstance.TFStreamInstanceModel{
4444
ID: types.StringValue(dummyStreamInstanceID),
4545
DataProcessRegion: tfRegionObject(t, cloudProvider, region),
4646
ProjectID: types.StringValue(dummyProjectID),
@@ -55,7 +55,7 @@ func TestStreamInstanceSDKToTFModel(t *testing.T) {
5555
GroupId: admin.PtrString(dummyProjectID),
5656
Name: admin.PtrString(instanceName),
5757
},
58-
expectedTFModel: &streaminstance.TFStreamInstanceRSModel{
58+
expectedTFModel: &streaminstance.TFStreamInstanceModel{
5959
ID: types.StringValue(dummyStreamInstanceID),
6060
DataProcessRegion: types.ObjectNull(streaminstance.ProcessRegionObjectType.AttrTypes),
6161
ProjectID: types.StringValue(dummyProjectID),
@@ -79,7 +79,7 @@ func TestStreamInstanceSDKToTFModel(t *testing.T) {
7979
}
8080

8181
type tfToSDKCreateModelTestCase struct {
82-
tfModel *streaminstance.TFStreamInstanceRSModel
82+
tfModel *streaminstance.TFStreamInstanceModel
8383
expectedSDKReq *admin.StreamsTenant
8484
name string
8585
}
@@ -88,7 +88,7 @@ func TestStreamInstanceTFToSDKCreateModel(t *testing.T) {
8888
testCases := []tfToSDKCreateModelTestCase{
8989
{
9090
name: "Complete TF state",
91-
tfModel: &streaminstance.TFStreamInstanceRSModel{
91+
tfModel: &streaminstance.TFStreamInstanceModel{
9292
DataProcessRegion: tfRegionObject(t, cloudProvider, region),
9393
ProjectID: types.StringValue(dummyProjectID),
9494
InstanceName: types.StringValue(instanceName),
@@ -118,7 +118,7 @@ func TestStreamInstanceTFToSDKCreateModel(t *testing.T) {
118118
}
119119

120120
type tfToSDKUpdateModelTestCase struct {
121-
tfModel *streaminstance.TFStreamInstanceRSModel
121+
tfModel *streaminstance.TFStreamInstanceModel
122122
expectedSDKReq *admin.StreamsDataProcessRegion
123123
name string
124124
}
@@ -127,7 +127,7 @@ func TestStreamInstanceTFToSDKUpdateModel(t *testing.T) {
127127
testCases := []tfToSDKUpdateModelTestCase{
128128
{
129129
name: "Complete TF state",
130-
tfModel: &streaminstance.TFStreamInstanceRSModel{
130+
tfModel: &streaminstance.TFStreamInstanceModel{
131131
ID: types.StringValue(dummyStreamInstanceID),
132132
DataProcessRegion: tfRegionObject(t, cloudProvider, region),
133133
ProjectID: types.StringValue(dummyProjectID),

internal/service/streaminstance/resource_stream_instance.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type streamInstanceRS struct {
3232
config.RSCommon
3333
}
3434

35-
type TFStreamInstanceRSModel struct {
35+
type TFStreamInstanceModel struct {
3636
ID types.String `tfsdk:"id"`
3737
InstanceName types.String `tfsdk:"instance_name"`
3838
ProjectID types.String `tfsdk:"project_id"`
@@ -88,7 +88,7 @@ func (r *streamInstanceRS) Schema(ctx context.Context, req resource.SchemaReques
8888
}
8989

9090
func (r *streamInstanceRS) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
91-
var streamInstancePlan TFStreamInstanceRSModel
91+
var streamInstancePlan TFStreamInstanceModel
9292
resp.Diagnostics.Append(req.Plan.Get(ctx, &streamInstancePlan)...)
9393
if resp.Diagnostics.HasError() {
9494
return
@@ -116,7 +116,7 @@ func (r *streamInstanceRS) Create(ctx context.Context, req resource.CreateReques
116116
}
117117

118118
func (r *streamInstanceRS) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
119-
var streamInstanceState TFStreamInstanceRSModel
119+
var streamInstanceState TFStreamInstanceModel
120120
resp.Diagnostics.Append(req.State.Get(ctx, &streamInstanceState)...)
121121
if resp.Diagnostics.HasError() {
122122
return
@@ -140,7 +140,7 @@ func (r *streamInstanceRS) Read(ctx context.Context, req resource.ReadRequest, r
140140
}
141141

142142
func (r *streamInstanceRS) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
143-
var streamInstancePlan TFStreamInstanceRSModel
143+
var streamInstancePlan TFStreamInstanceModel
144144
resp.Diagnostics.Append(req.Plan.Get(ctx, &streamInstancePlan)...)
145145
if resp.Diagnostics.HasError() {
146146
return
@@ -169,7 +169,7 @@ func (r *streamInstanceRS) Update(ctx context.Context, req resource.UpdateReques
169169
}
170170

171171
func (r *streamInstanceRS) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
172-
var streamInstanceState *TFStreamInstanceRSModel
172+
var streamInstanceState *TFStreamInstanceModel
173173
resp.Diagnostics.Append(req.State.Get(ctx, &streamInstanceState)...)
174174
if resp.Diagnostics.HasError() {
175175
return

internal/service/streaminstance/resource_stream_instance_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/mongodb/terraform-provider-mongodbatlas/internal/testutil/acc"
1414
)
1515

16-
func TestAccStreamInstance_basic(t *testing.T) {
16+
func TestAccStreamInstanceRS_basic(t *testing.T) {
1717
var (
1818
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
1919
projectName = acctest.RandomWithPrefix("test-acc-stream")
@@ -25,9 +25,11 @@ func TestAccStreamInstance_basic(t *testing.T) {
2525
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
2626
CheckDestroy: checkDestroyStreamInstance,
2727
Steps: []resource.TestStep{
28-
newStreamInstanceTestStep(resourceName, orgID, projectName, instanceName, "VIRGINIA_USA", "AWS"),
2928
{
30-
Config: streamInstanceConfig(orgID, projectName, instanceName, "VIRGINIA_USA", "AWS"), // as of now there are no values that can be updated because only one region is supported
29+
Config: streamInstanceConfig(orgID, projectName, instanceName, region, cloudProvider), // as of now there are no values that can be updated because only one region is supported
30+
Check: streamInstanceAttributeChecks(resourceName, orgID, projectName, instanceName, region, cloudProvider),
31+
},
32+
{
3133
ResourceName: resourceName,
3234
ImportStateIdFunc: checkStreamInstanceImportStateIDFunc(resourceName),
3335
ImportState: true,
@@ -37,7 +39,7 @@ func TestAccStreamInstance_basic(t *testing.T) {
3739
})
3840
}
3941

40-
func newStreamInstanceTestStep(resourceName, orgID, projectName, instanceName, region, cloudProvider string) resource.TestStep {
42+
func streamInstanceAttributeChecks(resourceName, orgID, projectName, instanceName, region, cloudProvider string) resource.TestCheckFunc {
4143
resourceChecks := []resource.TestCheckFunc{
4244
checkSearchInstanceExists(),
4345
resource.TestCheckResourceAttrSet(resourceName, "id"),
@@ -47,10 +49,7 @@ func newStreamInstanceTestStep(resourceName, orgID, projectName, instanceName, r
4749
resource.TestCheckResourceAttr(resourceName, "data_process_region.cloud_provider", cloudProvider),
4850
resource.TestCheckResourceAttr(resourceName, "hostnames.#", "1"),
4951
}
50-
return resource.TestStep{
51-
Config: streamInstanceConfig(orgID, projectName, instanceName, region, cloudProvider),
52-
Check: resource.ComposeTestCheckFunc(resourceChecks...),
53-
}
52+
return resource.ComposeTestCheckFunc(resourceChecks...)
5453
}
5554

5655
func streamInstanceConfig(orgID, projectName, instanceName, region, cloudProvider string) string {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
layout: "mongodbatlas"
3+
page_title: "MongoDB Atlas: stream instance"
4+
sidebar_current: "docs-mongodbatlas-datasource-stream-instance"
5+
description: |-
6+
Describes a Stream Instance.
7+
---
8+
9+
# Data Source: mongodbatlas_stream_instance
10+
11+
`mongodbatlas_stream_instance` describes a stream instance.
12+
13+
## Example Usage
14+
15+
```terraform
16+
data "mongodbatlas_stream_instance" "example" {
17+
project_id = "<PROJECT_ID>"
18+
instance_name = "<INSTANCE_NAME>"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
* `project_id` - (Required) Unique 24-hexadecimal digit string that identifies your project.
25+
* `instance_name` - (Required) Human-readable label that identifies the stream instance.
26+
27+
## Attributes Reference
28+
29+
* `data_process_region` - Defines the cloud service provider and region where MongoDB Cloud performs stream processing. See [data process region](#data-process-region).
30+
* `hostnames` - List that contains the hostnames assigned to the stream instance.
31+
32+
33+
### Data Process Region
34+
35+
* `cloud_provider` - Label that identifies the cloud service provider where MongoDB Cloud performs stream processing. The [MongoDB Atlas API](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Streams/operation/createStreamInstance) describes the valid values.
36+
* `region` - Name of the cloud provider region hosting Atlas Stream Processing. The [MongoDB Atlas API](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Streams/operation/createStreamInstance) describes the valid values.
37+
38+
To learn more, see: [MongoDB Atlas API - Stream Instance](https://www.mongodb.com/docs/atlas/reference/api-resources-spec/#tag/Streams/operation/createStreamInstance) Documentation.

0 commit comments

Comments
 (0)