Skip to content

Commit 4345089

Browse files
committed
don't fail require-all-resources-from-pmr.sentinel if destroy
1 parent 9a136cb commit 4345089

File tree

8 files changed

+257
-3
lines changed

8 files changed

+257
-3
lines changed

governance/third-generation/cloud-agnostic/require-all-resources-from-pmr.sentinel

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
# Import the tfconfig/v2 import, but use the alias "tfconfig"
66
import "tfconfig/v2" as tfconfig
7+
8+
# Import the tfrun import
9+
import "tfrun"
10+
711
# Standard strings import
812
import "strings"
913

@@ -20,7 +24,7 @@ violatingMCs = filter tfconfig.module_calls as index, mc {
2024
}
2125

2226
# Print violation messages for invalid modules
23-
if length(violatingMCs) > 0 {
27+
if length(violatingMCs) > 0 and not tfrun.is_destroy {
2428
print("All modules called from the root module must come from the",
2529
"private module registry", address + "/" + organization)
2630
for violatingMCs as address, mc {
@@ -35,14 +39,16 @@ rootModuleResources = filter tfconfig.resources as address, r {
3539
}
3640

3741
# Print violation messages for root module resources and data sources
38-
if length(rootModuleResources) > 0 {
42+
if length(rootModuleResources) > 0 and not tfrun.is_destroy {
3943
print("Resources and data sources are not allowed in the root module.")
4044
print("Your root module has", length(rootModuleResources), "resources and",
4145
"data sources.")
4246
}
4347

4448
# Main rule
45-
validated = length(violatingMCs) is 0 and length(rootModuleResources) is 0
49+
validated = tfrun.is_destroy or
50+
(length(violatingMCs) is 0 and
51+
length(rootModuleResources) is 0)
4652
main = rule {
4753
validated is true
4854
}

governance/third-generation/cloud-agnostic/test/require-all-resources-from-pmr/fail.hcl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ mock "tfconfig/v2" {
1212
}
1313
}
1414

15+
mock "tfrun" {
16+
module {
17+
source = "mock-tfrun-fail.sentinel"
18+
}
19+
}
20+
1521
test {
1622
rules = {
1723
main = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import "strings"
2+
3+
providers = {
4+
"aws": {
5+
"alias": "",
6+
"config": {},
7+
"module_address": "",
8+
"name": "aws",
9+
"provider_config_key": "aws",
10+
"version_constraint": "",
11+
},
12+
}
13+
14+
resources = {
15+
"module.nested.aws_instance.ubuntu": {
16+
"address": "module.nested.aws_instance.ubuntu",
17+
"config": {
18+
"ami": {
19+
"references": [
20+
"var.ami_id",
21+
],
22+
},
23+
"associate_public_ip_address": {
24+
"references": [
25+
"var.associate_public_ip_address",
26+
],
27+
},
28+
"availability_zone": {
29+
"references": [
30+
"var.aws_region",
31+
],
32+
},
33+
"instance_type": {
34+
"references": [
35+
"var.instance_type",
36+
],
37+
},
38+
"tags": {
39+
"references": [
40+
"var.name",
41+
],
42+
},
43+
},
44+
"count": {},
45+
"depends_on": [],
46+
"for_each": {},
47+
"mode": "managed",
48+
"module_address": "module.nested",
49+
"name": "ubuntu",
50+
"provider_config_key": "module.nested:aws",
51+
"provisioners": [],
52+
"type": "aws_instance",
53+
},
54+
}
55+
56+
provisioners = {}
57+
58+
variables = {
59+
"module.nested:ami_id": {
60+
"default": "ami-2e1ef954",
61+
"description": "ID of the AMI to provision. Default is Ubuntu 14.04 Base Image",
62+
"module_address": "module.nested",
63+
"name": "ami_id",
64+
},
65+
"module.nested:associate_public_ip_address": {
66+
"default": true,
67+
"description": "",
68+
"module_address": "module.nested",
69+
"name": "associate_public_ip_address",
70+
},
71+
"module.nested:aws_region": {
72+
"default": "us-east-1",
73+
"description": "AWS region",
74+
"module_address": "module.nested",
75+
"name": "aws_region",
76+
},
77+
"module.nested:instance_type": {
78+
"default": "t2.micro",
79+
"description": "type of EC2 instance to provision.",
80+
"module_address": "module.nested",
81+
"name": "instance_type",
82+
},
83+
"module.nested:name": {
84+
"default": "roger-demo-nested",
85+
"description": "name to pass to Name tag",
86+
"module_address": "module.nested",
87+
"name": "name",
88+
},
89+
"owners": {
90+
"default": [
91+
"099720109477",
92+
"099720109476",
93+
],
94+
"description": "list of owners of AMI",
95+
"module_address": "",
96+
"name": "owners",
97+
},
98+
}
99+
100+
outputs = {
101+
"module.nested:public_dns": {
102+
"depends_on": [],
103+
"description": "",
104+
"module_address": "module.nested",
105+
"name": "public_dns",
106+
"sensitive": false,
107+
"value": {
108+
"references": [
109+
"aws_instance.ubuntu",
110+
],
111+
},
112+
},
113+
}
114+
115+
module_calls = {
116+
"nested": {
117+
"config": {},
118+
"count": {},
119+
"for_each": {},
120+
"module_address": "",
121+
"name": "nested",
122+
"source": "app.terraform.io/Cloud-Operations/compute/aws",
123+
"version_constraint": "",
124+
},
125+
}
126+
127+
strip_index = func(addr) {
128+
s = strings.split(addr, ".")
129+
for s as i, v {
130+
s[i] = strings.split(v, "[")[0]
131+
}
132+
133+
return strings.join(s, ".")
134+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
id = "run-tnXKfMSc4LHMX14h"
2+
created_at = "2021-04-13T18:49:56.141Z"
3+
message = "accept 400"
4+
commit_sha = undefined
5+
speculative = false
6+
is_destroy = false
7+
target_addrs = null
8+
9+
variables = {}
10+
11+
organization = {
12+
"name": "Cloud-Operations",
13+
}
14+
15+
workspace = {
16+
"auto_apply": false,
17+
"created_at": "2019-05-11T19:13:07.766Z",
18+
"description": null,
19+
"id": "ws-fo8TJnedDLEWfP3K",
20+
"name": "aws-ec2-instance",
21+
"vcs_repo": null,
22+
"working_directory": "",
23+
}
24+
25+
cost_estimate = {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
id = "run-tnXKfMSc4LHMX14h"
2+
created_at = "2021-04-13T18:49:56.141Z"
3+
message = "accept 400"
4+
commit_sha = undefined
5+
speculative = false
6+
is_destroy = true
7+
target_addrs = null
8+
9+
variables = {}
10+
11+
organization = {
12+
"name": "Cloud-Operations",
13+
}
14+
15+
workspace = {
16+
"auto_apply": false,
17+
"created_at": "2019-05-11T19:13:07.766Z",
18+
"description": null,
19+
"id": "ws-fo8TJnedDLEWfP3K",
20+
"name": "aws-ec2-instance",
21+
"vcs_repo": null,
22+
"working_directory": "",
23+
}
24+
25+
cost_estimate = {}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
id = "run-tnXKfMSc4LHMX14h"
2+
created_at = "2021-04-13T18:49:56.141Z"
3+
message = "accept 400"
4+
commit_sha = undefined
5+
speculative = false
6+
is_destroy = false
7+
target_addrs = null
8+
9+
variables = {}
10+
11+
organization = {
12+
"name": "Cloud-Operations",
13+
}
14+
15+
workspace = {
16+
"auto_apply": false,
17+
"created_at": "2019-05-11T19:13:07.766Z",
18+
"description": null,
19+
"id": "ws-fo8TJnedDLEWfP3K",
20+
"name": "aws-ec2-instance",
21+
"vcs_repo": null,
22+
"working_directory": "",
23+
}
24+
25+
cost_estimate = {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
param "address" {
2+
value = "app.terraform.io"
3+
}
4+
5+
param "organization" {
6+
value = "Cloud-Operations"
7+
}
8+
9+
mock "tfconfig/v2" {
10+
module {
11+
source = "mock-tfconfig-pass-destroy.sentinel"
12+
}
13+
}
14+
15+
mock "tfrun" {
16+
module {
17+
source = "mock-tfrun-pass-destroy.sentinel"
18+
}
19+
}
20+
21+
22+
test {
23+
rules = {
24+
main = true
25+
}
26+
}

governance/third-generation/cloud-agnostic/test/require-all-resources-from-pmr/pass.hcl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ mock "tfconfig/v2" {
1212
}
1313
}
1414

15+
mock "tfrun" {
16+
module {
17+
source = "mock-tfrun-pass.sentinel"
18+
}
19+
}
20+
21+
1522
test {
1623
rules = {
1724
main = true

0 commit comments

Comments
 (0)