Skip to content

Commit 98097d2

Browse files
coderGo93Edgar López
andauthored
INTMDB-185: Added parameter regions for GCP network container (#418)
* chore: updated mod, need the official release first * feat: added parameter regions for gcp container(s) * tests: added acceptance test for containers using gcp regions * docs: added parameter regions in docs of network container(s) Co-authored-by: Edgar López <[email protected]>
1 parent bc3c9ce commit 98097d2

11 files changed

+203
-1
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ require (
1212
github.com/spf13/cast v1.3.1
1313
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20200518153306-40099de47e37
1414
github.com/terraform-providers/terraform-provider-google v1.20.1-0.20200518165017-1dd21651c496
15-
go.mongodb.org/atlas v0.7.2
15+
go.mongodb.org/atlas v0.7.3-0.20210302081736-7ccf7e1f4351
1616
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,8 @@ go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
731731
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
732732
go.mongodb.org/atlas v0.7.2 h1:wB3+hP71t3mK+JOSrjBFbrzb5MsZRzDtZlpEKp58KK0=
733733
go.mongodb.org/atlas v0.7.2/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
734+
go.mongodb.org/atlas v0.7.3-0.20210302081736-7ccf7e1f4351 h1:3NuN2K6VtDuDNfAKX/4j6H4pLaT8LIkvfXD3F7Oy30c=
735+
go.mongodb.org/atlas v0.7.3-0.20210302081736-7ccf7e1f4351/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
734736
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
735737
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
736738
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=

mongodbatlas/data_source_mongodbatlas_network_container.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ func dataSourceMongoDBAtlasNetworkContainer() *schema.Resource {
6161
Type: schema.TypeString,
6262
Computed: true,
6363
},
64+
"regions": {
65+
Type: schema.TypeList,
66+
Computed: true,
67+
Elem: &schema.Schema{
68+
Type: schema.TypeString,
69+
},
70+
},
6471
},
6572
}
6673
}
@@ -124,6 +131,10 @@ func dataSourceMongoDBAtlasNetworkContainerRead(d *schema.ResourceData, meta int
124131
return fmt.Errorf("error setting `vnet_name` for Network Container (%s): %s", d.Id(), err)
125132
}
126133

134+
if err = d.Set("regions", container.Regions); err != nil {
135+
return fmt.Errorf("error setting `regions` for Network Container (%s): %s", d.Id(), err)
136+
}
137+
127138
d.SetId(container.ID)
128139

129140
return nil

mongodbatlas/data_source_mongodbatlas_network_container_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,39 @@ func TestAccDataSourceMongoDBAtlasNetworkContainer_basic(t *testing.T) {
4545
})
4646
}
4747

48+
func TestAccDataSourceMongoDBAtlasNetworkContainer_WithGCPRegions(t *testing.T) {
49+
var container matlas.Container
50+
51+
randInt := acctest.RandIntRange(0, 255)
52+
53+
resourceName := "mongodbatlas_network_container.test_ds"
54+
cidrBlock := fmt.Sprintf("10.%d.0.0/21", randInt)
55+
dataSourceName := "data.mongodbatlas_network_container.test_ds"
56+
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
57+
projectName := acctest.RandomWithPrefix("test-acc")
58+
59+
providerName := "GCP"
60+
61+
resource.Test(t, resource.TestCase{
62+
PreCheck: func() { testAccPreCheck(t) },
63+
Providers: testAccProviders,
64+
CheckDestroy: testAccCheckMongoDBAtlasNetworkContainerDestroy,
65+
Steps: []resource.TestStep{
66+
{
67+
Config: testAccMongoDBAtlasNetworkContainerDSWithGCPRegionsConfig(projectName, orgID, cidrBlock),
68+
Check: resource.ComposeTestCheckFunc(
69+
testAccCheckMongoDBAtlasNetworkContainerExists(resourceName, &container),
70+
testAccCheckMongoDBAtlasNetworkContainerAttributes(&container, providerName),
71+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
72+
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
73+
resource.TestCheckResourceAttrSet(resourceName, "provisioned"),
74+
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
75+
),
76+
},
77+
},
78+
})
79+
}
80+
4881
func testAccMongoDBAtlasNetworkContainerDSConfig(projectName, orgID, cidrBlock string) string {
4982
return fmt.Sprintf(`
5083
resource "mongodbatlas_project" "test" {
@@ -65,3 +98,24 @@ func testAccMongoDBAtlasNetworkContainerDSConfig(projectName, orgID, cidrBlock s
6598
}
6699
`, projectName, orgID, cidrBlock)
67100
}
101+
102+
func testAccMongoDBAtlasNetworkContainerDSWithGCPRegionsConfig(projectName, orgID, cidrBlock string) string {
103+
return fmt.Sprintf(`
104+
resource "mongodbatlas_project" "test" {
105+
name = "%s"
106+
org_id = "%s"
107+
}
108+
109+
resource "mongodbatlas_network_container" "test_ds" {
110+
project_id = "${mongodbatlas_project.test.id}"
111+
atlas_cidr_block = "%s"
112+
provider_name = "GCP"
113+
regions = ["US_EAST_4", "US_WEST_3"]
114+
}
115+
116+
data "mongodbatlas_network_container" "test_ds" {
117+
project_id = mongodbatlas_network_container.test_ds.project_id
118+
container_id = mongodbatlas_network_container.test_ds.container_id
119+
}
120+
`, projectName, orgID, cidrBlock)
121+
}

