Skip to content

Commit

Permalink
INTMDB-185: Added parameter regions for GCP network container (#418)
Browse files Browse the repository at this point in the history
* 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 <edgarlopez@pop-os.localdomain>
  • Loading branch information
coderGo93 and Edgar López committed Mar 9, 2021
1 parent bc3c9ce commit 98097d2
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 1 deletion.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -12,5 +12,5 @@ require (
github.com/spf13/cast v1.3.1
github.com/terraform-providers/terraform-provider-aws v1.60.1-0.20200518153306-40099de47e37
github.com/terraform-providers/terraform-provider-google v1.20.1-0.20200518165017-1dd21651c496
go.mongodb.org/atlas v0.7.2
go.mongodb.org/atlas v0.7.3-0.20210302081736-7ccf7e1f4351
)
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -731,6 +731,8 @@ go.etcd.io/bbolt v1.3.3/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg=
go.mongodb.org/atlas v0.7.2 h1:wB3+hP71t3mK+JOSrjBFbrzb5MsZRzDtZlpEKp58KK0=
go.mongodb.org/atlas v0.7.2/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
go.mongodb.org/atlas v0.7.3-0.20210302081736-7ccf7e1f4351 h1:3NuN2K6VtDuDNfAKX/4j6H4pLaT8LIkvfXD3F7Oy30c=
go.mongodb.org/atlas v0.7.3-0.20210302081736-7ccf7e1f4351/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
Expand Down
11 changes: 11 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_network_container.go
Expand Up @@ -61,6 +61,13 @@ func dataSourceMongoDBAtlasNetworkContainer() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"regions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand Down Expand Up @@ -124,6 +131,10 @@ func dataSourceMongoDBAtlasNetworkContainerRead(d *schema.ResourceData, meta int
return fmt.Errorf("error setting `vnet_name` for Network Container (%s): %s", d.Id(), err)
}

if err = d.Set("regions", container.Regions); err != nil {
return fmt.Errorf("error setting `regions` for Network Container (%s): %s", d.Id(), err)
}

d.SetId(container.ID)

return nil
Expand Down
54 changes: 54 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_network_container_test.go
Expand Up @@ -45,6 +45,39 @@ func TestAccDataSourceMongoDBAtlasNetworkContainer_basic(t *testing.T) {
})
}

func TestAccDataSourceMongoDBAtlasNetworkContainer_WithGCPRegions(t *testing.T) {
var container matlas.Container

randInt := acctest.RandIntRange(0, 255)

resourceName := "mongodbatlas_network_container.test_ds"
cidrBlock := fmt.Sprintf("10.%d.0.0/21", randInt)
dataSourceName := "data.mongodbatlas_network_container.test_ds"
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName := acctest.RandomWithPrefix("test-acc")

providerName := "GCP"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasNetworkContainerDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasNetworkContainerDSWithGCPRegionsConfig(projectName, orgID, cidrBlock),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasNetworkContainerExists(resourceName, &container),
testAccCheckMongoDBAtlasNetworkContainerAttributes(&container, providerName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
resource.TestCheckResourceAttrSet(resourceName, "provisioned"),
resource.TestCheckResourceAttrSet(dataSourceName, "project_id"),
),
},
},
})
}

