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 test_no_key(self): params = dict( name="name", key="value" ) assert arguments.get_spec_payload(params) == dict()
def test_spec_payload(self): params = dict( name="name", key="value", ) assert arguments.get_spec_payload(params, "key") == dict(key="value", )
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', 'publish', 'round_robin', 'runtime_assets', 'stdin', 'subscriptions', 'timeout', 'ttl') if params['proxy_requests']: payload['proxy_requests'] = arguments.get_spec_payload( params['proxy_requests'], 'entity_attributes', 'splay', 'splay_coverage', ) 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 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 _update_payload_with_check_attributes(payload, check_attributes): if not check_attributes: return if check_attributes['status']: check_attributes['status'] = STATUS_MAP[check_attributes['status']] filtered_attributes = arguments.get_spec_payload(check_attributes, *check_attributes.keys()) payload['check'].update(filtered_attributes)
def _build_api_payload(client, params): payload = arguments.get_spec_payload(params, 'timestamp') payload['metadata'] = dict(namespace=params['namespace']) payload['entity'] = get_entity(client, params['namespace'], params['entity']) payload['check'] = get_check(client, params['namespace'], params['check']) _update_payload_with_check_attributes(payload, params['check_attributes']) _update_payload_with_metric_attributes(payload, params['metric_attributes']) return payload
def build_vault_provider_spec(params): if params["state"] == "absent": return {} client = arguments.get_spec_payload( params, "address", "token", "version", "max_retries", ) if params.get("tls"): client["tls"] = arguments.get_spec_payload( params["tls"], "ca_cert", "client_cert", "client_key", "cname", ) if params.get("timeout"): client["timeout"] = _format_seconds(params["timeout"]) if params.get("rate_limit") or params.get("burst_limit"): client["rate_limiter"] = arguments.get_renamed_spec_payload( params, dict( rate_limit="limit", burst_limit="burst", ) ) return dict(client=client)
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=arguments.get_spec("auth", "name", "state"), ) module.params['auth']['namespace'] = None client = arguments.get_sensu_client(module.params['auth']) path = '/namespaces/{0}'.format(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 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, metadata=dict(name=module.params["name"]), spec=arguments.get_spec_payload(module.params, "dsn", "pool_size"), ) try: changed, datastore = sync( module.params["state"], client, list_path, resource_path, payload, module.check_mode, ) module.exit_json(changed=changed, object=datastore) except errors.Error as e: module.fail_json(msg=str(e))
def _update_payload_with_metric_attributes(payload, metric_attributes): if not metric_attributes: return payload['metrics'] = arguments.get_spec_payload(metric_attributes, *metric_attributes.keys())