Skip to content

Commit

Permalink
[VPC] peering module (#9)
Browse files Browse the repository at this point in the history
[VPC] peering module

You can create vpc peering connections and accepts another ones by one module
Closes #7

Reviewed-by: Aloento
Reviewed-by: Artem Lifshits
  • Loading branch information
anton-sidelnikov authored Jun 2, 2023
1 parent df260a8 commit 1cb4da1
Show file tree
Hide file tree
Showing 10 changed files with 378 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/dns/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ data "opentelekomcloud_vpc_subnet_v1" "subnet" {

module "dns" {
source = "../../modules/dns"

dns_zone_settings = local.local_dns_settings
default_tags_set = var.default_tags_set
}
Expand Down
48 changes: 48 additions & 0 deletions examples/vpc-peering/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# VPC Peering

Configuration in this directory creates a 2 Peering connections and one of them will be accepted in second project.
Second project aliased in `settings.tf` also you should know `project_ids` and `vpc_ids` of peered projects which put in `peering.auto.tfvars`.

## Usage

To run this example you need to execute:

```bash
$ terraform init
$ terraform plan
$ terraform apply
```

Note that this example will create resources which can cost money. Run `terraform destroy` when you don't need these resources.

## Requirements

| Name | Version |
| ---------------------------------------------------------------------------------------------- |-----------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
| <a name="requirement_opentelekomcloud"></a> [opentelekomcloud](#requirement\_opentelekomcloud) | >= 1.23.9 |

## Providers

No providers.

## Modules

| Name | Source | Version |
|-----------------------------------------------------------------------|------------------------------------------------------------------|---------|
| <a name="module_vpc-peering"></a> [vpc-peering](#module\_vpc-peering) | "opentelekomcloud/modules/opentelekomcloud//modules/vpc-peering" | 0.0.2 |

## Resources

No resources.

## Inputs

No inputs.

## Outputs

| Name | Description |
|------------------------------------------------------------------------------------------------------------|----------------------------------|
| <a name="output_peering_connection_ids"></a> [peering\_connection\_ids](#output\_peering\_connection\_ids) | The Ids of created Zones |
| <a name="output_peering_accepter_ids"></a> [peering\_accepter\_ids](#output\_peering\_accepter\_ids) | The Names of created Record sets |
78 changes: 78 additions & 0 deletions examples/vpc-peering/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*=================================
PEERING VARIABLES
==================================*/
variable "requester_vpc_peering_settings" {
default = {}
description = "Map of peering properties"
}

variable "accepter_vpc_peering_settings" {
default = {}
description = "Map of peering accepter properties"
}

variable "main_vpc" {
default = "subnet-do-not-delete-pls"
description = "Subnet for Peering."
}
/*=================================
PEERING LOCALS
==================================*/

locals {
local_requester_vpc_peering_settings = {
"REQUESTER_PROJECT_1_TO_ACCEPTER_PROJECT_1" = {
peer_tenant_id = var.requester_vpc_peering_settings["REQUESTER_PROJECT_1_TO_ACCEPTER_PROJECT_1"]["peer_tenant_id"] # FIRST PROJECT
peer_vpc_id = var.requester_vpc_peering_settings["REQUESTER_PROJECT_1_TO_ACCEPTER_PROJECT_1"]["peer_vpc_id"] # VPC ID TO PEER FROM FIRST PROJECT
vpc_id = data.opentelekomcloud_vpc_subnet_v1.subnet.vpc_id
}
"REQUESTER_PROJECT_2_TO_ACCEPTER_PROJECT_2" = {
peer_tenant_id = var.requester_vpc_peering_settings["REQUESTER_PROJECT_2_TO_ACCEPTER_PROJECT_2"]["peer_tenant_id"] # SECOND PROJECT
peer_vpc_id = var.requester_vpc_peering_settings["REQUESTER_PROJECT_2_TO_ACCEPTER_PROJECT_2"]["peer_vpc_id"] # VPC ID TO PEER FROM SECOND PROJECT
vpc_id = data.opentelekomcloud_vpc_subnet_v1.subnet.vpc_id
}
}
requester_vpc_peering_settings = merge(var.requester_vpc_peering_settings, local.local_requester_vpc_peering_settings)
}
locals {
local_accepter_vpc_peering_settings = {
"ACCEPTER_PROJECT_1-WITH-REQUESTER_PROJECT_1" = {
peer_vpc_connection_id = module.peering-requester.peering_connections["REQUESTER_PROJECT_1_TO_ACCEPTER_PROJECT_1"].id
is_accept = true
}
}
accepter_vpc_peering_settings = merge(var.accepter_vpc_peering_settings, local.local_accepter_vpc_peering_settings)
}
/*=================================
PEERING MODULES
==================================*/
data "opentelekomcloud_vpc_subnet_v1" "subnet" {
name = var.main_vpc
}

module "peering-requester" {
source = "../../modules/vpc-peering"

requester_vpc_peering_settings = local.requester_vpc_peering_settings
}

module "peering-accepter" {
source = "../../modules/vpc-peering"

accepter_vpc_peering_settings = local.accepter_vpc_peering_settings

providers = {
opentelekomcloud = opentelekomcloud.second
}
}
/*=================================
PEERING OUTPUTS
==================================*/

output "peering_connection_ids" {
value = { for k, v in module.peering-requester.peering_connections : k => v.id }
}

output "peering_accepter_ids" {
value = { for k, v in module.peering-accepter.peering_accepters : k => v.id }
}
23 changes: 23 additions & 0 deletions examples/vpc-peering/peering.auto.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*=================================
PEERING PREPARED MAP
==================================*/

requester_vpc_peering_settings = {
"REQUESTER_PROJECT_1_TO_ACCEPTER_PROJECT_1" = {
peer_tenant_id = "5dd3c0b24cdc4d31952c49589182a80d"
peer_vpc_id = "a82d8c31-1f1b-4d55-bd45-e01ec3de417c"
vpc_id = "REWRITE_IN_LOCALS"
}
"REQUESTER_PROJECT_2_TO_ACCEPTER_PROJECT_2" = {
peer_tenant_id = "5dd3c0b24cdc4d31952c49589182a90d"
peer_vpc_id = "a82d8c31-t6nh-4d55-bd45-e01ec3de417c"
vpc_id = "REWRITE_IN_LOCALS"
}
}

accepter_vpc_peering_settings = {
"ACCEPTER_PROJECT_1-WITH-REQUESTER_PROJECT_1" = {
peer_vpc_connection_id = "REWRITE_IN_LOCALS"
is_accept = true
}
}
19 changes: 19 additions & 0 deletions examples/vpc-peering/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
required_providers {
opentelekomcloud = {
source = "opentelekomcloud/opentelekomcloud"
version = ">=1.34.4"
}
}
}

# Configure the OpenTelekomCloud Main Provider
provider "opentelekomcloud" {
cloud = "terraform"
}

# Configure the OpenTelekomCloud Second Provider for another project
provider "opentelekomcloud" {
alias = "second"
cloud = "dmd"
}
125 changes: 125 additions & 0 deletions modules/vpc-peering/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
# OpenTelekomCloud VPC Peering Terraform module

![GitHub tag (latest by date)](https://img.shields.io/github/v/tag/opentelekomcloud/terraform-opentelekomcloud-modules)
![Build (latest by date)](https://zuul.otc-service.com/api/tenant/eco/badge?project=opentelekomcloud/terraform-opentelekomcloud-modules&pipeline=check&branch=main)

_This module aims to create a module to create and accept peering connection on OpenTelekomCloud provider._

_These types of resources are supported:_

* [Connection](https://registry.terraform.io/providers/opentelekomcloud/opentelekomcloud/latest/docs/resources/vpc_peering_v2)
* [Accepter](https://registry.terraform.io/providers/opentelekomcloud/opentelekomcloud/latest/docs/resources/vpc_peering_accepter_v2)


## Where to find module documentations

You can find different documentations versioned by terraform registry [here](https://registry.terraform.io/modules/opentelekomcloud/modules/opentelekomcloud/latest).

## Terraform versions

Terraform 0.13 or higher.

## Usage

```hcl
requester_vpc_peering_settings = {
"REQUESTER_PROJECT_1_TO_ACCEPTER_PROJECT_1" = {
peer_tenant_id = "5dd3c0b24cdc4d31952c49589182a80d"
peer_vpc_id = "a82d8c31-1f1b-4d55-bd45-e01ec3de417c"
vpc_id = "a82d8c31-1f1b-8889-bd45-e01ec3de417c"
}
"REQUESTER_PROJECT_2_TO_ACCEPTER_PROJECT_2" = {
peer_tenant_id = "5dd3c0b24cdc4d31952c49589182a90d"
peer_vpc_id = "a82d8c31-t6nh-4d55-bd45-e01ec3de417c"
vpc_id = "a82d8c31-1f1b-8889-bd45-e01ec3de417c"
}
}
accepter_vpc_peering_settings = {
"ACCEPTER_PROJECT_1-WITH-REQUESTER_PROJECT_1" = {
peer_vpc_connection_id = "3a9f8c14-a523-4736-beb6-b6275a52388a"
is_accept = true
}
}
variable "requester_vpc_peering_settings" {
default = {}
description = "Map of peering properties"
}
variable "accepter_vpc_peering_settings" {
default = {}
description = "Map of peering accepter properties"
}
variable "main_vpc" {
default = "subnet-do-not-delete-pls"
description = "Subnet for Peering."
}
module "peering" {
source = "../../modules/vpc-peering"
requester_vpc_peering_settings = var.requester_vpc_peering_settings
accepter_vpc_peering_settings = var.accepter_vpc_peering_settings
}
```

## Examples

* [DNS](https://github.com/opentelekomcloud/terraform-opentelekomcloud-modules/blob/main/examples/vpc-peering)

## Requirements

| Name | Version |
| ---------------------------------------------------------------------------------------------- |-----------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.13.0 |
| <a name="requirement_opentelekomcloud"></a> [opentelekomcloud](#requirement\_opentelekomcloud) | >= 1.23.9 |

## Modules

No modules.

## Resources

| Name | Type | Count |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|----------|-----------|
| [opentelekomcloud_vpc_peering_connection_v2.peering-connection](https://registry.terraform.io/providers/opentelekomcloud/opentelekomcloud/latest/docs/resources/vpc_peering_v2) | resource | 1 or more |
| [opentelekomcloud_vpc_peering_connection_accepter_v2.peering-connection-accepter](https://registry.terraform.io/providers/opentelekomcloud/opentelekomcloud/latest/docs/resources/vpc_peering_accepter_v2) | resource | 1 or more |

## Inputs

| Name | Description | Type | Default | Required |
|------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|---------------|---------|:--------:|
| <a name="input_requester_vpc_peering_settings"></a> [requester\_vpc\_peering\_settings](#input\_requester\_vpc\_peering\_settings) | Map of peering settings. | `map(object)` | `{}` | no |
| <a name="input_accepter_vpc_peering_settings"></a> [accepter\_vpc\_peering\_settings](#input\_accepter\_vpc\_peering\_settings) | Map of peering accepter settings. | `map(object)` | `{}` | no |

### requester_vpc_peering_settings

| Name | Description | Type | Default | Required |
|----------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|----------|---------|:--------:|
| <a name="input_peer_tenant_id"></a> [peer\_tenant\_id](#input\_peer\_tenant\_id) | Specifies the ID of the project to which a VPC involved in the VPC peering connection belongs. | `string` | `null` | yes |
| <a name="input_peer_vpc_id"></a> [peer\_vpc\_id](#input\_peer\_vpc\_id) | Receiver VPC ID. | `string` | `null` | yes |
| <a name="input_vpc_id"></a> [vpc\_id](#input\_vpc\_id) | Requester VPC ID. | `string` | `null` | yes |

### accepter_vpc_peering_settings

| Name | Description | Type | Default | Required |
|------------------------------------------------------------------------------------------------------------|------------------------------------------|----------|---------|:--------:|
| <a name="input_peer_vpc_connection_id"></a> [peer\_vpc\_connection\_id](#input\_peer\_vpc\_connection\_id) | Specifies the VPC peering connection ID. | `string` | `null` | yes |
| <a name="input_is_accept"></a> [is\_accept](#input\_is\_accept) | Accept connection or not | `bool` | `null` | yes |

## Outputs

| Name | Description |
|-----------------------------------------------------------------------------------|---------------------------------|
| <a name="output_peering_connections"></a> [zones](#output\_peering\_connections) | The all VPC Peering connections |
| <a name="output_peering_accepters"></a> [recordsets](#output\_peering\_accepters) | The all VPC Peering accepters |

## Authors

Module managed by [Anton Sidelnikov](https://github.com/anton-sidelnikov).

## License

Apache 2 Licensed. See LICENSE for full details.
10 changes: 10 additions & 0 deletions modules/vpc-peering/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*=================================
OUTPUTS
==================================*/
output "peering_connections" {
value = opentelekomcloud_vpc_peering_connection_v2.peering-connection
}

output "peering_accepters" {
value = opentelekomcloud_vpc_peering_connection_accepter_v2.peering-connection-accepter
}
16 changes: 16 additions & 0 deletions modules/vpc-peering/peering.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*=================================
RESOURCES
#==================================*/
resource "opentelekomcloud_vpc_peering_connection_v2" "peering-connection" {
for_each = var.requester_vpc_peering_settings
name = each.key
peer_vpc_id = each.value["peer_vpc_id"]
vpc_id = each.value["vpc_id"]
peer_tenant_id = each.value["peer_tenant_id"]
}

resource "opentelekomcloud_vpc_peering_connection_accepter_v2" "peering-connection-accepter" {
for_each = var.accepter_vpc_peering_settings
vpc_peering_connection_id = each.value["peer_vpc_connection_id"]
accept = each.value["is_accept"]
}
47 changes: 47 additions & 0 deletions modules/vpc-peering/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*=================================
VARIABLES
==================================*/
variable "requester_vpc_peering_settings" {
default = {
/* Example variable value:
peering_name_1 = {
peer_tenant_id = "accepter_tenant_id_1"
peer_vpc_id = "accepter_vpc_id_1"
vpc_id = "requester_vpc_id_1"
} # Example value 1
peering_name_2 = {
peer_tenant_id = "accepter_tenant_id_2"
peer_vpc_id = "accepter_vpc_id_2"
vpc_id = "requester_vpc_id_2"
} # Example value 2
*/
}
type = map(object({
peer_tenant_id = string
peer_vpc_id = string
vpc_id = string
}))
description = "Map of peering settings (current values in top level vars file)"
}

variable "accepter_vpc_peering_settings" {
default = {
/* Examples
peering_name_1 = {
peer_vpc_connection_id = "peer_vpc_connection_id_1"
is_accept = true
} # Example value 1
peering_name_2 = {
peer_vpc_connection_id = "peer_vpc_connection_id_2"
is_accept = true
} # Example value 2
*/
}
type = map(object({
peer_vpc_connection_id = string
is_accept = bool
}))
description = "Map of peering accepter settings (current values in top level vars file)"
}
11 changes: 11 additions & 0 deletions modules/vpc-peering/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*=================================
PROVIDER SETTINGS
==================================*/
terraform {
required_providers {
opentelekomcloud = {
source = "opentelekomcloud/opentelekomcloud"
version = ">=1.34.4"
}
}
}

0 comments on commit 1cb4da1

Please sign in to comment.