def main(): # => Load the default arguments including those specific to the Cohesity Protection Jobs. argument_spec = cohesity_common_argument_spec() argument_spec.update( dict( state=dict(choices=['present', 'absent', 'started', 'stopped'], default='present'), name=dict(type='str', required=True, aliases=['job_name']), description=dict(type='str', default=''), environment=dict(default='kOracle'), protection_policy=dict(type='str', aliases=['policy'], default='Bronze'), storage_domain=dict(type='str', default='DefaultStorageDomain'), time_zone=dict(type='str', default=''), start_time=dict(type='str', default=''), delete_backups=dict(type='bool', default=False), ondemand_run_type=dict( choices=['Regular', 'Full', 'Log', 'System'], default='Regular'), cancel_active=dict(type='bool', default=False), validate_certs=dict(type='bool', default=False), endpoint=dict(type=str, default=''), databases=dict(type=list, default=[]), )) # => Create a new module object module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) if not module.params.get("time_zone"): module.params["time_zone"] = get_timezone() global cohesity_client cohesity_client = get_cohesity_client(module) results = dict(changed=False, msg='Attempting to manage Protection Source', state=module.params.get('state')) job_exists, job_meta_data = check__protection_job__exists(module) if module.check_mode: check_mode_results = dict( changed=False, msg= 'Check Mode: Cohesity Protection Job is not currently registered', id='') if module.params.get('state') == 'present': if job_exists: check_mode_results[ 'msg'] = 'Check Mode: Cohesity Protection Job is currently registered. No changes' else: check_mode_results[ 'msg'] = 'Check Mode: Cohesity Protection Job is not currently registered. This action would register the Cohesity Protection Job.' check_mode_results['id'] = job_exists else: if job_exists: check_mode_results[ 'msg'] = 'Check Mode: Cohesity Protection Job is currently registered. This action would unregister the Cohesity Protection Job.' check_mode_results['id'] = job_exists else: check_mode_results[ 'msg'] = 'Check Mode: Cohesity Protection Job is not currently registered. No changes.' module.exit_json(**check_mode_results) elif module.params.get('state') == 'present': parent_id, source_id = get_source_id_by_endpoint(module) if not (parent_id and source_id): module.fail_json( msg= "Source '%s' is not registered to cluster, Please register the source and try again." % module.params.get('endpoint')) check__mandatory__params(module) body = ProtectionJobRequestBody() body.name = module.params.get('name') body.parent_source_id = source_id body.source_ids = [source_id] body.view_box_id = get__storage_domain_id__by_name(module) body.environment = module.params.get('environment') body.policy_id = get__prot_policy_id__by_name(module) body.timezone = module.params.get('time_zone').strip() body.description = module.params.get('description') databases = module.params.get('databases') if databases: entity_ids = list() application_nodes = [] body.source_special_parameters = list() resp = cohesity_client.protection_sources.list_protection_sources( environment='kOracle', id=parent_id) if not resp: module.fail_json( msg="Oracle source is not available to protect") for node in resp[0].nodes: application_nodes.extend(node.get("applicationNodes", [])) # Make copy of database list and remove once entity id fetched. This check # is to ensure availability of databases in server. copy_database = copy.deepcopy(databases) for database in databases: for node in application_nodes: if node["protectionSource"]["name"] == database.strip(): entity_ids.append(node["protectionSource"]["id"]) copy_database.remove(database) if len(databases) == len(entity_ids): break if copy_database: module.fail_json( "Following list of databases are not available in the " "Oracle Server: %s" % ", ".join(copy_database)) spl_params = SourceSpecialParameter() spl_params.source_id = source_id spl_params.oracle_special_parameters = OracleSpecialParameters() spl_params.oracle_special_parameters.application_entity_ids = entity_ids body.source_special_parameters.append(spl_params) if module.params.get('start_time'): start_time = list(module.params.get('start_time').replace(':', '')) if not len(start_time) == 4: # => There are only so many options here but if we get more characters # => than four then we need to escape quickly. module.fail_json( msg='Invalid start_time selected (' + module.params.get('start_time') + '). Please review and submit the correct Protection Job Starting time.' ) body.start_time = dict(hour=int(start_time[0] + start_time[1]), minute=int(start_time[2] + start_time[3])) try: if job_exists: response = cohesity_client.protection_jobs.update_protection_job( body, job_exists) msg = 'Updation of Cohesity Protection Job Complete' else: response = cohesity_client.protection_jobs.create_protection_job( body) msg = 'Creation of Cohesity Protection Job Complete' response = dict(id=response.id, name=response.name, environment=response.environment) results = dict(changed=True, msg=msg, **response) except APIException as err: module.fail_json(msg=err.message) elif module.params.get('state') == 'absent': if job_exists: job_id = job_meta_data.uid.id status, _, _ = get_protection_run__status__by_id(module, job_id) if status: stop_job(module, job_id) while True: status, _, _ = get_protection_run__status__by_id( module, job_id) if not status: time.sleep(10) break response = unregister_job(module, job_exists) results = dict( changed=True, msg='Unregistration of Cohesity Protection Job Complete', id=job_exists, name=module.params.get('name')) else: results = dict( changed=False, msg= 'The Protection Job for this host is currently not registered', name=module.params.get('name')) elif module.params.get('state') == 'started': if job_exists: response = start_job(module) results = dict( changed=True, msg='The Protection Job for this host has been started', id=job_exists, name=module.params.get('name')) else: results = dict( changed=False, msg= 'The Protection Job for this host is currently not registered', name=module.params.get('name')) elif module.params.get('state') == 'stopped': if job_exists: job_id = job_meta_data.uid.id response = stop_job(module, job_id) results = dict( changed=True, msg='The Protection Job for this host has been stopped', id=job_id, name=module.params.get('name')) else: results = dict( changed=False, msg= 'The Protection Job for this host is currently not registered', name=module.params.get('name')) else: # => This error should never happen based on the set assigned to the parameter. # => However, in case, we should raise an appropriate error. module.fail_json(msg='Invalid State selected: {0}'.format( module.params.get('state')), changed=False) module.exit_json(**results)
def main(): # => Load the default arguments including those specific to the Cohesity Protection Jobs. argument_spec = cohesity_common_argument_spec() argument_spec.update( dict( state=dict(choices=['present', 'absent', 'started', 'stopped'], default='present'), name=dict(type='str', required=True, aliases=['job_name']), description=dict(type='str', default=''), environment=dict(default='kOracle'), protection_policy=dict(type='str', aliases=['policy'], default='Bronze'), storage_domain=dict(type='str', default='DefaultStorageDomain'), time_zone=dict(type='str', default='America/Los_Angeles'), start_time=dict(type='str', default=''), delete_backups=dict(type='bool', default=False), ondemand_run_type=dict( choices=['Regular', 'Full', 'Log', 'System'], default='Regular'), cancel_active=dict(type='bool', default=False), validate_certs=dict(type='bool', default=False), endpoint=dict(type=str, default=''), )) # => Create a new module object module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) global cohesity_client cohesity_client = get_cohesity_client(module) results = dict(changed=False, msg='Attempting to manage Protection Source', state=module.params.get('state')) job_exists, job_meta_data = check__protection_job__exists(module) if module.check_mode: check_mode_results = dict( changed=False, msg= 'Check Mode: Cohesity Protection Job is not currently registered', id='') if module.params.get('state') == 'present': if job_exists: check_mode_results[ 'msg'] = 'Check Mode: Cohesity Protection Job is currently registered. No changes' else: check_mode_results[ 'msg'] = 'Check Mode: Cohesity Protection Job is not currently registered. This action would register the Cohesity Protection Job.' check_mode_results['id'] = job_exists else: if job_exists: check_mode_results[ 'msg'] = 'Check Mode: Cohesity Protection Job is currently registered. This action would unregister the Cohesity Protection Job.' check_mode_results['id'] = job_exists else: check_mode_results[ 'msg'] = 'Check Mode: Cohesity Protection Job is not currently registered. No changes.' module.exit_json(**check_mode_results) elif module.params.get('state') == 'present': parent_id, source_id = get_source_id_by_endpoint(module) check__mandatory__params(module) body = ProtectionJobRequestBody() body.name = module.params.get('name') body.parent_source_id = parent_id body.source_ids = [source_id] body.view_box_id = get__storage_domain_id__by_name(module) body.environment = module.params.get('environment') body.policy_id = get__prot_policy_id__by_name(module) body.timezone = module.params.get('time_zone').strip() body.description = module.params.get('description') if module.params.get('start_time'): start_time = list(module.params.get('start_time').replace(':', '')) if not len(start_time) == 4: # => There are only so many options here but if we get more characters # => than four then we need to escape quickly. module.fail_json( msg='Invalid start_time selected (' + module.params.get('start_time') + '). Please review and submit the correct Protection Job Starting time.' ) body.start_time = dict(hour=int(start_time[0] + start_time[1]), minute=int(start_time[2] + start_time[3])) if job_exists: response = cohesity_client.protection_jobs.update_protection_job( body, job_exists) msg = 'Updation of Cohesity Protection Job Complete' else: response = cohesity_client.protection_jobs.create_protection_job( body) msg = 'Creation of Cohesity Protection Job Complete' response = dict(id=response.id, name=response.name, environment=response.environment) results = dict(changed=True, msg=msg, **response) elif module.params.get('state') == 'absent': if job_exists: job_id = job_meta_data.uid.id status, _, _ = get_protection_run__status__by_id(module, job_id) if status: stop_job(module, job_id) while True: status, _, _ = get_protection_run__status__by_id( module, job_id) if not status: time.sleep(10) break response = unregister_job(module, job_exists) results = dict( changed=True, msg='Unregistration of Cohesity Protection Job Complete', id=job_exists, name=module.params.get('name')) else: results = dict( changed=False, msg= 'The Protection Job for this host is currently not registered', name=module.params.get('name')) elif module.params.get('state') == 'started': if job_exists: response = start_job(module) results = dict( changed=True, msg='The Protection Job for this host has been started', id=job_exists, name=module.params.get('name')) else: results = dict( changed=False, msg= 'The Protection Job for this host is currently not registered', name=module.params.get('name')) elif module.params.get('state') == 'stopped': if job_exists: job_id = job_meta_data.uid.id response = stop_job(module, job_id) results = dict( changed=True, msg='The Protection Job for this host has been stopped', id=job_id, name=module.params.get('name')) else: results = dict( changed=False, msg= 'The Protection Job for this host is currently not registered', name=module.params.get('name')) else: # => This error should never happen based on the set assigned to the parameter. # => However, in case, we should raise an appropriate error. module.fail_json(msg='Invalid State selected: {0}'.format( module.params.get('state')), changed=False) module.exit_json(**results)