/
cdp_stack.py
247 lines (217 loc) · 9.46 KB
/
cdp_stack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
from typing import List
import pulumi
import pulumi_gcp as gcp
from pulumi_google_native.firestore import v1 as firestore
from ..database import DATABASE_MODELS
from ..version import __version__
###############################################################################
class GoverningBody:
city_council = "city council"
county_council = "county council"
school_board = "school board"
other = "other"
class CDPStack(pulumi.ComponentResource):
def __init__(
self,
gcp_project_id: str,
municipality_name: str = "dev_municipality",
hosting_github_url: str = "no_github_host",
hosting_web_app_address: str = "no_web_app_address",
firestore_location: str = "us-west2",
governing_body: str = GoverningBody.other,
opts: pulumi.ResourceOptions = None,
):
"""
Creates all required infrastructure and enables all required services for CDP
backend stacks.
Parameters
----------
gcp_project_id: str
The id of the gcp_project, the Pulumi stack, and any other required
names for resources created during infrastructure creation will use this id
as a prefix.
I.E. `cdp-seattle` would create a Cloud Firestore instance called
`cdp-seattle`, a GCP bucket called `cdp-seattle`, etc.
municipality_name: str
The name of the municipality this instance will be for.
I.E. "Seattle", "King County", etc.
hosting_github_url: str
The GitHub repo this instance configuration details will be stored.
I.E. https://github.com/councildataproject/seattle-staging
hosting_web_app_address: str
The web address for this instances web portal.
I.E. https://councildataproject.org/seattle-staging
firestore_location: str
The location for the Cloud Firestore database and file storage servers
to be hosted.
List of locations: https://firebase.google.com/docs/firestore/locations
Default: "us-west2"
governing_body: str
What governing body this instance will archive.
Default: GoverningBody.other
opts: pulumi.ResourceOptions
Extra resource options to initialize the entire stack with.
Default: None
Notes
-----
When using this resource it is recommended to run set Pulumi parallel resource
creation to five (5) max. GCP has limits on how many resources you can create
in parallel.
The default values for many parameters are set to fake values as this object
should primarily be used with the cookiecutter / automated scripts. The default
values in this case are set because when using this object outside of
the cookiecutter it is likely for dev infrastructures.
"""
super().__init__("CDPStack", gcp_project_id, None, opts)
# Store parameters
self.gcp_project_id = gcp_project_id
self.firestore_location = firestore_location
# Enable all required services
self.cloudresourcemanager = gcp.projects.Service(
f"{self.gcp_project_id}-cloudresourcemanager-service",
disable_dependent_services=True,
project=self.gcp_project_id,
service="cloudresourcemanager.googleapis.com",
opts=pulumi.ResourceOptions(parent=self),
)
self.speech_service = gcp.projects.Service(
f"{self.gcp_project_id}-speech-service",
disable_dependent_services=True,
project=self.gcp_project_id,
service="speech.googleapis.com",
opts=pulumi.ResourceOptions(
parent=self, depends_on=[self.cloudresourcemanager]
),
)
self.firebase_service = gcp.projects.Service(
f"{self.gcp_project_id}-firebase-service",
disable_dependent_services=True,
project=self.gcp_project_id,
service="firebase.googleapis.com",
opts=pulumi.ResourceOptions(
parent=self, depends_on=[self.cloudresourcemanager]
),
)
self.app_engine_service = gcp.projects.Service(
f"{self.gcp_project_id}-app-engine-service",
disable_dependent_services=True,
project=self.gcp_project_id,
service="appengine.googleapis.com",
opts=pulumi.ResourceOptions(
parent=self, depends_on=[self.cloudresourcemanager]
),
)
self.firestore_service = gcp.projects.Service(
f"{self.gcp_project_id}-firestore-service",
disable_dependent_services=True,
project=self.gcp_project_id,
service="firestore.googleapis.com",
opts=pulumi.ResourceOptions(
parent=self, depends_on=[self.cloudresourcemanager]
),
)
self.firebase_rules_service = gcp.projects.Service(
f"{self.gcp_project_id}-firebase-rules-service",
disable_dependent_services=True,
project=self.gcp_project_id,
service="firebaserules.googleapis.com",
opts=pulumi.ResourceOptions(
parent=self, depends_on=[self.cloudresourcemanager]
),
)
# Create the firestore application
self.firestore_app = gcp.appengine.Application(
f"{self.gcp_project_id}-firestore-app",
project=self.gcp_project_id,
location_id=self.firestore_location,
database_type="CLOUD_FIRESTORE",
opts=pulumi.ResourceOptions(
parent=self,
depends_on=[
self.firebase_service,
self.app_engine_service,
self.firestore_service,
self.firebase_rules_service,
],
),
)
# Init firebase project
self.firebase_init = gcp.firebase.Project(
resource_name=f"{self.gcp_project_id}-firebase-init",
project=self.gcp_project_id,
opts=pulumi.ResourceOptions(parent=self, depends_on=[self.firestore_app]),
)
# Connect app engine (firestore) + bucket
self.firebase_project = gcp.firebase.ProjectLocation(
resource_name=f"{self.gcp_project_id}-firebase-project",
project=self.gcp_project_id,
location_id=self.firestore_location,
opts=pulumi.ResourceOptions(parent=self.firebase_init),
)
# Set full public read on bucket
self.storage_public_read = gcp.storage.DefaultObjectAccessControl(
f"{self.gcp_project_id}-storage-acl-public-viewer",
bucket=self.firestore_app.default_bucket,
entity="allUsers",
role="READER",
opts=pulumi.ResourceOptions(parent=self.firestore_app),
)
# Create all firestore indexes
prior_index = None
for model_cls in DATABASE_MODELS:
for idx_field_set in model_cls._INDEXES:
# Add fields to field list
idx_set_name_parts = []
idx_set_fields = []
for idx_field in idx_field_set.fields:
idx_set_name_parts += [idx_field.name, idx_field.order]
idx_set_fields.append(
firestore.GoogleFirestoreAdminV1IndexFieldArgs(
field_path=idx_field.name,
order=idx_field.order,
)
)
# Finish creating the index set name
idx_set_name = "_".join(idx_set_name_parts)
fq_idx_set_name = f"{model_cls.collection_name}-{idx_set_name}"
# Create depends on list
# We don't want to create a ton of indexes in parallel
if prior_index is None:
depends_on: List[pulumi.Resource] = []
else:
depends_on = [prior_index]
# Create
prior_index = firestore.Index(
fq_idx_set_name,
project=self.gcp_project_id,
database_id="(default)",
collection_group_id=model_cls.collection_name,
fields=idx_set_fields,
query_scope="COLLECTION",
opts=pulumi.ResourceOptions(
parent=self.firestore_app,
depends_on=depends_on,
),
)
# Add metadata document
gcp.firestore.Document(
f"{self.gcp_project_id}-metadata-doc",
collection="metadata",
document_id="configuration",
fields=json.dumps(
{
"infrastructure_version": {"stringValue": __version__},
"municipality_name": {"stringValue": municipality_name},
"hosting_github_url": {"stringValue": hosting_github_url},
"hosting_web_app_address": {"stringValue": hosting_web_app_address},
"firestore_location": {"stringValue": firestore_location},
"governing_body": {"stringValue": governing_body},
}
),
project=self.gcp_project_id,
opts=pulumi.ResourceOptions(parent=self.firestore_app),
)
super().register_outputs({})