def main(): module = AnsibleModule( supports_check_mode=True, argument_spec=dict( arguments.get_spec('auth', 'namespace'), subscription=dict(), check=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", None if name == "*:*" else name, ) try: silences = utils.prepare_result_list(utils.get(client, path)) except errors.Error as e: module.fail_json(msg=str(e)) module.exit_json(changed=False, objects=silences)
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", "namespace", ), handlers=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, '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 main(): required_by = {'check': ['entity']} module = AnsibleModule( supports_check_mode=True, required_by=required_by, argument_spec=dict( arguments.get_spec("auth", "namespace"), check=dict(), entity=dict(), ), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'events', module.params['entity'], module.params['check'], ) try: events = utils.prepare_result_list(utils.get(client, path)) except errors.Error as e: module.fail_json(msg=str(e)) module.exit_json(changed=False, objects=events)
def get_check(client, namespace, check): check_path = utils.build_core_v2_path(namespace, 'checks', check) resp = client.get(check_path) if resp.status != 200: raise errors.SyncError( "Check with name '{0}' does not exist on remote.".format(check)) return resp.json
def get_entity(client, namespace, entity): entity_path = utils.build_core_v2_path(namespace, 'entities', entity) resp = client.get(entity_path) if resp.status != 200: raise errors.SyncError( "Entity with name '{0}' does not exist on remote.".format(entity)) return resp.json
def main(): required_if = [('state', 'enabled', ('password', ))] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict(arguments.get_spec("auth", "name"), state=dict( default='enabled', choices=['enabled', 'disabled'], ), password=dict(no_log=True), groups=dict(type='list', )), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path(None, 'users', module.params['name']) state = module.params['state'] remote_object = utils.get(client, path) if remote_object is None and state == 'disabled' and module.params[ 'password'] is None: module.fail_json(msg='Cannot disable a non existent user') payload = arguments.get_spec_payload(module.params, 'password', 'groups') payload['username'] = module.params['name'] payload['disabled'] = module.params['state'] == 'disabled' try: changed, user = sync(remote_object, state, client, path, payload, module.check_mode) module.exit_json(changed=changed, object=user) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [("state", "present", ["cluster_role"])] module = AnsibleModule(required_if=required_if, supports_check_mode=True, argument_spec=dict( arguments.get_spec("auth", "name", "state"), cluster_role=dict(), users=dict(type="list", ), groups=dict(type="list", ), )) msg = role_utils.validate_binding_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, "clusterrolebindings", module.params["name"], ) payload = build_api_payload(module.params) try: changed, cluster_role_binding = utils.sync( module.params["state"], client, path, payload, module.check_mode, role_utils.do_role_bindings_differ) module.exit_json(changed=changed, object=cluster_role_binding) 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 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"), ), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path(None, 'namespaces') try: namespaces = utils.get(client, path) except errors.Error as e: module.fail_json(msg=str(e)) module.exit_json(changed=False, objects=namespaces)
def main(): module = AnsibleModule( supports_check_mode=True, argument_spec=dict( arguments.get_spec("auth", "name"), state=dict( default='enabled', choices=['enabled', 'disabled'], ), password=dict( no_log=True ), groups=dict( type='list', elements='str', ) ), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path(None, 'users', module.params['name']) try: if not HAS_BCRYPT and client.version >= "5.21.0": module.fail_json( msg=missing_required_lib('bcrypt'), exception=BCRYPT_IMPORT_ERROR, ) except errors.SensuError as e: module.fail_json(msg=str(e)) try: remote_object = utils.get(client, path) except errors.Error as e: module.fail_json(msg=str(e)) if remote_object is None and module.params['password'] is None: module.fail_json(msg='Cannot create new user without a password') payload = arguments.get_spec_payload(module.params, 'password', 'groups') payload['username'] = module.params['name'] payload['disabled'] = module.params['state'] == 'disabled' try: changed, user = sync( remote_object, client, path, payload, module.check_mode ) module.exit_json(changed=changed, object=user) 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 main(): module = AnsibleModule(supports_check_mode=True, argument_spec=dict( arguments.get_spec('auth'), state=dict( choices=['enabled', 'disabled'], required=True, ))) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path(None, 'tessen') payload = dict(opt_out=module.params['state'] == 'disabled') try: changed, tessen = sync(client, path, payload, module.check_mode) module.exit_json(changed=changed, object=tessen) except errors.Error as e: module.fail_json(msg=str(e))
def main(): required_if = [("state", "present", ["builds"])] module = AnsibleModule( required_if=required_if, supports_check_mode=True, argument_spec=dict( arguments.get_spec( "auth", "name", "namespace", "state", "labels", "annotations", ), builds=dict(type="list", elements="dict", options=dict( url=dict(required=True, ), sha512=dict(required=True, ), filters=dict( type="list", elements="str", ), headers=dict(type="dict", ), )), ), ) msg = 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( module.params["namespace"], "assets", module.params["name"], ) payload = build_api_payload(module.params) try: changed, asset = utils.sync(module.params["state"], client, path, payload, module.check_mode, do_differ) module.exit_json(changed=changed, object=asset) 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=dict(), # Name is not required in info modules. ), ) client = arguments.get_sensu_client(module.params["auth"]) path = utils.build_core_v2_path(None, "users", module.params["name"]) try: users = utils.prepare_result_list(utils.get(client, path)) except errors.Error as e: module.fail_json(msg=str(e)) module.exit_json(changed=False, objects=users)
def main(): module = AnsibleModule( supports_check_mode=True, argument_spec=dict( arguments.get_spec("auth", "namespace"), timestamp=dict(type='int'), entity=dict(required=True), check=dict(required=True), check_attributes=dict( type='dict', options=dict( duration=dict(type='float'), executed=dict(type='int'), history=dict( type='list', elements='dict', ), issued=dict(type='int'), last_ok=dict(type='int'), output=dict(), state=dict(choices=['passing', 'failing', 'flapping']), status=dict( choices=['ok', 'warning', 'critical', 'unknown']), total_state_change=dict(type='int'))), metric_attributes=dict(type='dict', options=dict(handlers=dict(type='list', elements='str'), points=dict( type='list', elements='dict'))))) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'events', module.params['entity'], module.params['check'], ) try: payload = _build_api_payload(client, module.params) changed, event = send_event(client, path, payload, module.check_mode) module.exit_json(changed=changed, object=event) 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(): module = AnsibleModule(supports_check_mode=True, argument_spec=dict(arguments.get_spec("auth"), name=dict())) client = arguments.get_sensu_client(module.params["auth"]) path = utils.build_core_v2_path( None, "clusterrolebindings", module.params["name"], ) try: cluster_role_bindings = utils.prepare_result_list( utils.get(client, path)) except errors.Error as e: module.fail_json(msg=str(e)) module.exit_json(changed=False, objects=cluster_role_bindings)
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', ), 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 _api_key_login(self): # We check the API key validity by using it to fetch its metadata from # the backend. This should also take care of validating if the backend # even supports the API key authentication. url = "{0}{1}".format(self.address, utils.build_core_v2_path( None, "apikeys", self.api_key, )) headers = dict(Authorization="Key {0}".format(self.api_key)) resp = http.request("GET", url, headers=headers) if resp.status != 200: raise errors.SensuError( "The API key {0}...{1} seems to be invalid or the backend " "does not support the API key authentication".format( self.api_key[:5], self.api_key[-5:], ) ) return headers
def main(): module = AnsibleModule( supports_check_mode=True, argument_spec=arguments.get_spec("auth", "name", "state"), ) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( None, 'namespaces', module.params['name'], ) payload = arguments.get_spec_payload(module.params, 'name') try: changed, namespace = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=namespace) except errors.Error as e: module.fail_json(msg=str(e))
def test_build_url_with_namespace(self): assert utils.build_core_v2_path("default", "a").startswith( "/api/core/v2/namespaces/default/", )
def main(): required_if = [('state', 'present', ['subscriptions', 'command'])] mutually_exclusive = [('interval', 'cron')] module = AnsibleModule( supports_check_mode=True, required_if=required_if, mutually_exclusive=mutually_exclusive, argument_spec=dict( arguments.get_spec( "auth", "name", "state", "labels", "annotations", "namespace", ), command=dict(), subscriptions=dict(type='list'), handlers=dict(type='list'), interval=dict(type='int'), cron=dict(), publish=dict(type='bool'), timeout=dict(type='int'), ttl=dict(type='int'), stdin=dict(type='bool'), env_vars=dict(type='dict'), low_flap_threshold=dict(type='int'), high_flap_threshold=dict(type='int'), runtime_assets=dict(type='list'), check_hooks=dict(type='dict'), proxy_entity_name=dict(), proxy_requests=dict(type='dict', options=dict( entity_attributes=dict(type='list'), splay=dict(type='bool'), splay_coverage=dict(type='int'))), output_metric_format=dict(choices=[ 'nagios_perfdata', 'graphite_plaintext', 'influxdb_line', 'opentsdb_line' ]), output_metric_handlers=dict(type='list'), round_robin=dict(type='bool'))) validate_module_params(module) client = arguments.get_sensu_client(module.params['auth']) path = utils.build_core_v2_path( module.params['namespace'], 'checks', module.params['name'], ) payload = build_api_payload(module.params) try: changed, check = utils.sync( module.params['state'], client, path, payload, module.check_mode, ) module.exit_json(changed=changed, object=check) except errors.Error as e: module.fail_json(msg=str(e))
def test_build_path_no_namespace(self): assert utils.build_core_v2_path(None, "a").startswith("/api/core/v2/", )
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))