Skip to content

Commit 79f7c95

Browse files
authored
feat: Removed preconditions script from Terraform execution (#478)
1 parent d1665d1 commit 79f7c95

File tree

15 files changed

+81
-103
lines changed

15 files changed

+81
-103
lines changed

README.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,7 @@ determining that location is as follows:
135135
| lien | Add a lien on the project to prevent accidental deletion | bool | `"false"` | no |
136136
| name | The name for the project | string | n/a | yes |
137137
| org\_id | The organization ID. | string | n/a | yes |
138-
| pip\_executable\_path | Pip executable path for precondition requirements.txt install. | string | `"pip3"` | no |
139138
| project\_id | The ID to give the project. If not provided, the `name` will be used. | string | `""` | no |
140-
| python\_interpreter\_path | Python interpreter path for precondition check script. | string | `"python3"` | no |
141139
| random\_project\_id | Adds a suffix of 4 random characters to the `project_id` | bool | `"false"` | no |
142140
| sa\_role | A role to give the default Service Account for the project (defaults to none) | string | `""` | no |
143141
| shared\_vpc | The ID of the host project which hosts the shared VPC | string | `""` | no |
@@ -322,7 +320,7 @@ The precondition checker script can be directly invoked before running the
322320
project factory:
323321

324322
```sh
325-
./modules/core_project_factory/scripts/preconditions/preconditions.py \
323+
./helpers/preconditions/preconditions.py \
326324
--credentials_path "./credentials.json" \
327325
--billing_account 000000-000000-000000 \
328326
--org_id 000000000000 \
@@ -353,7 +351,7 @@ binary here:
353351
- https://releases.hashicorp.com/terraform/
354352

355353
[gsuite-enabled-module]: modules/gsuite_enabled/README.md
356-
[preconditions-checker-script]: modules/core_project_factory/scripts/preconditions/preconditions.py
354+
[preconditions-checker-script]: helpers/preconditions/preconditions.py
357355
[terraform-provider-google]: https://github.com/terraform-providers/terraform-provider-google
358356
[terraform-provider-google-beta]: https://github.com/terraform-providers/terraform-provider-google-beta
359357
[terraform-provider-gsuite]: https://github.com/DeviaVir/terraform-provider-gsuite
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# Running preconditions script
2+
This module provides a helper script in order to check if the SEED (project where the GCP Service Account was created) met the requirements to satisfy a project creation needs. For example, check billing account permissions or if certain service API is enabled or not.
3+
4+
5+
# VirtualEnv (Optional)
6+
We recommend running the script inside of a [Python virtual environment](https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/) to avoid installing extra packages in your Python default environment.
7+
8+
After installing virtual env by following the link above, create a new Python environment by running:
9+
```
10+
$ python3 -m venv /tmp/preconditions
11+
```
12+
or
13+
```
14+
$ python2 -m virtualenv /tmp/preconditions
15+
```
16+
17+
Finally, activate it:
18+
```
19+
$ source /tmp/preconditions/bin/activate
20+
```
21+
22+
# How to
23+
Do the following steps in order to run preconditions script:
24+
25+
1) Install Python dependencies
26+
```
27+
$ pip install -r helpers/preconditions/requirements.txt
28+
```
29+
<p><b>Note: If you are not running from virtualenv add the suffix --user on each command line</b></p>
30+
1) Execute script
31+
```
32+
$ GOOGLE_CLOUD_PROJECT=my-seed-project python helpers/preconditions/preconditions.py --billing_account [REDACTED] --org_id [REDACTED] --folder_id [REDACTED]
33+
[
34+
{
35+
"type": "Required APIs on service account project",
36+
"name": "projects/my-seed-project",
37+
"satisfied": [
38+
"iam.googleapis.com"
39+
],
40+
"unsatisfied": [
41+
"admin.googleapis.com",
42+
"cloudresourcemanager.googleapis.com",
43+
"cloudbilling.googleapis.com"
44+
]
45+
},
46+
{
47+
"type": "Service account permissions on billing account",
48+
"name": "billingAccounts/[REDACTED]",
49+
"satisfied": [
50+
"billing.resourceAssociations.create"
51+
],
52+
"unsatisfied": []
53+
},
54+
{
55+
"type": "Service account permissions on parent folder",
56+
"name": "folders/[REDACTED]",
57+
"satisfied": [
58+
"resourcemanager.projects.create"
59+
],
60+
"unsatisfied": []
61+
},
62+
{
63+
"type": "Service account permissions on organization",
64+
"name": "organizations/[REDACTED]",
65+
"satisfied": [],
66+
"unsatisfied": []
67+
}
68+
]
69+
```
70+
Check #1 (Required APIs on service account project) => It is missing to enable admin, cloudresourcemanager and cloudbilling services APIs in the <b>my-seed-project</b>.
71+
72+
Check #2 (Service account permissions on billing accoun) => The permission required to associate projects with billing accounts is okay.
73+
74+
Check #3 (Service account permissions on parent folder) => The permission to create new projects into the folder specified is granted.
75+
76+
Check #4 (Service account permissions on organization) => No permission required since we are creating the project under the folder instead of the organisation. If no folder is specified it would be step three and require projects.create permission.
77+
78+
You can add one last check by setting the `--shared-vpc` parameter.

main.tf

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ module "project-factory" {
5858
disable_services_on_destroy = var.disable_services_on_destroy
5959
default_service_account = var.default_service_account
6060
disable_dependent_services = var.disable_dependent_services
61-
python_interpreter_path = var.python_interpreter_path
62-
pip_executable_path = var.pip_executable_path
6361
use_tf_google_credentials_env_var = var.use_tf_google_credentials_env_var
6462
skip_gcloud_download = var.skip_gcloud_download
6563
vpc_service_control_attach_enabled = var.vpc_service_control_attach_enabled

modules/core_project_factory/locals.tf

Lines changed: 0 additions & 31 deletions
This file was deleted.

modules/core_project_factory/main.tf

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,6 @@ locals {
5959
shared_vpc_users_length = 3
6060
}
6161

62-
resource "null_resource" "preconditions" {
63-
triggers = {
64-
credentials_path = var.credentials_path
65-
billing_account = var.billing_account
66-
org_id = var.org_id
67-
folder_id = var.folder_id
68-
shared_vpc = var.shared_vpc
69-
}
70-
71-
provisioner "local-exec" {
72-
command = local.pip_requirements_absolute_path
73-
interpreter = [var.pip_executable_path, "install", "-r"]
74-
on_failure = continue
75-
}
76-
77-
provisioner "local-exec" {
78-
command = local.preconditions_command
79-
on_failure = continue
80-
environment = {
81-
GRACEFUL_IMPORTERROR = "true"
82-
}
83-
}
84-
}
85-
8662
/*******************************************
8763
Project creation
8864
*******************************************/
@@ -95,8 +71,6 @@ resource "google_project" "main" {
9571
auto_create_network = var.auto_create_network
9672

9773
labels = var.labels
98-
99-
depends_on = [null_resource.preconditions]
10074
}
10175

10276
/******************************************

modules/core_project_factory/variables.tf

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,18 +193,6 @@ variable "enable_shared_vpc_host_project" {
193193
default = false
194194
}
195195

196-
variable "python_interpreter_path" {
197-
description = "Python interpreter path for precondition check script."
198-
type = string
199-
default = "python3"
200-
}
201-
202-
variable "pip_executable_path" {
203-
description = "Pip executable path for precondition requirements.txt install."
204-
type = string
205-
default = "pip3"
206-
}
207-
208196
variable "use_tf_google_credentials_env_var" {
209197
description = "Use GOOGLE_CREDENTIALS environment variable to run gcloud auth activate-service-account with."
210198
type = bool

modules/gsuite_enabled/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ The roles granted are specifically:
8686
| name | The name for the project | string | n/a | yes |
8787
| org\_id | The organization ID. | string | n/a | yes |
8888
| project\_id | The ID to give the project. If not provided, the `name` will be used. | string | `""` | no |
89-
| python\_interpreter\_path | Python interpreter path for precondition check script. | string | `"python3"` | no |
9089
| random\_project\_id | Adds a suffix of 4 random characters to the `project_id` | string | `"false"` | no |
9190
| sa\_group | A G Suite group to place the default Service Account for the project in | string | `""` | no |
9291
| sa\_role | A role to give the default Service Account for the project (defaults to none) | string | `""` | no |

modules/gsuite_enabled/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ module "project-factory" {
9999
disable_services_on_destroy = var.disable_services_on_destroy
100100
default_service_account = var.default_service_account
101101
disable_dependent_services = var.disable_dependent_services
102-
python_interpreter_path = var.python_interpreter_path
103102
use_tf_google_credentials_env_var = var.use_tf_google_credentials_env_var
104103
skip_gcloud_download = var.skip_gcloud_download
105104
}

0 commit comments

Comments
 (0)