def main(): module = AnsibleModule( argument_spec={ "hostname": {"required": True, "type": 'str'}, "username": {"required": True, "type": 'str'}, "password": {"required": True, "type": 'str', "no_log": True}, "port": {"required": False, "type": 'int', "default": 443}, "baseline_name": {"type": 'str', "required": False}, "device_service_tags": {"required": False, "type": "list"}, "device_ids": {"required": False, "type": "list"}, "device_group_names": {"required": False, "type": "list"}, }, mutually_exclusive=[['baseline_name', 'device_service_tags', 'device_ids', 'device_group_names']], required_one_of=[['device_ids', 'device_service_tags', 'device_group_names', 'baseline_name']], supports_check_mode=False ) try: validate_inputs(module) with RestOME(module.params, req_session=True) as rest_obj: baseline_name = module.params.get("baseline_name") if baseline_name is not None: data = get_baseline_compliance_reports(rest_obj, module) else: data = get_baselines_report_by_device_ids(rest_obj, module) if data: module.exit_json(baseline_compliance_info=data) else: module.fail_json(msg="Failed to fetch the compliance baseline information.") except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule( argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "webserver_port": { "required": False, "type": "int" }, "webserver_timeout": { "required": False, "type": "int" }, }, required_one_of=[["webserver_port", "webserver_timeout"]], supports_check_mode=True) try: with RestOME(module.params, req_session=False) as rest_obj: updated_payload, port_change = get_updated_payload( rest_obj, module) msg = "Successfully updated network web server configuration." resp = rest_obj.invoke_request("PUT", WEBSERVER_CONFIG, data=updated_payload) module.exit_json(msg=msg, webserver_configuration=resp.json_data, changed=True) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except SSLError as err: if port_change: module.exit_json(msg="{0} Port has changed to {1}.".format( msg, port_change), webserver_configuration=updated_payload, changed=True) else: module.fail_json(msg=str(err)) except (IOError, ValueError, TypeError, ConnectionError, SSLValidationError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule( argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "power_state": { "required": True, "type": "str", "choices": ["on", "off", "coldboot", "warmboot", "shutdown"] }, "device_service_tag": { "required": False, "type": "str" }, "device_id": { "required": False, "type": "int" }, }, required_one_of=[["device_service_tag", "device_id"]], mutually_exclusive=[["device_service_tag", "device_id"]], supports_check_mode=True) try: if module.params['device_id'] is None and module.params[ 'device_service_tag'] is None: module.fail_json( msg= "device_id and device_service_tag attributes should not be None." ) job_status = {} with RestOME(module.params, req_session=True) as rest_obj: payload = get_device_resource(module, rest_obj) job_status = spawn_update_job(rest_obj, payload) except HTTPError as err: module.fail_json(msg=str(err), job_status=json.load(err)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: module.fail_json(msg=str(err)) module.exit_json(msg="Power State operation job submitted successfully.", job_status=job_status, changed=True)
def test_invoke_request_error_case_handling(self, exc, mock_response, mocker): open_url_mock = mocker.patch('ansible.module_utils.remote_management.dellemc.ome.open_url', return_value=mock_response) open_url_mock.side_effect = exc("test") module_params = {'hostname': '192.168.0.1', 'username': '******', 'password': '******', "port": 443} req_session = False with pytest.raises(exc) as e: with RestOME(module_params, req_session) as obj: obj.invoke_request("/testpath", "GET")
def test_invoke_request_without_session(self, mock_response, mocker): mocker.patch('ansible.module_utils.remote_management.dellemc.ome.open_url', return_value=mock_response) module_params = {'hostname': '192.168.0.1', 'username': '******', 'password': '******', "port": 443} req_session = False with RestOME(module_params, req_session) as obj: response = obj.invoke_request("/testpath", "GET") assert response.status_code == 200 assert response.json_data == {"value": "data"} assert response.success is True
def test_invoke_request_http_error_handling(self, mock_response, mocker): open_url_mock = mocker.patch('ansible.module_utils.remote_management.dellemc.ome.open_url', return_value=mock_response) open_url_mock.side_effect = HTTPError('http://testhost.com/', 400, 'Bad Request Error', {}, None) module_params = {'hostname': '192.168.0.1', 'username': '******', 'password': '******', "port": 443} req_session = False with pytest.raises(HTTPError) as e: with RestOME(module_params, req_session) as obj: obj.invoke_request("/testpath", "GET")
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": 'str' }, "username": { "required": True, "type": 'str' }, "password": { "required": True, "type": 'str', "no_log": True }, "port": { "required": False, "type": 'int', "default": 443 }, "account_id": { "type": 'int', "required": False }, }, supports_check_mode=False) module.deprecate( "The 'dellemc_ome_user_facts' module has been deprecated. " "Use 'ome_user_info' instead", version=3.3) account_uri = "AccountService/Accounts" try: with RestOME(module.params, req_session=True) as rest_obj: if module.params.get("account_id") is not None: # Fetch specific account account_id = module.params.get("account_id") account_path = "{0}('{1}')".format(account_uri, account_id) else: # Fetch all users account_path = account_uri resp = rest_obj.invoke_request('GET', account_path) user_facts = resp.json_data # check for 200 status as GET only returns this for success if resp.status_code == 200: module.exit_json( ansible_facts={module.params["hostname"]: user_facts}) else: module.fail_json(msg="Failed to fetch user facts") except HTTPError as err: module.fail_json(msg=json.load(err)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: module.fail_json(msg=str(err))
def main(): system_query_options = {"type": 'dict', "required": False, "options": { "device_id": {"type": 'list'}, "device_service_tag": {"type": 'list'}, "inventory_type": {"type": 'str'}, "filter": {"type": 'str', "required": False}, }} module = AnsibleModule( argument_spec={ "hostname": {"required": True, "type": 'str'}, "username": {"required": True, "type": 'str'}, "password": {"required": True, "type": 'str', "no_log": True}, "port": {"required": False, "default": 443, "type": 'int'}, "fact_subset": {"required": False, "default": "basic_inventory", "choices": ['basic_inventory', 'detailed_inventory', 'subsystem_health']}, "system_query_options": system_query_options, }, required_if=[['fact_subset', 'detailed_inventory', ['system_query_options']], ['fact_subset', 'subsystem_health', ['system_query_options']], ], supports_check_mode=False) try: _validate_inputs(module.params) with RestOME(module.params, req_session=True) as rest_obj: device_facts = _get_resource_parameters(module.params, rest_obj) resp_status = [] if device_facts.get("basic_inventory"): query_param = _get_query_parameters(module.params) resp = rest_obj.invoke_request('GET', device_facts["basic_inventory"], query_param=query_param) device_facts = resp.json_data resp_status.append(resp.status_code) else: for identifier_type, path_dict_map in device_facts.items(): for identifier, path in path_dict_map.items(): try: resp = rest_obj.invoke_request('GET', path) data = resp.json_data resp_status.append(resp.status_code) except HTTPError as err: data = str(err) path_dict_map[identifier] = data if any(device_fact_error_report): if "device_service_tag" in device_facts: device_facts["device_service_tag"].update(device_fact_error_report) else: device_facts["device_service_tag"] = device_fact_error_report if 200 in resp_status: module.exit_json(device_info=device_facts) else: module.fail_json(msg="Failed to fetch the device information") except (URLError, HTTPError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": 'str' }, "username": { "required": True, "type": 'str' }, "password": { "required": True, "type": 'str', "no_log": True }, "port": { "required": False, "type": 'int', "default": 443 }, "template_id": { "type": 'int', "required": False }, }, supports_check_mode=False) module.deprecate( "The 'dellemc_ome_template_facts' module has been deprecated. " "Use 'ome_template_info' instead", version=3.3) template_uri = "TemplateService/Templates" try: with RestOME(module.params, req_session=True) as rest_obj: if module.params.get("template_id") is not None: # Fetch specific template template_id = module.params.get("template_id") template_path = "{0}({1})".format(template_uri, template_id) else: # Fetch all templates template_path = template_uri resp = rest_obj.invoke_request('GET', template_path) template_facts = resp.json_data if resp.status_code == 200: module.exit_json( ansible_facts={module.params["hostname"]: template_facts}) else: module.fail_json(msg="Failed to fetch the template facts") except HTTPError as err: module.fail_json(msg=json.load(err)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "template_name": { "required": True, "type": "str" }, "identity_pool_name": { "required": False, "type": "str" }, }, ) try: with RestOME(module.params, req_session=True) as rest_obj: template_id = get_template_id(rest_obj, module) identity_id, message = 0, "Successfully detached identity pool from template." if module.params["identity_pool_name"] is not None: identity_id = get_identity_id(rest_obj, module) message = "Successfully attached identity pool to template." payload = { "TemplateId": template_id, "IdentityPoolId": identity_id } resp = rest_obj.invoke_request("POST", CONFIG_URI, data=payload) if resp.status_code == 200: module.exit_json(msg=message, changed=True) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (ValueError, TypeError, ConnectionError, SSLError, SSLValidationError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule( argument_spec={ "hostname": {"required": True, "type": 'str'}, "username": {"required": True, "type": 'str'}, "password": {"required": True, "type": 'str', "no_log": True}, "port": {"required": False, "default": 443, "type": 'int'}, "schedule": {"required": False, "default": "run_now", "type": 'str', "choices": ['run_now', 'run_later']}, "schedule_cron": {"required": False, "default": "startnow", "type": 'str'}, "name": {"required": True, "type": 'str'}, "discover_range": {"required": False, "type": 'list'}, "discover_username": {"required": False, "type": 'str'}, "discover_password": {"required": False, "type": 'str', "no_log": True}, "trap_destination": {"required": False, "default": False, "type": 'bool'}, "device_type": {"required": False, "default": "server", "type": 'str', "choices": ['server', 'chassis']}, "state": {"required": False, "default": "present", "choices": ['present', 'absent']} }, required_if=[['schedule', 'run_later', ['schedule_cron']], ['state', 'present', ['discover_range', 'discover_username', 'discover_password']]], supports_check_mode=False) try: with RestOME(module.params, req_session=True) as rest_obj: discovery_job = get_discovery_job(module.params['name'], rest_obj) if module.params['state'] == "present": discovery_payload, http_method, path, changed = update_discover_device_payload(module.params, discovery_job) if changed: resp = rest_obj.invoke_request(http_method, path, data=discovery_payload) if resp.success: if http_method == "POST": module.exit_json(msg="Successfully created a Discovery Job", changed=True, status=resp.json_data, data=discovery_payload) elif http_method == "PUT": module.exit_json(msg="Successfully modified a Discovery Job", changed=True, status=resp.json_data, data=discovery_payload) else: module.exit_json(msg="No changes made to Discovery Job", changed=False, status=resp.json_data, data=discovery_payload) elif module.params['state'] == "absent": if discovery_job: discovery_id = discovery_job['DiscoveryConfigGroupId'] delete_resp = delete_discovery_job(discovery_id, rest_obj) if delete_resp.success: module.exit_json(msg="Successfully deleted a Discovery Job", changed=True) else: fail_module(module, msg="Unable to find Discovery Job %s" % (module.params['name'])) except HTTPError as err: fail_module(module, msg=str(err), status=json.load(err)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: fail_module(module, msg=str(err))
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": 'str' }, "username": { "required": True, "type": 'str' }, "password": { "required": True, "type": 'str', "no_log": True }, "port": { "required": False, "type": 'int', "default": 443 }, "baseline_name": { "type": 'str', "required": False }, }, supports_check_mode=False) try: with RestOME(module.params, req_session=False) as rest_obj: baseline_name = module.params.get("baseline_name") resp = rest_obj.invoke_request('GET', "UpdateService/Baselines") data = resp.json_data if len(data["value"]) == 0: module.fail_json( msg="No firmware baseline exists in the system.") if baseline_name is not None: data = get_specific_baseline(module, baseline_name, data) module.exit_json( msg="Successfully fetched firmware baseline information.", baseline_info=data) except HTTPError as err: if err.getcode() == 404: module.fail_json( msg="404 Not Found.The requested resource is not available.") module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (IOError, ValueError, SSLError, TypeError, ConnectionError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))
def main(): settings_options = {"starting_mac_address": {"type": 'str'}, "identity_count": {"type": 'int'}} iscsi_specific_settings = {"starting_mac_address": {"type": 'str'}, "identity_count": {"type": 'int'}, "initiator_config": {"options": {"iqn_prefix": {"type": 'str'}}, "type": "dict"}, "initiator_ip_pool_settings": {"options": {"ip_range": {"type": 'str'}, "subnet_mask": {"type": 'str'}, "gateway": {"type": 'str'}, "primary_dns_server": {"type": 'str'}, "secondary_dns_server": {"type": 'str'}}, "type": "dict"}} fc_settings = {"starting_address": {"type": "str"}, "identity_count": {"type": "int"}} module = AnsibleModule( argument_spec={ "hostname": {"required": True, "type": "str"}, "username": {"required": True, "type": "str"}, "password": {"required": True, "type": "str", "no_log": True}, "port": {"required": False, "type": "int", "default": 443}, "state": {"type": "str", "required": False, "default": "present", "choices": ['present', 'absent']}, "pool_name": {"required": True, "type": "str"}, "new_pool_name": {"required": False, "type": "str"}, "pool_description": {"required": False, "type": "str"}, "ethernet_settings": {"required": False, "type": "dict", "options": settings_options}, "fcoe_settings": {"required": False, "type": "dict", "options": settings_options}, "iscsi_settings": {"required": False, "type": "dict", "options": iscsi_specific_settings}, "fc_settings": {"required": False, "type": "dict", "options": fc_settings}, }, ) try: with RestOME(module.params, req_session=True) as rest_obj: state = module.params["state"] if state == "present": message = pool_create_modify(module, rest_obj) module.exit_json(msg=message["msg"], pool_status=message["result"], changed=True) else: message = pool_delete(module, rest_obj) module.exit_json(msg=message["msg"], changed=True) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (IOError, ValueError, SSLError, TypeError, ConnectionError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))
def test_get_report_list_error_case(self, mock_response, mocker): mocker.patch( 'ansible.module_utils.remote_management.dellemc.ome.open_url', return_value=mock_response) invoke_obj = mocker.patch( 'ansible.module_utils.remote_management.dellemc.ome.RestOME.invoke_request', side_effect=HTTPError('http://testhost.com/', 400, 'Bad Request Error', {}, None)) module_params = { 'hostname': '100.xx.xx.xx', 'username': '******', 'password': '******', "port": 443 } with pytest.raises(HTTPError) as e: with RestOME(module_params, False) as obj: obj.get_all_report_details("DeviceService/Devices")
def main(): module = AnsibleModule( argument_spec={ "hostname": {"required": True, "type": 'str'}, "username": {"required": True, "type": 'str'}, "password": {"required": True, "type": 'str', "no_log": True}, "port": {"required": False, "default": 443, "type": 'int'}, "name": {"required": True, "type": 'str'}, "devices": {"required": True, "type": 'list'}, "state": {"required": False, "default": "present", "choices": ['present', 'absent']} }, supports_check_mode=False) try: with RestOME(module.params, req_session=True) as rest_obj: group = get_group(module.params['name'], rest_obj) if group: if module.params['state'] == 'present': if len(module.params['devices']) > 0: device_ids = [] group_device_ids = get_group_devices(group['Id'], rest_obj) for device_name in module.params['devices']: device = get_device(device_name, rest_obj) if device and device['Id'] not in group_device_ids: device_ids.append(device['Id']) if len(device_ids) > 0: device_resp = add_device_to_group(group['Id'], device_ids, rest_obj) if device_resp.success: module.exit_json(msg="Successfully added devices to group", changed=True) else: module.exit_json(msg="No devices added to group", changed=False) if module.params['state'] == 'absent': fail_module(module, msg="This feature is not supported at this time") else: fail_module(module, msg="Unable to find group %s" %s (module.params['name'])) except HTTPError as err: fail_module(module, msg=str(err), status=json.load(err)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: fail_module(module, msg=str(err))
def test_build_url(self, query_param, mocker): """builds complete url""" base_uri = 'https://100.100.0.9:443/api' path = "AccountService/Accounts" module_params = { 'hostname': '100.100.0.9', 'username': '******', 'password': '******', "port": 443 } mocker.patch( 'ansible.module_utils.remote_management.dellemc.ome.RestOME._get_base_url', return_value=base_uri) inp = query_param["inp"] out = query_param["out"] url = RestOME(module_params=module_params)._build_url(path, query_param=inp) assert url == base_uri + "/" + path + "?" + out assert "+" not in url
def main(): module = AnsibleModule( argument_spec={ "hostname": {"required": True, "type": 'str'}, "username": {"required": True, "type": 'str'}, "password": {"required": True, "type": 'str', "no_log": True}, "port": {"required": False, "type": 'int', "default": 443}, "template_id": {"type": 'int', "required": False}, "system_query_options": {"required": False, "type": 'dict', "options": {"filter": {"type": 'str', "required": False}} }, }, mutually_exclusive=[['template_id', 'system_query_options']], supports_check_mode=False ) template_uri = "TemplateService/Templates" try: with RestOME(module.params, req_session=True) as rest_obj: query_param = None if module.params.get("template_id") is not None: # Fetch specific template template_id = module.params.get("template_id") template_path = "{0}({1})".format(template_uri, template_id) elif module.params.get("system_query_options") is not None: # Fetch all the templates based on Name query_param = _get_query_parameters(module.params) template_path = template_uri else: # Fetch all templates template_path = template_uri resp = rest_obj.invoke_request('GET', template_path, query_param=query_param) template_facts = resp.json_data if resp.status_code == 200: module.exit_json(template_info={module.params["hostname"]: template_facts}) else: module.fail_json(msg="Failed to fetch the template facts") except HTTPError as err: module.fail_json(msg=json.load(err)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: module.fail_json(msg=str(err))
def test_get_all_report_details(self, mock_response, mocker): mock_response.success = True mock_response.status_code = 200 mock_response.json_data = { "@odata.count": 50, "value": list(range(51)) } mocker.patch( 'ansible.module_utils.remote_management.dellemc.ome.RestOME.invoke_request', return_value=mock_response) module_params = { 'hostname': '100.xx.xx.xx', 'username': '******', 'password': '******', "port": 443 } with RestOME(module_params, True) as obj: reports = obj.get_all_report_details("DeviceService/Devices") assert reports == { "resp_obj": mock_response, "report_list": list(range(51)) }
def main(): module = AnsibleModule( argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "enable_ntp": { "required": True, "type": "bool" }, "time_zone": { "required": False, "type": "str" }, "system_time": { "required": False, "type": "str" }, "primary_ntp_address": { "required": False, "type": "str" }, "secondary_ntp_address1": { "required": False, "type": "str" }, "secondary_ntp_address2": { "required": False, "type": "str" }, }, required_if=[[ 'enable_ntp', False, ( 'time_zone', 'system_time', ), True ], [ 'enable_ntp', True, ('time_zone', 'primary_ntp_address', 'secondary_ntp_address1', 'secondary_ntp_address2'), True ]], mutually_exclusive=[['system_time', 'primary_ntp_address'], ['system_time', 'secondary_ntp_address1'], ['system_time', 'secondary_ntp_address2']], supports_check_mode=True, ) try: validate_input(module) with RestOME(module.params, req_session=False) as rest_obj: validate_time_zone(module, rest_obj) payload = get_payload(module) updated_payload = get_updated_payload(rest_obj, module, payload) resp = rest_obj.invoke_request("PUT", TIME_CONFIG, data=updated_payload, api_timeout=150) module.exit_json(msg="Successfully configured network time.", time_configuration=resp.json_data, changed=True) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (IOError, ValueError, SSLError, TypeError, ConnectionError, SSLValidationError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule( argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "ip_address": { "required": False, "type": "str" }, "proxy_port": { "required": False, "type": "int" }, "enable_proxy": { "required": True, "type": "bool" }, "proxy_username": { "required": False, "type": "str" }, "proxy_password": { "required": False, "type": "str", "no_log": True }, "enable_authentication": { "required": False, "type": "bool" }, }, required_if=[ ['enable_proxy', True, ['ip_address', 'proxy_port']], [ 'enable_authentication', True, ['proxy_username', 'proxy_password'] ], ], ) try: with RestOME(module.params, req_session=True) as rest_obj: payload = get_payload(module) updated_payload = get_updated_payload(rest_obj, module, payload) resp = rest_obj.invoke_request("PUT", PROXY_CONFIG, data=updated_payload) module.exit_json( msg="Successfully updated network proxy configuration.", proxy_configuration=resp.json_data, changed=True) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (IOError, ValueError, SSLError, TypeError, ConnectionError, SSLValidationError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": 'str' }, "username": { "required": True, "type": 'str' }, "password": { "required": True, "type": 'str', "no_log": True }, "port": { "required": False, "type": 'int', "default": 443 }, "account_id": { "type": 'int', "required": False }, "system_query_options": { "required": False, "type": 'dict', "options": { "filter": { "type": 'str', "required": False }, } }, }, mutually_exclusive=[('account_id', 'system_query_options')], supports_check_mode=False) account_uri = "AccountService/Accounts" query_param = None try: with RestOME(module.params, req_session=True) as rest_obj: if module.params.get("account_id") is not None: # Fetch specific account account_id = module.params.get("account_id") account_path = "{0}('{1}')".format(account_uri, account_id) elif module.params.get("system_query_options") is not None: # Fetch all the user based on UserName query_param = _get_query_parameters(module.params) account_path = account_uri else: # Fetch all users account_path = account_uri resp = rest_obj.invoke_request('GET', account_path, query_param=query_param) user_facts = resp.json_data user_exists = True if "value" in user_facts and len(user_facts["value"]) == 0: user_exists = False # check for 200 status as GET only returns this for success if resp.status_code == 200 and user_exists: module.exit_json(user_info={module.params["hostname"]: user_facts}) else: module.fail_json(msg="Unable to retrieve the account details.") except HTTPError as err: module.fail_json(msg=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (SSLValidationError, ConnectionError, TypeError, ValueError) as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": 'str' }, "username": { "required": True, "type": 'str' }, "password": { "required": True, "type": 'str', "no_log": True }, "port": { "required": False, "type": 'int', "default": 443 }, "job_id": { "required": False, "type": 'int' }, "system_query_options": { "required": False, "type": 'dict', "options": { "top": { "type": 'int', "required": False }, "skip": { "type": 'int', "required": False }, "filter": { "type": 'str', "required": False }, } }, }, supports_check_mode=False) module.deprecate( "The 'dellemc_ome_job_facts' module has been deprecated. " "Use 'ome_job_info' instead", version=2.13) joburi = "JobService/Jobs" resp = None try: with RestOME(module.params, req_session=True) as rest_obj: if module.params.get("job_id") is not None: # Fetch specific job job_id = module.params.get("job_id") jpath = "{0}({1})".format(joburi, job_id) query_param = None else: # Fetch all jobs, filter and pagination options # query applicable only for all jobs list fetching query_param = _get_query_parameters(module.params) jpath = joburi resp = rest_obj.invoke_request('GET', jpath, query_param=query_param) job_facts = resp.json_data except HTTPError as httperr: module.fail_json(msg=str(httperr), job_facts=json.load(httperr)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: module.fail_json(msg=str(err)) # check for 200 status as GET only returns this for success if resp and resp.status_code == 200: module.exit_json(msg="Successfully fetched the job facts", job_facts=job_facts) else: module.fail_json(msg="Failed to fetch the job facts")
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": 'str' }, "username": { "required": True, "type": 'str' }, "password": { "required": True, "type": 'str', "no_log": True }, "port": { "required": False, "default": 443, "type": 'int' }, "state": { "required": False, "type": 'str', "default": "present", "choices": ['present', 'absent'] }, "user_id": { "required": False, "type": 'int' }, "name": { "required": False, "type": 'str' }, "attributes": { "required": False, "type": 'dict' }, }, mutually_exclusive=[ ['user_id', 'name'], ], required_if=[ ['state', 'present', ['attributes']], ], supports_check_mode=False) try: _validate_inputs(module) if module.params.get("attributes") is None: module.params["attributes"] = {} with RestOME(module.params, req_session=True) as rest_obj: method, path, payload = _get_resource_parameters(module, rest_obj) resp = rest_obj.invoke_request(method, path, data=payload) if resp.success: exit_module(module, resp, method) except HTTPError as err: fail_module(module, msg=str(err), user_status=json.load(err)) except (URLError, SSLValidationError, ConnectionError, TypeError, ValueError) as err: fail_module(module, msg=str(err))
def main(): module = AnsibleModule( argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "device_service_tag": { "required": False, "type": "list" }, "device_id": { "required": False, "type": "list" }, "dup_file": { "required": False, "type": "str" }, "device_group_names": { "required": False, "type": "list" }, "baseline_name": { "required": False, "type": "str" }, }, required_one_of=[[ "device_id", "device_service_tag", "device_group_names", "baseline_name" ]], mutually_exclusive=[["device_group_names", "device_id"], ["device_group_names", "device_service_tag"], ["baseline_name", "device_id"], ["baseline_name", "device_service_tag"], ["baseline_name", "device_group_names"]], ) validate_inputs(module) update_status, baseline_details = {}, None try: with RestOME(module.params, req_session=True) as rest_obj: if module.params.get( "baseline_name" ) is not None and module.params.get("dup_file") is None: baseline_details = get_baseline_ids(rest_obj, module) target_data = baseline_based_update(rest_obj, module, baseline_details) else: target_data = single_dup_update(rest_obj, module) job_payload = job_payload_for_update(rest_obj, module, target_data, baseline=baseline_details) update_status = spawn_update_job(rest_obj, job_payload) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (IOError, ValueError, SSLError, TypeError, ConnectionError, AttributeError) as err: module.fail_json(msg=str(err)) module.exit_json(msg="Successfully submitted the firmware update job.", update_status=update_status, changed=True)
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": 'str' }, "username": { "required": True, "type": 'str' }, "password": { "required": True, "type": 'str', "no_log": True }, "port": { "required": False, "default": 443, "type": 'int' }, "catalog_name": { "required": True, "type": 'str' }, "catalog_description": { "required": False, "type": 'str' }, "source": { "required": False, "type": 'str' }, "source_path": { "required": False, "type": 'str' }, "file_name": { "required": False, "type": 'str' }, "repository_type": { "required": False, "default": 'HTTPS', "choices": ["HTTP", "NFS", "CIFS", "HTTPS"] }, "repository_username": { "required": False, "type": 'str' }, "repository_password": { "required": False, "type": 'str', "no_log": True }, "repository_domain": { "required": False, "type": 'str' }, "check_certificate": { "required": False, "type": 'bool', "default": False }, }, supports_check_mode=False) try: with RestOME(module.params, req_session=True) as rest_obj: payload = _get_catalog_payload(module.params) resp = rest_obj.invoke_request("POST", "UpdateService/Catalogs", data=payload) if resp.success: resp_data = resp.json_data job_id = resp_data.get("TaskId") module.exit_json( msg="Successfully triggered the job to create a " "catalog with Task Id : {0}".format(job_id), catalog_status=resp_data, changed=True) else: module.fail_json( msg="Failed to trigger the job to create catalog.") except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except (URLError, SSLValidationError, SSLError, ConnectionError, TypeError, ValueError, KeyError) as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule( argument_spec={ "hostname": { "required": True, "type": 'str' }, "username": { "required": True, "type": 'str' }, "password": { "required": True, "type": 'str', "no_log": True }, "port": { "required": False, "default": 443, "type": 'int' }, "command": { "required": False, "default": "create", "aliases": ['state'], "choices": [ 'create', 'modify', 'deploy', 'delete', 'export', 'import', 'clone' ] }, "template_id": { "required": False, "type": 'int' }, "template_name": { "required": False, "type": 'str' }, "template_view_type": { "required": False, "default": 'Deployment', "choices": ['Deployment', 'Compliance', 'Inventory', 'Sample', 'None'] }, "device_id": { "required": False, "type": 'list', "default": [], "elements": 'int' }, "device_service_tag": { "required": False, "type": 'list', "default": [], "elements": 'str' }, "attributes": { "required": False, "type": 'dict' }, }, required_if=[['command', 'create', ['attributes']], ['command', 'modify', ['attributes']], ['command', 'import', ['attributes']], ['command', 'clone', ['attributes']]], mutually_exclusive=[["template_id", "template_name"]], supports_check_mode=False) try: _validate_inputs(module) with RestOME(module.params, req_session=True) as rest_obj: path, payload, rest_method = _get_resource_parameters( module, rest_obj) resp = rest_obj.invoke_request(rest_method, path, data=payload) if resp.success: exit_module(module, resp) except HTTPError as err: fail_module(module, msg=str(err), error_info=json.load(err)) except URLError as err: password_no_log(module.params.get("attributes")) module.exit_json(msg=str(err), unreachable=True) except (SSLValidationError, ConnectionError, TypeError, ValueError, KeyError) as err: fail_module(module, msg=str(err))
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "device_service_tag": { "required": False, "type": "list" }, "device_id": { "required": False, "type": "list" }, "dup_file": { "required": True, "type": "str" }, }, ) module.deprecate( "The 'dellemc_ome_firmware' module has been deprecated. " "Use 'ome_firmware' instead", version=3.2) update_status = {} try: device_id_tags = _validate_device_attributes(module) with RestOME(module.params, req_session=True) as rest_obj: device_ids = get_device_ids(rest_obj, module, device_id_tags) upload_status, token = upload_dup_file(rest_obj, module) if upload_status: report_payload = get_dup_applicability_payload( token, device_ids) if report_payload: target_data = get_applicable_components( rest_obj, report_payload, module) if target_data: job_payload = job_payload_for_update(target_data) update_status = spawn_update_job(rest_obj, job_payload) else: module.fail_json( msg="No components available for update.") except (IOError, ValueError, SSLError, TypeError, URLError, ConnectionError) as err: module.fail_json(msg=str(err)) except HTTPError as err: module.fail_json(msg=str(err), update_status=json.load(err)) module.exit_json(msg="Successfully updated the firmware.", update_status=update_status, changed=True)
def main(): port_untagged_spec = { "port": { "required": True, "type": "int" }, "untagged_network_id": { "type": "int" }, "untagged_network_name": { "type": "str" } } port_tagged_spec = { "port": { "required": True, "type": "int" }, "tagged_network_ids": { "type": "list", "elements": "int" }, "tagged_network_names": { "type": "list", "elements": "str" } } module = AnsibleModule( argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "template_name": { "required": False, "type": "str" }, "template_id": { "required": False, "type": "int" }, "nic_identifier": { "required": True, "type": "str" }, "untagged_networks": { "required": False, "type": "list", "elements": "dict", "options": port_untagged_spec }, "tagged_networks": { "required": False, "type": "list", "elements": "dict", "options": port_tagged_spec } }, required_one_of=[("template_id", "template_name"), ("untagged_networks", "tagged_networks")], mutually_exclusive=[("template_id", "template_name")], ) try: with RestOME(module.params, req_session=True) as rest_obj: untag_dict, tagged_dict = validate_vlans(module, rest_obj) payload = get_vlan_payload(module, rest_obj, untag_dict, tagged_dict) resp = rest_obj.invoke_request("POST", UPDATE_NETWORK_CONFIG, data=payload) if resp.success: module.exit_json( msg= "Successfully applied the network settings to the template", changed=True) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (IOError, ValueError, SSLError, TypeError, ConnectionError, SSLValidationError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))
def main(): ipv4_options = {"enable": {"required": True, "type": "bool"}, "enable_dhcp": {"required": False, "type": "bool"}, "static_ip_address": {"required": False, "type": "str"}, "static_subnet_mask": {"required": False, "type": "str"}, "static_gateway": {"required": False, "type": "str"}, "use_dhcp_for_dns_server_names": {"required": False, "type": "bool"}, "static_preferred_dns_server": {"required": False, "type": "str"}, "static_alternate_dns_server": {"required": False, "type": "str"}} ipv6_options = {"enable": {"required": True, "type": "bool"}, "enable_auto_configuration": {"required": False, "type": "bool"}, "static_ip_address": {"required": False, "type": "str"}, "static_prefix_length": {"required": False, "type": "int"}, "static_gateway": {"required": False, "type": "str"}, "use_dhcp_for_dns_server_names": {"required": False, "type": "bool"}, "static_preferred_dns_server": {"required": False, "type": "str"}, "static_alternate_dns_server": {"required": False, "type": "str"}} dns_options = {"register_with_dns": {"required": False, "type": "bool"}, "use_dhcp_for_dns_domain_name": {"required": False, "type": "bool"}, "dns_name": {"required": False, "type": "str"}, "dns_domain_name": {"required": False, "type": "str"}} management_vlan = {"enable_vlan": {"required": True, "type": "bool"}, "vlan_id": {"required": False, "type": "int"}} module = AnsibleModule( argument_spec={ "hostname": {"required": True, "type": "str"}, "username": {"required": True, "type": "str"}, "password": {"required": True, "type": "str", "no_log": True}, "port": {"required": False, "type": "int", "default": 443}, "enable_nic": {"required": False, "type": "bool", "default": True}, "interface_name": {"required": False, "type": "str"}, "ipv4_configuration": {"required": False, "type": "dict", "options": ipv4_options, "required_if": [ ['enable', True, ('enable_dhcp',), True], ['enable_dhcp', False, ('static_ip_address', 'static_subnet_mask', "static_gateway"), False], ['use_dhcp_for_dns_server_names', False, ('static_preferred_dns_server', 'static_alternate_dns_server'), True] ] }, "ipv6_configuration": {"required": False, "type": "dict", "options": ipv6_options, "required_if": [ ['enable', True, ('enable_auto_configuration',), True], ['enable_auto_configuration', False, ('static_ip_address', 'static_prefix_length', "static_gateway"), False], ['use_dhcp_for_dns_server_names', False, ('static_preferred_dns_server', 'static_alternate_dns_server'), True] ] }, "dns_configuration": {"required": False, "type": "dict", "options": dns_options, "required_if": [ ['register_with_dns', True, ('dns_name',), False], ['use_dhcp_for_dns_domain_name', False, ('dns_domain_name',)] ] }, "management_vlan": {"required": False, "type": "dict", "options": management_vlan, "required_if": [ ['enable_vlan', True, ('vlan_id',), True] ] }, "reboot_delay": {"required": False, "type": "int"} }, required_if=[ ["enable_nic", True, ("ipv4_configuration", "ipv6_configuration", "dns_configuration", "management_vlan"), True] ], ) try: with RestOME(module.params, req_session=True) as rest_obj: validate_input(module) ipv4_payload, ipv6_payload, dns_payload, vlan_payload = get_payload(module) updated_payload, rest_method, uri = get_updated_payload( rest_obj, module, ipv4_payload, ipv6_payload, dns_payload, vlan_payload) resp = rest_obj.invoke_request(rest_method, uri, data=updated_payload) if rest_method == "POST": module.exit_json(msg="Successfully triggered job to update network address configuration", network_configuration=updated_payload, job_info=resp.json_data, changed=True) module.exit_json(msg="Successfully updated network address configuration", network_configuration=resp.json_data, changed=True) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (IOError, ValueError, SSLError, TypeError, ConnectionError, SSLValidationError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))
def main(): module = AnsibleModule(argument_spec={ "hostname": { "required": True, "type": "str" }, "username": { "required": True, "type": "str" }, "password": { "required": True, "type": "str", "no_log": True }, "port": { "required": False, "type": "int", "default": 443 }, "command": { "type": "str", "required": False, "choices": ["generate_csr", "upload"], "default": "generate_csr" }, "distinguished_name": { "required": False, "type": "str" }, "department_name": { "required": False, "type": "str" }, "business_name": { "required": False, "type": "str" }, "locality": { "required": False, "type": "str" }, "country_state": { "required": False, "type": "str" }, "country": { "required": False, "type": "str" }, "email": { "required": False, "type": "str" }, "upload_file": { "required": False, "type": "str" }, }, required_if=[[ "command", "generate_csr", [ "distinguished_name", "department_name", "business_name", "locality", "country_state", "country", "email" ] ], ["command", "upload", ["upload_file"]]], supports_check_mode=False) header = { "Content-Type": "application/octet-stream", "Accept": "application/octet-stream" } try: with RestOME(module.params, req_session=False) as rest_obj: method, uri, payload = get_resource_parameters(module) command = module.params.get("command") dump = False if command == "upload" else True headers = header if command == "upload" else None resp = rest_obj.invoke_request(method, uri, headers=headers, data=payload, dump=dump) if resp.success: if command == "generate_csr": module.exit_json( msg= "Successfully generated certificate signing request.", csr_status=resp.json_data) module.exit_json( msg="Successfully uploaded application certificate.", changed=True) except HTTPError as err: module.fail_json(msg=str(err), error_info=json.load(err)) except URLError as err: module.exit_json(msg=str(err), unreachable=True) except (IOError, ValueError, SSLError, TypeError, ConnectionError, SSLValidationError) as err: module.fail_json(msg=str(err)) except Exception as err: module.fail_json(msg=str(err))