func testAccMongoDBAtlasNetworkContainerDSConfig(projectName, orgID, cidrBlock string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
Expand All @@ -65,3 +98,24 @@ func testAccMongoDBAtlasNetworkContainerDSConfig(projectName, orgID, cidrBlock s
}
`, projectName, orgID, cidrBlock)
}

func testAccMongoDBAtlasNetworkContainerDSWithGCPRegionsConfig(projectName, orgID, cidrBlock string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = "%s"
org_id = "%s"
}
resource "mongodbatlas_network_container" "test_ds" {
project_id = "${mongodbatlas_project.test.id}"
atlas_cidr_block = "%s"
provider_name = "GCP"
regions = ["US_EAST_4", "US_WEST_3"]
}
data "mongodbatlas_network_container" "test_ds" {
project_id = mongodbatlas_network_container.test_ds.project_id
container_id = mongodbatlas_network_container.test_ds.container_id
}
`, projectName, orgID, cidrBlock)
}
8 changes: 8 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_network_containers.go
Expand Up @@ -70,6 +70,13 @@ func dataSourceMongoDBAtlasNetworkContainers() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"regions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
Expand Down Expand Up @@ -118,6 +125,7 @@ func flattenNetworkContainers(containers []matlas.Container) []map[string]interf
"network_name": containers[i].NetworkName,
"vpc_id": containers[i].VPCID,
"vnet_name": containers[i].VNetName,
"regions": containers[i].Regions,
}
}
}
Expand Down
57 changes: 57 additions & 0 deletions mongodbatlas/data_source_mongodbatlas_network_containers_test.go
Expand Up @@ -46,6 +46,42 @@ func TestAccDataSourceMongoDBAtlasNetworkContainers_basic(t *testing.T) {
},
})
}
func TestAccDataSourceMongoDBAtlasNetworkContainers_WithGCPRegions(t *testing.T) {
var container matlas.Container

randInt := acctest.RandIntRange(0, 255)

resourceName := "mongodbatlas_network_container.test"
cidrBlock := fmt.Sprintf("10.%d.0.0/21", randInt)
dataSourceName := "data.mongodbatlas_network_containers.test"
orgID := os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName := acctest.RandomWithPrefix("test-acc")

providerName := "GCP"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasNetworkContainerDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasNetworkContainersDSWithGCPRegionsConfig(projectName, orgID, cidrBlock),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasNetworkContainerExists(resourceName, &container),
testAccCheckMongoDBAtlasNetworkContainerAttributes(&container, providerName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
resource.TestCheckResourceAttrSet(resourceName, "provisioned"),
resource.TestCheckResourceAttrSet(dataSourceName, "results.#"),
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.id"),
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.atlas_cidr_block"),
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.provider_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "results.0.provisioned"),
),
},
},
})
}

func testAccMongoDBAtlasNetworkContainersDSConfig(projectName, orgID, cidrBlock string) string {
return fmt.Sprintf(`
Expand All @@ -67,3 +103,24 @@ func testAccMongoDBAtlasNetworkContainersDSConfig(projectName, orgID, cidrBlock
}
`, projectName, orgID, cidrBlock)
}

func testAccMongoDBAtlasNetworkContainersDSWithGCPRegionsConfig(projectName, orgID, cidrBlock string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = "%s"
org_id = "%s"
}
resource "mongodbatlas_network_container" "test" {
project_id = mongodbatlas_project.test.id
atlas_cidr_block = "%s"
provider_name = "GCP"
regions = ["US_EAST_4", "US_WEST_3"]
}
data "mongodbatlas_network_containers" "test" {
project_id = mongodbatlas_network_container.test.project_id
provider_name = "GCP"
}
`, projectName, orgID, cidrBlock)
}
20 changes: 20 additions & 0 deletions mongodbatlas/resource_mongodbatlas_network_container.go
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/spf13/cast"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
Expand Down Expand Up @@ -86,6 +88,13 @@ func resourceMongoDBAtlasNetworkContainer() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"regions": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand Down Expand Up @@ -119,6 +128,13 @@ func resourceMongoDBAtlasNetworkContainerCreate(d *schema.ResourceData, meta int
containerRequest.Region = region
}

if providerName == "GCP" {
regions, ok := d.GetOk("regions")
if ok {
containerRequest.Regions = cast.ToStringSlice(regions)
}
}

container, _, err := conn.Containers.Create(context.Background(), projectID, containerRequest)
if err != nil {
return fmt.Errorf(errorContainterCreate, err)
Expand Down Expand Up @@ -188,6 +204,10 @@ func resourceMongoDBAtlasNetworkContainerRead(d *schema.ResourceData, meta inter
return fmt.Errorf("error setting `container_id` for Network Container (%s): %s", containerID, err)
}

if err = d.Set("regions", container.Regions); err != nil {
return fmt.Errorf("error setting `regions` for Network Container (%s): %s", containerID, err)
}

return nil
}

Expand Down
46 changes: 46 additions & 0 deletions mongodbatlas/resource_mongodbatlas_network_container_test.go
Expand Up @@ -139,6 +139,36 @@ func TestAccResourceMongoDBAtlasNetworkContainer_basicGCP(t *testing.T) {
})
}

func TestAccResourceMongoDBAtlasNetworkContainer_WithRegionsGCP(t *testing.T) {
var (
container matlas.Container
randInt = acctest.RandIntRange(0, 255)
resourceName = "mongodbatlas_network_container.test"
cidrBlock = fmt.Sprintf("10.%d.0.0/21", randInt)
providerName = "GCP"
orgID = os.Getenv("MONGODB_ATLAS_ORG_ID")
projectName = acctest.RandomWithPrefix("test-acc")
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasNetworkContainerDestroy,
Steps: []resource.TestStep{
{
Config: testAccMongoDBAtlasNetworkContainerConfigGCPWithRegions(projectName, orgID, cidrBlock, providerName),
Check: resource.ComposeTestCheckFunc(
testAccCheckMongoDBAtlasNetworkContainerExists(resourceName, &container),
testAccCheckMongoDBAtlasNetworkContainerAttributes(&container, providerName),
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
resource.TestCheckResourceAttr(resourceName, "provider_name", providerName),
resource.TestCheckResourceAttrSet(resourceName, "provisioned"),
),
},
},
})
}

func TestAccResourceMongoDBAtlasNetworkContainer_importBasic(t *testing.T) {
var (
randInt = acctest.RandIntRange(0, 255)
Expand Down Expand Up @@ -278,3 +308,19 @@ func testAccMongoDBAtlasNetworkContainerConfigGCP(projectName, orgID, cidrBlock,
}
`, projectName, orgID, cidrBlock, providerName)
}

