Skip to content

Commit 378c2c6

Browse files
coderGo93Edgar López
andauthored
New resource and datasource for Project IP Access list (#332)
* added validation when the github actions should run if it's in branch * chore:updated vendor * feat: added resource and datasource of access list * test: added tests for resource and datasources of access list * docs: added docs for resource and datasource of access list * refactor: made changes suggested by melissa * fix: fixes linter error about capitalization Co-authored-by: Edgar López <[email protected]>
1 parent 5ff01d4 commit 378c2c6

9 files changed

+1104
-3
lines changed

.github/workflows/automated-test-acceptances.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2020

2121
acceptances-tests:
22-
if: ${{ github.event.label.name == 'run-testacc' }}
22+
if: ${{ github.event.label.name == 'run-testacc' || github.ref == 'refs/heads/master' }}
2323
needs: [ authorize ]
2424
runs-on: ubuntu-latest
2525
steps:

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,6 @@ go.mongodb.org/atlas v0.4.1-0.20200903102338-049d0778b833 h1:gH8Ih2OacuB6qVitO+w
613613
go.mongodb.org/atlas v0.4.1-0.20200903102338-049d0778b833/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
614614
go.mongodb.org/atlas v0.4.1-0.20200916170654-ac3833accfa2 h1:qjEP4bC8yTi57jBYHtSSA8gzPN4vJl3XG23YBMXCgUg=
615615
go.mongodb.org/atlas v0.4.1-0.20200916170654-ac3833accfa2/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
616-
go.mongodb.org/atlas v0.5.1-0.20201005200951-cefc31adb4ca h1:aZlmsmzOW8kYabsoBdG6BAeSOP2QShQds+6gSoMnBcg=
617-
go.mongodb.org/atlas v0.5.1-0.20201005200951-cefc31adb4ca/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
618616
go.mongodb.org/atlas v0.5.1-0.20201007214134-b315fe7503d2 h1:b4Ng7d2sCSgYKwLMOetbwLcPE732SiBnJqH5rQrhZOs=
619617
go.mongodb.org/atlas v0.5.1-0.20201007214134-b315fe7503d2/go.mod h1:CIaBeO8GLHhtYLw7xSSXsw7N90Z4MFY87Oy9qcPyuEs=
620618
go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU=
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package mongodbatlas
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"errors"
7+
"fmt"
8+
"net"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
12+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
13+
matlas "go.mongodb.org/atlas/mongodbatlas"
14+
)
15+
16+
func dataSourceMongoDBAtlasProjectIPAccessList() *schema.Resource {
17+
return &schema.Resource{
18+
Read: dataSourceMongoDBAtlasProjectIPAccessListRead,
19+
Schema: map[string]*schema.Schema{
20+
"project_id": {
21+
Type: schema.TypeString,
22+
Required: true,
23+
},
24+
"cidr_block": {
25+
Type: schema.TypeString,
26+
Optional: true,
27+
Computed: true,
28+
ConflictsWith: []string{"aws_security_group", "ip_address"},
29+
ValidateFunc: func(i interface{}, k string) (s []string, es []error) {
30+
v, ok := i.(string)
31+
if !ok {
32+
es = append(es, fmt.Errorf("expected type of %s to be string", k))
33+
return
34+
}
35+
36+
_, ipnet, err := net.ParseCIDR(v)
37+
if err != nil {
38+
es = append(es, fmt.Errorf("expected %s to contain a valid CIDR, got: %s with err: %s", k, v, err))
39+
return
40+
}
41+
42+
if ipnet == nil || v != ipnet.String() {
43+
es = append(es, fmt.Errorf("expected %s to contain a valid network CIDR, expected %s, got %s", k, ipnet, v))
44+
return
45+
}
46+
return
47+
},
48+
},
49+
"ip_address": {
50+
Type: schema.TypeString,
51+
Optional: true,
52+
Computed: true,
53+
ConflictsWith: []string{"aws_security_group", "cidr_block"},
54+
ValidateFunc: validation.IsIPAddress,
55+
},
56+
"aws_security_group": {
57+
Type: schema.TypeString,
58+
Optional: true,
59+
Computed: true,
60+
ConflictsWith: []string{"ip_address", "cidr_block"},
61+
},
62+
"comment": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
},
66+
},
67+
}
68+
}
69+
70+
func dataSourceMongoDBAtlasProjectIPAccessListRead(d *schema.ResourceData, meta interface{}) error {
71+
conn := meta.(*matlas.Client)
72+
projectID := d.Get("project_id").(string)
73+
cidrBlock := d.Get("cidr_block").(string)
74+
ipAddress := d.Get("ip_address").(string)
75+
awsSecurityGroup := d.Get("aws_security_group").(string)
76+
77+
if cidrBlock == "" && ipAddress == "" && awsSecurityGroup == "" {
78+
return errors.New("cidr_block, ip_address or aws_security_group needs to contain a value")
79+
}
80+
var entry bytes.Buffer
81+
82+
entry.WriteString(cidrBlock)
83+
entry.WriteString(ipAddress)
84+
entry.WriteString(awsSecurityGroup)
85+
86+
accessList, _, err := conn.ProjectIPAccessList.Get(context.Background(), projectID, entry.String())
87+
if err != nil {
88+
return fmt.Errorf("error getting access list information: %s", err)
89+
}
90+
91+
if err := d.Set("cidr_block", accessList.CIDRBlock); err != nil {
92+
return fmt.Errorf("error setting `cidr_block` for the project access list: %s", err)
93+
}
94+
if err := d.Set("ip_address", accessList.IPAddress); err != nil {
95+
return fmt.Errorf("error setting `ip_address` for the project access list: %s", err)
96+
}
97+
if err := d.Set("aws_security_group", accessList.AwsSecurityGroup); err != nil {
98+
return fmt.Errorf("error setting `aws_security_group` for the project access list: %s", err)
99+
}
100+
if err := d.Set("comment", accessList.Comment); err != nil {
101+
return fmt.Errorf("error setting `comment` for the project access list: %s", err)
102+
}
103+
104+
d.SetId(resource.UniqueId())
105+
106+
return nil
107+
}
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package mongodbatlas
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+
)
11+
12+
func TestAccDataMongoDBAtlasProjectIPAccessList_SettingIPAddress(t *testing.T) {
13+
resourceName := "mongodbatlas_project_ip_access_list.test"
14+
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
15+
ipAddress := fmt.Sprintf("179.154.226.%d", acctest.RandIntRange(0, 255))
16+
comment := fmt.Sprintf("TestAcc for ipAddress (%s)", ipAddress)
17+
18+
resource.Test(t, resource.TestCase{
19+
PreCheck: func() { testAccPreCheck(t) },
20+
Providers: testAccProviders,
21+
Steps: []resource.TestStep{
22+
{
23+
Config: testAccDataMongoDBAtlasProjectIPAccessListConfigSettingIPAddress(projectID, ipAddress, comment),
24+
Check: resource.ComposeTestCheckFunc(
25+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
26+
resource.TestCheckResourceAttrSet(resourceName, "ip_address"),
27+
resource.TestCheckResourceAttrSet(resourceName, "comment"),
28+
29+
resource.TestCheckResourceAttr(resourceName, "project_id", projectID),
30+
resource.TestCheckResourceAttr(resourceName, "ip_address", ipAddress),
31+
resource.TestCheckResourceAttr(resourceName, "comment", comment),
32+
),
33+
},
34+
},
35+
})
36+
}
37+
38+
func TestAccDataMongoDBAtlasProjectIPAccessList_SettingCIDRBlock(t *testing.T) {
39+
resourceName := "mongodbatlas_project_ip_access_list.test"
40+
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
41+
cidrBlock := fmt.Sprintf("179.154.226.%d/32", acctest.RandIntRange(0, 255))
42+
comment := fmt.Sprintf("TestAcc for cidrBlock (%s)", cidrBlock)
43+
44+
resource.Test(t, resource.TestCase{
45+
PreCheck: func() { testAccPreCheck(t) },
46+
Providers: testAccProviders,
47+
Steps: []resource.TestStep{
48+
{
49+
Config: testAccDataMongoDBAtlasProjectIPAccessListConfigSettingCIDRBlock(projectID, cidrBlock, comment),
50+
Check: resource.ComposeTestCheckFunc(
51+
testAccCheckMongoDBAtlasProjectIPAccessListExists(resourceName),
52+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
53+
resource.TestCheckResourceAttrSet(resourceName, "cidr_block"),
54+
resource.TestCheckResourceAttrSet(resourceName, "comment"),
55+
56+
resource.TestCheckResourceAttr(resourceName, "project_id", projectID),
57+
resource.TestCheckResourceAttr(resourceName, "cidr_block", cidrBlock),
58+
resource.TestCheckResourceAttr(resourceName, "comment", comment),
59+
),
60+
},
61+
},
62+
})
63+
}
64+
65+
func TestAccDataMongoDBAtlasProjectIPAccessList_SettingAWSSecurityGroup(t *testing.T) {
66+
SkipTestExtCred(t)
67+
resourceName := "mongodbatlas_project_ip_access_list.test"
68+
vpcID := os.Getenv("AWS_VPC_ID")
69+
vpcCIDRBlock := os.Getenv("AWS_VPC_CIDR_BLOCK")
70+
awsAccountID := os.Getenv("AWS_ACCOUNT_ID")
71+
awsRegion := os.Getenv("AWS_REGION")
72+
providerName := "AWS"
73+
74+
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
75+
awsSGroup := os.Getenv("AWS_SECURITY_GROUP_ID")
76+
comment := fmt.Sprintf("TestAcc for awsSecurityGroup (%s)", awsSGroup)
77+
78+
resource.Test(t, resource.TestCase{
79+
PreCheck: func() { testAccPreCheck(t) },
80+
Providers: testAccProviders,
81+
Steps: []resource.TestStep{
82+
{
83+
Config: testAccDataMongoDBAtlasProjectIPAccessListConfigSettingAWSSecurityGroup(projectID, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion, awsSGroup, comment),
84+
Check: resource.ComposeTestCheckFunc(
85+
testAccCheckMongoDBAtlasProjectIPAccessListExists(resourceName),
86+
resource.TestCheckResourceAttrSet(resourceName, "project_id"),
87+
resource.TestCheckResourceAttrSet(resourceName, "aws_security_group"),
88+
resource.TestCheckResourceAttrSet(resourceName, "comment"),
89+
90+
resource.TestCheckResourceAttr(resourceName, "project_id", projectID),
91+
resource.TestCheckResourceAttr(resourceName, "aws_security_group", awsSGroup),
92+
resource.TestCheckResourceAttr(resourceName, "comment", comment),
93+
),
94+
},
95+
},
96+
})
97+
}
98+
99+
func testAccDataMongoDBAtlasProjectIPAccessListConfigSettingIPAddress(projectID, ipAddress, comment string) string {
100+
return fmt.Sprintf(`
101+
resource "mongodbatlas_project_ip_access_list" "test" {
102+
project_id = "%s"
103+
ip_address = "%s"
104+
comment = "%s"
105+
}
106+
107+
data "mongodbatlas_project_ip_access_list" "test" {
108+
project_id = mongodbatlas_project_ip_access_list.test.project_id
109+
ip_address = mongodbatlas_project_ip_access_list.test.ip_address
110+
}
111+
`, projectID, ipAddress, comment)
112+
}
113+
114+
func testAccDataMongoDBAtlasProjectIPAccessListConfigSettingCIDRBlock(projectID, cidrBlock, comment string) string {
115+
return fmt.Sprintf(`
116+
resource "mongodbatlas_project_ip_access_list" "test" {
117+
project_id = "%s"
118+
cidr_block = "%s"
119+
comment = "%s"
120+
}
121+
data "mongodbatlas_project_ip_access_list" "test" {
122+
project_id = mongodbatlas_project_ip_access_list.test.project_id
123+
cidr_block = mongodbatlas_project_ip_access_list.test.cidr_block
124+
}
125+
`, projectID, cidrBlock, comment)
126+
}
127+
128+
func testAccDataMongoDBAtlasProjectIPAccessListConfigSettingAWSSecurityGroup(projectID, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion, awsSGroup, comment string) string {
129+
return fmt.Sprintf(`
130+
resource "mongodbatlas_network_container" "test" {
131+
project_id = "%[1]s"
132+
atlas_cidr_block = "192.168.208.0/21"
133+
provider_name = "%[2]s"
134+
region_name = "%[6]s"
135+
}
136+
137+
resource "mongodbatlas_network_peering" "test" {
138+
accepter_region_name = lower(replace("%[6]s", "_", "-"))
139+
project_id = "%[1]s"
140+
container_id = mongodbatlas_network_container.test.container_id
141+
provider_name = "%[2]s"
142+
route_table_cidr_block = "%[5]s"
143+
vpc_id = "%[3]s"
144+
aws_account_id = "%[4]s"
145+
}
146+
147+
resource "mongodbatlas_project_ip_access_list" "test" {
148+
project_id = "%[1]s"
149+
aws_security_group = "%[7]s"
150+
comment = "%[8]s"
151+
152+
depends_on = ["mongodbatlas_network_peering.test"]
153+
}
154+
155+
data "mongodbatlas_project_ip_access_list" "test" {
156+
project_id = mongodbatlas_project_ip_access_list.test.project_id
157+
aws_security_group = mongodbatlas_project_ip_access_list.test.aws_security_group
158+
}
159+
`, projectID, providerName, vpcID, awsAccountID, vpcCIDRBlock, awsRegion, awsSGroup, comment)
160+
}

