FAST plugin system (#1266)

* plugin folder, gitignore, serverless connector example

* add support to fast plugin variables and outputs to tfdoc

* rename folder, READMEs

* add variable description

* show diffs

* check documentation, use multiple files

* debug check doc

* try a different glob

* debug tfdoc names

* more debug

* and even more debug

* fix gitignore

* fix links

* support extra files in tests

* fix fixture, switch stage 2 peering to new tests

* tfdoc

* Allow globs in extra files

---------

Co-authored-by: Julio Castillo <jccb@google.com>
This commit is contained in:
Ludovico Magnocavallo 2023-03-24 13:28:32 +01:00 committed by GitHub
parent 176c5e05cd
commit 3d41d01efc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
40 changed files with 401 additions and 145 deletions

View File

@ -56,7 +56,7 @@ jobs:
- name: Check documentation
id: documentation-fabric
run: |
python3 tools/check_documentation.py modules fast blueprints
python3 tools/check_documentation.py --show-diffs modules fast blueprints
- name: Check documentation links
id: documentation-links-fabric

View File

@ -23,7 +23,6 @@ Clone this repository or [open it in cloud shell](https://ssh.cloud.google.com/c
Once done testing, you can clean up resources by running `terraform destroy`.
<!-- BEGIN TFDOC -->
## Variables

2
fast/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
stages/[0-9]*/local-*.tf
stages-multitenant/[0-9]*/local-*.tf

View File

@ -0,0 +1,40 @@
# VPC Serverless Connector
This FAST plugin adds centralized [Serverless VPC Access Connectors](https://cloud.google.com/vpc/docs/serverless-vpc-access) to network stages.
This plugin does not manage
- IAM bindings for the connectors, which should be added via the stage project-level variables
- firewall rules for the connectors, which should be added via the stage factory
The plugin only requires a specific configuration if the defaults it uses need to be changed:
- the connector-specific subnets default to the `10.255.255.0` range
- the machine type, number of instances and thoughput use the API defaults
To enable the plugin, simply copy or link its files in the networking stage.
<!-- TFDOC OPTS files:1 show_extra:1 -->
<!-- BEGIN TFDOC -->
## Files
| name | description | modules | resources |
|---|---|---|---|
| [local-serverless-connector-outputs.tf](./local-serverless-connector-outputs.tf) | Serverless Connector outputs. | | <code>google_storage_bucket_object</code> · <code>local_file</code> |
| [local-serverless-connector-variables.tf](./local-serverless-connector-variables.tf) | Serverless Connector variables. | | |
| [local-serverless-connector.tf](./local-serverless-connector.tf) | Serverless Connector resources. | <code>net-vpc</code> | <code>google_vpc_access_connector</code> |
## Variables
| name | description | type | required | default | producer |
|---|---|:---:|:---:|:---:|:---:|
| [serverless_connector_config](local-serverless-connector-variables.tf#L19) | VPC Access Serverless Connectors configuration. | <code title="object&#40;&#123;&#10; dev-primary &#61; object&#40;&#123;&#10; ip_cidr_range &#61; optional&#40;string, &#34;10.255.255.128&#47;28&#34;&#41;&#10; machine_type &#61; optional&#40;string&#41;&#10; instances &#61; optional&#40;object&#40;&#123;&#10; max &#61; optional&#40;number&#41;&#10; min &#61; optional&#40;number&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; throughput &#61; optional&#40;object&#40;&#123;&#10; max &#61; optional&#40;number&#41;&#10; min &#61; optional&#40;number&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; &#125;&#41;&#10; prod-primary &#61; object&#40;&#123;&#10; ip_cidr_range &#61; optional&#40;string, &#34;10.255.255.0&#47;28&#34;&#41;&#10; machine_type &#61; optional&#40;string&#41;&#10; instances &#61; optional&#40;object&#40;&#123;&#10; max &#61; optional&#40;number&#41;&#10; min &#61; optional&#40;number&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; throughput &#61; optional&#40;object&#40;&#123;&#10; max &#61; optional&#40;number&#41;&#10; min &#61; optional&#40;number&#41;&#10; &#125;&#41;, &#123;&#125;&#41;&#10; &#125;&#41;&#10;&#125;&#41;">object&#40;&#123;&#8230;&#125;&#41;</code> | | <code title="&#123;&#10; dev-primary &#61; &#123;&#125;&#10; prod-primary &#61; &#123;&#125;&#10;&#125;">&#123;&#8230;&#125;</code> | |
## Outputs
| name | description | sensitive | consumers |
|---|---|:---:|---|
| [plugin_sc_connectors](local-serverless-connector-outputs.tf#L43) | VPC Access Connectors. | | |
<!-- END TFDOC -->

View File

@ -0,0 +1,46 @@
/**
* 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.
*/
# tfdoc:file:description Serverless Connector outputs.
locals {
plugin_sc_tfvars = {
dev = google_vpc_access_connector.dev-primary.0.id
prod = google_vpc_access_connector.prod-primary.0.id
}
}
# generate tfvars file for subsequent stages
resource "local_file" "plugin_sc_tfvars" {
for_each = var.outputs_location == null ? {} : { 1 = 1 }
file_permission = "0644"
filename = "${try(pathexpand(var.outputs_location), "")}/tfvars/2-networking-serverless-connnector.auto.tfvars.json"
content = jsonencode(local.plugin_sc_tfvars)
}
resource "google_storage_bucket_object" "plugin_sc_tfvars" {
bucket = var.automation.outputs_bucket
name = "tfvars/2-networking-serverless-connnector.auto.tfvars.json"
content = jsonencode(local.plugin_sc_tfvars)
}
# outputs
output "plugin_sc_connectors" {
description = "VPC Access Connectors."
value = local.plugin_sc_tfvars
}

View File

@ -0,0 +1,52 @@
/**
* 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.
*/
# tfdoc:file:description Serverless Connector variables.
variable "serverless_connector_config" {
description = "VPC Access Serverless Connectors configuration."
type = object({
dev-primary = object({
ip_cidr_range = optional(string, "10.255.255.128/28")
machine_type = optional(string)
instances = optional(object({
max = optional(number)
min = optional(number)
}), {})
throughput = optional(object({
max = optional(number)
min = optional(number)
}), {})
})
prod-primary = object({
ip_cidr_range = optional(string, "10.255.255.0/28")
machine_type = optional(string)
instances = optional(object({
max = optional(number)
min = optional(number)
}), {})
throughput = optional(object({
max = optional(number)
min = optional(number)
}), {})
})
})
default = {
dev-primary = {}
prod-primary = {}
}
nullable = false
}

View File

@ -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.
*/
# tfdoc:file:description Serverless Connector resources.
locals {
plugin_sc_subnets = {
dev = {
for k, v in module.dev-spoke-vpc-serverless.subnets : k => v.name
}
prod = {
for k, v in module.prod-spoke-vpc-serverless.subnets : k => v.name
}
}
}
module "dev-spoke-vpc-serverless" {
source = "../../../modules/net-vpc"
project_id = module.dev-spoke-project.project_id
name = module.dev-spoke-vpc.name
vpc_create = false
subnets = [{
name = "access-connector"
description = "VPC Serverless Connector for the primary region."
ip_cidr_range = var.serverless_connector_config.dev-primary.ip_cidr_range
region = var.regions.primary
}]
}
module "prod-spoke-vpc-serverless" {
source = "../../../modules/net-vpc"
project_id = module.prod-spoke-project.project_id
name = module.prod-spoke-vpc.name
vpc_create = false
subnets = [{
name = "access-connector"
description = "VPC Serverless Connector for the primary region."
ip_cidr_range = var.serverless_connector_config.prod-primary.ip_cidr_range
region = var.regions.primary
}]
}
resource "google_vpc_access_connector" "dev-primary" {
count = var.serverless_connector_config.dev-primary == null ? 0 : 1
project = module.dev-spoke-project.project_id
region = var.regions.primary
name = "access-connector-${local.region_shortnames[var.regions.primary]}"
subnet {
name = local.plugin_sc_subnets.dev["${var.regions.primary}/access-connector"]
}
machine_type = var.serverless_connector_config.dev-primary.machine_type
max_instances = var.serverless_connector_config.dev-primary.instances.max
max_throughput = var.serverless_connector_config.dev-primary.throughput.max
min_instances = var.serverless_connector_config.dev-primary.instances.min
min_throughput = var.serverless_connector_config.dev-primary.throughput.min
}
resource "google_vpc_access_connector" "prod-primary" {
count = var.serverless_connector_config.prod-primary == null ? 0 : 1
project = module.prod-spoke-project.project_id
region = var.regions.primary
name = "access-connector-${local.region_shortnames[var.regions.primary]}"
subnet {
name = local.plugin_sc_subnets.prod["${var.regions.primary}/access-connector"]
}
machine_type = var.serverless_connector_config.prod-primary.machine_type
max_instances = var.serverless_connector_config.prod-primary.instances.max
max_throughput = var.serverless_connector_config.prod-primary.throughput.max
min_instances = var.serverless_connector_config.prod-primary.instances.min
min_throughput = var.serverless_connector_config.prod-primary.throughput.min
}

25
fast/plugins/README.md Normal file
View File

@ -0,0 +1,25 @@
# FAST plugin system
This folders details a simple mechanism that can be used to add extra functionality to FAST stages, and a few examples that implement simple plugins that can be used as-is.
## Available plugins
### Networking
- [Serverless VPC Access Connector](./2-networking-serverless-connector/)
## Anatomy of a plugin
FAST plugins are much simpler and easier to code than full-blown stages: each plugin is meant to add a single feature using a small set of resources, and interacting directly with stage modules and variables.
A simple plugin might be composed of a single file with one resource, and grow up to the canonical set of one "main" (resources), one variables, and outputs file.
Plugin file names start with the `local-` prefix which is purposefully excluded in FAST stages via Git ignore, so that plugins are not accidentally committed to stages during development and staying aligned with our master branch is possible.
Plugins are structured here as individual folders, organized in top-level folders according to the FAST stage they are designed to work with.
As an example, the [`2-networking/serverless-connector` plugin](./2-networking-serverless-connector/) implements centralized [Serverless VPC Access Connectors](https://cloud.google.com/vpc/docs/serverless-vpc-access) for our networking stages, and is composed of three files:
- [`local-serverless-connector.tf`](./2-networking-serverless-connector/local-serverless-connector.tf) managing resources including the subnets needed in each VPC and the connectors themselves
- [`local-serverless-connector-outputs.tf`](./2-networking-serverless-connector/local-serverless-connector-outputs.tf) defining a single `serverless_connectors` output for the plugin, and optional output files
- [`local-serverless-connector-variables.tf`](./2-networking-serverless-connector/local-serverless-connector-variables.tf) defining a single `serverless_connector_config` variable used to configure the plugin

View File

@ -30,6 +30,7 @@ module "dev-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true

View File

@ -30,6 +30,7 @@ module "prod-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true

View File

@ -30,6 +30,7 @@ module "dev-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true

View File

@ -30,6 +30,7 @@ module "prod-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true

View File

@ -29,6 +29,7 @@ module "dev-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true

View File

@ -29,6 +29,7 @@ module "prod-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true

View File

@ -30,6 +30,7 @@ module "dev-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true

View File

@ -30,6 +30,7 @@ module "prod-spoke-project" {
"networkmanagement.googleapis.com",
"servicenetworking.googleapis.com",
"stackdriver.googleapis.com",
"vpcaccess.googleapis.com"
]
shared_vpc_host_config = {
enabled = true

View File

@ -37,6 +37,9 @@ class FabricTestFile(pytest.File):
have the following structure:
test-name:
extra_files:
- bar.tf
- foo.tf
tfvars:
- tfvars1.tfvars
- tfvars2.tfvars
@ -60,27 +63,32 @@ class FabricTestFile(pytest.File):
common = raw.pop('common_tfvars', [])
for test_name, spec in raw.get('tests', {}).items():
spec = {} if spec is None else spec
extra_files = spec.get('extra_files')
inventories = spec.get('inventory', [f'{test_name}.yaml'])
tfvars = common + [f'{test_name}.tfvars'] + spec.get('tfvars', [])
tf_var_files = common + [f'{test_name}.tfvars'] + spec.get('tfvars', [])
for i in inventories:
name = test_name
if isinstance(inventories, list) and len(inventories) > 1:
name = f'{test_name}[{i}]'
yield FabricTestItem.from_parent(self, name=name, module=module,
inventory=[i], tfvars=tfvars)
inventory=[i],
tf_var_files=tf_var_files,
extra_files=extra_files)
class FabricTestItem(pytest.Item):
def __init__(self, name, parent, module, inventory, tfvars):
def __init__(self, name, parent, module, inventory, tf_var_files,
extra_files=None):
super().__init__(name, parent)
self.module = module
self.inventory = inventory
self.tfvars = tfvars
self.tf_var_files = tf_var_files
self.extra_files = extra_files
def runtest(self):
s = plan_validator(self.module, self.inventory, self.parent.path.parent,
self.tfvars)
self.tf_var_files, self.extra_files)
def reportinfo(self):
return self.path, None, self.name

View File

@ -1,4 +1,3 @@
data_dir = "../../../fast/stages/2-networking-a-peering/data/"
automation = {
outputs_bucket = "test"
}

View File

@ -11,3 +11,7 @@
# 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.
counts:
modules: 31
resources: 122

View File

@ -14,27 +14,14 @@
import hashlib
from pathlib import Path
from deepdiff import DeepDiff
BASEDIR = Path(__file__).parent
FIXTURE_PEERING = BASEDIR / 'fixture'
FIXTURE_VPN = BASEDIR.parent / 's2_networking_b_vpn/fixture'
from pathlib import Path
STAGES = Path(__file__).parents[4] / 'fast/stages'
STAGE_PEERING = STAGES / '2-networking-a-peering'
STAGE_VPN = STAGES / '2-networking-b-vpn'
def test_counts(plan_summary):
"Test stage."
summary = plan_summary("fast/stages/2-networking-a-peering",
tf_var_files=["common.tfvars"])
assert summary.counts["modules"] > 0
assert summary.counts["resources"] > 0
def test_vpn_peering_parity(plan_summary):
'''Ensure VPN- and peering-based networking stages are identical except
for VPN and VPC peering resources'''
@ -65,7 +52,7 @@ def compute_md5(filename):
return md5hash.hexdigest()
def test_vpn_peering_checksums(e2e_plan_runner):
def test_vpn_peering_checksums():
'''Compare MD5 sums of common files in the vpn and peering
networking stages'''
peering_files = {

View File

@ -1,4 +1,4 @@
# Copyright 2022 Google LLC
# 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.
@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.
module: fast/stages/2-networking-a-peering
common_tfvars:
- common.tfvars
def test_counts(plan_summary):
"Test stage."
summary = plan_summary("fast/stages/2-networking-b-vpn",
tf_var_files=["common.tfvars"])
assert summary.counts["modules"] > 0
assert summary.counts["resources"] > 0
tests:
stage:
extra_files:
- ../../plugins/2-networking-serverless-connector/*.tf

View File

@ -1,4 +1,3 @@
data_dir = "../../../../../fast/stages/2-networking-b-vpn/data/"
automation = {
outputs_bucket = "test"
}

View File

@ -1,54 +0,0 @@
/**
* Copyright 2022 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.
*/
module "stage" {
source = "../../../../../fast/stages/02-networking-vpn"
data_dir = "../../../../../fast/stages/02-networking-vpn/data/"
automation = {
outputs_bucket = "test"
}
billing_account = {
id = "000000-111111-222222"
organization_id = 123456789012
}
custom_roles = {
service_project_network_admin = "organizations/123456789012/roles/foo"
}
folder_ids = {
networking = null
networking-dev = null
networking-prod = null
}
region_trigram = {
europe-west1 = "ew1"
europe-west3 = "ew3"
europe-west8 = "ew8"
}
service_accounts = {
data-platform-dev = "string"
data-platform-prod = "string"
gke-dev = "string"
gke-prod = "string"
project-factory-dev = "string"
project-factory-prod = "string"
}
organization = {
domain = "fast.example.com"
id = 123456789012
customer_id = "C00000000"
}
prefix = "fast2"
}

View File

@ -1,4 +1,4 @@
# Copyright 2022 Google LLC
# 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.
@ -11,3 +11,7 @@
# 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.
counts:
modules: 33
resources: 159

View File

@ -0,0 +1,22 @@
# 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.
module: fast/stages/2-networking-b-vpn
common_tfvars:
- common.tfvars
tests:
stage:
extra_files:
- ../../plugins/2-networking-serverless-connector/*.tf

View File

@ -1,4 +1,3 @@
data_dir = "../../../fast/stages/2-networking-c-nva/data/"
automation = {
outputs_bucket = "test"
}

View File

@ -1,4 +1,4 @@
# Copyright 2022 Google LLC
# 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.
@ -11,3 +11,7 @@
# 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.
counts:
modules: 45
resources: 168

View File

@ -1,21 +0,0 @@
# Copyright 2022 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.
def test_counts(plan_summary):
"Test stage."
summary = plan_summary("fast/stages/2-networking-c-nva",
tf_var_files=["common.tfvars"])
assert summary.counts["modules"] > 0
assert summary.counts["resources"] > 0

View File

@ -0,0 +1,22 @@
# 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.
module: fast/stages/2-networking-c-nva
common_tfvars:
- common.tfvars
tests:
stage:
extra_files:
- ../../plugins/2-networking-serverless-connector/*.tf

View File

@ -1,4 +1,3 @@
data_dir = "../../../../../fast/stages/2-networking-d-separate-envs/data/"
automation = {
outputs_bucket = "test"
}

View File

@ -0,0 +1,17 @@
# 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.
counts:
modules: 28
resources: 122

View File

@ -1,21 +0,0 @@
# Copyright 2022 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.
def test_counts(plan_summary):
"Test stage."
summary = plan_summary("fast/stages/2-networking-d-separate-envs",
tf_var_files=["common.tfvars"])
assert summary.counts["modules"] > 0
assert summary.counts["resources"] > 0

View File

@ -0,0 +1,22 @@
# 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.
module: fast/stages/2-networking-d-separate-envs
common_tfvars:
- common.tfvars
tests:
stage:
extra_files:
- ../../plugins/2-networking-serverless-connector/*.tf

View File

@ -1,4 +1,4 @@
# Copyright 2022 Google LLC
# 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.
@ -15,7 +15,7 @@
import collections
import contextlib
import itertools
import glob
import os
import shutil
import tempfile
@ -34,7 +34,7 @@ def _prepare_root_module(path):
If the TFTEST_COPY environment variable is set, `path` is copied to
a temporary directory and a few terraform files (e.g.
terraform.tfvars) are delete to ensure a clean test environment.
terraform.tfvars) are deleted to ensure a clean test environment.
Otherwise, `path` is simply returned untouched.
"""
if os.environ.get('TFTEST_COPY'):
@ -62,7 +62,8 @@ def _prepare_root_module(path):
yield path
def plan_summary(module_path, basedir, tf_var_files=None, **tf_vars):
def plan_summary(module_path, basedir, tf_var_files=None, extra_files=None,
**tf_vars):
"""
Run a Terraform plan on the module located at `module_path`.
@ -97,7 +98,10 @@ def plan_summary(module_path, basedir, tf_var_files=None, **tf_vars):
with _prepare_root_module(module_path) as test_path:
binary = os.environ.get('TERRAFORM', 'terraform')
tf = tftest.TerraformTest(test_path, binary=binary)
tf.setup(upgrade=True)
extra_files = [(module_path / filename).resolve()
for x in extra_files or []
for filename in glob.glob(x, root_dir=module_path)]
tf.setup(extra_files=extra_files, upgrade=True)
tf_var_files = [(basedir / x).resolve() for x in tf_var_files or []]
plan = tf.plan(output=True, tf_var_file=tf_var_files, tf_vars=tf_vars)
@ -135,19 +139,21 @@ def plan_summary_fixture(request):
to the directory of the calling test
"""
def inner(module_path, basedir=None, tf_var_files=None, **tf_vars):
def inner(module_path, basedir=None, tf_var_files=None, extra_files=None,
**tf_vars):
if basedir is None:
basedir = Path(request.fspath).parent
return plan_summary(module_path=module_path, basedir=basedir,
tf_var_files=tf_var_files, **tf_vars)
tf_var_files=tf_var_files, extra_files=extra_files,
**tf_vars)
return inner
def plan_validator(module_path, inventory_paths, basedir, tf_var_files=None,
**tf_vars):
extra_files=None, **tf_vars):
summary = plan_summary(module_path=module_path, tf_var_files=tf_var_files,
basedir=basedir, **tf_vars)
extra_files=extra_files, basedir=basedir, **tf_vars)
# allow single single string for inventory_paths
if not isinstance(inventory_paths, list):

View File

@ -78,11 +78,10 @@ def _check_dir(dir_name, exclude_files=None, files=False, show_extra=False):
try:
new_doc = tfdoc.create_doc(readme_path.parent, files, show_extra,
exclude_files, readme)
# TODO: support variables in multiple files
newvars = new_doc.variables
newouts = new_doc.outputs
variables = [v.name for v in newvars if v.file == "variables.tf"]
outputs = [o.name for o in newouts if o.file == "outputs.tf"]
variables = [v.name for v in newvars if v.file.endswith('variables.tf')]
outputs = [o.name for o in newouts if o.file.endswith('outputs.tf')]
except SystemExit:
state = state.SKIP
else:

View File

@ -175,7 +175,9 @@ def parse_files(basepath, exclude_files=None):
def parse_outputs(basepath, exclude_files=None):
'Return a list of Output named tuples for root module outputs*.tf.'
exclude_files = exclude_files or []
for name in glob.glob(os.path.join(basepath, 'outputs*tf')):
names = glob.glob(os.path.join(basepath, 'outputs*tf'))
names += glob.glob(os.path.join(basepath, 'local-*outputs*tf'))
for name in names:
shortname = os.path.basename(name)
if shortname in exclude_files:
continue
@ -196,7 +198,9 @@ def parse_outputs(basepath, exclude_files=None):
def parse_variables(basepath, exclude_files=None):
'Return a list of Variable named tuples for root module variables*.tf.'
exclude_files = exclude_files or []
for name in glob.glob(os.path.join(basepath, 'variables*tf')):
names = glob.glob(os.path.join(basepath, 'variables*tf'))
names += glob.glob(os.path.join(basepath, 'local-*variables*tf'))
for name in names:
shortname = os.path.basename(name)
if shortname in exclude_files:
continue