async def get_metadata(): """Retrieves JSON-formatted metadata from the local metadata server.""" request = HTTPRequest(METADATA_SERVER, headers=METADATA_HEADER) client = AsyncHTTPClient() metadata = {} try: app_log.debug('Retrieving GCE Metadata') response = await client.fetch(request) metadata = json.loads(response.body) except HTTPClientError as e: app_log.error('Unable to retrieve Metadata %s: %s', e.code, e.message) return metadata
async def get_pricelist(): """Retrieves JSON-formatted pricelist data from cloud pricing calculator.""" client = AsyncHTTPClient() pricelist = {} try: app_log.debug('Retrieving pricelist') response = await client.fetch( "https://cloudpricingcalculator.appspot.com/static/data/pricelist.json" ) pricelist = json.loads(response.body) except HTTPClientError as e: app_log.error('Unable to retrieve pricelist %s: %s', e.code, e.message) return pricelist
async def _make_request(self, base64_url, method='GET', body=None): try: url = b64decode(base64_url).decode() if not (url.startswith('https://') and 'googleapis.com' in url): raise ValueError('URL is not a valid Google API service') except ValueError as e: app_log.exception(e) self.set_status(400) self.finish({ 'error': { 'code': 400, 'status': 'BAD_REQUEST', 'message': str(e) } }) return try: headers = AuthProvider.get().get_header() except: self.set_status(403) self.finish({ 'error': { 'code': 403, 'status': 'UNAUTHORIZED', 'message': 'Unable to obtain authentication token' } }) return headers['Content-Type'] = 'application/json' user_agent = 'jupyterlab_gcpextension/{}'.format(VERSION) request = HTTPRequest(url, method, headers, body, user_agent=user_agent) client = AsyncHTTPClient() try: app_log.info('Proxying GCP %s request to %s', method, url) response = await client.fetch(request) self.finish(response.body) except HTTPClientError as e: app_log.error('GCP %s request to %s returned %s: %s', method, url, e.code, e.message) self.set_status(e.code) self.finish(e.response.body)
async def get_gpu_list(zone): """Uses gcloud to return a list of available Accelerator Types.""" accelerator_types = [] zone = zone[zone.rindex('/') + 1:] app_log.debug('Getting Accelerator Types from gcloud') process = await asyncio.create_subprocess_shell( '{} --filter="zone:{}"'.format(ACCELERATOR_TYPES_CMD, zone), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) stdout, _ = await process.communicate() if process.returncode != 0: app_log.error('Unable to obtain Accelerator Types from gcloud') return accelerator_types accelerator_types = json.loads(stdout.decode()) return accelerator_types
async def get_machine_type_details(zone, machine_type): """Uses gcloud to return the Machine Type details.""" machine_type_name = machine_type[machine_type.rindex('/') + 1:] details = {NAME: machine_type_name, DESCRIPTION: ''} app_log.debug('Getting Machine Type details from gcloud') process = await asyncio.create_subprocess_shell( '{} {} --zone {}'.format(MACHINE_TYPE_CMD, machine_type_name, zone), stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE) stdout, _ = await process.communicate() if process.returncode != 0: app_log.error('Unable to obtain Machine Type details for %s', machine_type) return details for line in stdout.decode().splitlines(): if line.startswith(DESCRIPTION): details[DESCRIPTION] = line.split(': ')[1].strip() elif line.startswith(NAME): details[NAME] = line.split(': ')[1].strip() return details