mongodbatlas/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ func Provider() terraform.ResourceProvider {
6262
"mongodbatlas_cloud_provider_snapshot_backup_policy": dataSourceMongoDBAtlasCloudProviderSnapshotBackupPolicy(),
6363
"mongodbatlas_third_party_integrations": dataSourceMongoDBAtlasThirdPartyIntegrations(),
6464
"mongodbatlas_third_party_integration": dataSourceMongoDBAtlasThirdPartyIntegration(),
65+
"mongodbatlas_project_ip_access_list": dataSourceMongoDBAtlasProjectIPAccessList(),
6566
},
6667

6768
ResourcesMap: map[string]*schema.Resource{
@@ -87,6 +88,7 @@ func Provider() terraform.ResourceProvider {
8788
"mongodbatlas_private_endpoint_interface_link": resourceMongoDBAtlasPrivateEndpointInterfaceLink(),
8889
"mongodbatlas_cloud_provider_snapshot_backup_policy": resourceMongoDBAtlasCloudProviderSnapshotBackupPolicy(),
8990
"mongodbatlas_third_party_integration": resourceMongoDBAtlasThirdPartyIntegration(),
91+
"mongodbatlas_project_ip_access_list": resourceMongoDBAtlasProjectIPAccessList(),
9092
},
9193

9294
ConfigureFunc: providerConfigure,

0 commit comments

Comments
 (0)