Skip to content

Commit 474f8e4

Browse files
authored
INTMDB-263: Create Resource and Datasource for private_link_endpoint_adl (#640)
* added privatelink_endpoint_service_adl resource and datasource * fix tests * changed go-client method in resource * updated go.mod atlas dep * fix lint issueS * fix * fix * fix documentation
1 parent ab4e946 commit 474f8e4

11 files changed

+1250
-2
lines changed

go.sum

Lines changed: 452 additions & 2 deletions
Large diffs are not rendered by default.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
)
10+
11+
func dataSourceMongoDBAtlasPrivateLinkEndpointServiceADL() *schema.Resource {
12+
return &schema.Resource{
13+
ReadContext: dataSourceMongoDBAtlasPrivateLinkEndpointServiceADLRead,
14+
Schema: map[string]*schema.Schema{
15+
"project_id": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"endpoint_id": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"provider_name": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
"type": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
"comment": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
},
36+
}
37+
}
38+
39+
func dataSourceMongoDBAtlasPrivateLinkEndpointServiceADLRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
40+
// Get client connection.
41+
conn := meta.(*MongoDBClient).Atlas
42+
projectID := d.Get("project_id").(string)
43+
endpointID := d.Get("endpoint_id").(string)
44+
45+
privateLinkResponse, _, err := conn.DataLakes.GetPrivateLinkEndpoint(ctx, projectID, endpointID)
46+
if err != nil {
47+
// case 404
48+
// deleted in the backend case
49+
if strings.Contains(err.Error(), "404") {
50+
d.SetId("")
51+
return nil
52+
}
53+
54+
return diag.Errorf("error getting ADL PrivateLink Endpoint Information: %s", err)
55+
}
56+
57+
if err := d.Set("endpoint_id", privateLinkResponse.EndpointID); err != nil {
58+
return diag.Errorf("error setting `endpoint_id` for endpoint_id (%s): %s", d.Id(), err)
59+
}
60+
61+
if err := d.Set("type", privateLinkResponse.Type); err != nil {
62+
return diag.Errorf("error setting `type` for endpoint_id (%s): %s", d.Id(), err)
63+
}
64+
65+
if err := d.Set("comment", privateLinkResponse.Comment); err != nil {
66+
return diag.Errorf("error setting `comment` for endpoint_id (%s): %s", d.Id(), err)
67+
}
68+
69+
if err := d.Set("provider_name", privateLinkResponse.Provider); err != nil {
70+
return diag.Errorf("error setting `provider_name` for endpoint_id (%s): %s", d.Id(), err)
71+
}
72+
73+
d.SetId(encodeStateID(map[string]string{
74+
"project_id": projectID,
75+
"endpoint_id": privateLinkResponse.EndpointID,
76+
}))
77+
78+
return nil
79+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package mongodbatlas
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccDataSourceMongoDBAtlasPrivateLinkEndpointServiceADL_basic(t *testing.T) {
12+
datasourceName := "data.mongodbatlas_privatelink_endpoint_service_adl.test"
13+
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
14+
endpointID := "vpce-jjg5e24qp93513h03"
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: testAccProviderFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccMongoDBAtlasPrivateLinkEndpointADLDataSourceConfig(projectID, endpointID),
22+
Check: resource.ComposeTestCheckFunc(
23+
testAccCheckMongoDBAtlasPrivateLinkEndpointServiceADLExists(datasourceName),
24+
resource.TestCheckResourceAttr(datasourceName, "endpoint_id", endpointID),
25+
resource.TestCheckResourceAttr(datasourceName, "type", "DATA_LAKE"),
26+
resource.TestCheckResourceAttr(datasourceName, "provider_name", "AWS"),
27+
resource.TestCheckResourceAttr(datasourceName, "comment", "private link adl comment"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
func testAccMongoDBAtlasPrivateLinkEndpointADLDataSourceConfig(projectID, endpointID string) string {
35+
return fmt.Sprintf(`
36+
resource "mongodbatlas_privatelink_endpoint_service_adl" "test" {
37+
project_id = "%[1]s"
38+
endpoint_id = "%[2]s"
39+
comment = "private link adl comment"
40+
type = "DATA_LAKE"
41+
provider_name = "AWS"
42+
}
43+
44+
data "mongodbatlas_privatelink_endpoint_service_adl" "test" {
45+
project_id = mongodbatlas_privatelink_endpoint_service_adl.test.project_id
46+
endpoint_id = mongodbatlas_privatelink_endpoint_service_adl.test.endpoint_id
47+
}
48+
`, projectID, endpointID)
49+
}
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package mongodbatlas
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
9+
matlas "go.mongodb.org/atlas/mongodbatlas"
10+
)
11+
12+
func dataSourceMongoDBAtlasPrivateLinkEndpointsServiceADL() *schema.Resource {
13+
return &schema.Resource{
14+
ReadContext: dataSourceMongoDBAtlasPrivateLinkEndpointsServiceADLRead,
15+
Schema: map[string]*schema.Schema{
16+
"project_id": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
},
20+
"links": {
21+
Type: schema.TypeList,
22+
Computed: true,
23+
Elem: &schema.Resource{
24+
Schema: map[string]*schema.Schema{
25+
"href": {
26+
Type: schema.TypeString,
27+
Computed: true,
28+
},
29+
"rel": {
30+
Type: schema.TypeString,
31+
Computed: true,
32+
},
33+
},
34+
},
35+
},
36+
"results": {
37+
Type: schema.TypeList,
38+
Computed: true,
39+
Elem: &schema.Resource{
40+
Schema: map[string]*schema.Schema{
41+
"endpoint_id": {
42+
Type: schema.TypeString,
43+
Computed: true,
44+
},
45+
"provider_name": {
46+
Type: schema.TypeString,
47+
Computed: true,
48+
},
49+
"type": {
50+
Type: schema.TypeString,
51+
Computed: true,
52+
},
53+
"comment": {
54+
Type: schema.TypeString,
55+
Computed: true,
56+
},
57+
},
58+
},
59+
},
60+
"total_count": {
61+
Type: schema.TypeInt,
62+
Computed: true,
63+
},
64+
},
65+
}
66+
}
67+
68+
func dataSourceMongoDBAtlasPrivateLinkEndpointsServiceADLRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
69+
// Get client connection.
70+
conn := meta.(*MongoDBClient).Atlas
71+
projectID := d.Get("project_id").(string)
72+
73+
privateLinkEndpoints, _, err := conn.DataLakes.ListPrivateLinkEndpoint(ctx, projectID)
74+
if err != nil {
75+
return diag.Errorf("error getting ADL PrivateLink Endpoints Information: %s", err)
76+
}
77+
78+
if err := d.Set("links", flattenADLPrivateEndpointLinks(privateLinkEndpoints.Links)); err != nil {
79+
return diag.Errorf("error setting `results`: %s", err)
80+
}
81+
82+
if err := d.Set("results", flattenADLPrivateLinkEndpoints(privateLinkEndpoints.Results)); err != nil {
83+
return diag.Errorf("error setting `results`: %s", err)
84+
}
85+
86+
if err := d.Set("total_count", privateLinkEndpoints.TotalCount); err != nil {
87+
return diag.Errorf("error setting `total_count`: %s", err)
88+
}
89+
90+
d.SetId(resource.UniqueId())
91+
92+
return nil
93+
}
94+
95+
func flattenADLPrivateEndpointLinks(links []*matlas.Link) []map[string]interface{} {
96+
linksList := make([]map[string]interface{}, 0)
97+
98+
for _, link := range links {
99+
mLink := map[string]interface{}{
100+
"href": link.Href,
101+
"rel": link.Rel,
102+
}
103+
linksList = append(linksList, mLink)
104+
}
105+
106+
return linksList
107+
}
108+
109+
func flattenADLPrivateLinkEndpoints(privateLinks []*matlas.PrivateLinkEndpointDataLake) []map[string]interface{} {
110+
var results []map[string]interface{}
111+
112+
if len(privateLinks) == 0 {
113+
return results
114+
}
115+
116+
results = make([]map[string]interface{}, len(privateLinks))
117+
118+
for k, privateLink := range privateLinks {
119+
results[k] = map[string]interface{}{
120+
"endpoint_id": privateLink.EndpointID,
121+
"type": privateLink.Type,
122+
"provider_name": privateLink.Provider,
123+
"comment": privateLink.Comment,
124+
}
125+
}
126+
127+
return results
128+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package mongodbatlas
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
)
10+
11+
func TestAccDataSourceMongoDBAtlasPrivateLinkEndpointsServiceADL_basic(t *testing.T) {
12+
datasourceName := "data.mongodbatlas_privatelink_endpoints_service_adl.test"
13+
projectID := os.Getenv("MONGODB_ATLAS_PROJECT_ID")
14+
endpointID := "vpce-jjg5e24qp93513h03"
15+
16+
resource.ParallelTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
ProviderFactories: testAccProviderFactories,
19+
Steps: []resource.TestStep{
20+
{
21+
Config: testAccMongoDBAtlasPrivateLinkEndpointsADLDataSourceConfig(projectID, endpointID),
22+
Check: resource.ComposeTestCheckFunc(
23+
resource.TestCheckResourceAttrSet(datasourceName, "project_id"),
24+
resource.TestCheckResourceAttrSet(datasourceName, "links.#"),
25+
resource.TestCheckResourceAttrSet(datasourceName, "results.#"),
26+
resource.TestCheckResourceAttrSet(datasourceName, "results.0.endpoint_id"),
27+
resource.TestCheckResourceAttrSet(datasourceName, "results.0.type"),
28+
resource.TestCheckResourceAttrSet(datasourceName, "results.0.provider_name"),
29+
resource.TestCheckResourceAttrSet(datasourceName, "results.0.comment"),
30+
resource.TestCheckResourceAttr(datasourceName, "total_count", "1"),
31+
),
32+
},
33+
},
34+
})
35+
}
36+
37+
func testAccMongoDBAtlasPrivateLinkEndpointsADLDataSourceConfig(projectID, endpointID string) string {
38+
return fmt.Sprintf(`
39+
resource "mongodbatlas_privatelink_endpoint_service_adl" "test" {
40+
project_id = "%[1]s"
41+
endpoint_id = "%[2]s"
42+
comment = "private link adl comment"
43+
type = "DATA_LAKE"
44+
provider_name = "AWS"
45+
}
46+
47+
data "mongodbatlas_privatelink_endpoints_service_adl" "test" {
48+
project_id = mongodbatlas_privatelink_endpoint_service_adl.test.project_id
49+
}
50+
`, projectID, endpointID)
51+
}

