def get_or_create_new_project(config): """Creates the new GCP project if it does not exist.""" project_id = config.project['project_id'] # Attempt to get the project number first and fall back to creating the # project. # Note that it is possible that the `gcloud projects describe` command fails # due to reasons other than project does not exist (e.g. caller does not have # sufficient permission). In that case, project could exist and the code will # still attempt to create the project and fail if the project already exists. # # In the case where project exists, the organization_id / billing_account / # folder_id could be different from those specified in the config. # TODO: add check to enforce the metadata set in the config on the # existing project. try: config.generated_fields['projects'][project_id]['project_number'] = ( utils.get_project_number(project_id)) logging.info('Project %s exists, skip creating project.', project_id) except subprocess.CalledProcessError: overall_config = config.root['overall'] org_id = overall_config.get('organization_id') folder_id = config.project.get('folder_id', overall_config.get('folder_id')) create_project_command = ['projects', 'create', project_id] if folder_id: create_project_command.extend(['--folder', folder_id]) elif org_id: create_project_command.extend(['--organization', org_id]) else: logging.info('Deploying without a parent organization or folder.') # Create the new project. runner.run_gcloud_command(create_project_command, project_id=None) config.generated_fields['projects'][project_id]['project_number'] = ( utils.get_project_number(project_id))
def add_generated_fields(project): """Adds a generated_fields block to a project definition, if not already set. Args: project (dict): Config dictionary of a single project. """ if 'generated_fields' not in project: project_id = project['project_id'] project['generated_fields'] = { 'project_number': utils.get_project_number(project_id), 'log_sink_service_account': utils.get_log_sink_service_account(_LOG_SINK_NAME, project_id), } gce_instance_info = utils.get_gce_instance_info(project_id) if gce_instance_info: project['generated_fields']['gce_instance_info'] = gce_instance_info
def add_project_generated_fields(config): """Adds a generated_fields block to a project definition.""" project_id = config.project['project_id'] logging.info('Adding project post deployment fields for %s', project_id) if _GENERATED_FIELDS_NAME in config.project: return config.project[_GENERATED_FIELDS_NAME] = { 'project_number': utils.get_project_number(project_id), 'log_sink_service_account': utils.get_log_sink_service_account(_LOG_SINK_NAME, project_id), } gce_instance_info = utils.get_gce_instance_info(project_id) if gce_instance_info: config.project[_GENERATED_FIELDS_NAME][ 'gce_instance_info'] = gce_instance_info
def add_project_generated_fields(config): """Adds a generated_fields block to a project definition.""" project_id = config.project['project_id'] generated_fields = field_generation.get_generated_fields_ref( project_id, config.root) if 'project_number' not in generated_fields: generated_fields['project_number'] = utils.get_project_number( project_id) if 'log_sink_service_account' not in generated_fields: generated_fields[ 'log_sink_service_account'] = utils.get_log_sink_service_account( _LOG_SINK_NAME, project_id) gce_instance_info = utils.get_gce_instance_info(project_id) if gce_instance_info: generated_fields['gce_instance_info'] = gce_instance_info
def create_new_project(config): """Creates the new GCP project.""" project_id = config.project['project_id'] overall_config = config.root['overall'] org_id = overall_config.get('organization_id') folder_id = config.project.get('folder_id', overall_config.get('folder_id')) create_project_command = ['projects', 'create', project_id] if folder_id: create_project_command.extend(['--folder', folder_id]) elif org_id: create_project_command.extend(['--organization', org_id]) else: logging.info('Deploying without a parent organization or folder.') # Create the new project. runner.run_gcloud_command(create_project_command, project_id=None) generated_fields = field_generation.get_generated_fields_ref( project_id, config.root) generated_fields['project_number'] = utils.get_project_number(project_id)
def add_project_generated_fields(config): """Adds a generated_fields block to a project definition.""" project_id = config.project['project_id'] generated_fields = config.project.get(_GENERATED_FIELDS_NAME) if not generated_fields: generated_fields = {} config.project[_GENERATED_FIELDS_NAME] = generated_fields if 'project_number' not in generated_fields: generated_fields['project_number'] = utils.get_project_number(project_id) if 'log_sink_service_account' not in generated_fields: generated_fields[ 'log_sink_service_account'] = utils.get_log_sink_service_account( _LOG_SINK_NAME, project_id) gce_instance_info = utils.get_gce_instance_info(project_id) if gce_instance_info: generated_fields['gce_instance_info'] = gce_instance_info