|
2 | 2 | import asyncio |
3 | 3 | from abc import abstractmethod |
4 | 4 | from dataclasses import dataclass |
| 5 | +from pathlib import Path |
5 | 6 | from typing import Dict, Callable |
6 | 7 |
|
7 | 8 | from gcp_pilot.build import CloudBuild |
8 | 9 | from gcp_pilot.dns import CloudDNS, RecordType |
9 | | -from gcp_pilot.exceptions import NotFound |
| 10 | +from gcp_pilot.exceptions import NotFound, AlreadyExists |
10 | 11 | from gcp_pilot.iam import IdentityAccessManager |
11 | 12 | from gcp_pilot.resource import ResourceManager |
| 13 | +from gcp_pilot.api_gateway import APIGateway |
| 14 | +from gcp_pilot.service_usage import ServiceUsage |
12 | 15 | from gcp_pilot.run import CloudRun |
13 | 16 | from gcp_pilot.sql import CloudSQL |
14 | 17 | from gcp_pilot.storage import CloudStorage |
@@ -117,8 +120,83 @@ def get_jobs(self) -> Dict[str, Callable]: |
117 | 120 | } |
118 | 121 |
|
119 | 122 | async def setup_placeholder(self): |
120 | | - url = self.app.factory.get_url() |
121 | | - App.documents.update(pk=self.app.pk, endpoint=url) |
| 123 | + run = CloudRun() |
| 124 | + service_params = dict( |
| 125 | + service_name=self.app.name, |
| 126 | + location=self.app.region, |
| 127 | + project_id=self.app.project.id, |
| 128 | + ) |
| 129 | + |
| 130 | + try: |
| 131 | + run.create_service( |
| 132 | + service_account=self.app.service_account.email, |
| 133 | + **service_params, |
| 134 | + ) |
| 135 | + except AlreadyExists: |
| 136 | + pass |
| 137 | + |
| 138 | + url = None |
| 139 | + while not url: |
| 140 | + service = run.get_service(**service_params) |
| 141 | + url = service['status'].get('url') |
| 142 | + |
| 143 | + extra_update = {} |
| 144 | + if self.app.gateway: |
| 145 | + labels = {label.key: label.value for label in self.app.get_all_labels()} |
| 146 | + gateway = APIGateway() |
| 147 | + |
| 148 | + try: |
| 149 | + gateway_api = gateway.create_api( |
| 150 | + api_name=self.app.gateway.api_name, |
| 151 | + labels=labels, |
| 152 | + project_id=self.app.project.id, |
| 153 | + ) |
| 154 | + except AlreadyExists: |
| 155 | + gateway_api = gateway.get_api( |
| 156 | + api_name=self.app.gateway.api_name, |
| 157 | + project_id=self.app.project.id, |
| 158 | + ) |
| 159 | + |
| 160 | + try: |
| 161 | + gateway_config = gateway.create_config( |
| 162 | + config_name=f"{self.app.name}-placeholder", |
| 163 | + api_name=self.app.gateway.api_name, |
| 164 | + service_account=self.app.service_account.email, |
| 165 | + open_api_file=Path(__file__).parent / "placeholder.yaml", |
| 166 | + labels=labels, |
| 167 | + project_id=self.app.project.id, |
| 168 | + ) |
| 169 | + except AlreadyExists: |
| 170 | + gateway_config = gateway.get_config( |
| 171 | + config_name=f"{self.app.name}-placeholder", |
| 172 | + api_name=self.app.gateway.api_name, |
| 173 | + project_id=self.app.project.id, |
| 174 | + ) |
| 175 | + |
| 176 | + try: |
| 177 | + gateway_service = gateway.create_gateway( |
| 178 | + gateway_name=self.app.name, |
| 179 | + api_name=self.app.gateway.api_name, |
| 180 | + config_name=f"{self.app.name}-placeholder", |
| 181 | + labels=labels, |
| 182 | + project_id=self.app.project.id, |
| 183 | + location=self.app.region, |
| 184 | + ) |
| 185 | + except AlreadyExists: |
| 186 | + gateway_service = gateway.get_gateway( |
| 187 | + gateway_name=self.app.name, |
| 188 | + project_id=self.app.project.id, |
| 189 | + location=self.app.region, |
| 190 | + ) |
| 191 | + |
| 192 | + gateway_info = self.app.gateway |
| 193 | + gateway_info.gateway_service = gateway_api["managedService"] |
| 194 | + gateway_info.gateway_endpoint = "https://" + gateway_service["defaultHostname"] |
| 195 | + extra_update["gateway"] = gateway_info |
| 196 | + |
| 197 | + ServiceUsage().enable_service(service_name=gateway_info.gateway_service, project_id=self.app.project.id) |
| 198 | + |
| 199 | + App.documents.update(pk=self.app.pk, endpoint=url, **extra_update) |
122 | 200 |
|
123 | 201 | async def setup_bucket(self): |
124 | 202 | bucket = self.app.bucket |
|
0 commit comments