mongodbatlas/data_source_mongodbatlas_network_containers.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ func dataSourceMongoDBAtlasNetworkContainers() *schema.Resource {
7070
Type: schema.TypeString,
7171
Computed: true,
7272
},
73+
"regions": {
74+
Type: schema.TypeList,
75+
Computed: true,
76+
Elem: &schema.Schema{
77+
Type: schema.TypeString,
78+
},
79+
},
7380
},
7481
},
7582
},
@@ -118,6 +125,7 @@ func flattenNetworkContainers(containers []matlas.Container) []map[string]interf
118125
"network_name": containers[i].NetworkName,
119126
"vpc_id": containers[i].VPCID,
120127
"vnet_name": containers[i].VNetName,
128+
"regions": containers[i].Regions,
121129
}
122130
}
123131
}

mongodbatlas/data_source_mongodbatlas_network_containers_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ func TestAccDataSourceMongoDBAtlasNetworkContainers_basic(t *testing.T) {
4646
},
4747
})
4848
}
49+
func TestAccDataSourceMongoDBAtlasNetworkContainers_WithGCPRegions(t *testing.T) {
50+
var container matlas.Container
51+
52+
randInt := acctest.RandIntRange(0, 255)
53+
54+
resourceName := "mongodbatlas_network_container.test"
55+
cidrBlock := fmt.Sprintf("10.%d.0.0/21", randInt)
56+
dataSourceName := "data.mongodbatlas_network_containers.test"
57+
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
58+
projectName := acctest.RandomWithPrefix("test-acc")
59+
60+
providerName := "GCP"
61+
62+
resource.Test(t, resource.TestCase{
63+
PreCheck: func() { testAccPreCheck(t) },
64+
Providers: testAccProviders,
65+
CheckDestroy: testAccCheckMongoDBAtlasNetworkContainerDestroy,
66+
Steps: []resource.TestStep{
67+
{
68+
Config: testAccMongoDBAtlasNetworkContainersDSWithGCPRegionsConfig(projectName, orgID, cidrBlock),
69+
Check: resource.ComposeTestCheckFunc(
70+
testAccCheckMongoDBAtlasNetworkContainerExists(resourceName, &container),
71+
testAccCheckMongoDBAtlasNetworkContainerAttributes(&container, providerName),
72+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
73+
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
74+
resource.TestCheckResourceAttrSet(resourceName, "provisioned"),
75+
resource.TestCheckResourceAttrSet(dataSourceName, "results.#"),
76+
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.id"),
77+
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.atlas_cidr_block"),
78+
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.provider_name"),
79+
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.provisioned"),
80+
),
81+
},
82+
},
83+
})
84+
}
4985

5086
func testAccMongoDBAtlasNetworkContainersDSConfig(projectName, orgID, cidrBlock string) string {
5187
return fmt.Sprintf(`
@@ -67,3 +103,24 @@ func testAccMongoDBAtlasNetworkContainersDSConfig(projectName, orgID, cidrBlock
67103
}
68104
`, projectName, orgID, cidrBlock)
69105
}
106+
107+
func testAccMongoDBAtlasNetworkContainersDSWithGCPRegionsConfig(projectName, orgID, cidrBlock string) string {
108+
return fmt.Sprintf(`
109+
resource "mongodbatlas_project" "test" {
110+
name = "%s"
111+
org_id = "%s"
112+
}
113+
114+
resource "mongodbatlas_network_container" "test" {
115+
project_id = mongodbatlas_project.test.id
116+
atlas_cidr_block = "%s"
117+
provider_name = "GCP"
118+
regions = ["US_EAST_4", "US_WEST_3"]
119+
}
120+
121+
data "mongodbatlas_network_containers" "test" {
122+
project_id = mongodbatlas_network_container.test.project_id
123+
provider_name = "GCP"
124+
}
125+
`, projectName, orgID, cidrBlock)
126+
}

mongodbatlas/resource_mongodbatlas_network_container.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import (
1010
"strings"
1111
"time"
1212

13+
"github.com/spf13/cast"
14+
1315
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
1416
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
1517
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
@@ -86,6 +88,13 @@ func resourceMongoDBAtlasNetworkContainer() *schema.Resource {
8688
Type: schema.TypeString,
8789
Computed: true,
8890
},
91+
"regions": {
92+
Type: schema.TypeList,
93+
Optional: true,
94+
Elem: &schema.Schema{
95+
Type: schema.TypeString,
96+
},
97+
},
8998
},
9099
}
91100
}
@@ -119,6 +128,13 @@ func resourceMongoDBAtlasNetworkContainerCreate(d *schema.ResourceData, meta int
119128
containerRequest.Region = region
120129
}
121130

