Skip to content

Commit

Permalink
feat(cae): add new resource to manage cae environment access vpc
Browse files Browse the repository at this point in the history
  • Loading branch information
wuzhuanhong committed Jan 9, 2025
1 parent 3bd2841 commit 7a850bc
Show file tree
Hide file tree
Showing 4 changed files with 455 additions and 0 deletions.
58 changes: 58 additions & 0 deletions docs/resources/cae_vpc_egress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
subcategory: "Cloud Application Engine (CAE)"
layout: "huaweicloud"
page_title: "HuaweiCloud: huaweicloud_cae_notification_rule"
description: |-
Manage a CAE environment to access VPC resource within HuaweiCloud.
---

# huaweicloud_cae_vpc_egress

Manage a CAE environment to access VPC resource within HuaweiCloud.

## Example Usage

```hcl
variable "environment_id" {}
variable "route_table_id" {}
variable "cidr" {}
resource "huaweicloud_cae_vpc_egress" "test" {
environment_id = var.environment_id
route_table_id = var.route_table_id
cidr = var.cidr
}
```

## Argument Reference

The following arguments are supported:

* `region` - (Optional, String, ForceNew) Specifies the region in which to create the resource.
If omitted, the provider-level region will be used.
Changing this creates a new resource.

* `environment_id` - (Required, String, ForceNew) The ID of the environment.
Changing this creates a new resource.

* `route_table_id` - (Required, String, ForceNew) The ID of the route table corresponding to the subnet to which
the CAE environment belongs.
Changing this creates a new resource.

* `cidr` - (Required, String, ForceNew) The destination CIDR of the routing table corresponding to the subnet to which
the CAE environment belongs.
Changing this creates a new resource.

## Attribute Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The resource ID, in UUID format.

## Import

The resource can be imported using `environment_id`, `route_table_id`, and `cidr`, separated by commas (,), e.g.

```bash
$ terraform import huaweicloud_cae_vpc_egress.test <environment_id>,<route_table_id>,<cidr>
```
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1408,6 +1408,7 @@ func Provider() *schema.Provider {
"huaweicloud_cae_component_configurations": cae.ResourceComponentConfigurations(),
"huaweicloud_cae_component_deployment": cae.ResourceComponentDeployment(),
"huaweicloud_cae_notification_rule": cae.ResourceNotificationRule(),
"huaweicloud_cae_vpc_egress": cae.ResourceVpcEgress(),

"huaweicloud_cbr_backup_share_accepter": cbr.ResourceBackupShareAccepter(),
"huaweicloud_cbr_backup_share": cbr.ResourceBackupShare(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package cae

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/cae"
)

func getVpcEgressFunc(cfg *config.Config, state *terraform.ResourceState) (interface{}, error) {
client, err := cfg.NewServiceClient("cae", acceptance.HW_REGION_NAME)
if err != nil {
return nil, fmt.Errorf("error creating CAE client: %s", err)
}

environmentId := state.Primary.Attributes["environment_id"]
return cae.GetVpcEgressById(client, environmentId, state.Primary.ID)
}

func TestAccVpcEgress_basic(t *testing.T) {
var (
obj interface{}

name = acceptance.RandomAccResourceName()

rName = "huaweicloud_cae_vpc_egress.test"
rc = acceptance.InitResourceCheck(
rName,
&obj,
getVpcEgressFunc,
)
)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acceptance.TestAccPreCheck(t)
acceptance.TestAccPreCheckCaeEnvironment(t)
},
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: rc.CheckResourceDestroy(),
Steps: []resource.TestStep{
{
Config: testAccVpcEgress_basic(name),
Check: resource.ComposeTestCheckFunc(
rc.CheckResourceExists(),
resource.TestCheckResourceAttr(rName, "environment_id", acceptance.HW_CAE_ENVIRONMENT_ID),
resource.TestCheckResourceAttrPair(rName, "route_table_id", "huaweicloud_vpc_route_table.test", "id"),
resource.TestCheckResourceAttrPair(rName, "cidr", "huaweicloud_vpc_route_table.test", "route.0.destination"),
),
},
{
ResourceName: rName,
ImportState: true,
ImportStateVerify: true,
ImportStateIdFunc: testAccVpcEgressImportStateFunc(rName),
},
},
})
}

func testAccVpcEgress_base(name string) string {
return fmt.Sprintf(`
data "huaweicloud_cae_environments" "test" {
environment_id = "%[1]s"
}
locals {
vpc_id = data.huaweicloud_cae_environments.test.environments[0].annotations.vpc_id
}
resource "huaweicloud_vpc" "test" {
name = "%[2]s"
cidr = "192.168.10.0/24"
}
resource "huaweicloud_vpc_peering_connection" "test" {
name = "%[2]s"
vpc_id = local.vpc_id
peer_vpc_id = huaweicloud_vpc.test.id
}
resource "huaweicloud_vpc_route_table" "test" {
name = "%[2]s"
vpc_id = local.vpc_id
subnets = [
data.huaweicloud_cae_environments.test.environments[0].annotations.subnet_id,
]
route {
destination = cidrsubnet(huaweicloud_vpc.test.cidr, 4, 1)
type = "peering"
nexthop = huaweicloud_vpc_peering_connection.test.id
}
route {
destination = cidrsubnet(huaweicloud_vpc.test.cidr, 4, 2)
type = "peering"
nexthop = huaweicloud_vpc_peering_connection.test.id
}
}
`, acceptance.HW_CAE_ENVIRONMENT_ID, name)
}

func testAccVpcEgress_basic(name string) string {
return fmt.Sprintf(`
%[1]s
resource "huaweicloud_cae_vpc_egress" "test" {
environment_id = "%[2]s"
route_table_id = huaweicloud_vpc_route_table.test.id
cidr = tolist(huaweicloud_vpc_route_table.test.route)[0].destination
}
`, testAccVpcEgress_base(name), acceptance.HW_CAE_ENVIRONMENT_ID)
}

func testAccVpcEgressImportStateFunc(name string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[name]
if !ok {
return "", fmt.Errorf("resource (%s) not found: %s", name, rs)
}

var (
environmentId = rs.Primary.Attributes["environment_id"]
routeTableId = rs.Primary.Attributes["route_table_id"]
cidr = rs.Primary.Attributes["cidr"]
)

if environmentId == "" || routeTableId == "" || cidr == "" {
return "", fmt.Errorf("some import IDs are missing, want '<environment_id>,<route_table_id>,<cidr>', but got '%s,%s,%s'",
environmentId, routeTableId, cidr)
}

return fmt.Sprintf("%s,%s,%s", environmentId, routeTableId, cidr), nil
}
}
Loading

0 comments on commit 7a850bc

Please sign in to comment.