From 8a2ad25a453b8e9a78380f5bf4c846a461b63014 Mon Sep 17 00:00:00 2001 From: Simone Ruffilli Date: Tue, 7 Dec 2021 09:35:24 +0100 Subject: [PATCH] Updated README.md. Added tests. --- modules/net-vpc/README.md | 29 +++++++++++++++++++ .../net_vpc/fixture/data/factory-subnet.yaml | 9 ++++++ tests/modules/net_vpc/fixture/main.tf | 1 + tests/modules/net_vpc/fixture/variables.tf | 6 ++++ tests/modules/net_vpc/test_plan_subnets.py | 19 ++++++++++-- 5 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 tests/modules/net_vpc/fixture/data/factory-subnet.yaml diff --git a/modules/net-vpc/README.md b/modules/net-vpc/README.md index 6b00ac590..98c6e99e8 100644 --- a/modules/net-vpc/README.md +++ b/modules/net-vpc/README.md @@ -170,6 +170,35 @@ module "vpc" { # tftest:modules=1:resources=3 ``` +### Subnet Factory +The `net-vpc` module includes a subnet factory (see [Resource Factories](../../factories/)) for the massive creation of subnets leveraging one configuration file per subnet. + + +```hcl +module "vpc" { + source = "./modules/net-vpc" + project_id = "my-project" + name = "my-network" + data_folder = "config/subnets" +} +# tftest:skip +``` + +```yaml +# ./config/subnets/subnet-name.yaml +region: europe-west1 # Region where the subnet will be creted +description: Sample description # Description +ip_cidr_range: 10.0.0.0/24 # Primary IP range for the subnet +private_ip_google_access: false # Opt- Enables PGA. Defaults to true +iam_users: ["foobar@example.com"] # Opt- Users to grant compute/networkUser to +iam_groups: ["lorem@example.com"] # Opt- Groups to grant compute/networkUser to +iam_service_accounts: ["foobar@project-id.iam.gserviceaccount.com"] + # Opt- SAs to grant compute/networkUser to +secondary_ip_ranges: # Opt- List of secondary IP ranges + - secondary-range-a: 192.168.0.0/24 + # Secondary ranges in name: cidr format +``` + ## Variables diff --git a/tests/modules/net_vpc/fixture/data/factory-subnet.yaml b/tests/modules/net_vpc/fixture/data/factory-subnet.yaml new file mode 100644 index 000000000..bdb3c4c92 --- /dev/null +++ b/tests/modules/net_vpc/fixture/data/factory-subnet.yaml @@ -0,0 +1,9 @@ +region: europe-west1 +description: Sample description +ip_cidr_range: 10.128.0.0/24 +private_ip_google_access: false +iam_users: ["foobar@example.com"] +iam_groups: ["lorem@example.com"] +iam_service_accounts: ["foobar@project-id.iam.gserviceaccount.com"] +secondary_ip_range: + secondary-range-a: 192.168.128.0/24 \ No newline at end of file diff --git a/tests/modules/net_vpc/fixture/main.tf b/tests/modules/net_vpc/fixture/main.tf index 42217101b..5812bba62 100644 --- a/tests/modules/net_vpc/fixture/main.tf +++ b/tests/modules/net_vpc/fixture/main.tf @@ -31,4 +31,5 @@ module "test" { subnet_private_access = var.subnet_private_access auto_create_subnetworks = var.auto_create_subnetworks private_service_networking_range = var.private_service_networking_range + data_folder = var.data_folder } diff --git a/tests/modules/net_vpc/fixture/variables.tf b/tests/modules/net_vpc/fixture/variables.tf index efa2ba7a5..806284733 100644 --- a/tests/modules/net_vpc/fixture/variables.tf +++ b/tests/modules/net_vpc/fixture/variables.tf @@ -125,3 +125,9 @@ variable "private_service_networking_range" { type = string default = null } + +variable "data_folder" { + description = "An optional folder containing the subnet configurations in YaML format." + type = string + default = null +} diff --git a/tests/modules/net_vpc/test_plan_subnets.py b/tests/modules/net_vpc/test_plan_subnets.py index 99375e8b6..166487b73 100644 --- a/tests/modules/net_vpc/test_plan_subnets.py +++ b/tests/modules/net_vpc/test_plan_subnets.py @@ -29,6 +29,19 @@ _VAR_SUBNETS = ( ']' ) +_VAR_DATA_FOLDER = "data" + + +def test_subnet_factory(plan_runner): + "Test subnet factory." + _, resources = plan_runner(FIXTURES_DIR, data_folder=_VAR_DATA_FOLDER) + assert len(resources) == 3 + subnets = [r['values'] + for r in resources if r['type'] == 'google_compute_subnetwork'] + assert set(s['name'] for s in subnets) == set( + ['factory-subnet']) + assert set(len(s['secondary_ip_range']) for s in subnets) == set([1]) + def test_subnets_simple(plan_runner): "Test subnets variable." @@ -58,9 +71,9 @@ def test_subnet_log_configs(plan_runner): for r in resources: if r['type'] != 'google_compute_subnetwork': continue - flow_logs[r['values']['name']] = [{key: config[key] for key in config.keys() - & {'aggregation_interval', 'flow_sampling', 'metadata'}} - for config in r['values']['log_config']] + flow_logs[r['values']['name']] = [{key: config[key] for key in config.keys() + & {'aggregation_interval', 'flow_sampling', 'metadata'}} + for config in r['values']['log_config']] assert flow_logs == { # enable, override one default option 'a': [{