def create(self, name='', description='', job_type='run', playbook='ping.yml', credential=Credential, inventory=Inventory, project=None, **kwargs): payload, credential = self.create_payload(name=name, description=description, job_type=job_type, playbook=playbook, credential=credential, inventory=inventory, project=project, **kwargs) ret = self.update_identity(JobTemplates(self.connection).post(payload)) if credential: with suppress(exc.NoContent): self.related.credentials.post(dict(id=credential.id)) if 'vault_credential' in kwargs: with suppress(exc.NoContent): if not isinstance(kwargs['vault_credential'], int): raise ValueError( "Expected 'vault_credential' value to be an integer, the id of the desired vault credential" ) self.related.credentials.post( dict(id=kwargs['vault_credential'])) return ret
def add_host(self, host=None): if host is None: host = self.related.hosts.create(inventory=self.ds.inventory) with suppress(exc.NoContent): host.related.groups.post(dict(id=self.id)) return host if isinstance(host, base.Base): host = host.json with suppress(exc.NoContent): self.related.hosts.post(host) return host
def add_galaxy_credential(self, credential): if isinstance(credential, page.Page): credential = credential.json with suppress(exc.NoContent): self.related.galaxy_credentials.post({ "id": credential.id, })
def add_notification_template(self, notification_template, endpoint="notification_templates_success"): from awxkit.api.pages.workflow_job_templates import WorkflowJobTemplate supported_endpoints = wfjt_notification_endpoints if isinstance(self, WorkflowJobTemplate) else notification_endpoints if endpoint not in supported_endpoints: raise ValueError('Unsupported notification endpoint "{0}". Please use one of {1}.'.format(endpoint, notification_endpoints)) with suppress(exc.NoContent): self.related[endpoint].post(dict(id=notification_template.id))
def remove_galaxy_credential(self, credential): if isinstance(credential, page.Page): credential = credential.json with suppress(exc.NoContent): self.related.galaxy_credentials.post({ "id": credential.id, "disassociate": True, })
def add_host(self, host=None): if host is None: return self.related.hosts.create(inventory=self) if isinstance(host, base.Base): host = host.json with suppress(exc.NoContent): self.related.hosts.post(host) return host
def add_notification_template(self, notification_template, endpoint="notification_templates_success"): if endpoint not in notification_endpoints: raise ValueError( 'Unsupported notification endpoint "{0}". Please use one of {1}.' .format(endpoint, notification_endpoints)) with suppress(exc.NoContent): self.related[endpoint].post(dict(id=notification_template.id))
def check_related(resource): examined = [] for related in resource.related.values(): if related in examined: continue print(related) with utils.suppress(exceptions.NotFound): child_related = related.get() examined.append(related) if 'results' in child_related and child_related.results: child_related = child_related.results.pop() if 'related' in child_related: for _related in child_related.related.values(): if not isinstance(_related, api.page.TentativePage) or _related in examined: continue print(_related) with utils.suppress(exceptions.NotFound): _related.get() examined.append(_related)
def wait_for_job(self, interval=5, timeout=60, **kw): """Waits until node's job exists""" adjusted_timeout = timeout - seconds_since_date_string(self.created) with suppress(WaitUntilTimeout): poll_until(self.job_exists, interval=interval, timeout=adjusted_timeout, **kw) return self
def test_suppress(): callback = RecordingCallback() with utils.suppress(ZeroDivisionError, IndexError): raise ZeroDivisionError callback() raise IndexError raise KeyError assert callback.call_count == 0 with utils.suppress(ZeroDivisionError, IndexError): raise IndexError callback() raise ZeroDivisionError raise KeyError assert callback.call_count == 0 with pytest.raises(KeyError): with utils.suppress(ZeroDivisionError, IndexError): raise KeyError callback() raise ZeroDivisionError raise IndexError assert callback.call_count == 0
def _associate(self, resource, job_result='any', disassociate=False): if job_result not in job_results: raise ValueError( 'Unsupported job_result type "{0}". Please use one of {1}.'. format(job_result, job_results)) result_attr = 'notification_templates_{0}'.format(job_result) if result_attr not in resource.related: raise ValueError( 'Unsupported resource "{0}". Does not have a related {1} field.' .format(resource, result_attr)) payload = dict(id=self.id) if disassociate: payload['disassociate'] = True with suppress(exc.NoContent): getattr(resource.related, result_attr).post(payload)
def remove_credential(self, credential): with suppress(exc.NoContent): self.related.credentials.post( dict(id=credential.id, disassociate=True))
def add_instance_group(self, instance_group): with suppress(exc.NoContent): self.related['instance_groups'].post(dict(id=instance_group.id))
def add_extra_credential(self, credential): with suppress(exc.NoContent): self.related.extra_credentials.post( dict(id=credential.id, associate=True))
def add_group(self, group): if isinstance(group, page.Page): group = group.json with suppress(exc.NoContent): self.related.children.post(group)
def remove_group(self, group): if isinstance(group, page.Page): group = group.json with suppress(exc.NoContent): self.related.children.post(dict(id=group.id, disassociate=True))
def add_user(self, user): if isinstance(user, page.Page): user = user.json with suppress(NoContent): self.related.users.post(user)
def remove_instance_group(self, instance_group): with suppress(exc.NoContent): self.related['instance_groups'].post( dict(id=instance_group.id, disassociate=instance_group.id))
def add_instance(self, instance): with suppress(exc.NoContent): self.related.instances.post(dict(id=instance.id))
def remove_instance(self, instance): with suppress(exc.NoContent): self.related.instances.post(dict(id=instance.id, disassociate=True))
def delete(self): r = self.connection.delete(self.endpoint) with suppress(exc.NoContent): return self.page_identity(r)
def add_credential(self, cred): with suppress(exc.NoContent): self.related.credentials.post(dict(id=cred.id))
def remove_all_credentials(self): for cred in self.related.credentials.get().results: with suppress(exc.NoContent): self.related.credentials.post( dict(id=cred.id, disassociate=True))
def add_label(self, label): if isinstance(label, page.Page): label = label.json with suppress(exc.NoContent): self.related.labels.post(label)
def add_admin(self, user): if isinstance(user, page.Page): user = user.json with suppress(exc.NoContent): self.related.admins.post(user)