def sink_exists(organization, sink_name): result_ = base.run_command_readonly([ 'gcloud', 'logging', 'sinks', 'describe', sink_name, '--organization', organization ]) return not result_.decode("utf-8").strip().__contains__( 'NOT_FOUND: Sink {} does not exist'.format(sink_name))
def deployment_exists(project_name, deployment_name): result_ = base.run_command_readonly([ 'gcloud', 'deployment-manager', 'deployments', 'list', '--filter', 'NAME=' + deployment_name, '--format', 'json', '--project', project_name ]) return result_.decode("utf-8").strip() != '[]'
def get_backend_service(project_id, _filter, value='name'): result_ = run_command_readonly([ 'gcloud', 'compute', 'backend-services', 'list', '--project', project_id, "--format", "value({})".format(value), "--filter='{}'".format(_filter) ]) return result_.decode("utf-8").strip()
def get_firewall_rules(project): firewall_rules = base.run_command_readonly([ 'gcloud', 'compute', 'firewall-rules', 'list', '--format=\'value(NAME)\'', '--project', project ]) return firewall_rules.decode( 'utf-8').splitlines() if firewall_rules else []
def iam_org_level_role_exist(organization_id, role): role_id_full_name = build_org_level_role_full_name(organization_id, role) current_role_name = base.run_command_readonly([ 'gcloud', 'iam', 'roles', 'list', '--organization', organization_id, '--filter', role, '--format', "value(name)" ]) return current_role_name.decode("utf-8").strip() == role_id_full_name
def __get_functions(project): cmd = [ 'gcloud', 'functions', 'list', '--project', project, '--format=json' ] functions = run_command_readonly(cmd).decode("utf-8").strip() functions = json.loads(functions) return functions
def get_enabled_apis_on_project(project_id): print('Getting enabled apis on project {}\n'.format(project_id)) result_ = run_command_readonly([ 'gcloud', 'services', 'list', '--enabled', '--format="(NAME:format=json)"', '--project', project_id ]) return json.loads(result_.decode("utf-8").strip())
def get_cluster_version(project_id, zone): """Get version of GKE""" versions = run_command_readonly([ 'gcloud', 'container', 'get-server-config', '--quiet', '--zone', zone, '--project', project_id, '--format="value(validMasterVersions[0])"' ]) return versions.decode("utf-8").splitlines()[-1]
def service_account_exists(service_account_email, project): cmd = [ 'gcloud', 'iam', 'service-accounts', 'list', '--filter="email={}"'.format(service_account_email), '--project', project, '--format', 'json' ] result = base.run_command_readonly(cmd).decode('utf8').strip() return result != '[]'
def __get_subscriptions(project): cmd = [ 'gcloud', 'pubsub', 'subscriptions', 'list', '--project', project, '--format=json' ] subscriptions = run_command_readonly(cmd).decode("utf-8").strip() subscriptions = json.loads(subscriptions) return subscriptions
def __get_dataflow_jobs(project): cmd = [ 'gcloud', 'dataflow', 'jobs', 'list', '--project', project, '--filter=state=running', '--format=json' ] jobs = run_command_readonly(cmd).decode("utf-8").strip() jobs = json.loads(jobs) return jobs
def get_region_by_zone(zone): """Get the region based on specified zone""" cmd = [ 'gcloud', 'compute', 'zones', 'describe', zone, '--format=value(region)' ] region_url = str(run_command_readonly(cmd).decode('utf-8')).split('/') return region_url[len(region_url) - 1].replace('\n', '')
def get_dns_record(dns_project_id, dns_zone, record_name, record_type): dns_ip = run_command_readonly([ 'gcloud', 'dns', 'record-sets', 'list', '--name', record_name, '-z', dns_zone, '--type', record_type, '--format', 'value(DATA)', '--project', dns_project_id ]) if dns_ip: return dns_ip.decode("utf-8").strip() return None
def get_dns_ttl(dns_project_id, dns_zone, record_name, record_type): ttl = run_command_readonly([ 'gcloud', 'dns', 'record-sets', 'list', '--name', record_name, '-z', dns_zone, '--type', record_type, '--format', 'value(ttl)', '--project', dns_project_id ]) if ttl: return ttl.decode("utf-8").strip() return None
def cloud_sql_instance_running(instance_name, project): """Verify if the given SQL instance has started""" command_ = [ 'gcloud', 'sql', 'instances', 'describe', instance_name, '--format', 'value(settings.activationPolicy)', '--project', project ] result_ = run_command_readonly(command_).decode("utf-8").strip() return result_ == 'ALWAYS'
def gae_exists(project_name): if helpers.DRY_RUN: return False result_ = base.run_command_readonly([ 'gcloud', 'app', 'describe', '--project', project_name, '--format', 'json' ]) clean_result = result_.decode("utf-8").strip() return 'does not contain an App Engine application' not in clean_result
def gae_service_exist(project_name, service_name): if helpers.DRY_RUN: return False result_ = base.run_command_readonly([ 'gcloud', 'app', 'services', 'describe', service_name, '--project', project_name, '--format', 'value(id)' ]) clean_result = result_.decode("utf-8").strip() return service_name == clean_result
def bucket_status(bucket_name): result_ = base.run_command_readonly( ['gsutil', 'ls', '-L', '-b', 'gs://' + bucket_name]) clean_result = result_.decode("utf-8").strip() if "BucketNotFoundException" in clean_result: return "NotFound" if "AccessDeniedException" in clean_result: return "AccessDenied" return "Found"
def gcp_zone_list(project_id): """Validate if given project id exists on GCP and user has access""" cmd = [ 'gcloud', 'compute', 'zones', 'list', '--format="value(NAME)"', '--project', project_id ] result = run_command_readonly(cmd).decode('utf8').strip() return result
def get_managed_zones_by_project(project_id): """Get managed zones for the given project id""" cmd = [ 'gcloud', 'dns', 'managed-zones', 'list', '--project', project_id, '--format=value(name)' ] result = run_command_readonly(cmd).decode('utf8').strip() return result
def gcp_project_exists(project_id): """Validate if given project id exists on GCP and user has access""" cmd = [ 'gcloud', 'projects', 'list', '--filter', 'PROJECT_ID=' + project_id, '--format', 'json' ] result = run_command_readonly(cmd).decode('utf8').strip() return result != '[]'
def get_external_ip(dns_project_id, name): cmd = [ 'gcloud', 'compute', 'addresses', 'list', '--filter="name:{}"'.format(name), "--format='value(address)'", '--project', dns_project_id ] return run_command_readonly(cmd)\ .decode("utf-8")\ .replace("\r", "")\ .replace("\n", "")
def get_servers_names(dns_zone, dns_project_id): LOGGER.info("Retrieving names servers in %s, project %s", dns_zone, dns_project_id) returned_servers = run_command_readonly([ 'gcloud', 'dns', 'managed-zones', 'describe', dns_zone, '--project', dns_project_id, "--format='value(nameServers)'" ]) servers = returned_servers.decode("utf-8")\ .replace("\r", "").replace("\n", "").split(";") return servers
def retrieve_servers_names(dns_zone, dns_project_id): LOGGER.info("Retrieving names servers in %s, project %s", dns_zone, dns_project_id) returned_servers = run_command_readonly([ 'gcloud', 'dns', 'managed-zones', 'describe', dns_zone, '--project', dns_project_id ]) servers = returned_servers.decode("utf-8") LOGGER.warning("Keep these names servers for the next step\n") LOGGER.warning(servers)
def default_network_has_no_instances(project): result_ = run_command_readonly([ 'gcloud', 'compute', 'instances', 'list', '--filter', '"networkInterfaces[].network:default"', '--format', 'json', '--project', project, ]) return result_.decode("utf-8").strip() == '[]'
def default_network_has_no_instances(project_id): """ Check if default network has instances associated. :param project_id: project id :return: if has instances associated. """ result_ = run_command_readonly([ 'gcloud', 'compute', 'instances', 'list', '--filter', '"networkInterfaces[].network:default"', '--format', 'json', '--project', project_id, ]) return result_.decode("utf-8").strip() == '[]'
def topic_exists(project_name, topic_name): topic = 'projects/{}/topics/{}'.format(project_name, topic_name) result_ = base.run_command_readonly( ['gcloud', 'pubsub', 'topics', 'describe', topic]) return result_.decode("utf-8").strip().__contains__(topic)
def subscription_exists(project_name, subscription_name): subscription = 'projects/{}/subscriptions/{}'.format( project_name, subscription_name) result_ = base.run_command_readonly( ['gcloud', 'pubsub', 'subscriptions', 'describe', subscription]) return result_.decode("utf-8").strip().__contains__(subscription)
def project_exists(project_id): result_ = base.run_command_readonly([ 'gcloud', 'projects', 'list', '--filter', 'PROJECT_ID=' + project_id, '--format', 'json' ]) return result_.decode("utf-8").strip() != '[]'
def project_has_billing(project_id): result = base.run_command_readonly([ 'gcloud', 'beta', 'billing', 'projects', 'describe', project_id, '--format', 'value(billingEnabled)' ]) return 'TRUE' == result.decode("utf-8").strip().upper()