Skip to content

Commit 9064241

Browse files
committed
feat(example/er): add example for shared er instance
1 parent 71108f3 commit 9064241

File tree

3 files changed

+229
-0
lines changed

3 files changed

+229
-0
lines changed

examples/er/share_instance/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Create an ER instance to share with other accounts
2+
3+
Configuration in this directory creates an ER instance and RAM share resource. The example includes an ER instance,
4+
a RAM share resource, a RAM share accepter resource, a VPC configuration, an ER attachment and
5+
an ER attachment accepter resource.
6+
Configuration in this directory describes how to share an ER instance with other accounts, and the sharer accepts or
7+
rejects attachment requests from other accounts.
8+
9+
To run, configure your Huaweicloud provider as described in the
10+
[document](https://registry.terraform.io/providers/huaweicloud/huaweicloud/latest/docs).
11+
12+
## Usage
13+
14+
```
15+
terraform init
16+
terraform plan
17+
terraform apply
18+
terraform destroy
19+
```
20+
21+
## Requirements
22+
23+
| Name | Version |
24+
| ---- | ---- |
25+
| terraform | >= 0.12.0 |
26+
| huaweicloud | >= 1.73.4 |

examples/er/share_instance/main.tf

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Share (owner).
2+
provider "huaweicloud" {
3+
region = var.region_name
4+
alias = "owner"
5+
access_key = var.owner_ak
6+
secret_key = var.owner_sk
7+
}
8+
9+
# Other account (principal).
10+
provider "huaweicloud" {
11+
region = var.region_name
12+
alias = "principal"
13+
access_key = var.principal_ak
14+
secret_key = var.principal_sk
15+
}
16+
17+
data "huaweicloud_er_availability_zones" "test" {
18+
provider = huaweicloud.owner
19+
}
20+
21+
# Owner creates an ER instance to share.
22+
resource "huaweicloud_er_instance" "test" {
23+
provider = huaweicloud.owner
24+
25+
availability_zones = slice(data.huaweicloud_er_availability_zones.test.names, 0, 1)
26+
27+
name = var.er_instance_name
28+
asn = "64512"
29+
description = "Create an ER instace to share"
30+
31+
enable_default_propagation = true
32+
enable_default_association = true
33+
auto_accept_shared_attachments = false
34+
}
35+
36+
data "huaweicloud_ram_resource_permissions" "test" {
37+
provider = huaweicloud.owner
38+
39+
resource_type = "er:instances"
40+
41+
depends_on = [huaweicloud_er_instance.test]
42+
}
43+
44+
# Owner creates a RAM shared resource to initiate a shared ER request.
45+
resource "huaweicloud_ram_resource_share" "test" {
46+
provider = huaweicloud.owner
47+
48+
name = var.resource_share_name
49+
principals = [var.principal_account_id]
50+
resource_urns = ["er:${var.region_name}:${var.owner_account_id}:instances:${huaweicloud_er_instance.test.id}"]
51+
52+
permission_ids = data.huaweicloud_ram_resource_permissions.test.permissions[*].id
53+
}
54+
55+
# Principal queries the shared ER requests that need to be accepted.
56+
data "huaweicloud_ram_resource_share_invitations" "test" {
57+
provider = huaweicloud.principal
58+
59+
status = "pending"
60+
61+
depends_on = [huaweicloud_ram_resource_share.test]
62+
}
63+
64+
# Principal (ER instance acceptor) to accept request from owner shared ER.
65+
resource "huaweicloud_ram_resource_share_accepter" "test" {
66+
provider = huaweicloud.principal
67+
68+
resource_share_invitation_id = try([for v in data.huaweicloud_ram_resource_share_invitations.test.resource_share_invitations : v.id if v.resource_share_id == huaweicloud_ram_resource_share.test.id][0], "")
69+
action = "accept"
70+
71+
# After accepting the request, querying data.huaweicloud_ram_resource_share_invitations again will be empty.
72+
# This resource is a one-time resource. Add ignore_changes to prevent resource changes when executing terraform plan.
73+
lifecycle {
74+
ignore_changes = [
75+
resource_share_invitation_id,
76+
]
77+
}
78+
}
79+
80+
resource "huaweicloud_vpc" "test" {
81+
provider = huaweicloud.principal
82+
83+
name = var.vpc_name
84+
cidr = "192.168.0.0/16"
85+
}
86+
87+
resource "huaweicloud_vpc_subnet" "test" {
88+
provider = huaweicloud.principal
89+
90+
vpc_id = huaweicloud_vpc.test.id
91+
name = var.subnet_name
92+
cidr = "192.168.0.0/24"
93+
gateway_ip = "192.168.0.1"
94+
}
95+
96+
# Principal creates a VPC attachment.
97+
resource "huaweicloud_er_vpc_attachment" "test" {
98+
provider = huaweicloud.principal
99+
100+
instance_id = huaweicloud_er_instance.test.id
101+
vpc_id = huaweicloud_vpc.test.id
102+
subnet_id = huaweicloud_vpc_subnet.test.id
103+
name = var.attachment_name
104+
105+
depends_on = [huaweicloud_ram_resource_share_accepter.test]
106+
}
107+
108+
# The owner accepts attachment from principals.
109+
resource "huaweicloud_er_attachment_accepter" "test" {
110+
provider = huaweicloud.owner
111+
112+
instance_id = huaweicloud_er_instance.test.id
113+
attachment_id = huaweicloud_er_vpc_attachment.test.id
114+
action = "accept"
115+
}
116+
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
variable "region_name" {
2+
default = "cn-north-4"
3+
}
4+
5+
variable "owner_account_id" {
6+
type = string
7+
description = "The account ID of the ER instance sharer"
8+
9+
default = ""
10+
sensitive = true
11+
}
12+
13+
variable "owner_ak" {
14+
type = string
15+
description = "The AK of the ER instance sharer"
16+
17+
default = ""
18+
sensitive = true
19+
}
20+
21+
variable "owner_sk" {
22+
type = string
23+
description = "The SK of the ER instance sharer"
24+
25+
default = ""
26+
sensitive = true
27+
}
28+
29+
variable "principal_account_id" {
30+
type = string
31+
description = "The account ID of the ER instance accepter"
32+
33+
default = ""
34+
sensitive = true
35+
}
36+
37+
38+
variable "principal_ak" {
39+
type = string
40+
description = "The AK of the ER instance accepter"
41+
42+
default = ""
43+
sensitive = true
44+
}
45+
46+
variable "principal_sk" {
47+
type = string
48+
description = "The sk of the ER instance accepter"
49+
50+
default = ""
51+
sensitive = true
52+
}
53+
54+
variable "er_instance_name" {
55+
type = string
56+
description = "The ID of the shared ER instance"
57+
58+
default = "share_attachment"
59+
}
60+
61+
variable "resource_share_name" {
62+
type = string
63+
description = "The ID of the shared ER instance"
64+
65+
default = "resource-share-er"
66+
}
67+
68+
variable "vpc_name" {
69+
type = string
70+
description = "The ID of the VPC of the VPC attachment"
71+
72+
default = "er_attachment_vpc_test"
73+
}
74+
75+
variable "subnet_name" {
76+
type = string
77+
description = "The ID of the subnet of the VPC attachment"
78+
79+
default = "er_attachment_subnet_test"
80+
}
81+
82+
variable "attachment_name" {
83+
type = string
84+
description = "The ID of the VPC attachment"
85+
86+
default = "shared_attachment"
87+
}

0 commit comments

Comments
 (0)