Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add apigee non-vpc peering mode support #1648

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 85 additions & 5 deletions modules/apigee/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This module simplifies the creation of a Apigee resources (organization, environ

## Example

### All resources (CLOUD)
### All resources (CLOUD - VPC Peering Provisioning Mode)

```hcl
module "apigee" {
Expand All @@ -18,6 +18,7 @@ module "apigee" {
billing_type = "PAYG"
database_encryption_key = "123456789"
analytics_region = "europe-west1"
disable_vpc_peering = false
}
envgroups = {
test = ["test.example.com"]
Expand Down Expand Up @@ -65,6 +66,62 @@ module "apigee" {
# tftest modules=1 resources=15
```

### All resources (CLOUD - Non-VPC Peering Provisioning Mode)

```hcl
module "apigee" {
source = "./fabric/modules/apigee"
project_id = "my-project"
organization = {
display_name = "My Organization"
description = "My Organization"
runtime_type = "CLOUD"
billing_type = "PAYG"
database_encryption_key = "123456789"
analytics_region = "europe-west1"
disable_vpc_peering = true
}
envgroups = {
test = ["test.example.com"]
prod = ["prod.example.com"]
}
environments = {
apis-test = {
display_name = "APIs test"
description = "APIs Test"
envgroups = ["test"]
regions = ["europe-west1"]
}
apis-prod = {
display_name = "APIs prod"
description = "APIs prod"
envgroups = ["prod"]
regions = ["europe-west3"]
iam = {
"roles/viewer" = ["group:[email protected]"]
}
}
}
instances = {
europe-west1 = {}
europe-west3 = {
enable_nat = true
}
}
endpoint_attachments = {
endpoint-backend-1 = {
region = "europe-west1"
service_attachment = "projects/my-project-1/serviceAttachments/gkebackend1"
}
endpoint-backend-2 = {
region = "europe-west1"
service_attachment = "projects/my-project-2/serviceAttachments/gkebackend2"
}
}
}
# tftest modules=1 resources=15
```

### All resources (HYBRID control plane)

```hcl
Expand Down Expand Up @@ -129,7 +186,7 @@ module "apigee" {
# tftest modules=1 resources=1
```

### New instance
### New instance (VPC Peering Provisioning Mode)

```hcl
module "apigee" {
Expand All @@ -145,6 +202,28 @@ module "apigee" {
# tftest modules=1 resources=1
```

### New instance (Non-VPC Peering Provisioning Mode)

```hcl
module "apigee" {
source = "./fabric/modules/apigee"
project_id = "my-project"
organization = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add more examples and keep this one as it was?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

display_name = "My Organization"
description = "My Organization"
runtime_type = "CLOUD"
billing_type = "Pay-as-you-go"
database_encryption_key = "123456789"
analytics_region = "europe-west1"
disable_vpc_peering = true
}
instances = {
europe-west1 = {}
}
}
# tftest modules=1 resources=2
```

### New endpoint attachment

Endpoint attachments allow to implement [Apigee southbound network patterns](https://cloud.google.com/apigee/docs/api-platform/architecture/southbound-networking-patterns-endpoints#create-the-psc-attachments).
Expand Down Expand Up @@ -180,13 +259,13 @@ module "apigee" {

| name | description | type | required | default |
|---|---|:---:|:---:|:---:|
| [project_id](variables.tf#L95) | Project ID. | <code>string</code> | ✓ | |
| [project_id](variables.tf#L96) | Project ID. | <code>string</code> | ✓ | |
| [addons_config](variables.tf#L17) | Addons configuration. | <code title="object&#40;&#123;&#10; advanced_api_ops &#61; optional&#40;bool, false&#41;&#10; api_security &#61; optional&#40;bool, false&#41;&#10; connectors_platform &#61; optional&#40;bool, false&#41;&#10; integration &#61; optional&#40;bool, false&#41;&#10; monetization &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [endpoint_attachments](variables.tf#L29) | Endpoint attachments. | <code title="map&#40;object&#40;&#123;&#10; region &#61; string&#10; service_attachment &#61; string&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [envgroups](variables.tf#L39) | Environment groups (NAME => [HOSTNAMES]). | <code>map&#40;list&#40;string&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [environments](variables.tf#L46) | Environments. | <code title="map&#40;object&#40;&#123;&#10; display_name &#61; optional&#40;string&#41;&#10; description &#61; optional&#40;string, &#34;Terraform-managed&#34;&#41;&#10; deployment_type &#61; optional&#40;string&#41;&#10; api_proxy_type &#61; optional&#40;string&#41;&#10; node_config &#61; optional&#40;object&#40;&#123;&#10; min_node_count &#61; optional&#40;number&#41;&#10; max_node_count &#61; optional&#40;number&#41;&#10; &#125;&#41;&#41;&#10; iam &#61; optional&#40;map&#40;list&#40;string&#41;&#41;&#41;&#10; envgroups &#61; optional&#40;list&#40;string&#41;&#41;&#10; regions &#61; optional&#40;list&#40;string&#41;&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [instances](variables.tf#L65) | Instances ([REGION] => [INSTANCE]). | <code title="map&#40;object&#40;&#123;&#10; display_name &#61; optional&#40;string&#41;&#10; description &#61; optional&#40;string, &#34;Terraform-managed&#34;&#41;&#10; runtime_ip_cidr_range &#61; string&#10; troubleshooting_ip_cidr_range &#61; string&#10; disk_encryption_key &#61; optional&#40;string&#41;&#10; consumer_accept_list &#61; optional&#40;list&#40;string&#41;&#41;&#10; enable_nat &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [organization](variables.tf#L80) | Apigee organization. If set to null the organization must already exist. | <code title="object&#40;&#123;&#10; display_name &#61; optional&#40;string&#41;&#10; description &#61; optional&#40;string, &#34;Terraform-managed&#34;&#41;&#10; authorized_network &#61; optional&#40;string&#41;&#10; runtime_type &#61; optional&#40;string, &#34;CLOUD&#34;&#41;&#10; billing_type &#61; optional&#40;string&#41;&#10; database_encryption_key &#61; optional&#40;string&#41;&#10; analytics_region &#61; optional&#40;string, &#34;europe-west1&#34;&#41;&#10; retention &#61; optional&#40;string&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |
| [instances](variables.tf#L65) | Instances ([REGION] => [INSTANCE]). | <code title="map&#40;object&#40;&#123;&#10; display_name &#61; optional&#40;string&#41;&#10; description &#61; optional&#40;string, &#34;Terraform-managed&#34;&#41;&#10; runtime_ip_cidr_range &#61; optional&#40;string&#41;&#10; troubleshooting_ip_cidr_range &#61; optional&#40;string&#41;&#10; disk_encryption_key &#61; optional&#40;string&#41;&#10; consumer_accept_list &#61; optional&#40;list&#40;string&#41;&#41;&#10; enable_nat &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;&#41;">map&#40;object&#40;&#123;&#8230;&#125;&#41;&#41;</code> | | <code>&#123;&#125;</code> |
| [organization](variables.tf#L80) | Apigee organization. If set to null the organization must already exist. | <code title="object&#40;&#123;&#10; display_name &#61; optional&#40;string&#41;&#10; description &#61; optional&#40;string, &#34;Terraform-managed&#34;&#41;&#10; authorized_network &#61; optional&#40;string&#41;&#10; runtime_type &#61; optional&#40;string, &#34;CLOUD&#34;&#41;&#10; billing_type &#61; optional&#40;string&#41;&#10; database_encryption_key &#61; optional&#40;string&#41;&#10; analytics_region &#61; optional&#40;string, &#34;europe-west1&#34;&#41;&#10; retention &#61; optional&#40;string&#41;&#10; disable_vpc_peering &#61; optional&#40;bool, false&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code>null</code> |

## Outputs

Expand All @@ -202,3 +281,4 @@ module "apigee" {
| [organization](outputs.tf#L55) | Organization. | |
| [service_attachments](outputs.tf#L60) | Service attachments. | |
<!-- END TFDOC -->

25 changes: 20 additions & 5 deletions modules/apigee/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
*/

locals {
org_id = try(google_apigee_organization.organization[0].id, "organizations/${var.project_id}")
org_name = try(google_apigee_organization.organization[0].name, var.project_id)
org_id = try(google_apigee_organization.organization[0].id, "organizations/${var.project_id}")
org_name = try(google_apigee_organization.organization[0].name, var.project_id)
disable_vpc_peering = try(var.organization.disable_vpc_peering, false)
}

resource "google_apigee_organization" "organization" {
Expand All @@ -28,6 +29,14 @@ resource "google_apigee_organization" "organization" {
runtime_type = var.organization.runtime_type
runtime_database_encryption_key_name = var.organization.database_encryption_key
retention = var.organization.retention
disable_vpc_peering = var.organization.disable_vpc_peering

lifecycle {
precondition {
condition = (var.organization.disable_vpc_peering == false && var.organization.authorized_network != null && var.organization.runtime_type == "CLOUD") || (var.organization.disable_vpc_peering == true && var.organization.authorized_network == null && var.organization.runtime_type == "CLOUD") || (var.organization.disable_vpc_peering == false && var.organization.authorized_network == null && var.organization.runtime_type == "HYBRID")
error_message = "For `var.organization.runtime_type = \"CLOUD\"`, if `var.organization.disable_vpc_peering` is set to `true`, `var.organization.authorized_network` should be `null`. If `var.organization.authorized_network` is set to name of some VPC Network, `var.organization.disable_vpc_peering` should be set to `false`. `var.organization.authorized_network` is used for Apigee X VPC Peering Provisioning Mode and `var.organization.disable_vpc_peering` is used for Apigee X Non-VPC Peering Provisioning Mode. For `var.organization.runtime_type = \"HYBRID\"`, `var.organization.disable_vpc_peering` cannot be set to `true` and `var.organization.authorized_network` cannot be `null`."
}
}
}

resource "google_apigee_envgroup" "envgroups" {
Expand Down Expand Up @@ -91,9 +100,16 @@ resource "google_apigee_instance" "instances" {
description = each.value.description
location = each.key
org_id = local.org_id
ip_range = "${each.value.runtime_ip_cidr_range},${each.value.troubleshooting_ip_cidr_range}"
ip_range = local.disable_vpc_peering ? null : "${each.value.runtime_ip_cidr_range},${each.value.troubleshooting_ip_cidr_range}"
disk_encryption_key_name = each.value.disk_encryption_key
consumer_accept_list = each.value.consumer_accept_list

lifecycle {
precondition {
condition = (local.disable_vpc_peering == true && each.value.runtime_ip_cidr_range == null && each.value.troubleshooting_ip_cidr_range == null) || (local.disable_vpc_peering == false && each.value.runtime_ip_cidr_range != null && each.value.troubleshooting_ip_cidr_range != null)
error_message = "When using Apigee X Non-VPC Peering Provisioning Mode i.e. when `var.organization.disable_vpc_peering = true`, Runtime IP CIDR Ranges are not required. However, when using Apigee X VPC Peering Provisioning Mode, Runtime IP CIDR Ranges are required and `var.organization.disable_vpc_peering = false` should be set."
}
}
}

resource "google_apigee_nat_address" "apigee_nat" {
Expand All @@ -115,8 +131,7 @@ resource "google_apigee_instance_attachment" "instance_attachments" {
}
}])...)
instance_id = google_apigee_instance.instances[each.value.region].id
environment = try(google_apigee_environment.environments[each.value.environment].name,
"${local.org_id}/environments/${each.value.environment}")
environment = google_apigee_environment.environments[each.value.environment].name
}

resource "google_apigee_endpoint_attachment" "endpoint_attachments" {
Expand Down
5 changes: 3 additions & 2 deletions modules/apigee/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ variable "instances" {
type = map(object({
display_name = optional(string)
description = optional(string, "Terraform-managed")
runtime_ip_cidr_range = string
troubleshooting_ip_cidr_range = string
runtime_ip_cidr_range = optional(string)
troubleshooting_ip_cidr_range = optional(string)
disk_encryption_key = optional(string)
consumer_accept_list = optional(list(string))
enable_nat = optional(bool, false)
Expand All @@ -88,6 +88,7 @@ variable "organization" {
database_encryption_key = optional(string)
analytics_region = optional(string, "europe-west1")
retention = optional(string)
disable_vpc_peering = optional(bool, false)
})
default = null
}
Expand Down
45 changes: 45 additions & 0 deletions tests/modules/apigee/all_psc_mode.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
project_id = "my-project"
organization = {
display_name = "My Organization"
description = "My Organization"
runtime_type = "CLOUD"
billing_type = "Pay-as-you-go"
database_encryption_key = "123456789"
analytics_region = "europe-west1"
disable_vpc_peering = true
}
envgroups = {
test = ["test.example.com"]
prod = ["prod.example.com"]
}
environments = {
apis-test = {
display_name = "APIs test"
description = "APIs Test"
envgroups = ["test"]
regions = ["europe-west1"]
}
apis-prod = {
display_name = "APIs prod"
description = "APIs prod"
envgroups = ["prod"]
regions = ["europe-west3"]
iam = {
"roles/viewer" = ["group:[email protected]"]
}
}
}
instances = {
europe-west1 = {}
europe-west3 = {}
}
endpoint_attachments = {
endpoint-backend-1 = {
region = "europe-west1"
service_attachment = "projects/my-project-1/serviceAttachments/gkebackend1"
}
endpoint-backend-2 = {
region = "europe-west1"
service_attachment = "projects/my-project-2/serviceAttachments/gkebackend2"
}
}
84 changes: 84 additions & 0 deletions tests/modules/apigee/all_psc_mode.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

values:
google_apigee_endpoint_attachment.endpoint_attachments["endpoint-backend-1"]:
endpoint_attachment_id: endpoint-backend-1
location: europe-west1
service_attachment: projects/my-project-1/serviceAttachments/gkebackend1
google_apigee_endpoint_attachment.endpoint_attachments["endpoint-backend-2"]:
endpoint_attachment_id: endpoint-backend-2
location: europe-west1
service_attachment: projects/my-project-2/serviceAttachments/gkebackend2
google_apigee_envgroup.envgroups["prod"]:
hostnames:
- prod.example.com
name: prod
google_apigee_envgroup.envgroups["test"]:
hostnames:
- test.example.com
name: test
google_apigee_envgroup_attachment.envgroup_attachments["apis-prod-prod"]:
environment: apis-prod
google_apigee_envgroup_attachment.envgroup_attachments["apis-test-test"]:
environment: apis-test
google_apigee_environment.environments["apis-prod"]:
description: APIs prod
display_name: APIs prod
name: apis-prod
google_apigee_environment.environments["apis-test"]:
description: APIs Test
display_name: APIs test
name: apis-test
google_apigee_environment_iam_binding.binding["apis-prod-roles/viewer"]:
condition: []
env_id: apis-prod
members:
- group:[email protected]
role: roles/viewer
google_apigee_instance.instances["europe-west3"]:
description: Terraform-managed
disk_encryption_key_name: null
display_name: null
ip_range: null
location: europe-west3
name: instance-europe-west3
google_apigee_instance.instances["europe-west1"]:
description: Terraform-managed
disk_encryption_key_name: null
display_name: null
ip_range: null
location: europe-west1
name: instance-europe-west1
google_apigee_organization.organization[0]:
analytics_region: europe-west1
authorized_network: null
billing_type: Pay-as-you-go
description: null
display_name: null
project_id: my-project
retention: DELETION_RETENTION_UNSPECIFIED
runtime_database_encryption_key_name: '123456789'
runtime_type: CLOUD
disable_vpc_peering: true

counts:
google_apigee_endpoint_attachment: 2
google_apigee_envgroup: 2
google_apigee_envgroup_attachment: 2
google_apigee_environment: 2
google_apigee_environment_iam_binding: 1
google_apigee_instance: 2
google_apigee_instance_attachment: 2
google_apigee_organization: 1
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ organization = {
billing_type = "Pay-as-you-go"
database_encryption_key = "123456789"
analytics_region = "europe-west1"
disable_vpc_peering = false
}
envgroups = {
test = ["test.example.com"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ values:
retention: DELETION_RETENTION_UNSPECIFIED
runtime_database_encryption_key_name: '123456789'
runtime_type: CLOUD
disable_vpc_peering: false

counts:
google_apigee_endpoint_attachment: 2
Expand Down
13 changes: 13 additions & 0 deletions tests/modules/apigee/instance_only_psc_mode.tfvars
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
project_id = "my-project"
organization = {
display_name = "My Organization"
description = "My Organization"
runtime_type = "CLOUD"
billing_type = "Pay-as-you-go"
database_encryption_key = "123456789"
analytics_region = "europe-west1"
disable_vpc_peering = true
}
instances = {
europe-west1 = {}
}
Loading