Skip to content

Commit 785dee1

Browse files
coderGo93Edgar López
andauthored
INTMDB-163: Wrong order for PrivateLink Endpoint Service and detects unnecessary changes (#388)
* fix: added parameters provider name and region for private endpoint in import and read, and modified docs of how to import * fix: set the other parameters for private link and endpoint to avoid detecting changes while terraform plan after import * fix: added parameter region in state id for import and other func to avoid detecting unnecessary changes * fix: changed the wrong order for private link and endpoint service id in private link service * docs: updated terraform import example for privatelink endpoint Co-authored-by: Edgar López <[email protected]>
1 parent b44afaa commit 785dee1

13 files changed

+89
-45
lines changed

mongodbatlas/data_source_mongodbatlas_privatelink_endpoint_service.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mongodbatlas
33
import (
44
"context"
55
"fmt"
6+
"net/url"
67

78
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
89
"github.com/spf13/cast"
@@ -74,8 +75,9 @@ func dataSourceMongoDBAtlasPrivateEndpointServiceLinkRead(d *schema.ResourceData
7475
privateLinkID := d.Get("private_link_id").(string)
7576
endpointServiceID := d.Get("endpoint_service_id").(string)
7677
providerName := d.Get("provider_name").(string)
78+
encodedEndpointID := url.PathEscape(endpointServiceID)
7779

78-
serviceEndpoint, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, privateLinkID)
80+
serviceEndpoint, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, encodedEndpointID)
7981
if err != nil {
8082
return fmt.Errorf(errorServiceEndpointRead, endpointServiceID, err)
8183
}
@@ -95,7 +97,7 @@ func dataSourceMongoDBAtlasPrivateEndpointServiceLinkRead(d *schema.ResourceData
9597
d.SetId(encodeStateID(map[string]string{
9698
"project_id": projectID,
9799
"private_link_id": privateLinkID,
98-
"endpoint_service_id": serviceEndpoint.ID,
100+
"endpoint_service_id": endpointServiceID,
99101
"provider_name": providerName,
100102
}))
101103

mongodbatlas/data_source_mongodbatlas_privatelink_endpoint_service_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestAccDataSourceMongoDBAtlasPrivateLinkEndpointServiceAWS_basic(t *testing
2424
securityGroupID := os.Getenv("AWS_SECURITY_GROUP_ID")
2525

2626
resource.ParallelTest(t, resource.TestCase{
27-
PreCheck: func() { testAccPreCheck(t); checkAwsEnv(t); checkPeeringEnvAWS(t) },
27+
PreCheck: func() { testAccPreCheck(t); checkAwsEnv(t) },
2828
Providers: testAccProviders,
2929
Steps: []resource.TestStep{
3030
{
@@ -66,8 +66,8 @@ func testAccMongoDBAtlasPrivateLinkEndpointServiceDataSourceConfig(awsAccessKey,
6666
6767
resource "mongodbatlas_privatelink_endpoint_service" "test" {
6868
project_id = mongodbatlas_privatelink_endpoint.test.project_id
69-
private_link_id = aws_vpc_endpoint.ptfe_service.id
70-
endpoint_service_id = mongodbatlas_privatelink_endpoint.test.private_link_id
69+
endpoint_service_id = aws_vpc_endpoint.ptfe_service.id
70+
private_link_id = mongodbatlas_privatelink_endpoint.test.private_link_id
7171
provider_name = "%[4]s"
7272
}
7373

mongodbatlas/resource_mongodbatlas_private_endpoint.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ func resourceMongoDBAtlasPrivateEndpointRead(d *schema.ResourceData, meta interf
118118
ids := decodeStateID(d.Id())
119119
projectID := ids["project_id"]
120120
privateLinkID := ids["private_link_id"]
121+
providerName := ids["provider_name"]
122+
region := ids["region"]
121123

122124
privateEndpoint, _, err := conn.PrivateEndpointsDeprecated.Get(context.Background(), projectID, privateLinkID)
123125
if err != nil {
@@ -144,6 +146,14 @@ func resourceMongoDBAtlasPrivateEndpointRead(d *schema.ResourceData, meta interf
144146
return fmt.Errorf(errorPrivateEndpointsSetting, "status", privateLinkID, err)
145147
}
146148

149+
if err := d.Set("provider_name", providerName); err != nil {
150+
return fmt.Errorf(errorPrivateEndpointsSetting, "provider_name", privateLinkID, err)
151+
}
152+
153+
if err := d.Set("region", region); err != nil {
154+
return fmt.Errorf(errorPrivateEndpointsSetting, "provider_name", privateLinkID, err)
155+
}
156+
147157
return nil
148158
}
149159

@@ -185,13 +195,15 @@ func resourceMongoDBAtlasPrivateEndpointDelete(d *schema.ResourceData, meta inte
185195
func resourceMongoDBAtlasPrivateEndpointImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
186196
conn := meta.(*matlas.Client)
187197

188-
parts := strings.SplitN(d.Id(), "-", 2)
189-
if len(parts) != 2 {
190-
return nil, errors.New("import format error: to import a MongoDB Private Endpoint, use the format {project_id}-{private_link_id}")
198+
parts := strings.Split(d.Id(), "-")
199+
if len(parts) != 6 {
200+
return nil, errors.New("import format error: to import a MongoDB Private Endpoint, use the format {project_id}-{private_link_id}-{provider_name}-{region} ")
191201
}
192202

193203
projectID := parts[0]
194204
privateLinkID := parts[1]
205+
providerName := parts[2]
206+
region := fmt.Sprintf("%s-%s-%s", parts[3], parts[4], parts[5])
195207

196208
privateEndpoint, _, err := conn.PrivateEndpointsDeprecated.Get(context.Background(), projectID, privateLinkID)
197209
if err != nil {
@@ -205,6 +217,8 @@ func resourceMongoDBAtlasPrivateEndpointImportState(d *schema.ResourceData, meta
205217
d.SetId(encodeStateID(map[string]string{
206218
"private_link_id": privateEndpoint.ID,
207219
"project_id": projectID,
220+
"provider_name": providerName,
221+
"region": region,
208222
}))
209223

210224
return []*schema.ResourceData{d}, nil

mongodbatlas/resource_mongodbatlas_private_endpoint_interface_link.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ func resourceMongoDBAtlasPrivateEndpointInterfaceLinkRead(d *schema.ResourceData
121121
return fmt.Errorf(errorInterfaceEndpointSetting, "connection_status", interfaceEndpointID, err)
122122
}
123123

124+
if err := d.Set("private_link_id", privateLinkID); err != nil {
125+
return fmt.Errorf(errorPrivateEndpointsSetting, "private_link_id", privateLinkID, err)
126+
}
127+
128+
if err := d.Set("interface_endpoint_id", interfaceEndpointID); err != nil {
129+
return fmt.Errorf(errorPrivateEndpointsSetting, "interface_endpoint_id", privateLinkID, err)
130+
}
131+
124132
return nil
125133
}
126134

mongodbatlas/resource_mongodbatlas_privatelink_endpoint.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ func resourceMongoDBAtlasPrivateLinkEndpointCreate(d *schema.ResourceData, meta
9393
conn := meta.(*matlas.Client)
9494
projectID := d.Get("project_id").(string)
9595
providerName := d.Get("provider_name").(string)
96+
region := d.Get("region").(string)
9697

9798
request := &matlas.PrivateEndpointConnection{
9899
ProviderName: providerName,
99-
Region: d.Get("region").(string),
100+
Region: region,
100101
}
101102

102103
privateEndpointConn, _, err := conn.PrivateEndpoints.Create(context.Background(), projectID, request)
@@ -123,6 +124,7 @@ func resourceMongoDBAtlasPrivateLinkEndpointCreate(d *schema.ResourceData, meta
123124
"private_link_id": privateEndpointConn.ID,
124125
"project_id": projectID,
125126
"provider_name": providerName,
127+
"region": region,
126128
}))
127129

128130
return resourceMongoDBAtlasPrivateLinkEndpointRead(d, meta)
@@ -135,6 +137,7 @@ func resourceMongoDBAtlasPrivateLinkEndpointRead(d *schema.ResourceData, meta in
135137
projectID := ids["project_id"]
136138
privateLinkID := ids["private_link_id"]
137139
providerName := ids["provider_name"]
140+
region := ids["region"]
138141

139142
privateEndpoint, _, err := conn.PrivateEndpoints.Get(context.Background(), projectID, providerName, privateLinkID)
140143
if err != nil {
@@ -173,6 +176,14 @@ func resourceMongoDBAtlasPrivateLinkEndpointRead(d *schema.ResourceData, meta in
173176
return fmt.Errorf(errorPrivateLinkEndpointsSetting, "status", privateLinkID, err)
174177
}
175178

179+
if err := d.Set("provider_name", providerName); err != nil {
180+
return fmt.Errorf(errorPrivateLinkEndpointsSetting, "provider_name", privateLinkID, err)
181+
}
182+
183+
if err := d.Set("region", region); err != nil {
184+
return fmt.Errorf(errorPrivateLinkEndpointsSetting, "region", privateLinkID, err)
185+
}
186+
176187
return nil
177188
}
178189

@@ -215,14 +226,18 @@ func resourceMongoDBAtlasPrivateLinkEndpointDelete(d *schema.ResourceData, meta
215226
func resourceMongoDBAtlasPrivateLinkEndpointImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
216227
conn := meta.(*matlas.Client)
217228

218-
parts := strings.SplitN(d.Id(), "-", 3)
219-
if len(parts) != 3 {
220-
return nil, errors.New("import format error: to import a MongoDB Private Endpoint, use the format {project_id}-{private_link_id}-{provider_name}")
229+
parts := strings.Split(d.Id(), "-")
230+
if len(parts) != 6 && len(parts) != 4 {
231+
return nil, errors.New("import format error: to import a MongoDB Private Endpoint, use the format {project_id}-{private_link_id}-{provider_name}-{region}")
221232
}
222233

223234
projectID := parts[0]
224235
privateLinkID := parts[1]
225236
providerName := parts[2]
237+
region := parts[3] // If region it's azure or Atlas format like US_EAST_1
238+
if len(parts) == 6 {
239+
region = fmt.Sprintf("%s-%s-%s", parts[3], parts[4], parts[5])
240+
}
226241

227242
privateEndpoint, _, err := conn.PrivateEndpoints.Get(context.Background(), projectID, providerName, privateLinkID)
228243
if err != nil {
@@ -237,6 +252,7 @@ func resourceMongoDBAtlasPrivateLinkEndpointImportState(d *schema.ResourceData,
237252
"private_link_id": privateEndpoint.ID,
238253
"project_id": projectID,
239254
"provider_name": providerName,
255+
"region": region,
240256
}))
241257

242258
return []*schema.ResourceData{d}, nil

mongodbatlas/resource_mongodbatlas_privatelink_endpoint_service.go

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkCreate(d *schema.ResourceData
9595
endpointServiceID := d.Get("endpoint_service_id").(string)
9696

9797
request := &matlas.InterfaceEndpointConnection{
98-
ID: privateLinkID,
98+
ID: endpointServiceID,
9999
PrivateEndpointIPAddress: d.Get("private_endpoint_ip_address").(string),
100100
}
101101

102-
_, _, err := conn.PrivateEndpoints.AddOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, request)
102+
_, _, err := conn.PrivateEndpoints.AddOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, request)
103103
if err != nil {
104104
return fmt.Errorf(errorServiceEndpointAdd, providerName, privateLinkID, err)
105105
}
@@ -136,9 +136,9 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkRead(d *schema.ResourceData,
136136
privateLinkID := ids["private_link_id"]
137137
endpointServiceID := ids["endpoint_service_id"]
138138
providerName := ids["provider_name"]
139-
encodedPrivateLinkID := url.PathEscape(privateLinkID)
139+
encodedEndpointID := url.PathEscape(endpointServiceID)
140140

141-
privateEndpoint, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, encodedPrivateLinkID)
141+
privateEndpoint, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, encodedEndpointID)
142142
if err != nil {
143143
return fmt.Errorf(errorServiceEndpointRead, endpointServiceID, err)
144144
}
@@ -175,6 +175,10 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkRead(d *schema.ResourceData,
175175
return fmt.Errorf(errorEndpointSetting, "endpoint_service_id", endpointServiceID, err)
176176
}
177177

178+
if err := d.Set("private_link_id", privateLinkID); err != nil {
179+
return fmt.Errorf(errorEndpointSetting, "private_link_id", endpointServiceID, err)
180+
}
181+
178182
return nil
179183
}
180184

@@ -186,10 +190,10 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkDelete(d *schema.ResourceData
186190
privateLinkID := ids["private_link_id"]
187191
endpointServiceID := ids["endpoint_service_id"]
188192
providerName := ids["provider_name"]
189-
encodedPrivateLinkID := url.PathEscape(privateLinkID)
193+
encodedEndpointID := url.PathEscape(endpointServiceID)
190194

191195
if endpointServiceID != "" {
192-
_, err := conn.PrivateEndpoints.DeleteOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, encodedPrivateLinkID)
196+
_, err := conn.PrivateEndpoints.DeleteOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, encodedEndpointID)
193197
if err != nil {
194198
return fmt.Errorf(errorEndpointDelete, endpointServiceID, err)
195199
}
@@ -225,9 +229,9 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkImportState(d *schema.Resourc
225229
privateLinkID := parts[1]
226230
endpointServiceID := parts[2]
227231
providerName := parts[3]
228-
encodedPrivateLinkID := url.PathEscape(privateLinkID)
232+
encodedEndpointID := url.PathEscape(endpointServiceID)
229233

230-
_, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, encodedPrivateLinkID)
234+
_, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, encodedEndpointID)
231235
if err != nil {
232236
return nil, fmt.Errorf(errorServiceEndpointRead, endpointServiceID, err)
233237
}
@@ -260,7 +264,7 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkImportState(d *schema.Resourc
260264

261265
func resourceServiceEndpointRefreshFunc(client *matlas.Client, projectID, providerName, privateLinkID, endpointServiceID string) resource.StateRefreshFunc {
262266
return func() (interface{}, string, error) {
263-
i, resp, err := client.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, privateLinkID)
267+
i, resp, err := client.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, endpointServiceID)
264268
if err != nil {
265269
if resp != nil && resp.StatusCode == 404 {
266270
return "", "DELETED", nil

mongodbatlas/resource_mongodbatlas_privatelink_endpoint_service_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package mongodbatlas
33
import (
44
"context"
55
"fmt"
6+
"net/url"
67
"os"
78
"testing"
89

@@ -28,7 +29,7 @@ func TestAccResourceMongoDBAtlasPrivateLinkEndpointServiceAWS_Complete(t *testin
2829
)
2930

3031
resource.Test(t, resource.TestCase{
31-
PreCheck: func() { testAccPreCheck(t); checkAwsEnv(t); checkPeeringEnvAWS(t) },
32+
PreCheck: func() { testAccPreCheck(t); checkAwsEnv(t) },
3233
Providers: testAccProviders,
3334
CheckDestroy: testAccCheckMongoDBAtlasPrivateLinkEndpointServiceDestroy,
3435
Steps: []resource.TestStep{
@@ -117,7 +118,7 @@ func testAccCheckMongoDBAtlasPrivateLinkEndpointServiceExists(resourceName strin
117118

118119
ids := decodeStateID(rs.Primary.ID)
119120

120-
_, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), ids["project_id"], ids["provider_name"], ids["endpoint_service_id"], ids["private_link_id"])
121+
_, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), ids["project_id"], ids["provider_name"], ids["private_link_id"], url.QueryEscape(ids["endpoint_service_id"]))
121122
if err == nil {
122123
return nil
123124
}
@@ -169,8 +170,8 @@ func testAccMongoDBAtlasPrivateLinkEndpointServiceConfigCompleteAWS(awsAccessKey
169170
170171
resource "mongodbatlas_privatelink_endpoint_service" "test" {
171172
project_id = mongodbatlas_privatelink_endpoint.test.project_id
172-
private_link_id = aws_vpc_endpoint.ptfe_service.id
173-
endpoint_service_id = mongodbatlas_privatelink_endpoint.test.private_link_id
173+
endpoint_service_id = aws_vpc_endpoint.ptfe_service.id
174+
private_link_id = mongodbatlas_privatelink_endpoint.test.private_link_id
174175
provider_name = "%[4]s"
175176
}
176177
`, awsAccessKey, awsSecretKey, projectID, providerName, region, vpcID, subnetID, securityGroupID)

mongodbatlas/resource_mongodbatlas_privatelink_endpoint_test.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ func TestAccResourceMongoDBAtlasPrivateLinkEndpointAWS_import(t *testing.T) {
6868
),
6969
},
7070
{
71-
ResourceName: resourceName,
72-
ImportStateIdFunc: testAccCheckMongoDBAtlasPrivateLinkEndpointImportStateIDFunc(resourceName),
73-
ImportState: true,
74-
ImportStateVerify: true,
75-
ImportStateVerifyIgnore: []string{"provider_name", "region"},
71+
ResourceName: resourceName,
72+
ImportStateIdFunc: testAccCheckMongoDBAtlasPrivateLinkEndpointImportStateIDFunc(resourceName),
73+
ImportState: true,
74+
ImportStateVerify: true,
7675
},
7776
},
7877
})
@@ -134,11 +133,10 @@ func TestAccResourceMongoDBAtlasPrivateLinkEndpointAzure_import(t *testing.T) {
134133
),
135134
},
136135
{
137-
ResourceName: resourceName,
138-
ImportStateIdFunc: testAccCheckMongoDBAtlasPrivateLinkEndpointImportStateIDFunc(resourceName),
139-
ImportState: true,
140-
ImportStateVerify: true,
141-
ImportStateVerifyIgnore: []string{"provider_name", "region"},
136+
ResourceName: resourceName,
137+
ImportStateIdFunc: testAccCheckMongoDBAtlasPrivateLinkEndpointImportStateIDFunc(resourceName),
138+
ImportState: true,
139+
ImportStateVerify: true,
142140
},
143141
},
144142
})
@@ -153,7 +151,7 @@ func testAccCheckMongoDBAtlasPrivateLinkEndpointImportStateIDFunc(resourceName s
153151

154152
ids := decodeStateID(rs.Primary.ID)
155153

156-
return fmt.Sprintf("%s-%s-%s", ids["project_id"], ids["private_link_id"], ids["provider_name"]), nil
154+
return fmt.Sprintf("%s-%s-%s-%s", ids["project_id"], ids["private_link_id"], ids["provider_name"], ids["region"]), nil
157155
}
158156
}
159157

website/docs/d/privatelink_endpoint_service.html.markdown

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ resource "aws_vpc_endpoint" "ptfe_service" {
3232
resource "mongodbatlas_privatelink_endpoint_service" "test" {
3333
project_id = "${mongodbatlas_privatelink_endpoint.test.project_id}"
3434
private_link_id = "${mongodbatlas_privatelink_endpoint.test.private_link_id}"
35-
interface_endpoint_id = "${aws_vpc_endpoint.ptfe_service.id}"
35+
endpoint_service_id = "${aws_vpc_endpoint.ptfe_service.id}"
36+
provider_name ="AWS"
3637
}
3738
3839
data "mongodbatlas_privatelink_endpoint_service" "test" {
@@ -45,8 +46,8 @@ data "mongodbatlas_privatelink_endpoint_service" "test" {
4546
## Argument Reference
4647

4748
* `project_id` - (Required) Unique identifier for the project.
48-
* `private_link_id` - (Required) Unique identifier of the `AWS` or `AZURE` PrivateLink connection.
49-
* `endpoint_service_id` - (Required) Unique identifier of the private endpoint service for which you want to create a private endpoint service.
49+
* `private_link_id` - (Required) Unique identifier of the private endpoint service for which you want to retrieve a private endpoint.
50+
* `endpoint_service_id` - (Required) Unique identifier of the `AWS` or `AZURE` resource.
5051
* `provider_name` - (Required) Cloud provider for which you want to create a private endpoint. Atlas accepts `AWS` or `AZURE`.
5152

5253
## Attributes Reference

website/docs/guides/0.8.0-upgrade-guide.html.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ In order to transition from the deprecated resources to the new without disablin
134134
3) Then [import](https://www.terraform.io/docs/commands/import.html) the privatelink information into the new resources, e.g:
135135

136136
```hcl
137-
terraform import mongodbatlas_privatelink_endpoint.test {project_id}-{private_link_id}-{provider_name}
137+
terraform import mongodbatlas_privatelink_endpoint.test {project_id}-{private_link_id}-{provider_name}-{region}
138138
139139
terraform import mongodbatlas_privatelink_endpoint_service.test {project_id}--{private_link_id}--{endpoint_service_id}--{provider_name}
140140
```

0 commit comments

Comments
 (0)