131+
if providerName == "GCP" {
132+
regions, ok := d.GetOk("regions")
133+
if ok {
134+
containerRequest.Regions = cast.ToStringSlice(regions)
135+
}
136+
}
137+
122138
container, _, err := conn.Containers.Create(context.Background(), projectID, containerRequest)
123139
if err != nil {
124140
return fmt.Errorf(errorContainterCreate, err)
@@ -188,6 +204,10 @@ func resourceMongoDBAtlasNetworkContainerRead(d *schema.ResourceData, meta inter
188204
return fmt.Errorf("error setting `container_id` for Network Container (%s): %s", containerID, err)
189205
}
190206

207+
if err = d.Set("regions", container.Regions); err != nil {
208+
return fmt.Errorf("error setting `regions` for Network Container (%s): %s", containerID, err)
209+
}
210+
191211
return nil
192212
}
193213

mongodbatlas/resource_mongodbatlas_network_container_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,36 @@ func TestAccResourceMongoDBAtlasNetworkContainer_basicGCP(t *testing.T) {
139139
})
140140
}
141141

142+
func TestAccResourceMongoDBAtlasNetworkContainer_WithRegionsGCP(t *testing.T) {
143+
var (
144+
container matlas.Container
145+
randInt = acctest.RandIntRange(0, 255)
146+
resourceName = "mongodbatlas_network_container.test"
147+
cidrBlock = fmt.Sprintf("10.%d.0.0/21", randInt)
148+
providerName = "GCP"
149+
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
150+
projectName = acctest.RandomWithPrefix("test-acc")
151+
)
152+
153+
resource.Test(t, resource.TestCase{
154+
PreCheck: func() { testAccPreCheck(t) },
155+
Providers: testAccProviders,
156+
CheckDestroy: testAccCheckMongoDBAtlasNetworkContainerDestroy,
157+
Steps: []resource.TestStep{
158+
{
159+
Config: testAccMongoDBAtlasNetworkContainerConfigGCPWithRegions(projectName, orgID, cidrBlock, providerName),
160+
Check: resource.ComposeTestCheckFunc(
161+
testAccCheckMongoDBAtlasNetworkContainerExists(resourceName, &container),
162+
testAccCheckMongoDBAtlasNetworkContainerAttributes(&container, providerName),
163+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
164+
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
165+
resource.TestCheckResourceAttrSet(resourceName, "provisioned"),
166+
),
167+
},
168+
},
169+
})
170+
}
171+
142172
func TestAccResourceMongoDBAtlasNetworkContainer_importBasic(t *testing.T) {
143173
var (
144174
randInt = acctest.RandIntRange(0, 255)
@@ -278,3 +308,19 @@ func testAccMongoDBAtlasNetworkContainerConfigGCP(projectName, orgID, cidrBlock,
278308
}
279309
`, projectName, orgID, cidrBlock, providerName)
280310
}
311+
312+
func testAccMongoDBAtlasNetworkContainerConfigGCPWithRegions(projectName, orgID, cidrBlock, providerName string) string {
313+
return fmt.Sprintf(`
314+
resource "mongodbatlas_project" "test" {
315+
name = "%s"
316+
org_id = "%s"
317+
}
318+
319+
resource "mongodbatlas_network_container" "test" {
320+
project_id = mongodbatlas_project.test.id
321+
atlas_cidr_block = "%s"
322+
provider_name = "%s"
323+
regions = ["US_EAST_4", "US_WEST_3"]
324+
}
325+
`, projectName, orgID, cidrBlock, providerName)
326+
}

website/docs/d/network_container.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ In addition to all arguments above, the following attributes are exported:
5555
* `network_name` - Name of the Network Peering connection in the Atlas project.
5656
* `vpc_id` - Unique identifier of the project’s VPC.
5757
* `vnet_name` - The name of the Azure VNet. This value is null until you provision an Azure VNet in the container.
58+
* `regions` - Atlas GCP regions where the container resides.
5859

5960

6061
See detailed information for arguments and attributes: [MongoDB API Network Peering Container](https://docs.atlas.mongodb.com/reference/api/vpc-create-container/)

website/docs/d/network_containers.html.markdown

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ In addition to all arguments above, the following attributes are exported:
5858
* `network_name` - Name of the Network Peering connection in the Atlas project.
5959
* `vpc_id` - Unique identifier of the project’s VPC.
6060
* `vnet_name` - The name of the Azure VNet. This value is null until you provision an Azure VNet in the container.
61+
* `regions` - Atlas GCP regions where the container resides.
6162

6263

6364
See detailed information for arguments and attributes: [MongoDB API Network Peering Container](https://docs.atlas.mongodb.com/reference/api/vpc-get-containers-list/)

0 commit comments

Comments
 (0)