mongodbatlas/provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ func getDataSourcesMap() map[string]*schema.Resource {
9898
"mongodbatlas_x509_authentication_database_user": dataSourceMongoDBAtlasX509AuthDBUser(),
9999
"mongodbatlas_privatelink_endpoint": dataSourceMongoDBAtlasPrivateLinkEndpoint(),
100100
"mongodbatlas_privatelink_endpoint_service": dataSourceMongoDBAtlasPrivateEndpointServiceLink(),
101+
"mongodbatlas_privatelink_endpoint_service_adl": dataSourceMongoDBAtlasPrivateLinkEndpointServiceADL(),
102+
"mongodbatlas_privatelink_endpoints_service_adl": dataSourceMongoDBAtlasPrivateLinkEndpointsServiceADL(),
101103
"mongodbatlas_cloud_provider_snapshot_backup_policy": dataSourceMongoDBAtlasCloudProviderSnapshotBackupPolicy(),
102104
"mongodbatlas_cloud_backup_schedule": dataSourceMongoDBAtlasCloudBackupSchedule(),
103105
"mongodbatlas_third_party_integrations": dataSourceMongoDBAtlasThirdPartyIntegrations(),
@@ -148,6 +150,7 @@ func getResourcesMap() map[string]*schema.Resource {
148150
"mongodbatlas_x509_authentication_database_user": resourceMongoDBAtlasX509AuthDBUser(),
149151
"mongodbatlas_privatelink_endpoint": resourceMongoDBAtlasPrivateLinkEndpoint(),
150152
"mongodbatlas_privatelink_endpoint_service": resourceMongoDBAtlasPrivateEndpointServiceLink(),
153+
"mongodbatlas_privatelink_endpoint_service_adl": resourceMongoDBAtlasPrivateLinkEndpointServiceADL(),
151154
"mongodbatlas_cloud_provider_snapshot_backup_policy": resourceMongoDBAtlasCloudProviderSnapshotBackupPolicy(),
152155
"mongodbatlas_third_party_integration": resourceMongoDBAtlasThirdPartyIntegration(),
153156
"mongodbatlas_project_ip_access_list": resourceMongoDBAtlasProjectIPAccessList(),

0 commit comments

Comments
 (0)