-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cae): add new resource to manage cae environment access vpc
- Loading branch information
1 parent
3bd2841
commit 7a850bc
Showing
4 changed files
with
455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
huaweicloud/services/acceptance/cae/resource_huaweicloud_cae_vpc_egress_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
Oops, something went wrong.