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

Avoid setting empty IAM binding in subnet factory #731

Merged
merged 2 commits into from
Jul 11, 2022
Merged
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
9 changes: 5 additions & 4 deletions modules/net-vpc/subnets.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ locals {
subnet = k
role = "roles/compute.networkUser"
members = concat(
formatlist("group:%s", try(v.iam_groups, [])),
formatlist("user:%s", try(v.iam_users, [])),
formatlist("serviceAccount:%s", try(v.iam_service_accounts, []))
formatlist("group:%s", lookup(v, "iam_groups", [])),
formatlist("user:%s", lookup(v, "iam_users", [])),
formatlist("serviceAccount:%s", lookup(v, "iam_service_accounts", []))
)
}
]
Expand Down Expand Up @@ -73,7 +73,8 @@ locals {
local._factory_descriptions, var.subnet_descriptions
)
subnet_iam_members = concat(
local._factory_iam_members, local._subnet_iam_members
[for k in local._factory_iam_members : k if length(k.members) > 0],
local._subnet_iam_members
)
subnet_flow_logs = merge(
local._factory_flow_logs, local._subnet_flow_logs
Expand Down
28 changes: 14 additions & 14 deletions tests/modules/net_vpc/test_plan_subnets.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@
' secondary_ip_range=null},'
'{name = "c", region = "europe-west1", ip_cidr_range = "10.0.2.0/24",'
' secondary_ip_range={a="192.168.0.0/24", b="192.168.1.0/24"}},'
']'
)
']')

_VAR_DATA_FOLDER = "data"


def test_subnet_factory(plan_runner):
"Test subnet factory."
_, resources = plan_runner(data_folder=_VAR_DATA_FOLDER)
assert len(resources) == 5
subnets = [r['values']
for r in resources if r['type'] == 'google_compute_subnetwork']
assert len(resources) == 3
subnets = [
r['values'] for r in resources if r['type'] == 'google_compute_subnetwork'
]
assert {s['name'] for s in subnets} == {'factory-subnet', 'factory-subnet2'}
assert {len(s['secondary_ip_range']) for s in subnets} == {0, 1}

Expand All @@ -40,8 +40,9 @@ def test_subnets_simple(plan_runner):
"Test subnets variable."
_, resources = plan_runner(subnets=_VAR_SUBNETS)
assert len(resources) == 4
subnets = [r['values']
for r in resources if r['type'] == 'google_compute_subnetwork']
subnets = [
r['values'] for r in resources if r['type'] == 'google_compute_subnetwork'
]
assert {s['name'] for s in subnets} == {'a', 'b', 'c'}
assert {len(s['secondary_ip_range']) for s in subnets} == {0, 0, 2}

Expand All @@ -51,21 +52,20 @@ def test_subnet_log_configs(plan_runner):
log_config = '{"europe-west1/a" = { flow_sampling = 0.1 }}'
log_config_defaults = (
'{aggregation_interval = "INTERVAL_10_MIN", flow_sampling = 0.5, '
'metadata = "INCLUDE_ALL_METADATA"}'
)
'metadata = "INCLUDE_ALL_METADATA"}')
subnet_flow_logs = '{"europe-west1/a"=true, "europe-west1/b"=true}'
_, resources = plan_runner(subnets=_VAR_SUBNETS,
log_configs=log_config,
_, resources = plan_runner(subnets=_VAR_SUBNETS, log_configs=log_config,
log_config_defaults=log_config_defaults,
subnet_flow_logs=subnet_flow_logs)
assert len(resources) == 4
flow_logs = {}
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': [{
Expand Down