func testAccMongoDBAtlasNetworkContainerConfigGCPWithRegions(projectName, orgID, cidrBlock, providerName string) string {
return fmt.Sprintf(`
resource "mongodbatlas_project" "test" {
name = "%s"
org_id = "%s"
}
resource "mongodbatlas_network_container" "test" {
project_id = mongodbatlas_project.test.id
atlas_cidr_block = "%s"
provider_name = "%s"
regions = ["US_EAST_4", "US_WEST_3"]
}
`, projectName, orgID, cidrBlock, providerName)
}
1 change: 1 addition & 0 deletions website/docs/d/network_container.html.markdown
Expand Up @@ -55,6 +55,7 @@ In addition to all arguments above, the following attributes are exported:
* `network_name` - Name of the Network Peering connection in the Atlas project.
* `vpc_id` - Unique identifier of the project’s VPC.
* `vnet_name` - The name of the Azure VNet. This value is null until you provision an Azure VNet in the container.
* `regions` - Atlas GCP regions where the container resides.


See detailed information for arguments and attributes: [MongoDB API Network Peering Container](https://docs.atlas.mongodb.com/reference/api/vpc-create-container/)
1 change: 1 addition & 0 deletions website/docs/d/network_containers.html.markdown
Expand Up @@ -58,6 +58,7 @@ In addition to all arguments above, the following attributes are exported:
* `network_name` - Name of the Network Peering connection in the Atlas project.
* `vpc_id` - Unique identifier of the project’s VPC.
* `vnet_name` - The name of the Azure VNet. This value is null until you provision an Azure VNet in the container.
* `regions` - Atlas GCP regions where the container resides.


See detailed information for arguments and attributes: [MongoDB API Network Peering Container](https://docs.atlas.mongodb.com/reference/api/vpc-get-containers-list/)
2 changes: 2 additions & 0 deletions website/docs/r/network_container.html.markdown
Expand Up @@ -40,6 +40,7 @@ resource "mongodbatlas_network_container" "test" {
project_id = "<YOUR-PROJECT-ID>"
atlas_cidr_block = "10.8.0.0/21"
provider_name = "GCP"
regions = ["US_EAST_4", "US_WEST_3"]
}
```

Expand Down Expand Up @@ -69,6 +70,7 @@ resource "mongodbatlas_network_container" "test" {
* `provider_name` - (Required GCP and AZURE, Optional but recommended for AWS) Cloud provider for this Network Peering connection. Accepted values are GCP, AWS, AZURE. If omitted, Atlas sets this parameter to AWS.
* `region_name` - (Required AWS only) The Atlas AWS region name for where this container will exist, see the reference list for Atlas AWS region names [AWS](https://docs.atlas.mongodb.com/reference/amazon-aws/).
* `region` - (Required AZURE only) Atlas region where the container resides, see the reference list for Atlas Azure region names [Azure](https://docs.atlas.mongodb.com/reference/microsoft-azure/).
* `regions` - (Optional GCP only) Atlas regions where the container resides. Provide this field only if you provide an `atlas_cidr_block` smaller than `/18`. [GCP Regions values](https://docs.atlas.mongodb.com/reference/api/vpc-create-container/#request-body-parameters).



Expand Down

0 comments on commit 98097d2

Please sign in to comment.