|
|
@ -26,7 +26,7 @@ from framework.infrastructure import gcp |
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
logger = logging.getLogger(__name__) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
class ComputeV1(gcp.api.GcpProjectApiResource): # pylint: disable=too-many-public-methods |
|
|
|
# TODO(sergiitk): move someplace better |
|
|
|
# TODO(sergiitk): move someplace better |
|
|
|
_WAIT_FOR_BACKEND_SEC = 60 * 10 |
|
|
|
_WAIT_FOR_BACKEND_SEC = 60 * 10 |
|
|
|
_WAIT_FOR_OPERATION_SEC = 60 * 10 |
|
|
|
_WAIT_FOR_OPERATION_SEC = 60 * 10 |
|
|
@ -58,7 +58,7 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
name: str, |
|
|
|
name: str, |
|
|
|
protocol: HealthCheckProtocol, |
|
|
|
protocol: HealthCheckProtocol, |
|
|
|
*, |
|
|
|
*, |
|
|
|
port: Optional[int] = None) -> GcpResource: |
|
|
|
port: Optional[int] = None) -> 'GcpResource': |
|
|
|
if protocol is self.HealthCheckProtocol.TCP: |
|
|
|
if protocol is self.HealthCheckProtocol.TCP: |
|
|
|
health_check_field = 'tcpHealthCheck' |
|
|
|
health_check_field = 'tcpHealthCheck' |
|
|
|
elif protocol is self.HealthCheckProtocol.GRPC: |
|
|
|
elif protocol is self.HealthCheckProtocol.GRPC: |
|
|
@ -88,7 +88,7 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
|
|
|
|
|
|
|
|
def create_firewall_rule(self, name: str, network_url: str, |
|
|
|
def create_firewall_rule(self, name: str, network_url: str, |
|
|
|
source_ranges: List[str], |
|
|
|
source_ranges: List[str], |
|
|
|
ports: List[str]) -> Optional[GcpResource]: |
|
|
|
ports: List[str]) -> Optional['GcpResource']: |
|
|
|
try: |
|
|
|
try: |
|
|
|
return self._insert_resource( |
|
|
|
return self._insert_resource( |
|
|
|
self.api.firewalls(), { |
|
|
|
self.api.firewalls(), { |
|
|
@ -107,7 +107,7 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
# TODO(lidiz) use status_code() when we upgrade googleapiclient |
|
|
|
# TODO(lidiz) use status_code() when we upgrade googleapiclient |
|
|
|
if http_error.resp.status == 409: |
|
|
|
if http_error.resp.status == 409: |
|
|
|
logger.debug('Firewall rule %s already existed', name) |
|
|
|
logger.debug('Firewall rule %s already existed', name) |
|
|
|
return |
|
|
|
return None |
|
|
|
else: |
|
|
|
else: |
|
|
|
raise |
|
|
|
raise |
|
|
|
|
|
|
|
|
|
|
@ -117,10 +117,10 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
def create_backend_service_traffic_director( |
|
|
|
def create_backend_service_traffic_director( |
|
|
|
self, |
|
|
|
self, |
|
|
|
name: str, |
|
|
|
name: str, |
|
|
|
health_check: GcpResource, |
|
|
|
health_check: 'GcpResource', |
|
|
|
affinity_header: str = None, |
|
|
|
affinity_header: Optional[str] = None, |
|
|
|
protocol: Optional[BackendServiceProtocol] = None, |
|
|
|
protocol: Optional[BackendServiceProtocol] = None, |
|
|
|
subset_size: Optional[int] = None) -> GcpResource: |
|
|
|
subset_size: Optional[int] = None) -> 'GcpResource': |
|
|
|
if not isinstance(protocol, self.BackendServiceProtocol): |
|
|
|
if not isinstance(protocol, self.BackendServiceProtocol): |
|
|
|
raise TypeError(f'Unexpected Backend Service protocol: {protocol}') |
|
|
|
raise TypeError(f'Unexpected Backend Service protocol: {protocol}') |
|
|
|
body = { |
|
|
|
body = { |
|
|
@ -144,7 +144,7 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
} |
|
|
|
} |
|
|
|
return self._insert_resource(self.api.backendServices(), body) |
|
|
|
return self._insert_resource(self.api.backendServices(), body) |
|
|
|
|
|
|
|
|
|
|
|
def get_backend_service_traffic_director(self, name: str) -> GcpResource: |
|
|
|
def get_backend_service_traffic_director(self, name: str) -> 'GcpResource': |
|
|
|
return self._get_resource(self.api.backendServices(), |
|
|
|
return self._get_resource(self.api.backendServices(), |
|
|
|
backendService=name) |
|
|
|
backendService=name) |
|
|
|
|
|
|
|
|
|
|
@ -185,9 +185,9 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
name: str, |
|
|
|
name: str, |
|
|
|
matcher_name: str, |
|
|
|
matcher_name: str, |
|
|
|
src_hosts, |
|
|
|
src_hosts, |
|
|
|
dst_default_backend_service: GcpResource, |
|
|
|
dst_default_backend_service: 'GcpResource', |
|
|
|
dst_host_rule_match_backend_service: Optional[GcpResource] = None, |
|
|
|
dst_host_rule_match_backend_service: Optional['GcpResource'] = None, |
|
|
|
) -> GcpResource: |
|
|
|
) -> 'GcpResource': |
|
|
|
if dst_host_rule_match_backend_service is None: |
|
|
|
if dst_host_rule_match_backend_service is None: |
|
|
|
dst_host_rule_match_backend_service = dst_default_backend_service |
|
|
|
dst_host_rule_match_backend_service = dst_default_backend_service |
|
|
|
return self._insert_resource( |
|
|
|
return self._insert_resource( |
|
|
@ -206,10 +206,10 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
}], |
|
|
|
}], |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
def create_url_map_with_content(self, url_map_body: Any) -> GcpResource: |
|
|
|
def create_url_map_with_content(self, url_map_body: Any) -> 'GcpResource': |
|
|
|
return self._insert_resource(self.api.urlMaps(), url_map_body) |
|
|
|
return self._insert_resource(self.api.urlMaps(), url_map_body) |
|
|
|
|
|
|
|
|
|
|
|
def patch_url_map(self, url_map: GcpResource, body, **kwargs): |
|
|
|
def patch_url_map(self, url_map: 'GcpResource', body, **kwargs): |
|
|
|
self._patch_resource(collection=self.api.urlMaps(), |
|
|
|
self._patch_resource(collection=self.api.urlMaps(), |
|
|
|
urlMap=url_map.name, |
|
|
|
urlMap=url_map.name, |
|
|
|
body=body, |
|
|
|
body=body, |
|
|
@ -221,9 +221,9 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
def create_target_grpc_proxy( |
|
|
|
def create_target_grpc_proxy( |
|
|
|
self, |
|
|
|
self, |
|
|
|
name: str, |
|
|
|
name: str, |
|
|
|
url_map: GcpResource, |
|
|
|
url_map: 'GcpResource', |
|
|
|
validate_for_proxyless: bool = True, |
|
|
|
validate_for_proxyless: bool = True, |
|
|
|
) -> GcpResource: |
|
|
|
) -> 'GcpResource': |
|
|
|
return self._insert_resource( |
|
|
|
return self._insert_resource( |
|
|
|
self.api.targetGrpcProxies(), { |
|
|
|
self.api.targetGrpcProxies(), { |
|
|
|
'name': name, |
|
|
|
'name': name, |
|
|
@ -238,8 +238,8 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
def create_target_http_proxy( |
|
|
|
def create_target_http_proxy( |
|
|
|
self, |
|
|
|
self, |
|
|
|
name: str, |
|
|
|
name: str, |
|
|
|
url_map: GcpResource, |
|
|
|
url_map: 'GcpResource', |
|
|
|
) -> GcpResource: |
|
|
|
) -> 'GcpResource': |
|
|
|
return self._insert_resource(self.api.targetHttpProxies(), { |
|
|
|
return self._insert_resource(self.api.targetHttpProxies(), { |
|
|
|
'name': name, |
|
|
|
'name': name, |
|
|
|
'url_map': url_map.url, |
|
|
|
'url_map': url_map.url, |
|
|
@ -252,10 +252,10 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
def create_forwarding_rule(self, |
|
|
|
def create_forwarding_rule(self, |
|
|
|
name: str, |
|
|
|
name: str, |
|
|
|
src_port: int, |
|
|
|
src_port: int, |
|
|
|
target_proxy: GcpResource, |
|
|
|
target_proxy: 'GcpResource', |
|
|
|
network_url: str, |
|
|
|
network_url: str, |
|
|
|
*, |
|
|
|
*, |
|
|
|
ip_address: str = '0.0.0.0') -> GcpResource: |
|
|
|
ip_address: str = '0.0.0.0') -> 'GcpResource': |
|
|
|
return self._insert_resource( |
|
|
|
return self._insert_resource( |
|
|
|
self.api.globalForwardingRules(), |
|
|
|
self.api.globalForwardingRules(), |
|
|
|
{ |
|
|
|
{ |
|
|
@ -367,14 +367,14 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
}).execute() |
|
|
|
}).execute() |
|
|
|
|
|
|
|
|
|
|
|
def _get_resource(self, collection: discovery.Resource, |
|
|
|
def _get_resource(self, collection: discovery.Resource, |
|
|
|
**kwargs) -> GcpResource: |
|
|
|
**kwargs) -> 'GcpResource': |
|
|
|
resp = collection.get(project=self.project, **kwargs).execute() |
|
|
|
resp = collection.get(project=self.project, **kwargs).execute() |
|
|
|
logger.info('Loaded compute resource:\n%s', |
|
|
|
logger.info('Loaded compute resource:\n%s', |
|
|
|
self.resource_pretty_format(resp)) |
|
|
|
self.resource_pretty_format(resp)) |
|
|
|
return self.GcpResource(resp['name'], resp['selfLink']) |
|
|
|
return self.GcpResource(resp['name'], resp['selfLink']) |
|
|
|
|
|
|
|
|
|
|
|
def _exists_resource(self, collection: discovery.Resource, |
|
|
|
def _exists_resource( |
|
|
|
filter: str) -> bool: |
|
|
|
self, collection: discovery.Resource, filter: str) -> bool: # pylint: disable=redefined-builtin |
|
|
|
resp = collection.list( |
|
|
|
resp = collection.list( |
|
|
|
project=self.project, filter=filter, |
|
|
|
project=self.project, filter=filter, |
|
|
|
maxResults=1).execute(num_retries=self._GCP_API_RETRIES) |
|
|
|
maxResults=1).execute(num_retries=self._GCP_API_RETRIES) |
|
|
@ -384,7 +384,7 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
return 'items' in resp and resp['items'] |
|
|
|
return 'items' in resp and resp['items'] |
|
|
|
|
|
|
|
|
|
|
|
def _insert_resource(self, collection: discovery.Resource, |
|
|
|
def _insert_resource(self, collection: discovery.Resource, |
|
|
|
body: Dict[str, Any]) -> GcpResource: |
|
|
|
body: Dict[str, Any]) -> 'GcpResource': |
|
|
|
logger.info('Creating compute resource:\n%s', |
|
|
|
logger.info('Creating compute resource:\n%s', |
|
|
|
self.resource_pretty_format(body)) |
|
|
|
self.resource_pretty_format(body)) |
|
|
|
resp = self._execute(collection.insert(project=self.project, body=body)) |
|
|
|
resp = self._execute(collection.insert(project=self.project, body=body)) |
|
|
@ -420,11 +420,12 @@ class ComputeV1(gcp.api.GcpProjectApiResource): |
|
|
|
def _operation_status_done(operation): |
|
|
|
def _operation_status_done(operation): |
|
|
|
return 'status' in operation and operation['status'] == 'DONE' |
|
|
|
return 'status' in operation and operation['status'] == 'DONE' |
|
|
|
|
|
|
|
|
|
|
|
def _execute(self, |
|
|
|
def _execute( # pylint: disable=arguments-differ |
|
|
|
request, |
|
|
|
self, |
|
|
|
*, |
|
|
|
request, |
|
|
|
test_success_fn=None, |
|
|
|
*, |
|
|
|
timeout_sec=_WAIT_FOR_OPERATION_SEC): |
|
|
|
test_success_fn=None, |
|
|
|
|
|
|
|
timeout_sec=_WAIT_FOR_OPERATION_SEC): |
|
|
|
operation = request.execute(num_retries=self._GCP_API_RETRIES) |
|
|
|
operation = request.execute(num_retries=self._GCP_API_RETRIES) |
|
|
|
logger.debug('Response %s', operation) |
|
|
|
logger.debug('Response %s', operation) |
|
|
|
|
|
|
|
|
|
|
|