Python oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_dict() Examples
The following are 28
code examples of oauth2client.service_account.ServiceAccountCredentials.from_json_keyfile_dict().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
oauth2client.service_account.ServiceAccountCredentials
, or try the search function
.
Example #1
Source File: gsheet.py From avrae with GNU General Public License v3.0 | 7 votes |
def _init_gsheet_client(): with GoogleSheet._client_lock(): def _(): if config.GOOGLE_SERVICE_ACCOUNT is not None: credentials = ServiceAccountCredentials.from_json_keyfile_dict( json.loads(config.GOOGLE_SERVICE_ACCOUNT), scopes=SCOPES) else: credentials = ServiceAccountCredentials.from_json_keyfile_name( "avrae-google.json", scopes=SCOPES) return gspread.authorize(credentials) try: GoogleSheet.g_client = await asyncio.get_event_loop().run_in_executor(None, _) except: GoogleSheet._client_initializing = False raise GoogleSheet._token_expiry = datetime.datetime.now() + datetime.timedelta( seconds=ServiceAccountCredentials.MAX_TOKEN_LIFETIME_SECS) log.info("Logged in to google")
Example #2
Source File: fetch_artifact.py From clusterfuzz with Apache License 2.0 | 7 votes |
def get_client(): """Return client with connection to build apiary.""" # Connect using build apiary service account credentials. build_apiary_service_account_private_key = db_config.get_value( 'build_apiary_service_account_private_key') if not build_apiary_service_account_private_key: logs.log( 'Android build apiary credentials are not set, skip artifact fetch.') return None credentials = ServiceAccountCredentials.from_json_keyfile_dict( json.loads(build_apiary_service_account_private_key), scopes='https://www.googleapis.com/auth/androidbuild.internal') client = apiclient.discovery.build( 'androidbuildinternal', 'v2beta1', credentials=credentials, cache_discovery=False) return client
Example #3
Source File: create_gke_cluster_legacy.py From cloudbolt-forge with Apache License 2.0 | 6 votes |
def __init__(self, environment, cluster_name): self.environment = environment self.cluster_name = cluster_name self.handler = environment.resource_handler.cast() self.zone = environment.gcp_zone self.project = self.handler.gpc_projects self.credentials = ServiceAccountCredentials.from_json_keyfile_dict({ 'client_email': self.handler.serviceaccount, 'private_key': self.handler.servicepasswd, 'type': 'service_account', 'client_id': None, 'private_key_id': None, }) self.container_client = self.get_client('container') self.compute_client = self.get_client('compute')
Example #4
Source File: google_analytics_hook.py From google_analytics_plugin with Apache License 2.0 | 6 votes |
def get_service_object(self, name): service = GoogleAnalyticsHook._services[name] if self.connection.password: credentials = AccessTokenCredentials(self.connection.password, 'Airflow/1.0') elif hasattr(self, 'client_secrets'): credentials = ServiceAccountCredentials.from_json_keyfile_dict(self.client_secrets, service.scopes) elif hasattr(self, 'file_location'): credentials = ServiceAccountCredentials.from_json_keyfile_name(self.file_location, service.scopes) else: raise ValueError('No valid credentials could be found') return build(service.name, service.version, credentials=credentials)
Example #5
Source File: views.py From wagalytics with MIT License | 6 votes |
def get_access_token_from_str(ga_key_content): """Get the access token from a string. from https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/ Defines a method to get an access token from the credentials object. The access token is automatically refreshed if it has expired. """ # The scope for the OAuth2 request. SCOPE = 'https://www.googleapis.com/auth/analytics.readonly' # Construct a credentials objects from the key data and OAuth2 scope. keyDict = json.loads(ga_key_content.replace('\n', '').replace('\r', '')) _credentials = ServiceAccountCredentials.from_json_keyfile_dict( keyDict, SCOPE) return _credentials.get_access_token().access_token
Example #6
Source File: google_cloud_logging.py From journalpump with Apache License 2.0 | 5 votes |
def __init__(self, *, config, googleapiclient_request_builder=None, **kwargs): super().__init__( config=config, max_send_interval=config.get("max_send_interval", 0.3), **kwargs ) credentials = None google_service_account = config.get("google_service_account_credentials") self.project_id = config["google_cloud_logging_project_id"] self.log_id = config["google_cloud_logging_log_id"] self.resource_labels = config.get("google_cloud_logging_resource_labels", None) if google_service_account: credentials = ServiceAccountCredentials.from_json_keyfile_dict(google_service_account) self.project_id = google_service_account["project_id"] else: credentials = ServiceAccountCredentials.get_application_default() if googleapiclient_request_builder is not None: self._logs = build( "logging", "v2", credentials=credentials, requestBuilder=googleapiclient_request_builder ) else: self._logs = build( "logging", "v2", credentials=credentials ) self.mark_connected()
Example #7
Source File: googlesheets.py From dataiku-contrib with Apache License 2.0 | 5 votes |
def get_spreadsheet(credentials, doc_id, tab_id): """ Inputs params: * credentials * doc_id * tab_id Returns a gspread's worksheet object. """ credentials = get_credentials(credentials) scope = [ 'https://www.googleapis.com/auth/spreadsheets' ] gspread_client = gspread.authorize(ServiceAccountCredentials.from_json_keyfile_dict(credentials, scope)) try: return gspread_client.open_by_key(doc_id).worksheet(tab_id) except gspread.exceptions.SpreadsheetNotFound as e: raise Exception("Trying to open non-existent or inaccessible spreadsheet document.") except gspread.exceptions.WorksheetNotFound as e: raise Exception("Trying to open non-existent sheet. Verify that the sheet name exists (%s)." % tab_id) except gspread.exceptions.APIError as e: if hasattr(e, 'response'): error_json = e.response.json() print(error_json) error_status = error_json.get("error", {}).get("status") email = credentials.get("client_email", "(email missing)") if error_status == 'PERMISSION_DENIED': error_message = error_json.get("error", {}).get("message", "") raise Exception("Access was denied with the following error: %s. Have you enabled the Sheets API? Have you shared the spreadsheet with %s?" % (error_message, email)) if error_status == 'NOT_FOUND': raise Exception("Trying to open non-existent spreadsheet document. Verify the document id exists (%s)." % doc_id) raise Exception("The Google API returned an error: %s" % e)
Example #8
Source File: create_gke_cluster.py From cloudbolt-forge with Apache License 2.0 | 5 votes |
def __init__(self, environment, zone, cluster_name): self.environment = environment self.cluster_name = cluster_name self.handler = environment.resource_handler.cast() self.zone = zone gcp_project = GCPProject.objects.get(id=self.environment.gcp_project) self.project_name = self.environment.gcp_project try: service_account_key = json.loads(gcp_project.service_account_info) except Exception: service_account_key = json.loads(gcp_project.service_account_key) client_email = service_account_key.get("client_email") private_key = service_account_key.get("private_key") set_progress("Using client_email: {}".format(client_email)) set_progress( "Make sure that the associated service account has permission to edit GKE Nodes" ) self.credentials = ServiceAccountCredentials.from_json_keyfile_dict( { "client_email": client_email, "private_key": private_key, "type": "service_account", "client_id": None, "private_key_id": None, } ) self.container_client = self.get_client("container") self.compute_client = self.get_client("compute")
Example #9
Source File: google_sheets.py From docassemble with MIT License | 5 votes |
def append_to_sheet(sheet_name, vals, worksheet_index=0): creds = ServiceAccountCredentials.from_json_keyfile_dict(credential_info, scope) client = gspread.authorize(creds) sheet = client.open(sheet_name).get_worksheet(worksheet_index) sheet.append_row(vals)
Example #10
Source File: google_sheets.py From docassemble with MIT License | 5 votes |
def read_sheet(sheet_name, worksheet_index=0): creds = ServiceAccountCredentials.from_json_keyfile_dict(credential_info, scope) client = gspread.authorize(creds) sheet = client.open(sheet_name).get_worksheet(worksheet_index) return sheet.get_all_records()
Example #11
Source File: google_api.py From docassemble with MIT License | 5 votes |
def google_api_credentials(scope): """Returns an OAuth2 credentials object for the given scope.""" if credential_info is None: raise Exception("google service account credentials not defined in configuration") if scope is None: scope = ['https://www.googleapis.com/auth/drive'] if type(scope) is not list: scope = [scope] return ServiceAccountCredentials.from_json_keyfile_dict(credential_info, scope)
Example #12
Source File: gcp.py From dcos-e2e with Apache License 2.0 | 5 votes |
def __init__(self, credentials_dict): """ Build GCE service account credentials from info stored in environment variables, then build a GCE API wrapper with those credentials. Only one of the two environment variables must be set. :param creds_env_var_name: JSON string that contains your GCE service account credentials :param creds_path_env_var_name: string that contains the path to the file containing your GCE service account credentials. """ credentials = ServiceAccountCredentials.from_json_keyfile_dict( credentials_dict, scopes='https://www.googleapis.com/auth/cloud-platform') self.compute = discovery.build('compute', 'v1', credentials=credentials) self.deployment_manager = discovery.build('deploymentmanager', 'v2', credentials=credentials) self.project_id = credentials_dict['project_id']
Example #13
Source File: gcp.py From dcos-e2e with Apache License 2.0 | 5 votes |
def __init__(self, credentials_dict): """ Build GCE service account credentials from info stored in environment variables, then build a GCE API wrapper with those credentials. Only one of the two environment variables must be set. :param creds_env_var_name: JSON string that contains your GCE service account credentials :param creds_path_env_var_name: string that contains the path to the file containing your GCE service account credentials. """ credentials = ServiceAccountCredentials.from_json_keyfile_dict( credentials_dict, scopes='https://www.googleapis.com/auth/cloud-platform') self.compute = discovery.build('compute', 'v1', credentials=credentials) self.deployment_manager = discovery.build('deploymentmanager', 'v2', credentials=credentials) self.project_id = credentials_dict['project_id']
Example #14
Source File: google.py From k8s-snapshots with BSD 2-Clause "Simplified" License | 5 votes |
def get_gcloud(ctx, version: str= 'v1'): """ Get a configured Google Compute API Client instance. Note that the Google API Client is not threadsafe. Cache the instance locally if you want to avoid OAuth overhead between calls. Parameters ---------- version Compute API version """ SCOPES = 'https://www.googleapis.com/auth/compute' credentials = None if ctx.config.get('gcloud_credentials_file'): credentials = ServiceAccountCredentials.from_json_keyfile_name( ctx.config.get('gcloud_credentials_file'), scopes=SCOPES) if ctx.config.get('google_application_credentials'): keyfile = json.loads(ctx.config.get('google_application_credentials')) credentials = ServiceAccountCredentials.from_json_keyfile_dict( keyfile, scopes=SCOPES) if not credentials: credentials = GoogleCredentials.get_application_default() if not credentials: raise RuntimeError("Auth for Google Cloud was not configured") compute = discovery.build( 'compute', version, credentials=credentials, # https://github.com/google/google-api-python-client/issues/299#issuecomment-268915510 cache_discovery=False ) return compute
Example #15
Source File: client.py From luci-py with Apache License 2.0 | 5 votes |
def _get_application_default_credential_from_file(filename): """Build the Application Default Credentials from file.""" # read the credentials from the file with open(filename) as file_obj: client_credentials = json.load(file_obj) credentials_type = client_credentials.get('type') if credentials_type == AUTHORIZED_USER: required_fields = set(['client_id', 'client_secret', 'refresh_token']) elif credentials_type == SERVICE_ACCOUNT: required_fields = set(['client_id', 'client_email', 'private_key_id', 'private_key']) else: raise ApplicationDefaultCredentialsError( "'type' field should be defined (and have one of the '" + AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)") missing_fields = required_fields.difference(client_credentials.keys()) if missing_fields: _raise_exception_for_missing_fields(missing_fields) if client_credentials['type'] == AUTHORIZED_USER: return GoogleCredentials( access_token=None, client_id=client_credentials['client_id'], client_secret=client_credentials['client_secret'], refresh_token=client_credentials['refresh_token'], token_expiry=None, token_uri=GOOGLE_TOKEN_URI, user_agent='Python client library') else: # client_credentials['type'] == SERVICE_ACCOUNT from oauth2client.service_account import ServiceAccountCredentials return ServiceAccountCredentials.from_json_keyfile_dict( client_credentials)
Example #16
Source File: client.py From luci-py with Apache License 2.0 | 5 votes |
def _get_application_default_credential_from_file(filename): """Build the Application Default Credentials from file.""" # read the credentials from the file with open(filename) as file_obj: client_credentials = json.load(file_obj) credentials_type = client_credentials.get('type') if credentials_type == AUTHORIZED_USER: required_fields = set(['client_id', 'client_secret', 'refresh_token']) elif credentials_type == SERVICE_ACCOUNT: required_fields = set(['client_id', 'client_email', 'private_key_id', 'private_key']) else: raise ApplicationDefaultCredentialsError( "'type' field should be defined (and have one of the '" + AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)") missing_fields = required_fields.difference(client_credentials.keys()) if missing_fields: _raise_exception_for_missing_fields(missing_fields) if client_credentials['type'] == AUTHORIZED_USER: return GoogleCredentials( access_token=None, client_id=client_credentials['client_id'], client_secret=client_credentials['client_secret'], refresh_token=client_credentials['refresh_token'], token_expiry=None, token_uri=GOOGLE_TOKEN_URI, user_agent='Python client library') else: # client_credentials['type'] == SERVICE_ACCOUNT from oauth2client.service_account import ServiceAccountCredentials return ServiceAccountCredentials.from_json_keyfile_dict( client_credentials)
Example #17
Source File: client.py From luci-py with Apache License 2.0 | 5 votes |
def _get_application_default_credential_from_file(filename): """Build the Application Default Credentials from file.""" # read the credentials from the file with open(filename) as file_obj: client_credentials = json.load(file_obj) credentials_type = client_credentials.get('type') if credentials_type == AUTHORIZED_USER: required_fields = set(['client_id', 'client_secret', 'refresh_token']) elif credentials_type == SERVICE_ACCOUNT: required_fields = set(['client_id', 'client_email', 'private_key_id', 'private_key']) else: raise ApplicationDefaultCredentialsError( "'type' field should be defined (and have one of the '" + AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)") missing_fields = required_fields.difference(client_credentials.keys()) if missing_fields: _raise_exception_for_missing_fields(missing_fields) if client_credentials['type'] == AUTHORIZED_USER: return GoogleCredentials( access_token=None, client_id=client_credentials['client_id'], client_secret=client_credentials['client_secret'], refresh_token=client_credentials['refresh_token'], token_expiry=None, token_uri=GOOGLE_TOKEN_URI, user_agent='Python client library') else: # client_credentials['type'] == SERVICE_ACCOUNT from oauth2client.service_account import ServiceAccountCredentials return ServiceAccountCredentials.from_json_keyfile_dict( client_credentials)
Example #18
Source File: client.py From luci-py with Apache License 2.0 | 5 votes |
def _get_application_default_credential_from_file(filename): """Build the Application Default Credentials from file.""" # read the credentials from the file with open(filename) as file_obj: client_credentials = json.load(file_obj) credentials_type = client_credentials.get('type') if credentials_type == AUTHORIZED_USER: required_fields = set(['client_id', 'client_secret', 'refresh_token']) elif credentials_type == SERVICE_ACCOUNT: required_fields = set(['client_id', 'client_email', 'private_key_id', 'private_key']) else: raise ApplicationDefaultCredentialsError( "'type' field should be defined (and have one of the '" + AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)") missing_fields = required_fields.difference(client_credentials.keys()) if missing_fields: _raise_exception_for_missing_fields(missing_fields) if client_credentials['type'] == AUTHORIZED_USER: return GoogleCredentials( access_token=None, client_id=client_credentials['client_id'], client_secret=client_credentials['client_secret'], refresh_token=client_credentials['refresh_token'], token_expiry=None, token_uri=GOOGLE_TOKEN_URI, user_agent='Python client library') else: # client_credentials['type'] == SERVICE_ACCOUNT from oauth2client.service_account import ServiceAccountCredentials return ServiceAccountCredentials.from_json_keyfile_dict( client_credentials)
Example #19
Source File: client.py From luci-py with Apache License 2.0 | 5 votes |
def _get_application_default_credential_from_file(filename): """Build the Application Default Credentials from file.""" # read the credentials from the file with open(filename) as file_obj: client_credentials = json.load(file_obj) credentials_type = client_credentials.get('type') if credentials_type == AUTHORIZED_USER: required_fields = set(['client_id', 'client_secret', 'refresh_token']) elif credentials_type == SERVICE_ACCOUNT: required_fields = set(['client_id', 'client_email', 'private_key_id', 'private_key']) else: raise ApplicationDefaultCredentialsError( "'type' field should be defined (and have one of the '" + AUTHORIZED_USER + "' or '" + SERVICE_ACCOUNT + "' values)") missing_fields = required_fields.difference(client_credentials.keys()) if missing_fields: _raise_exception_for_missing_fields(missing_fields) if client_credentials['type'] == AUTHORIZED_USER: return GoogleCredentials( access_token=None, client_id=client_credentials['client_id'], client_secret=client_credentials['client_secret'], refresh_token=client_credentials['refresh_token'], token_expiry=None, token_uri=GOOGLE_TOKEN_URI, user_agent='Python client library') else: # client_credentials['type'] == SERVICE_ACCOUNT from oauth2client.service_account import ServiceAccountCredentials return ServiceAccountCredentials.from_json_keyfile_dict( client_credentials)
Example #20
Source File: gcp.py From dcos-launch with Apache License 2.0 | 5 votes |
def __init__(self, credentials_dict): """ Build GCE service account credentials from info stored in environment variables, then build a GCE API wrapper with those credentials. Only one of the two environment variables must be set. :param creds_env_var_name: JSON string that contains your GCE service account credentials :param creds_path_env_var_name: string that contains the path to the file containing your GCE service account credentials. """ credentials = ServiceAccountCredentials.from_json_keyfile_dict( credentials_dict, scopes='https://www.googleapis.com/auth/cloud-platform') self.compute = discovery.build('compute', 'v1', credentials=credentials) self.deployment_manager = discovery.build('deploymentmanager', 'v2', credentials=credentials) self.project_id = credentials_dict['project_id']
Example #21
Source File: gsuite.py From lego with MIT License | 5 votes |
def get_credentials(self): """ Build GSuite credentials """ if settings.GSUITE_CREDENTIALS is None: raise ImproperlyConfigured("Missing GSuite credentials") credentials = ServiceAccountCredentials.from_json_keyfile_dict( settings.GSUITE_CREDENTIALS, scopes ) return credentials.create_delegated(settings.GSUITE_DELEGATED_ACCOUNT)
Example #22
Source File: service.py From sheetfu with MIT License | 5 votes |
def __init__(self, path_to_secret=None, from_env=False): if from_env: config_dict = self._build_keyfile_dict() self.credentials = ServiceAccountCredentials.from_json_keyfile_dict(config_dict, self.scopes) else: self.path_to_secret = path_to_secret if self.path_to_secret is None: self.credentials = None else: self.credentials = ServiceAccountCredentials.from_json_keyfile_name(self.path_to_secret, self.scopes)
Example #23
Source File: provider.py From cloudbridge with MIT License | 5 votes |
def _credentials(self): if not self.credentials_obj: if self.credentials_dict: self.credentials_obj = ( ServiceAccountCredentials.from_json_keyfile_dict( self.credentials_dict)) else: self.credentials_obj = ( GoogleCredentials.get_application_default()) return self.credentials_obj
Example #24
Source File: put_repo_requests_in_db.py From oadoi with MIT License | 5 votes |
def get_repo_request_rows(): from oauth2client.service_account import ServiceAccountCredentials # this file inspired by https://www.twilio.com/blog/2017/02/an-easy-way-to-read-and-write-to-a-google-spreadsheet-in-python.html # use creds to create a client to interact with the Google Drive API scopes = ['https://spreadsheets.google.com/feeds'] json_creds = os.getenv("GOOGLE_SHEETS_CREDS_JSON") creds_dict = json.loads(json_creds) # hack to get around ugly new line escaping issues # this works for me, but later found links to what might be cleaner solutions: # use ast.literal_eval? https://github.com/googleapis/google-api-go-client/issues/185#issuecomment-422732250 # or maybe dumping like this might fix it? https://coreyward.svbtle.com/how-to-send-a-multiline-file-to-heroku-config creds_dict["private_key"] = creds_dict["private_key"].replace("\\\\n", "\n") # now continue creds = ServiceAccountCredentials.from_json_keyfile_dict(creds_dict, scopes) client = gspread.authorize(creds) # Find a workbook by url spreadsheet = client.open_by_url("https://docs.google.com/spreadsheets/d/1RcQuetbKVYRRf0GhGZQi38okY8gT1cPUs6l3RM94yQo/edit#gid=704459328") sheet = spreadsheet.sheet1 # Extract and print all of the values rows = sheet.get_all_values() print(rows[0:1]) return rows
Example #25
Source File: google.py From wrapanapi with MIT License | 4 votes |
def __init__(self, project=None, zone=None, file_type=None, **kwargs): """ The last three argumets are optional and required only if you want to use json or p12 files. By default, we expecting that service_account arg contains service account data. Args: project: name of the project, so called project_id zone: zone of cloud service_account: service_account_content scope: compute engine, container engine, sqlservice end etc cache_discovery: turn on cache discovery default off file_path: path to json or p12 file file_type: p12 or json client_email: Require for p12 file Returns: A :py:class:`GoogleCloudSystem` object. """ super(GoogleCloudSystem, self).__init__(**kwargs) self._project = project self._zone = zone self._region = kwargs.get('region') scope = kwargs.get('scope', self.default_scope) cache_discovery = kwargs.get("cache_discovery", False) if 'service_account' in kwargs: service_account = kwargs.get('service_account').copy() service_account['private_key'] = service_account['private_key'].replace('\\n', '\n') service_account['type'] = service_account.get('type', 'service_account') # default it credentials = ServiceAccountCredentials.from_json_keyfile_dict(service_account, scopes=scope) elif file_type == 'json': file_path = kwargs.get('file_path', None) credentials = ServiceAccountCredentials.from_json_keyfile_name(file_path, scopes=scope) elif file_type == 'p12': file_path = kwargs.get('file_path', None) client_email = kwargs.get('client_email', None) credentials = ServiceAccountCredentials.from_p12_keyfile(client_email, file_path, scopes=scope) http_auth = credentials.authorize(httplib2.Http()) self._compute = build('compute', 'v1', http=http_auth, cache_discovery=cache_discovery) self._storage = build('storage', 'v1', http=http_auth, cache_discovery=cache_discovery) self._instances = self._compute.instances() self._forwarding_rules = self._compute.forwardingRules() self._buckets = self._storage.buckets()
Example #26
Source File: delete.py From cloudbolt-forge with Apache License 2.0 | 4 votes |
def run(job, logger=None, **kwargs): resource = kwargs.get("resource") try: instance_name = resource.attributes.get( field__name="gcp_sql_instance_name" ).value rh_id = resource.attributes.get(field__name="gcp_sql_rh_id").value rh = GCPHandler.objects.get(id=rh_id) set_progress("Connecting to Google Cloud") project = resource.attributes.get(field__name="gcp_sql_project").value try: account_info = json.loads( rh.gcp_projects.get(id=project).service_account_info ) except Exception: account_info = json.loads( rh.gcp_projects.get(id=project).service_account_key ) credentials = ServiceAccountCredentials.from_json_keyfile_dict(account_info) service_name = "sqladmin" version = "v1beta4" client = build(service_name, version, credentials=credentials) set_progress("Connection established") set_progress("Deleting instance %s..." % instance_name) # It takes awhile for the DB to be deleted client.instances().delete(project=project, instance=instance_name).execute() # Verify that instance was deleted: inst_data = client.instances().list(project=project).execute() if "items" in inst_data: for inst in inst_data["items"]: if inst["name"] == instance_name: return ( "WARNING", 'Server instance "%s" could not be deleted' % instance_name, ) set_progress("\nInstance %s is now deleted" % instance_name) except Exception as e: return "ERROR", 'Server instance "%s" could not be deleted' % resource.name, e
Example #27
Source File: sync.py From cloudbolt-forge with Apache License 2.0 | 4 votes |
def discover_resources(**kwargs): discovered_mysqldb = [] existing_instances = set() existing_bps = ServiceBlueprint.objects.filter( name__contains="Google MySQL Database" ) for bp in existing_bps: for resource in bp.resource_set.filter(lifecycle="ACTIVE"): existing_instances.add( (resource.gcp_sql_instance_name, resource.gcp_sql_project) ) for project in GCPProject.objects.filter(imported=True): try: account_info = json.loads(project.service_account_key) except: # noqa: E722 account_info = json.loads(project.service_account_info) environment = project.environment # noqa: F841 resource_handler = project.handler credentials = ServiceAccountCredentials.from_json_keyfile_dict(account_info) client_email = account_info["client_email"] set_progress( "Connecting to Google Cloud through service account email {}".format( client_email ) ) set_progress("RH: %s" % resource_handler.name) service_name = "sqladmin" version = "v1beta4" client = build(service_name, version, credentials=credentials) try: instances = ( client.instances().list(project=project.id).execute().get("items", None) ) if instances: for instance in instances: set_progress( "Found Database named {}: checking to see if the instance already exists on CB".format( instance["name"] ) ) if (instance["name"], project.name) not in existing_instances: discovered_mysqldb.append( { "name": instance["name"], "gcp_sql_instance_name": instance["name"], "gcp_sql_rh_id": resource_handler.id, "gcp_sql_project": project.name, } ) except Exception as e: set_progress( "Could not list sql servers for {}, error message is as follows:{}".format( project.name, e ) ) return discovered_mysqldb
Example #28
Source File: delete_gke_cluster_legacy.py From cloudbolt-forge with Apache License 2.0 | 4 votes |
def run(job=None, logger=None, resource=None, **kwargs): # Get cluster information cluster_name = resource.create_gke_k8s_cluster_name if not cluster_name: return "WARNING", "No cluster associated with this resource", "" env_id = resource.create_gke_k8s_cluster_env try: environment = Environment.objects.get(id=env_id) except Environment.DoesNotExist: return ("FAILURE", "The environment used to create this cluster no longer exists", "") handler = environment.resource_handler.cast() project = handler.gcp_projects zone = environment.gcp_zone # Get client credentials = ServiceAccountCredentials.from_json_keyfile_dict({ 'client_email': handler.serviceaccount, 'private_key': handler.servicepasswd, 'type': 'service_account', 'client_id': None, 'private_key_id': None, }) client = build('container', 'v1', credentials=credentials) cluster_resource = client.projects().zones().clusters() # Delete cluster job.set_progress("Deleting cluster {}...".format(cluster_name)) try: cluster_resource.delete( projectId=project, zone=zone, clusterId=cluster_name).execute() except HttpError as error: if error.resp['status'] == '404': return ("WARNING", "Cluster {} was not found. It may have already been " "deleted.".format(cluster_name), "") raise # In CB 7.6 and before, this will delete any existing Kubernetes Resources. # Starting in CB 7.7, they will be marked HISTORICAL instead. kubernetes = Kubernetes.objects.get(id=resource.create_gke_k8s_cluster_id) kubernetes.delete()