Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit ca52cb2

Browse files
committed
feat: add API Gateway placeholder
1 parent 092a5af commit ca52cb2

File tree

3 files changed

+118
-23
lines changed

3 files changed

+118
-23
lines changed

flamingo/services/builders.py

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -401,26 +401,7 @@ def get_url(self):
401401
url = service['status']['url']
402402
except NotFound as e:
403403
logger.warning(str(e))
404-
url = self._create_placeholder()
405-
return url
406-
407-
def _create_placeholder(self):
408-
run = CloudRun()
409-
service_params = dict(
410-
service_name=self.service_name,
411-
location=self.app.region,
412-
project_id=self.app.project.id,
413-
)
414-
415-
run.create_service(
416-
service_account=self.app.service_account.email,
417-
**service_params,
418-
)
419-
420-
url = None
421-
while not url:
422-
service = run.get_service(**service_params)
423-
url = service['status'].get('url')
404+
url = self._create_run_placeholder()
424405
return url
425406

426407

flamingo/services/foundations.py

Lines changed: 81 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
import asyncio
33
from abc import abstractmethod
44
from dataclasses import dataclass
5+
from pathlib import Path
56
from typing import Dict, Callable
67

78
from gcp_pilot.build import CloudBuild
89
from gcp_pilot.dns import CloudDNS, RecordType
9-
from gcp_pilot.exceptions import NotFound
10+
from gcp_pilot.exceptions import NotFound, AlreadyExists
1011
from gcp_pilot.iam import IdentityAccessManager
1112
from gcp_pilot.resource import ResourceManager
13+
from gcp_pilot.api_gateway import APIGateway
14+
from gcp_pilot.service_usage import ServiceUsage
1215
from gcp_pilot.run import CloudRun
1316
from gcp_pilot.sql import CloudSQL
1417
from gcp_pilot.storage import CloudStorage
@@ -117,8 +120,83 @@ def get_jobs(self) -> Dict[str, Callable]:
117120
}
118121

119122
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)
122200

123201
async def setup_bucket(self):
124202
bucket = self.app.bucket

flamingo/services/placeholder.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
swagger: '2.0'
2+
info:
3+
title: sample-api
4+
contact:
5+
email: tech@nilo.co
6+
version: v1
7+
host: 0.0.0.0
8+
schemes:
9+
- http
10+
basePath: /
11+
consumes:
12+
- application/json
13+
produces:
14+
- application/json
15+
securityDefinitions:
16+
Bearer:
17+
type: apiKey
18+
name: x-api-key
19+
in: header
20+
security:
21+
- Bearer: []
22+
paths:
23+
/healthcheck:
24+
get:
25+
operationId: healthcheck_list
26+
description: ''
27+
parameters: []
28+
responses:
29+
'200':
30+
description: ''
31+
tags:
32+
- healthcheck
33+
security: []
34+
parameters: []
35+
x-google-backend:
36+
address: https://sample-api-pawsjwquka-ue.a.run.app

0 commit comments

Comments
 (0)