def test_namespace_is_none(self): params = dict( name="name", namespace=None, ) with pytest.raises(AssertionError, match="BUG"): arguments.get_mutation_payload(params)
def main(): required_if = [('state', 'present', ['command'])] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict( arguments.MUTATION_ARGUMENTS, command=dict(), timeout=dict(type='int', ), env_vars=dict(type='dict'), runtime_assets=dict(type='list'), ), ) client = arguments.get_sensu_client(module.params['auth']) path = '/mutators/{0}'.format(module.params['name']) payload = arguments.get_mutation_payload(module.params, 'command', 'timeout', 'runtime_assets') if module.params['env_vars']: payload['env_vars'] = utils.dict_to_key_value_strings( module.params['env_vars']) try: changed, mutator = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=mutator) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [ ('state', 'present', ['action', 'expressions']) ] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict( arguments.get_spec( "auth", "name", "state", "labels", "annotations", "namespace", ), action=dict(choices=['allow', 'deny']), expressions=dict( type='list', elements='str', ), runtime_assets=dict( type='list', elements='str', ), ), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'filters', module.params['name'], ) payload = arguments.get_mutation_payload( module.params, 'action', 'expressions', 'runtime_assets' ) try: changed, sensu_filter = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=sensu_filter) except errors.Error as e: module.fail_json(msg=str(e))
def build_api_payload(params): payload = arguments.get_mutation_payload(params) payload["subjects"] = role_utils.build_subjects(params["groups"], params["users"]) payload["role_ref"] = role_utils.type_name_dict(*infer_role(params)) return payload
def build_api_payload(params): payload = arguments.get_mutation_payload( params, 'command', 'cron', 'handlers', 'high_flap_threshold', 'interval', 'low_flap_threshold', 'output_metric_format', 'output_metric_handlers', 'proxy_entity_name', 'proxy_requests', 'publish', 'round_robin', 'runtime_assets', 'stdin', 'subscriptions', 'timeout', 'ttl' ) if params['check_hooks']: payload['check_hooks'] = utils.dict_to_single_item_dicts(params['check_hooks']) if params['env_vars']: payload['env_vars'] = utils.dict_to_key_value_strings(params['env_vars']) return payload
def main(): required_if = [('state', 'present', ['action', 'expressions'])] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict( arguments.MUTATION_ARGUMENTS, action=dict(choices=['allow', 'deny']), expressions=dict(type='list', ), runtime_assets=dict(type='list', ), ), ) client = arguments.get_sensu_client(module.params['auth']) path = '/filters/{0}'.format(module.params['name']) payload = arguments.get_mutation_payload(module.params, 'action', 'expressions', 'runtime_assets') try: changed, sensu_filter = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=sensu_filter) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [('state', 'present', ['handlers'])] module = AnsibleModule( supports_check_mode=True, required_if=required_if, argument_spec=dict( arguments.get_spec( "auth", "name", "state", "labels", "annotations", ), handlers=dict(type='list'), ), ) client = arguments.get_sensu_client(module.params['auth']) path = '/handlers/{0}'.format(module.params['name']) payload = arguments.get_mutation_payload(module.params, 'handlers') payload['type'] = 'set' try: changed, handler = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=handler) except errors.Error as e: module.fail_json(msg=str(e))
def build_api_payload(params): payload = arguments.get_mutation_payload(params) if params['state'] == 'present': builds = [ arguments.get_spec_payload(b, *b.keys()) for b in params['builds'] ] payload["builds"] = builds return payload
def test_namespace_is_none(self): params = dict( auth=dict(namespace=None), name="name", ) assert arguments.get_mutation_payload(params) == dict( metadata=dict(name="name"), )
def test_wanted_key(self): params = dict( name="name", key="value", ) assert arguments.get_mutation_payload(params, "key") == dict( key="value", metadata=dict(name="name", ), )
def test_name_only(self): params = dict( name="name", ) assert arguments.get_mutation_payload(params) == dict( metadata=dict( name="name", ), )
def test_name_and_namespace(self): params = dict( name="name", namespace="space", ) assert arguments.get_mutation_payload(params) == dict(metadata=dict( name="name", namespace="space", ), )
def main(): required_if = [('state', 'present', ['command'])] module = AnsibleModule( supports_check_mode=True, required_if=required_if, argument_spec=dict( arguments.get_spec( "auth", "name", "state", "labels", "annotations", "namespace", ), command=dict(), filters=dict( type='list', elements='str', ), mutator=dict(), timeout=dict(type='int'), env_vars=dict(type='dict'), runtime_assets=dict( type='list', elements='str', ), ), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'handlers', module.params['name'], ) payload = arguments.get_mutation_payload(module.params, 'command', 'filters', 'mutator', 'timeout', 'runtime_assets') payload['type'] = 'pipe' if module.params['env_vars']: payload['env_vars'] = utils.dict_to_key_value_strings( module.params['env_vars']) try: changed, handler = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=handler) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [('state', 'present', ['entity_class'])] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict(arguments.get_spec( "auth", "name", "state", "labels", "annotations", "namespace", ), entity_class=dict(), subscriptions=dict( type='list', elements='str', ), system=dict(type='dict'), last_seen=dict(type='int'), deregister=dict(type='bool'), deregistration_handler=dict(), redact=dict( type='list', elements='str', ), user=dict()), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'entities', module.params['name'], ) payload = arguments.get_mutation_payload(module.params, 'entity_class', 'subscriptions', 'system', 'last_seen', 'deregister', 'redact', 'user') if module.params['deregistration_handler']: payload['deregistration'] = dict( handler=module.params['deregistration_handler']) try: changed, entity = utils.sync( module.params['state'], client, path, payload, module.check_mode, do_differ, ) module.exit_json(changed=changed, object=entity) except errors.Error as e: module.fail_json(msg=str(e))
def main(): module = AnsibleModule( supports_check_mode=True, argument_spec=dict( arguments.get_spec("auth", "name", "state"), rules=dict( type="list", elements="dict", options=dict( verbs=dict( required=True, type="list", elements="str", choices=["get", "list", "create", "update", "delete"], ), resources=dict( required=True, type="list", elements="str", ), resource_names=dict( type="list", elements="str", ), ) ) ) ) msg = role_utils.validate_module_params(module.params) if msg: module.fail_json(msg=msg) client = arguments.get_sensu_client(module.params["auth"]) path = utils.build_core_v2_path( None, "clusterroles", module.params["name"], ) payload = arguments.get_mutation_payload( module.params, "rules" ) try: changed, cluster_role = utils.sync( module.params['state'], client, path, payload, module.check_mode, role_utils.do_roles_differ ) module.exit_json(changed=changed, object=cluster_role) except errors.Error as e: module.fail_json(msg=str(e))
def test_labels(self): params = dict( name="name", labels=dict( some="label", numeric=3, ), ) assert arguments.get_mutation_payload(params) == dict(metadata=dict( name="name", labels=dict( some="label", numeric="3", ), ), )
def test_annotations(self): params = dict( name="name", annotations=dict( my="Annotation", number=45, ), ) assert arguments.get_mutation_payload(params) == dict(metadata=dict( name="name", annotations=dict( my="Annotation", number="45", ), ), )
def main(): required_if = [('state', 'present', ['type', 'host', 'port'])] module = AnsibleModule( supports_check_mode=True, required_if=required_if, argument_spec=dict(arguments.get_spec( "auth", "name", "state", "labels", "annotations", "namespace", ), type=dict(choices=['tcp', 'udp']), filters=dict( type='list', elements='str', ), mutator=dict(), timeout=dict(type='int'), host=dict(), port=dict(type='int')), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'handlers', module.params['name'], ) payload = arguments.get_mutation_payload(module.params, 'type', 'filters', 'mutator', 'timeout') payload['socket'] = dict(host=module.params['host'], port=module.params['port']) try: changed, handler = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=handler) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_one_of = [['subscription', 'check']] module = AnsibleModule( supports_check_mode=True, required_one_of=required_one_of, argument_spec=dict(arguments.get_spec( 'auth', 'state', 'labels', 'annotations', 'namespace', ), subscription=dict(), check=dict(), begin=dict(type='int', ), expire=dict(type='int', ), expire_on_resolve=dict(type='bool'), reason=dict()), ) name = '{0}:{1}'.format(module.params['subscription'] or '*', module.params['check'] or '*') client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'silenced', name, ) # We add name parameter because it is actually required and must match the name that is # autogenerated on the API module.params['name'] = name payload = arguments.get_mutation_payload(module.params, 'subscription', 'check', 'begin', 'expire', 'expire_on_resolve', 'reason') try: changed, silence = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=silence) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [('state', 'present', ['command', 'timeout'])] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict( arguments.get_spec( "auth", "name", "state", "labels", "annotations", "namespace", ), command=dict(), timeout=dict(type='int', ), stdin=dict(type='bool'), runtime_assets=dict( type='list', elements='str', ), ), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'hooks', module.params['name'], ) payload = arguments.get_mutation_payload(module.params, 'command', 'timeout', 'stdin', 'runtime_assets') try: changed, hook = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=hook) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [("state", "present", ["dsn"])] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict(arguments.get_spec("auth", "name", "state"), dsn=dict(), pool_size=dict(type="int", )), ) client = arguments.get_sensu_client(module.params["auth"]) list_path = utils.build_url_path(API_GROUP, API_VERSION, None, "provider") resource_path = utils.build_url_path( API_GROUP, API_VERSION, None, "provider", module.params["name"], ) payload = dict( type="PostgresConfig", api_version=API_VERSION, spec=arguments.get_mutation_payload(module.params, "dsn", "pool_size"), ) try: changed, datastore = sync( module.params["state"], client, list_path, resource_path, payload, module.check_mode, ) # We simulate the behavior of v2 API here and only return the spec. module.exit_json( changed=changed, object=datastore and datastore["spec"], ) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [("state", "present", ["url", "sha512"])] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict( arguments.get_spec( "auth", "name", "state", "labels", "annotations", ), url=dict(), sha512=dict(), filters=dict(type="list", ), headers=dict(type="dict", ), ), ) client = arguments.get_sensu_client(module.params["auth"]) path = "/assets/{0}".format(module.params["name"]) payload = arguments.get_mutation_payload( module.params, "url", "sha512", "filters", "headers", ) try: changed, asset = utils.sync( module.params["state"], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=asset) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [ ('state', 'present', ['type', 'host', 'port']) ] module = AnsibleModule( supports_check_mode=True, required_if=required_if, argument_spec=dict( arguments.MUTATION_ARGUMENTS, type=dict(choices=['tcp', 'udp']), filters=dict( type='list', ), mutator=dict(), timeout=dict( type='int' ), host=dict(), port=dict( type='int' ) ), ) client = arguments.get_sensu_client(module.params['auth']) path = '/handlers/{0}'.format(module.params['name']) payload = arguments.get_mutation_payload( module.params, 'type', 'filters', 'mutator', 'timeout' ) payload['socket'] = dict(host=module.params['host'], port=module.params['port']) try: changed, handler = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=handler) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [('state', 'present', ['entity_class'])] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict(arguments.MUTATION_ARGUMENTS, entity_class=dict(), subscriptions=dict(type='list', ), system=dict(type='dict'), last_seen=dict(type='int'), deregister=dict(type='bool'), deregistration_handler=dict(), redact=dict(type='list'), user=dict()), ) client = arguments.get_sensu_client(module.params['auth']) path = '/entities/{0}'.format(module.params['name']) payload = arguments.get_mutation_payload(module.params, 'entity_class', 'subscriptions', 'system', 'last_seen', 'deregister', 'redact', 'user') if module.params['deregistration_handler']: payload['deregistration'] = dict( handler=module.params['deregistration_handler']) try: changed, entity = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=entity) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [ ('state', 'present', ['entity_class']) ] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict( arguments.get_spec( "auth", "name", "state", "labels", "annotations", "namespace", ), entity_class=dict(), subscriptions=dict( type='list', elements='str', ), system=dict( type='dict' ), last_seen=dict( type='int' ), deregister=dict( type='bool' ), deregistration_handler=dict(), redact=dict( type='list', elements='str', ), user=dict() ), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'entities', module.params['name'], ) payload = arguments.get_mutation_payload( module.params, 'entity_class', 'subscriptions', 'system', 'last_seen', 'deregister', 'redact', 'user' ) if module.params['deregistration_handler']: payload['deregistration'] = dict(handler=module.params['deregistration_handler']) # As per conversation with @echlebek, the only two supported entity # classes are agent and proxy. All other classes can lead to undefined # behavior and should not be used. eclass = payload.get('entity_class') if eclass and eclass not in ('agent', 'proxy'): module.deprecate( 'The `entity_class` parameter should be set to either `agent` or ' '`proxy`. All other values can result in undefined behavior of ' 'the Sensu Go backend.', version='2.0.0' ) # Agent entities always have entity:{entity_name} subscription enabled # even if we pass an empty subscriptions. In order to prevent falsely # reporting changed: true, we always add this subscription to the agent # entities. if eclass == 'agent': entity_sub = 'entity:' + module.params['name'] subs = payload.get('subscriptions', []) if entity_sub not in subs: # Copy subs in order to avoid mutating module params payload['subscriptions'] = subs + [entity_sub] try: changed, entity = utils.sync( module.params['state'], client, path, payload, module.check_mode, do_differ, ) module.exit_json(changed=changed, object=entity) except errors.Error as e: module.fail_json(msg=str(e))