Skip to content

Commit

Permalink
fix: changed the wrong order for private link and endpoint service id…
Browse files Browse the repository at this point in the history
… in private link service
  • Loading branch information
Edgar López committed Jan 23, 2021
1 parent 3965b3e commit 712bd37
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongodbatlas
import (
"context"
"fmt"
"net/url"

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

serviceEndpoint, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, privateLinkID)
serviceEndpoint, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, encodedEndpointID)
if err != nil {
return fmt.Errorf(errorServiceEndpointRead, endpointServiceID, err)
}
Expand All @@ -95,7 +97,7 @@ func dataSourceMongoDBAtlasPrivateEndpointServiceLinkRead(d *schema.ResourceData
d.SetId(encodeStateID(map[string]string{
"project_id": projectID,
"private_link_id": privateLinkID,
"endpoint_service_id": serviceEndpoint.ID,
"endpoint_service_id": endpointServiceID,
"provider_name": providerName,
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAccDataSourceMongoDBAtlasPrivateLinkEndpointServiceAWS_basic(t *testing
securityGroupID := os.Getenv("AWS_SECURITY_GROUP_ID")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); checkAwsEnv(t); checkPeeringEnvAWS(t) },
PreCheck: func() { testAccPreCheck(t); checkAwsEnv(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Expand Down Expand Up @@ -66,8 +66,8 @@ func testAccMongoDBAtlasPrivateLinkEndpointServiceDataSourceConfig(awsAccessKey,
resource "mongodbatlas_privatelink_endpoint_service" "test" {
project_id = mongodbatlas_privatelink_endpoint.test.project_id
private_link_id = aws_vpc_endpoint.ptfe_service.id
endpoint_service_id = mongodbatlas_privatelink_endpoint.test.private_link_id
endpoint_service_id = aws_vpc_endpoint.ptfe_service.id
private_link_id = mongodbatlas_privatelink_endpoint.test.private_link_id
provider_name = "%[4]s"
}
Expand Down
22 changes: 13 additions & 9 deletions mongodbatlas/resource_mongodbatlas_privatelink_endpoint_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,11 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkCreate(d *schema.ResourceData
endpointServiceID := d.Get("endpoint_service_id").(string)

request := &matlas.InterfaceEndpointConnection{
ID: privateLinkID,
ID: endpointServiceID,
PrivateEndpointIPAddress: d.Get("private_endpoint_ip_address").(string),
}

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

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

if err := d.Set("private_link_id", privateLinkID); err != nil {
return fmt.Errorf(errorEndpointSetting, "private_link_id", endpointServiceID, err)
}

return nil
}

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

if endpointServiceID != "" {
_, err := conn.PrivateEndpoints.DeleteOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, encodedPrivateLinkID)
_, err := conn.PrivateEndpoints.DeleteOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, encodedEndpointID)
if err != nil {
return fmt.Errorf(errorEndpointDelete, endpointServiceID, err)
}
Expand Down Expand Up @@ -225,9 +229,9 @@ func resourceMongoDBAtlasPrivateEndpointServiceLinkImportState(d *schema.Resourc
privateLinkID := parts[1]
endpointServiceID := parts[2]
providerName := parts[3]
encodedPrivateLinkID := url.PathEscape(privateLinkID)
encodedEndpointID := url.PathEscape(endpointServiceID)

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

func resourceServiceEndpointRefreshFunc(client *matlas.Client, projectID, providerName, privateLinkID, endpointServiceID string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
i, resp, err := client.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, endpointServiceID, privateLinkID)
i, resp, err := client.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), projectID, providerName, privateLinkID, endpointServiceID)
if err != nil {
if resp != nil && resp.StatusCode == 404 {
return "", "DELETED", nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mongodbatlas
import (
"context"
"fmt"
"net/url"
"os"
"testing"

Expand All @@ -28,7 +29,7 @@ func TestAccResourceMongoDBAtlasPrivateLinkEndpointServiceAWS_Complete(t *testin
)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); checkAwsEnv(t); checkPeeringEnvAWS(t) },
PreCheck: func() { testAccPreCheck(t); checkAwsEnv(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckMongoDBAtlasPrivateLinkEndpointServiceDestroy,
Steps: []resource.TestStep{
Expand Down Expand Up @@ -117,7 +118,7 @@ func testAccCheckMongoDBAtlasPrivateLinkEndpointServiceExists(resourceName strin

ids := decodeStateID(rs.Primary.ID)

_, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), ids["project_id"], ids["provider_name"], ids["endpoint_service_id"], ids["private_link_id"])
_, _, err := conn.PrivateEndpoints.GetOnePrivateEndpoint(context.Background(), ids["project_id"], ids["provider_name"], ids["private_link_id"], url.QueryEscape(ids["endpoint_service_id"]))
if err == nil {
return nil
}
Expand Down Expand Up @@ -169,8 +170,8 @@ func testAccMongoDBAtlasPrivateLinkEndpointServiceConfigCompleteAWS(awsAccessKey
resource "mongodbatlas_privatelink_endpoint_service" "test" {
project_id = mongodbatlas_privatelink_endpoint.test.project_id
private_link_id = aws_vpc_endpoint.ptfe_service.id
endpoint_service_id = mongodbatlas_privatelink_endpoint.test.private_link_id
endpoint_service_id = aws_vpc_endpoint.ptfe_service.id
private_link_id = mongodbatlas_privatelink_endpoint.test.private_link_id
provider_name = "%[4]s"
}
`, awsAccessKey, awsSecretKey, projectID, providerName, region, vpcID, subnetID, securityGroupID)
Expand Down
7 changes: 4 additions & 3 deletions website/docs/d/privatelink_endpoint_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ resource "aws_vpc_endpoint" "ptfe_service" {
resource "mongodbatlas_privatelink_endpoint_service" "test" {
project_id = "${mongodbatlas_privatelink_endpoint.test.project_id}"
private_link_id = "${mongodbatlas_privatelink_endpoint.test.private_link_id}"
interface_endpoint_id = "${aws_vpc_endpoint.ptfe_service.id}"
endpoint_service_id = "${aws_vpc_endpoint.ptfe_service.id}"
provider_name ="AWS"
}
data "mongodbatlas_privatelink_endpoint_service" "test" {
Expand All @@ -45,8 +46,8 @@ data "mongodbatlas_privatelink_endpoint_service" "test" {
## Argument Reference

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

## Attributes Reference
Expand Down
2 changes: 1 addition & 1 deletion website/docs/r/privatelink_endpoint_service.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ In addition to all arguments above, the following attributes are exported:
Private Endpoint Link Connection can be imported using project ID and username, in the format `{project_id}--{private_link_id}--{endpoint_service_id}--{provider_name}`, e.g.

```
$ terraform import mongodbatlas_privatelink_endpoint_service.test 1112222b3bf99403840e8934--vpce-4242342343--3242342343112--AWS
$ terraform import mongodbatlas_privatelink_endpoint_service.test 1112222b3bf99403840e8934--3242342343112--vpce-4242342343--AWS
```

See detailed information for arguments and attributes: [MongoDB API Private Endpoint Link Connection](https://docs.atlas.mongodb.com/reference/api/private-endpoints-endpoint-create-one/)

0 comments on commit 712bd37

Please sign in to comment.