def payload(self, organization, scm_type='git', **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'Project - {}'.format( random_title()), description=kwargs.get('description') or random_title(10), scm_type=scm_type, scm_url=kwargs.get('scm_url') or config.project_urls.get( scm_type, '')) if organization is not None: payload.organization = organization.id if kwargs.get('credential'): payload.credential = kwargs.get('credential').id fields = ( 'scm_branch', 'local_path', 'scm_clean', 'scm_delete_on_update', 'scm_update_cache_timeout', 'scm_update_on_launch', 'scm_refspec', 'allow_override') update_payload(payload, fields, kwargs) return payload
def payload(self, workflow_job_template, unified_job_template, **kwargs): if not unified_job_template: # May pass "None" to explicitly create an approval node payload = PseudoNamespace(workflow_job_template=workflow_job_template.id) else: payload = PseudoNamespace(workflow_job_template=workflow_job_template.id, unified_job_template=unified_job_template.id) optional_fields = ( 'diff_mode', 'extra_data', 'limit', 'scm_branch', 'job_tags', 'job_type', 'skip_tags', 'verbosity', 'extra_data', 'identifier', 'all_parents_must_converge', ) update_payload(payload, optional_fields, kwargs) if 'inventory' in kwargs: payload['inventory'] = kwargs['inventory'].id return payload
def payload(self, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'WorkflowJobTemplate - {}'.format(random_title()), description=kwargs.get('description') or random_title(10)) optional_fields = ("allow_simultaneous", "ask_variables_on_launch", "ask_inventory_on_launch", "ask_scm_branch_on_launch", "ask_limit_on_launch", "limit", "scm_branch", "survey_enabled") update_payload(payload, optional_fields, kwargs) extra_vars = kwargs.get('extra_vars', not_provided) if extra_vars != not_provided: if isinstance(extra_vars, dict): extra_vars = json.dumps(extra_vars) payload.update(extra_vars=extra_vars) if kwargs.get('organization'): payload.organization = kwargs.get('organization').id if kwargs.get('inventory'): payload.inventory = kwargs.get('inventory').id return payload
def payload(self, job_type='run', playbook='ping.yml', **kwargs): name = kwargs.get('name') or 'JobTemplate - {}'.format(random_title()) description = kwargs.get('description') or random_title(10) payload = PseudoNamespace(name=name, description=description, job_type=job_type) optional_fields = ('ask_scm_branch_on_launch', 'ask_credential_on_launch', 'ask_diff_mode_on_launch', 'ask_inventory_on_launch', 'ask_job_type_on_launch', 'ask_limit_on_launch', 'ask_skip_tags_on_launch', 'ask_tags_on_launch', 'ask_variables_on_launch', 'ask_verbosity_on_launch', 'allow_simultaneous', 'become_enabled', 'diff_mode', 'force_handlers', 'forks', 'host_config_key', 'job_tags', 'limit', 'skip_tags', 'start_at_task', 'survey_enabled', 'timeout', 'use_fact_cache', 'vault_credential', 'verbosity', 'job_slice_count', 'scm_branch') update_payload(payload, optional_fields, kwargs) extra_vars = kwargs.get('extra_vars', not_provided) if extra_vars != not_provided: if isinstance(extra_vars, dict): extra_vars = json.dumps(extra_vars) payload.update(extra_vars=extra_vars) if kwargs.get('project'): payload.update(project=kwargs.get('project').id, playbook=playbook) if kwargs.get('inventory'): payload.update(inventory=kwargs.get('inventory').id) if kwargs.get('credential'): payload.update(credential=kwargs.get('credential').id) return payload
def payload(self, organization, scm_type='git', **kwargs): payload = PseudoNamespace(name=kwargs.get('name') or 'Project - {}'.format(random_title()), description=kwargs.get('description') or random_title(10), scm_type=scm_type, scm_url=kwargs.get('scm_url') or config.project_urls.get(scm_type, '')) if organization is not None: payload.organization = organization.id if kwargs.get('credential'): payload.credential = kwargs.get('credential').id fields = ('scm_branch', 'local_path', 'scm_clean', 'scm_delete_on_update', 'scm_update_cache_timeout', 'scm_update_on_launch', 'scm_refspec', 'allow_override') update_payload(payload, fields, kwargs) for fk_field in ('execution_environment', 'default_environment'): rel_obj = kwargs.get(fk_field) if rel_obj is None: continue elif isinstance(rel_obj, int): payload.update(**{fk_field: int(rel_obj)}) elif hasattr(rel_obj, 'id'): payload.update(**{fk_field: rel_obj.id}) else: raise AttributeError( f'Related field {fk_field} must be either integer of pkid or object' ) return payload
def payload(self, inventory, source='custom', credential=None, source_script=None, project=None, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'InventorySource - {}'.format(random_title()), description=kwargs.get('description') or random_title(10), inventory=inventory.id, source=source) if credential: payload.credential = credential.id if source_script: payload.source_script = source_script.id if project: payload.source_project = project.id optional_fields = ('source_path', 'source_vars', 'timeout', 'overwrite', 'overwrite_vars', 'update_cache_timeout', 'update_on_launch', 'update_on_project_update', 'verbosity') update_payload(payload, optional_fields, kwargs) return payload
def payload(self, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'Instance Group - {}'.format(random_title())) fields = ('policy_instance_percentage', 'policy_instance_minimum', 'policy_instance_list') update_payload(payload, fields, kwargs) return payload
def payload(self, **kwargs): payload = PseudoNamespace(description=kwargs.get('description') or random_title(10), scope=kwargs.get('scope', 'write')) if kwargs.get('oauth_2_application'): payload.application = kwargs['oauth_2_application'].id optional_fields = ('expires',) update_payload(payload, optional_fields, kwargs) return payload
def payload(self, kind='cloud', **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'CredentialType - {}'.format(random_title()), description=kwargs.get('description') or random_title(10), kind=kind) fields = ('inputs', 'injectors') update_payload(payload, fields, kwargs) return payload
def payload(self, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'Instance Group - {}'.format(random_title())) fields = ('policy_instance_percentage', 'policy_instance_minimum', 'policy_instance_list', 'is_container_group') update_payload(payload, fields, kwargs) set_payload_foreign_key_args(payload, ('credential', ), kwargs) return payload
def payload(self, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'OAuth2Application - {}'.format(random_title()), description=kwargs.get('description') or random_title(10), client_type=kwargs.get('client_type', 'public'), authorization_grant_type=kwargs.get('authorization_grant_type', 'password'), ) if kwargs.get('organization'): payload.organization = kwargs['organization'].id optional_fields = ('redirect_uris', 'skip_authorization') update_payload(payload, optional_fields, kwargs) return payload
def payload(self, workflow_job_template, unified_job_template, **kwargs): payload = PseudoNamespace( workflow_job_template=workflow_job_template.id, unified_job_template=unified_job_template.id) optional_fields = ('diff_mode', 'extra_data', 'limit', 'job_tags', 'job_type', 'skip_tags', 'verbosity', 'extra_data') update_payload(payload, optional_fields, kwargs) if 'inventory' in kwargs: payload['inventory'] = kwargs['inventory'].id return payload
def test_falsy_kwargs(self): fields = ('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight') kwargs = dict(one=False, two=(), three='', four=None, five=0, six={}, seven=set(), eight=[]) payload = {} utils.update_payload(payload, fields, kwargs) assert payload == kwargs
def payload(self, organization, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'Inventory - {}'.format(random_title()), description=kwargs.get('description') or random_title(10), organization=organization.id, ) optional_fields = ('host_filter', 'kind', 'variables') update_payload(payload, optional_fields, kwargs) if 'variables' in payload and isinstance(payload.variables, dict): payload.variables = json.dumps(payload.variables) return payload
def payload(self, inventory, credential=None, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'Group{}'.format(random_title(non_ascii=False)), description=kwargs.get('description') or random_title(10), inventory=inventory.id) if credential: payload.credential = credential.id update_payload(payload, ('variables', ), kwargs) if 'variables' in payload and isinstance(payload.variables, dict): payload.variables = json.dumps(payload.variables) return payload
def add_schedule(self, name='', description='', enabled=True, rrule=None, **kwargs): if rrule is None: rrule = "DTSTART:30180101T000000Z RRULE:FREQ=YEARLY;INTERVAL=1" payload = dict(name=name or "{0} Schedule {1}".format(self.name, random_title()), description=description or random_title(10), enabled=enabled, rrule=str(rrule)) update_payload(payload, self.optional_schedule_fields, kwargs) return self.related.schedules.post(payload)
def add_schedule(self, name='', description='', enabled=True, rrule=None, **kwargs): if rrule is None: rrule = "DTSTART:30180101T000000Z RRULE:FREQ=YEARLY;INTERVAL=1" payload = dict( name=name or "{0} Schedule {1}".format(self.name, random_title()), description=description or random_title(10), enabled=enabled, rrule=str(rrule) ) update_payload(payload, self.optional_schedule_fields, kwargs) schedule = self.related.schedules.post(payload) # register schedule in temporary dependency store as means of # getting its teardown method to run on cleanup if not hasattr(self, '_schedules_store'): self._schedules_store = set() if schedule not in self._schedules_store: self._schedules_store.add(schedule) return schedule
def payload(self, inventory, credential, module_name='ping', **kwargs): payload = PseudoNamespace(inventory=inventory.id, credential=credential.id, module_name=module_name) optional_fields = ('diff_mode', 'extra_vars', 'module_args', 'job_type', 'limit', 'forks', 'verbosity') return update_payload(payload, optional_fields, kwargs)
def payload(self, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'WorkflowJobTemplate - {}'.format(random_title()), description=kwargs.get('description') or random_title(10)) optional_fields = ( "allow_simultaneous", "ask_variables_on_launch", "ask_inventory_on_launch", "ask_scm_branch_on_launch", "ask_limit_on_launch", "limit", "scm_branch", "survey_enabled", "webhook_service", "webhook_credential", ) update_payload(payload, optional_fields, kwargs) extra_vars = kwargs.get('extra_vars', not_provided) if extra_vars != not_provided: if isinstance(extra_vars, dict): extra_vars = json.dumps(extra_vars) payload.update(extra_vars=extra_vars) if kwargs.get('organization'): payload.organization = kwargs.get('organization').id if kwargs.get('inventory'): payload.inventory = kwargs.get('inventory').id if kwargs.get('webhook_credential'): webhook_cred = kwargs.get('webhook_credential') if isinstance(webhook_cred, int): payload.update(webhook_credential=int(webhook_cred)) elif hasattr(webhook_cred, 'id'): payload.update(webhook_credential=webhook_cred.id) else: raise AttributeError( "Webhook credential must either be integer of pkid or Credential object" ) return payload
def payload(self, inventory, **kwargs): payload = PseudoNamespace( name=kwargs.get('name') or 'Host{}'.format(random_title(non_ascii=False)), description=kwargs.get('description') or random_title(10), inventory=inventory.id, ) optional_fields = ('enabled', 'instance_id') update_payload(payload, optional_fields, kwargs) variables = kwargs.get('variables', not_provided) if variables is None: variables = dict(ansible_host='127.0.0.1', ansible_connection='local') if variables != not_provided: if isinstance(variables, dict): variables = json.dumps(variables) payload.variables = variables return payload
def payload(self, job_type='run', playbook='ping.yml', **kwargs): name = kwargs.get('name') or 'JobTemplate - {}'.format(random_title()) description = kwargs.get('description') or random_title(10) payload = PseudoNamespace( name=name, description=description, job_type=job_type) optional_fields = ( 'ask_scm_branch_on_launch', 'ask_credential_on_launch', 'ask_diff_mode_on_launch', 'ask_inventory_on_launch', 'ask_job_type_on_launch', 'ask_limit_on_launch', 'ask_skip_tags_on_launch', 'ask_tags_on_launch', 'ask_variables_on_launch', 'ask_verbosity_on_launch', 'allow_simultaneous', 'become_enabled', 'diff_mode', 'force_handlers', 'forks', 'host_config_key', 'job_tags', 'limit', 'skip_tags', 'start_at_task', 'survey_enabled', 'timeout', 'use_fact_cache', 'vault_credential', 'verbosity', 'job_slice_count', 'webhook_service', 'webhook_credential', 'scm_branch') update_payload(payload, optional_fields, kwargs) extra_vars = kwargs.get('extra_vars', not_provided) if extra_vars != not_provided: if isinstance(extra_vars, dict): extra_vars = json.dumps(extra_vars) payload.update(extra_vars=extra_vars) if kwargs.get('project'): payload.update(project=kwargs.get('project').id, playbook=playbook) for fk_field in ('inventory', 'credential', 'webhook_credential', 'execution_environment'): rel_obj = kwargs.get(fk_field) if rel_obj is None: continue elif isinstance(rel_obj, int): payload.update(**{fk_field: int(rel_obj)}) elif hasattr(rel_obj, 'id'): payload.update(**{fk_field: rel_obj.id}) else: raise AttributeError(f'Related field {fk_field} must be either integer of pkid or object') return payload
def test_empty_payload(self): fields = ('one', 'two', 'three', 'four') kwargs = dict(two=2, four=4) payload = {} utils.update_payload(payload, fields, kwargs) assert payload == kwargs
def test_untouched_payload(self): fields = ('not', 'in', 'kwargs') kwargs = dict(one=1, two=2) payload = dict(three=3, four=4) utils.update_payload(payload, fields, kwargs) assert payload == dict(three=3, four=4)
def test_overwritten_payload(self): fields = ('one', 'two') kwargs = dict(one=1, two=2) payload = dict(one='one', two='two') utils.update_payload(payload, fields, kwargs) assert payload == kwargs
def test_not_provided_strips_payload(self): fields = ('one', 'two') kwargs = dict(one=utils.not_provided) payload = dict(one=1, two=2) utils.update_payload(payload, fields, kwargs) assert payload == dict(two=2)