Skip to content

Commit

Permalink
[Refactor] VPCEP resources and data-sources (#2762)
Browse files Browse the repository at this point in the history
[Refactor] VPCEP resources and data-sources

Summary of the Pull Request
Refactoring old resources
PR Checklist

 Refers to: #xxx
 Tests added/passed.
 Documentation updated.
 Schema updated.
 Release notes added.

Acceptance Steps Performed
=== RUN   TestVPCEPService_basic
=== PAUSE TestVPCEPService_basic
=== CONT  TestVPCEPService_basic
--- PASS: TestVPCEPService_basic (94.16s)
PASS

Process finished with the exit code 0

=== RUN   TestVPCEndpoint_basic
=== PAUSE TestVPCEndpoint_basic
=== CONT  TestVPCEndpoint_basic
--- PASS: TestVPCEndpoint_basic (75.05s)
PASS

Process finished with the exit code 0

=== RUN   TestDataSourceVPCEPPublicService
=== PAUSE TestDataSourceVPCEPPublicService
=== CONT  TestDataSourceVPCEPPublicService
--- PASS: TestDataSourceVPCEPPublicService (21.19s)
PASS

Process finished with the exit code 0

=== RUN   TestDataSourceVPCEPService
=== PAUSE TestDataSourceVPCEPService
=== CONT  TestDataSourceVPCEPService
--- PASS: TestDataSourceVPCEPService (84.09s)
PASS

Process finished with the exit code 0

Reviewed-by: Aloento
Reviewed-by: Artem Lifshits
Reviewed-by: Anton Sidelnikov
Reviewed-by: Muneeb H. Jan <muneebhafeezjan@gmail.com>
  • Loading branch information
anton-sidelnikov authored Dec 12, 2024
1 parent d5ed85b commit 3ecca09
Show file tree
Hide file tree
Showing 14 changed files with 299 additions and 211 deletions.
24 changes: 15 additions & 9 deletions docs/resources/vpcep_endpoint_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,35 @@ resource "opentelekomcloud_vpcep_endpoint_v1" "endpoint" {

The following arguments are supported:

* `service_id` - (Required) Specifies the ID of the VPC endpoint service.
* `service_id` - (Required, String, ForceNew) Specifies the ID of the VPC endpoint service.

* `vpc_id` - (Required) Specifies the ID of the VPC (OpenStack router) where the VPC endpoint is to be created.
* `vpc_id` - (Required, String, ForceNew) Specifies the ID of the VPC (OpenStack router) where the VPC endpoint is to be created.

* `subnet_id` - (Optional) The value must be the ID of the subnet (OpenStack network) created in the VPC specified
* `subnet_id` - (Optional, String, ForceNew) The value must be the ID of the subnet (OpenStack network) created in the VPC specified
by `vpc_id` and in the format of the UUID.
This parameter is mandatory only if you create a VPC endpoint for connecting to an interface VPC endpoint service.

~>
The CIDR block of the VPC subnet cannot overlap with `198.19.128.0/20`. The destination address of the custom route in
the VPC route table cannot overlap with the CIDR block `198.19.128.0/20`.

* `enable_dns` - (Optional) Specifies whether to create a private domain name. The default value is `false`.
* `enable_dns` - (Optional, Bool, ForceNew) Specifies whether to create a private domain name. The default value is `false`.

* `route_tables` - (Optional) Lists the IDs of route tables.
* `description` - (Optional, String, ForceNew) Specifies the description of the VPC endpoint. The value can contain
characters such as letters and digits, but cannot contain less than signs (<) and great than signs (>).

* `port_ip` - (Optional) Specifies the IP address for accessing the associated VPC endpoint service.
* `route_tables` - (Optional, List, ForceNew) Lists the IDs of route tables.

* `whitelist` - (Optional) Specifies an array of whitelisted IPs for controlling access to the VPC endpoint.
* `port_ip` - (Optional, String, ForceNew) Specifies the IP address for accessing the associated VPC endpoint service.

* `whitelist` - (Optional, List, ForceNew) Specifies an array of whitelisted IPs for controlling access to the VPC endpoint.
``IPv4 addresses`` or ``CIDR blocks`` can be specified to control access when you create a VPC endpoint.
This parameter is mandatory only when you create a ``VPC endpoint`` for connecting to an interface VPC endpoint service.

* `enable_whitelist` - (Optional) Specifies whether to enable access control.
* `enable_whitelist` - (Optional, Bool, ForceNew) Specifies whether to enable access control.
This parameter is available only if you create a ``VPC endpoint`` for connecting to an interface VPC endpoint service.

* `tags` - (Optional) Lists the resource tags.
* `tags` - (Optional, Map) The key/value pairs to associate with the VPC endpoint.

## Attributes Reference

Expand All @@ -105,6 +108,9 @@ In addition to all arguments above, the following attributes are exported:

* `project_id` - Specifies the project ID.

* `status` - The status of the VPC endpoint. The value can be `pendingAcceptance`, `creating`, `accepted`,
`rejected`, `failed`, `deleting`.

## Import

VPC endpoint can be imported using the `id`, e.g.
Expand Down
39 changes: 25 additions & 14 deletions docs/resources/vpcep_service_v1.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,26 @@ resource "opentelekomcloud_vpcep_service_v1" "service" {

The following arguments are supported:

* `name` - (Optional) Specifies the name of the VPC endpoint service.
* `name` - (Optional, String) Specifies the name of the VPC endpoint service.
The value contains a maximum of 16 characters, including letters, digits, underscores (_), and hyphens (-).
* If you do not specify this parameter, the VPC endpoint service name is in the format: `regionName.serviceId`.
* If you specify this parameter, the VPC endpoint service name is in the format: `regionName.serviceName.serviceId`.

* `port_id` - (Required) Specifies the ID for identifying the backend resource of the VPC endpoint service.
* `description` - (Optional, String) Specifies the description of the VPC endpoint service.

* `port_id` - (Required, String) Specifies the ID for identifying the backend resource of the VPC endpoint service.
The value is as follows:
* If the backend service is an enhanced load balancer, the value is the ID of the port bound to the private IP address of the load balancer.
* the backend resource is an ECS, the value is the NIC ID of the ECS where the VPC endpoint service is deployed.
* the backend resource is a virtual IP address, the value is the NIC ID of the physical server where virtual resources are created.

* `pool_id` - (Optional) Specifies the ID of the cluster associated with the target VPCEP resource.
* `pool_id` - (Optional, String, ForceNew) Specifies the ID of the cluster associated with the target VPCEP resource.

* `vip_port_id` - (Optional) Specifies the ID of the virtual NIC to which the virtual IP address is bound.
* `vip_port_id` - (Optional, String) Specifies the ID of the virtual NIC to which the virtual IP address is bound.

* `vpc_id` - (Optional) Specifies the ID of the VPC (router) to which the backend resource of the VPC endpoint service belongs.
* `vpc_id` - (Required, String, ForceNew) Specifies the ID of the VPC (router) to which the backend resource of the VPC endpoint service belongs.

* `approval_enabled` - (Optional) Specifies whether connection approval is required.
* `approval_enabled` - (Optional, Bool) Specifies whether connection approval is required.

* `false`: indicates that connection approval is not required.
The created VPC endpoint is in the `accepted` state.
Expand All @@ -86,16 +88,16 @@ The following arguments are supported:
* `interface`: VPC endpoint services of this type include cloud services configured by operations people
and private services created by yourselves. You cannot configure these cloud services, but can use them.

* `server_type` - (Required) Specifies the resource type.
* `server_type` - (Required, String, ForceNew) Specifies the resource type.
* `VM`: The backend resource is a server.
* `VIP`: The backend resource is a virtual IP address that functions as a physical server hosting virtual resources.
* `LB`: The backend resource is an enhanced load balancer.

* `port` - (Required) Lists the port mappings opened to the VPC endpoint service. See below for the details.
* `port` - (Required, List) Lists the port mappings opened to the VPC endpoint service. See below for the details.

* `whitelist` - (Optional) Lists of domain IDs of target users.
* `whitelist` - (Optional, List) Lists of domain IDs of target users.

* `tcp_proxy` - (Optional) Specifies whether the client IP address and port number or `marker_id` information is
* `tcp_proxy` - (Optional, String) Specifies whether the client IP address and port number or `marker_id` information is
transmitted to the server.
This parameter is available only when the server can parse fields tcp option and tcp payload.

Expand All @@ -108,15 +110,15 @@ The following arguments are supported:

The default value is `close`.

* `tags` - (Optional) Map of the resource tags.
* `tags` - (Optional, Map) Map of the resource tags.

The `port` block supports:

* `client_port` - (Required) Specifies the port for accessing the VPC endpoint.
* `client_port` - (Required, Int) Specifies the port for accessing the VPC endpoint.

* `server_port` - (Required) Specifies the port for accessing the VPC endpoint service.
* `server_port` - (Required, Int) Specifies the port for accessing the VPC endpoint service.

* `protocol` - (Required) Specifies the protocol used in port mappings. The value can be `TCP` or `UDP`.
* `protocol` - (Required, String) Specifies the protocol used in port mappings. The value can be `TCP` or `UDP`.
The default value is `TCP`.

## Attributes Reference
Expand All @@ -125,6 +127,15 @@ In addition to all arguments above, the following attributes are exported:

* `id` - ID of VPC endpoint service

* `status` - The status of the VPC endpoint service. The value can be **available** or **failed**.

* `connections` - An array of VPC endpoints connect to the VPC endpoint service. Structure is documented below.
+ `endpoint_id` - The unique ID of the VPC endpoint.
+ `packet_id` - The packet ID of the VPC endpoint.
+ `domain_id` - The user's domain ID.
+ `status` - The connection status of the VPC endpoint.
+ `description` - The description of the VPC endpoint service connection.

## Import

VPC endpoint service can be imported using the `id`, e.g.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20200123153347-de78d2cb44f4
github.com/mitchellh/go-homedir v1.1.0
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241210160040-3b0fb9159f71
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241211090426-8e0297aa1379
github.com/unknwon/com v1.0.1
golang.org/x/crypto v0.23.0
golang.org/x/sync v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLA
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
github.com/oklog/run v1.0.0/go.mod h1:dlhp/R75TPv97u0XWUtDeV/lRKWPKSdTuV0TZvrmrQA=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241210160040-3b0fb9159f71 h1:K+DlJ0svuSiXfuaN+M2pG1LZvgtnq7uMjCLpCeTMBJQ=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241210160040-3b0fb9159f71/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241211090426-8e0297aa1379 h1:E4Oc5QRAPwLanhJv0igiuVPQ5j1+4WYzck8EVNV+u/A=
github.com/opentelekomcloud/gophertelekomcloud v0.9.4-0.20241211090426-8e0297aa1379/go.mod h1:M1F6OfSRZRzAmAFKQqSLClX952at5hx5rHe4UTEykgg=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (

const dataSourcePublicService = "data.opentelekomcloud_vpcep_public_service_v1.obs"

func TestDataSourcePublicService(t *testing.T) {
func TestDataSourceVPCEPPublicService(t *testing.T) {
dc := common.InitDataSourceCheck(dataSourcePublicService)
t.Parallel()
resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: checkServiceDestroy,
CheckDestroy: dc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testDataSourcePublicService,
Check: resource.ComposeTestCheckFunc(
dc.CheckResourceExists(),
resource.TestCheckResourceAttr(dataSourcePublicService, "owner", "OTC"),
resource.TestCheckResourceAttr(dataSourcePublicService, "service_type", "gateway"),
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (

const dataSourceServiceName = "data.opentelekomcloud_vpcep_service_v1.service"

func TestDataSourceService(t *testing.T) {
func TestDataSourceVPCEPService(t *testing.T) {
dc := common.InitDataSourceCheck(dataSourceServiceName)
name := tools.RandomString("tf-test-", 4)
t.Parallel()
quotas.BookOne(t, serviceQuota)

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: checkServiceDestroy,
CheckDestroy: dc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testServiceBasic(name),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,58 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/acceptance/tools"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/vpcep/v1/endpoints"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/common/quotas"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/acceptance/env"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/common/cfg"
"github.com/opentelekomcloud/terraform-provider-opentelekomcloud/opentelekomcloud/services/vpcep"
)

const resourceEndpointName = "opentelekomcloud_vpcep_endpoint_v1.endpoint"

func TestEndpoint_basic(t *testing.T) {
func getVPCEndpointFunc(config *cfg.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := config.VpcEpV1Client(env.OS_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating VPCEP v1 client: %s", err)
}
return endpoints.Get(client, state.Primary.ID)
}

func TestVPCEndpoint_basic(t *testing.T) {
var ep endpoints.Endpoint
name := tools.RandomString("tf-test-ep-", 4)

rc := common.InitResourceCheck(
resourceEndpointName,
&ep,
getVPCEndpointFunc,
)

t.Parallel()
quotas.BookMany(t, endpointQuotas())

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: checkEndpointDestroy,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testEndpointBasic(name),
Check: resource.ComposeTestCheckFunc(
checkEndpointExists(resourceEndpointName, &ep),
resource.TestCheckResourceAttr(resourceEndpointName, "port_ip", "192.168.0.12"),
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(resourceEndpointName, "tags.fizz", "buzz"),
resource.TestCheckResourceAttr(resourceEndpointName, "enable_dns", "true"),
resource.TestCheckResourceAttr(resourceEndpointName, "dns_names.#", "1"),
resource.TestCheckResourceAttr(resourceEndpointName, "service_name", name),
),
},
},
})
}

func TestEndpoint_import(t *testing.T) {
name := tools.RandomString("tf-test-ep-", 4)
t.Parallel()
quotas.BookMany(t, endpointQuotas())

resource.Test(t, resource.TestCase{
PreCheck: func() { common.TestAccPreCheck(t) },
ProviderFactories: common.TestAccProviderFactories,
CheckDestroy: checkEndpointDestroy,
Steps: []resource.TestStep{
{
Config: testEndpointImport(name),
Config: testEndpointBasic_Update(name),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceEndpointName, "status", "accepted"),
resource.TestCheckResourceAttr(resourceEndpointName, "tags.owner", "tf-acc-update"),
resource.TestCheckResourceAttr(resourceEndpointName, "tags.foo", "bar"),
),
},
{
ImportState: true,
Expand All @@ -74,75 +76,31 @@ resource "opentelekomcloud_vpcep_endpoint_v1" "endpoint" {
service_id = opentelekomcloud_vpcep_service_v1.service.id
vpc_id = opentelekomcloud_vpcep_service_v1.service.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.id
port_ip = "192.168.0.12"
port_ip = cidrhost(data.opentelekomcloud_vpc_subnet_v1.shared_subnet.cidr, 32)
enable_dns = true
tags = {
"fizz" : "buzz"
}
}
`, testServiceBasic(name)) // without acceptance required
`, testServiceBasic(name))
}

func testEndpointImport(name string) string {
func testEndpointBasic_Update(rName string) string {
return fmt.Sprintf(`
%s
resource "opentelekomcloud_vpcep_endpoint_v1" "endpoint" {
service_id = opentelekomcloud_vpcep_service_v1.service.id
vpc_id = opentelekomcloud_vpcep_service_v1.service.vpc_id
subnet_id = data.opentelekomcloud_vpc_subnet_v1.shared_subnet.id
port_ip = "192.168.0.14"
port_ip = cidrhost(data.opentelekomcloud_vpc_subnet_v1.shared_subnet.cidr, 32)
enable_dns = true
tags = {
"fizz" : "buzz"
owner = "tf-acc-update"
foo = "bar"
}
}
`, testServiceBasic(name)) // without acceptance required
}

func checkEndpointDestroy(s *terraform.State) error {
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.VpcEpV1Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf(vpcep.ErrClientCreate, err)
}
for _, rs := range s.RootModule().Resources {
if rs.Type != "opentelekomcloud_vpcep_service_v1" {
continue
}
svc, err := endpoints.Get(client, rs.Primary.ID).Extract()
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return nil
}
return fmt.Errorf("error getting service state: %w", err)
}
return fmt.Errorf("VPC Endpoint %s still exists", svc.ServiceName)
}
return nil
}

func checkEndpointExists(name string, ep *endpoints.Endpoint) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("not found: %s", name)
}
if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}
config := common.TestAccProvider.Meta().(*cfg.Config)
client, err := config.VpcEpV1Client(env.OS_REGION_NAME)
if err != nil {
return fmt.Errorf(vpcep.ErrClientCreate, err)
}
found, err := endpoints.Get(client, rs.Primary.ID).Extract()
if err != nil {
return fmt.Errorf("error getting endpoint: %w", err)
}
*ep = *found
return nil
}
`, testServiceBasic(rName))
}
Loading

0 comments on commit 3ecca09

Please sign in to comment.