Example #1
0
def full_url_get(is_https, fos_ip_addr, path):
    if isinstance(is_https, bool):
        if is_https:
            return HTTPS + fos_ip_addr + str_to_yang(path), True
        else:
            return HTTP + fos_ip_addr + str_to_yang(path), False
    elif is_https.lower() == SELF_SIGNED:
        return HTTPS + fos_ip_addr + str_to_yang(path), False
    else:
        # by default, return HTTP
        return HTTP + fos_ip_addr + str_to_yang(path), False
Example #2
0
def singleton_get(login, password, fos_ip_addr, module_name, obj_name,
                  fos_version, is_https, auth, vfid, result, ssh_hostkeymust,
                  timeout):
    """
        retrieve existing user config configuration 

        :param fos_ip_addr: ip address of FOS switch
        :type fos_ip_addr: str
        :param is_https: indicate to use HTTP or HTTPS
        :type is_https: bool
        :param auth: authorization struct from login
        :type struct: dict
        :param result: dict to keep track of execution msgs
        :type result: dict
        :return: code to indicate failure or success
        :rtype: int
        :return: dict of ipfilter policy configurations
        :rtype: dict
    """
    if module_name == "brocade_chassis" and obj_name == "chassis":
        return chassis_get(login, password, fos_ip_addr, fos_version, is_https,
                           auth, vfid, result, ssh_hostkeymust, timeout)

    if module_name == "brocade_fibrechannel_configuration" and obj_name == "fabric":
        return fabric_get(login, password, fos_ip_addr, fos_version, is_https,
                          auth, vfid, result, ssh_hostkeymust, timeout)

    if module_name == "brocade_fibrechannel_configuration" and obj_name == "port_configuration":
        return port_configuration_get(login, password, fos_ip_addr,
                                      fos_version, is_https, auth, vfid,
                                      result, ssh_hostkeymust, timeout)

    # get is not support for these modules. Just return empty
    if module_name == "brocade_security" and obj_name == "security_certificate_action":
        return 0, ({"Response": {str_to_yang(obj_name): {}}})
    if module_name == "brocade_security" and obj_name == "security_certificate_generate":
        return 0, ({"Response": {str_to_yang(obj_name): {}}})
    if module_name == "brocade_security" and obj_name == "sshutil_public_key_action":
        return 0, ({"Response": {str_to_yang(obj_name): {}}})

    full_url, validate_certs = full_url_get(
        is_https, fos_ip_addr, REST_PREFIX + module_name + "/" + obj_name)

    ret, resp = url_get_to_dict(fos_ip_addr, is_https, auth, vfid, result,
                                full_url, timeout)

    if ret == ERROR_LIST_EMPTY:
        # return empty dict. GET isn't supported
        return 0, ({"Response": {str_to_yang(obj_name): {}}})

    return ret, resp
Example #3
0
def singleton_xml_str(result, obj_name, attributes):
    obj_name_yang = str_to_yang(obj_name)
    xml_str = ""

    xml_str = xml_str + "<" + obj_name_yang + ">"

    for k, v in attributes.items():
        xml_str = xml_str + "<" + k + ">"

        if isinstance(v, dict):
            for k1, v1 in v.items():
                if isinstance(v1, list):
                    for entry in v1:
                        xml_str = xml_str + "<" + k1 + ">" + str(
                            entry) + "</" + k1 + ">"
                else:
                    xml_str = xml_str + "<" + k1 + ">" + str(
                        v1) + "</" + k1 + ">"
        else:
            xml_str = xml_str + str(v)

        xml_str = xml_str + "</" + k + ">"

    xml_str = xml_str + "</" + obj_name_yang + ">"

    return xml_str
Example #4
0
def list_xml_str(result, module_name, list_name, entries):
    list_name_yang = str_to_yang(list_name)
    xml_str = ""

    for entry in entries:
        xml_str = xml_str + "<" + list_name_yang + ">"

        # add the key entries first
        for k, v in entry.items():
            if str_to_human(k) in list_entry_keys(module_name, list_name):
                result[k] = "key identified"
                xml_str = xml_str + "<" + k + ">" + str(v) + "</" + k + ">"

        # add non key entries next
        for k, v in entry.items():
            if str_to_human(k) not in list_entry_keys(module_name, list_name):
                xml_str = xml_str + "<" + k + ">"

                if isinstance(v, dict):
                    for k1, v1 in v.items():
                        if isinstance(v1, list):
                            for entry in v1:
                                xml_str = xml_str + "<" + k1 + ">" + str(
                                    entry) + "</" + k1 + ">"
                        else:
                            if v1 == None:
                                xml_str = xml_str + "<" + k1 + "></" + k1 + ">"
                            else:
                                xml_str = xml_str + "<" + k1 + ">" + str(
                                    v1) + "</" + k1 + ">"
                else:
                    if v == None:
                        xml_str = xml_str
                    else:
                        xml_str = xml_str + str(v)

                xml_str = xml_str + "</" + k + ">"

        xml_str = xml_str + "</" + list_name_yang + ">"

    return xml_str
Example #5
0
def main():
    """
    Main function
    """

    argument_spec = dict(credential=dict(required=True,
                                         type='dict',
                                         no_log=True),
                         vfid=dict(required=False, type='int'),
                         throttle=dict(required=False, type='float'),
                         timeout=dict(required=False, type='float'),
                         gather_subset=dict(required=True, type='list'))

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=False)

    input_params = module.params

    # Set up state variables
    fos_ip_addr = input_params['credential']['fos_ip_addr']
    fos_user_name = input_params['credential']['fos_user_name']
    fos_password = input_params['credential']['fos_password']
    https = input_params['credential']['https']
    ssh_hostkeymust = True
    if 'ssh_hostkeymust' in input_params['credential']:
        ssh_hostkeymust = input_params['credential']['ssh_hostkeymust']
    throttle = input_params['throttle']
    timeout = input_params['timeout']
    vfid = input_params['vfid']
    gather_subset = input_params['gather_subset']
    result = {"changed": False}

    if vfid is None:
        vfid = 128

    ret_code, auth, fos_version = login(fos_ip_addr, fos_user_name,
                                        fos_password, https, throttle, result,
                                        timeout)
    if ret_code != 0:
        module.exit_json(**result)

    facts = {}

    facts['ssh_hostkeymust'] = ssh_hostkeymust

    if gather_subset is not None:
        for subset in gather_subset:
            if subset != "all" and subset not in valid_areas:
                result["failed"] = True
                result[
                    "msg"] = "Request for unknown module and object " + subset
                logout(fos_ip_addr, https, auth, result, timeout)
                module.exit_json(**result)

    for area in valid_areas:
        if (gather_subset is None or area in gather_subset
                or "all" in gather_subset):
            get_list = False
            get_singleton = False

            if area == "brocade_access_gateway_port_group":
                module_name = "brocade_access_gateway"
                list_name = "port_group"
                get_list = True
            elif area == "brocade_access_gateway_n_port_map":
                module_name = "brocade_access_gateway"
                list_name = "n_port_map"
                get_list = True
            elif area == "brocade_access_gateway_f_port_list":
                module_name = "brocade_access_gateway"
                list_name = "f_port_list"
                get_list = True
            elif area == "brocade_access_gateway_device_list":
                module_name = "brocade_access_gateway"
                list_name = "device_list"
                get_list = True
            elif area == "brocade_access_gateway_policy":
                module_name = "brocade_access_gateway"
                obj_name = "policy"
                get_singleton = True
            elif area == "brocade_access_gateway_n_port_settings":
                module_name = "brocade_access_gateway"
                obj_name = "n_port_settings"
                get_singleton = True
            elif area == "brocade_interface_fibrechannel":
                module_name = "brocade_interface"
                list_name = "fibrechannel"
                get_list = True
            elif area == "brocade_fibrechannel_switch":
                module_name = "brocade_fibrechannel_switch"
                list_name = "fibrechannel_switch"
                get_list = True
            elif area == "brocade_logging_syslog_server":
                module_name = "brocade_logging"
                list_name = "syslog_server"
                get_list = True
            elif area == "brocade_security_ipfilter_rule":
                module_name = "brocade_security"
                list_name = "ipfilter_rule"
                get_list = True
            elif area == "brocade_security_ipfilter_policy":
                module_name = "brocade_security"
                list_name = "ipfilter_policy"
                get_list = True
            elif area == "brocade_security_user_config":
                module_name = "brocade_security"
                list_name = "user_config"
                get_list = True
            elif area == "brocade_security_security_certificate":
                module_name = "brocade_security"
                list_name = "security_certificate"
                get_list = True
            elif area == "brocade_security_sshutil_public_key":
                module_name = "brocade_security"
                list_name = "sshutil_public_key"
                get_list = True
            elif area == "brocade_media_media_rdp":
                module_name = "brocade_media"
                list_name = "media_rdp"
                get_list = True
            elif area == "brocade_fibrechannel_trunk_trunk":
                module_name = "brocade_fibrechannel_trunk"
                list_name = "trunk"
                get_list = True
            elif area == "brocade_fibrechannel_trunk_performance":
                module_name = "brocade_fibrechannel_trunk"
                list_name = "performance"
                get_list = True
            elif area == "brocade_fibrechannel_trunk_trunk_area":
                module_name = "brocade_fibrechannel_trunk"
                list_name = "trunk_area"
                get_list = True
            elif area == "brocade_fabric_fabric_switch":
                module_name = "brocade_fabric"
                list_name = "fabric_switch"
                get_list = True
            elif area == "brocade_security_password_cfg":
                module_name = "brocade_security"
                obj_name = "password_cfg"
                get_singleton = True
            elif area == "brocade_chassis_chassis":
                module_name = "brocade_chassis"
                obj_name = "chassis"
                get_singleton = True
            elif area == "brocade_fibrechannel_configuration_fabric":
                module_name = "brocade_fibrechannel_configuration"
                obj_name = "fabric"
                get_singleton = True
            elif area == "brocade_fibrechannel_configuration_port_configuration":
                module_name = "brocade_fibrechannel_configuration"
                obj_name = "port_configuration"
                get_singleton = True
            elif area == "brocade_time_clock_server":
                module_name = "brocade_time"
                obj_name = "clock_server"
                get_singleton = True
            elif area == "brocade_time_time_zone":
                module_name = "brocade_time"
                obj_name = "time_zone"
                get_singleton = True
            elif area == "brocade_logging_audit":
                module_name = "brocade_logging"
                obj_name = "audit"
                get_singleton = True
            elif area == "brocade_snmp_system":
                module_name = "brocade_snmp"
                obj_name = "system"
                get_singleton = True

            if get_singleton:
                ret_code, response = singleton_get(fos_user_name, fos_password,
                                                   fos_ip_addr, module_name,
                                                   obj_name, fos_version,
                                                   https, auth, vfid, result,
                                                   ssh_hostkeymust, timeout)
                if ret_code != 0:
                    result[module_name + "_" + obj_name + "_get"] = ret_code
                    exit_after_login(fos_ip_addr, https, auth, result, module,
                                     timeout)

                obj = response["Response"][str_to_yang(obj_name)]

                to_human_singleton(module_name, obj_name, obj)

                facts[area] = obj
            elif get_list:
                ret_code, response = list_get(fos_user_name, fos_password,
                                              fos_ip_addr, module_name,
                                              list_name, fos_version, https,
                                              auth, vfid, result,
                                              ssh_hostkeymust, timeout)
                if ret_code != 0:
                    result[module_name + "_" + list_name + "_get"] = ret_code
                    exit_after_login(fos_ip_addr, https, auth, result, module,
                                     timeout)

                obj_list = response["Response"][str_to_yang(list_name)]
                if not isinstance(obj_list, list):
                    if obj_list is None:
                        obj_list = []
                    else:
                        obj_list = [obj_list]

                to_human_list(module_name, list_name, obj_list, result)
                facts[area] = obj_list
            elif area == "brocade_zoning":
                ret_code, response = defined_get(fos_ip_addr, https, auth,
                                                 vfid, result, timeout)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module,
                                     timeout)

                zoning = {}
                zoning["defined-configuration"] = (
                    response["Response"]["defined-configuration"])

                ret_code, response = effective_get(fos_ip_addr, https, auth,
                                                   vfid, result, timeout)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module,
                                     timeout)

                zoning["effective-configuration"] = (
                    response["Response"]["effective-configuration"])

                to_human_zoning(zoning["effective-configuration"])

                facts[area] = zoning

    result["ansible_facts"] = facts

    logout(fos_ip_addr, https, auth, result, timeout)
    module.exit_json(**result)
Example #6
0
def list_helper(module, fos_ip_addr, fos_user_name, fos_password, https,
                ssh_hostkeymust, throttle, vfid, module_name, list_name,
                entries, all_entries, result, timeout):

    if not is_full_human(entries, result):
        module.exit_json(**result)

    if all_entries == None:
        result["all_entries_default"] = all_entries
        all_entries = True

    if vfid is None:
        vfid = 128

    if entries == None:
        entries = []

    ret_code, auth, fos_version = login(fos_ip_addr, fos_user_name,
                                        fos_password, https, throttle, result,
                                        timeout)
    if ret_code != 0:
        module.exit_json(**result)

    ret_code, response = list_get(fos_user_name, fos_password, fos_ip_addr,
                                  module_name, list_name, fos_version, https,
                                  auth, vfid, result, ssh_hostkeymust, timeout)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    current_entries = response["Response"][str_to_yang(list_name)]
    if not isinstance(current_entries, list):
        if current_entries is None:
            current_entries = []
        else:
            current_entries = [current_entries]

    to_human_list(module_name, list_name, current_entries, result)

    # for switch list object only, we only support one for now
    # and allow users to not specifcy the WWN of the switch
    # thus missing key of the entry. We'll get it from the switch
    if module_name == "brocade_fibrechannel_switch" and list_name == "fibrechannel_switch":
        if len(entries) != 1:
            result["failed"] = True
            result["msg"] = "Only one entry in an array is supported"
            exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

        entries[0]["name"] = current_entries[0]["name"]

    if module_name == "brocade_access_gateway" and list_name == "port_group":
        for port_group in current_entries:
            if "port_group_n_ports" in port_group and port_group[
                    "port_group_n_ports"] == None:
                port_group["port_group_n_ports"] = {"n_port": None}
            if "port_group_f_ports" in port_group and port_group[
                    "port_group_f_ports"] == None:
                port_group["port_group_f_ports"] = {"f_port": None}

    diff_entries = []
    for entry in entries:
        for current_entry in current_entries:
            if list_entry_keys_matched(entry, current_entry, module_name,
                                       list_name):
                diff_attributes = generate_diff(result, current_entry, entry)
                if len(diff_attributes) > 0:
                    for key in list_entry_keys(module_name, list_name):
                        diff_attributes[key] = entry[key]
                    diff_entries.append(diff_attributes)

    if module_name == "brocade_security" and list_name == "user_config":
        new_diff_entries = []
        for diff_entry in diff_entries:
            # password canot change using patch update
            # any entries with password are popp'ed off.
            if not "password" in diff_entry:
                new_diff_entries.append(diff_entry)
        diff_entries = new_diff_entries

    ret_code = to_fos_list(module_name, list_name, diff_entries, result)
    result["diff_retcode"] = ret_code
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    add_entries = []
    for entry in entries:

        # check to see if the new entry matches any of the old ones
        found = False
        for current_entry in current_entries:
            if list_entry_keys_matched(entry, current_entry, module_name,
                                       list_name):
                found = True
                break

        if not found:
            new_entry = {}
            for k, v in entry.items():
                new_entry[k] = v
            add_entries.append(new_entry)

    if module_name == "brocade_logging" and list_name == "syslog_server":
        new_add_entries = []
        for add_entry in add_entries:
            secured = ("secured_mode" in add_entry
                       and add_entry["secured_mode"] == True)
            if not secured:
                new_add_entry = {}
                new_add_entry["server"] = add_entry["server"]
                new_add_entries.append(new_add_entry)
            else:
                new_add_entries.append(add_entry)
        add_entries = new_add_entries

    ret_code = to_fos_list(module_name, list_name, add_entries, result)
    result["add_retcode"] = ret_code
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    delete_entries = []
    for current_entry in current_entries:
        found = False
        for entry in entries:
            if list_entry_keys_matched(entry, current_entry, module_name,
                                       list_name):
                found = True
                break

        if not found:
            delete_entry = {}
            for key in list_entry_keys(module_name, list_name):
                delete_entry[key] = current_entry[key]

            delete_entries.append(delete_entry)

    ret_code = to_fos_list(module_name, list_name, delete_entries, result)
    result["delete_retcode"] = ret_code
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    result["response"] = response
    result["current_entries"] = current_entries
    result["diff_entries"] = diff_entries
    result["add_entries"] = add_entries
    result["delete_entries"] = delete_entries

    if len(diff_entries) > 0:
        if not module.check_mode:
            ret_code = 0
            ret_code = list_patch(fos_user_name, fos_password, fos_ip_addr,
                                  module_name, list_name, fos_version, https,
                                  auth, vfid, result, diff_entries,
                                  ssh_hostkeymust, timeout)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module,
                                 timeout)

        result["changed"] = True

    if len(add_entries) > 0:
        if not module.check_mode:
            ret_code = list_post(fos_user_name, fos_password, fos_ip_addr,
                                 module_name, list_name, fos_version, https,
                                 auth, vfid, result, add_entries,
                                 ssh_hostkeymust, timeout)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module,
                                 timeout)

        result["changed"] = True

    if len(delete_entries) > 0 and all_entries:
        if not module.check_mode:
            ret_code = list_delete(fos_user_name, fos_password, fos_ip_addr,
                                   module_name, list_name, fos_version, https,
                                   auth, vfid, result, delete_entries,
                                   ssh_hostkeymust, timeout)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module,
                                 timeout)

        result["changed"] = True

    logout(fos_ip_addr, https, auth, result, timeout)
    module.exit_json(**result)
Example #7
0
def singleton_helper(module, fos_ip_addr, fos_user_name, fos_password, https,
                     ssh_hostkeymust, throttle, vfid, module_name, obj_name,
                     attributes, result, timeout):

    if not is_full_human(attributes, result):
        module.exit_json(**result)

    if vfid is None:
        vfid = 128

    ret_code, auth, fos_version = login(fos_ip_addr, fos_user_name,
                                        fos_password, https, throttle, result,
                                        timeout)
    if ret_code != 0:
        module.exit_json(**result)

    result['ssh_hostkeymust'] = ssh_hostkeymust

    ret_code, response = singleton_get(fos_user_name, fos_password,
                                       fos_ip_addr, module_name, obj_name,
                                       fos_version, https, auth, vfid, result,
                                       ssh_hostkeymust, timeout)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    resp_attributes = response["Response"][str_to_yang(obj_name)]

    to_human_singleton(module_name, obj_name, resp_attributes)

    diff_attributes = generate_diff(result, resp_attributes, attributes)

    # any object specific special processing
    if module_name == "brocade_maps" and obj_name == "maps_config":
        # relay_ip_address and domain_name needs to be specifid
        # at the same time based on FOS REST requirements
        if "relay_ip_address" in diff_attributes and "domain_name" not in diff_attributes:
            diff_attributes["domain_name"] = resp_attributes["domain_name"]
            result["kept the same"] = "domain_name"
        elif "relay_ip_address" not in diff_attributes and "domain_name" in diff_attributes:
            diff_attributes["relay_ip_address"] = resp_attributes[
                "relay_ip_address"]
            result["kept the same"] = "relay_ip_address"

        if "relay_ip_address" in diff_attributes and diff_attributes[
                "relay_ip_address"] == None:
            result["failed"] = True
            result['msg'] = "must specify relay_ip_address if configured empty"
            exit_after_login(fos_ip_addr, https, auth, result, module, timeout)
        elif "domain_name" in diff_attributes and diff_attributes[
                "domain_name"] == None:
            result["failed"] = True
            result['msg'] = "must specify domain_name if configured empty"
            exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    result["diff_attributes"] = diff_attributes
    result["current_attributes"] = resp_attributes
    result["new_attributes"] = attributes

    if len(diff_attributes) > 0:
        ret_code = to_fos_singleton(module_name, obj_name, diff_attributes,
                                    result)
        if ret_code != 0:
            exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

        if not module.check_mode:
            ret_code = 0
            ret_code = singleton_patch(fos_user_name, fos_password,
                                       fos_ip_addr, module_name, obj_name,
                                       fos_version, https, auth, vfid, result,
                                       diff_attributes, ssh_hostkeymust,
                                       timeout)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module,
                                 timeout)

        result["changed"] = True
    else:
        logout(fos_ip_addr, https, auth, result, timeout)
        module.exit_json(**result)

    logout(fos_ip_addr, https, auth, result, timeout)
    module.exit_json(**result)
Example #8
0
def list_delete_helper(module, fos_ip_addr, fos_user_name, fos_password, https,
                       ssh_hostkeymust, throttle, vfid, module_name, list_name,
                       entries, all_entries, result, timeout):

    if not is_full_human(entries, result):
        module.exit_json(**result)

    if all_entries == None:
        result["all_entries_default"] = all_entries
        all_entries = True

    if vfid is None:
        vfid = 128

    ret_code, auth, fos_version = login(fos_ip_addr, fos_user_name,
                                        fos_password, https, throttle, result,
                                        timeout)
    if ret_code != 0:
        module.exit_json(**result)

    ret_code, response = list_get(fos_user_name, fos_password, fos_ip_addr,
                                  module_name, list_name, fos_version, https,
                                  auth, vfid, result, ssh_hostkeymust, timeout)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    current_entries = response["Response"][str_to_yang(list_name)]
    if not isinstance(current_entries, list):
        if current_entries is None:
            current_entries = []
        else:
            current_entries = [current_entries]

    to_human_list(module_name, list_name, current_entries, result)

    delete_entries = []
    for entry in entries:

        # check to see if the new entry matches any of the old ones
        found = False
        for current_entry in current_entries:
            if list_entry_keys_matched(entry, current_entry, module_name,
                                       list_name):
                found = True
                break

        if found:
            new_entry = {}
            for k, v in entry.items():
                new_entry[k] = v
            delete_entries.append(new_entry)

    ret_code = to_fos_list(module_name, list_name, delete_entries, result)
    result["add_retcode"] = ret_code
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    result["response"] = response
    result["current_entries"] = current_entries
    result["delete_entries"] = delete_entries

    if len(delete_entries) > 0:
        if not module.check_mode:
            ret_code = list_delete(fos_user_name, fos_password, fos_ip_addr,
                                   module_name, list_name, fos_version, https,
                                   auth, vfid, result, delete_entries,
                                   ssh_hostkeymust, timeout)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module,
                                 timeout)

        result["changed"] = True

    logout(fos_ip_addr, https, auth, result, timeout)
    module.exit_json(**result)
def main():
    """
    Main function
    """

    argument_spec = dict(credential=dict(required=True,
                                         type='dict',
                                         no_log=True),
                         vfid=dict(required=False, type='int'),
                         throttle=dict(required=False, type='float'),
                         timeout=dict(required=False, type='float'),
                         pid=dict(required=True, type='str'))

    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=False)

    input_params = module.params

    # Set up state variables
    fos_ip_addr = input_params['credential']['fos_ip_addr']
    fos_user_name = input_params['credential']['fos_user_name']
    fos_password = input_params['credential']['fos_password']
    https = input_params['credential']['https']
    ssh_hostkeymust = True
    if 'ssh_hostkeymust' in input_params['credential']:
        ssh_hostkeymust = input_params['credential']['ssh_hostkeymust']
    throttle = input_params['throttle']
    timeout = input_params['timeout']
    vfid = input_params['vfid']
    pid = input_params['pid']
    result = {"changed": False}

    if vfid is None:
        vfid = 128

    ret_code, auth, fos_version = login(fos_ip_addr, fos_user_name,
                                        fos_password, https, throttle, result,
                                        timeout)
    if ret_code != 0:
        module.exit_json(**result)

    facts = {}

    facts['ssh_hostkeymust'] = ssh_hostkeymust

    module_name = "brocade_fabric"
    list_name = "fabric_switch"

    ret_code, response = list_get(fos_user_name, fos_password, fos_ip_addr,
                                  module_name, list_name, fos_version, https,
                                  auth, vfid, result, ssh_hostkeymust, timeout)
    if ret_code != 0:
        result["list_get"] = ret_code
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    obj_list = response["Response"][str_to_yang(list_name)]
    if not isinstance(obj_list, list):
        if obj_list is None:
            obj_list = []
        else:
            obj_list = [obj_list]

    to_human_list(module_name, list_name, obj_list, result)

    result["obj_list"] = obj_list

    did = int(pid, 16) >> 16

    result["did"] = did

    ret_dict = {}

    for obj in obj_list:
        if obj["domain_id"] == str(did):
            ret_dict[list_name] = obj

    result["ansible_facts"] = ret_dict

    logout(fos_ip_addr, https, auth, result, timeout)
    module.exit_json(**result)
Example #10
0
def main():
    """
    Main function
    """

    argument_spec = dict(
        credential=dict(required=True, type='dict', no_log=True),
        vfid=dict(required=False, type='int'),
        throttle=dict(required=False, type='float'),
        timeout=dict(required=False, type='float'),
        module_name=dict(required=True, type='str'),
        list_name=dict(required=True, type='str'),
        attributes=dict(required=False, type='dict'))

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=False
    )

    input_params = module.params

    # Set up state variables
    fos_ip_addr = input_params['credential']['fos_ip_addr']
    fos_user_name = input_params['credential']['fos_user_name']
    fos_password = input_params['credential']['fos_password']
    https = input_params['credential']['https']
    ssh_hostkeymust = True
    if 'ssh_hostkeymust' in input_params['credential']:
        ssh_hostkeymust = input_params['credential']['ssh_hostkeymust']
    throttle = input_params['throttle']
    timeout = input_params['timeout']
    vfid = input_params['vfid']
    module_name = str_to_human(input_params['module_name'])
    list_name = str_to_human(input_params['list_name'])
    attributes = input_params['attributes']
    result = {"changed": False}

    if vfid is None:
        vfid = 128

    ret_code, auth, fos_version = login(fos_ip_addr,
                           fos_user_name, fos_password,
                           https, throttle, result, timeout)
    if ret_code != 0:
        module.exit_json(**result)

    facts = {}

    facts['ssh_hostkeymust'] = ssh_hostkeymust

    ret_code, response = list_get(fos_user_name, fos_password, fos_ip_addr,
                                  module_name, list_name, fos_version,
                                  https, auth, vfid, result,
                                  ssh_hostkeymust, timeout)
    if ret_code != 0:
        result["list_get"] = ret_code
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    obj_list = response["Response"][str_to_yang(list_name)]
    if not isinstance(obj_list, list):
        if obj_list is None:
            obj_list = []
        else:
            obj_list = [obj_list]

    to_human_list(module_name, list_name, obj_list, result)

    result["obj_list"] = obj_list

    ret_dict = {}
    ret_list = []
    for obj in obj_list:
        if attributes == None:
            ret_list.append(obj)
        else:
            matched_all = 0
            for k, v in attributes.items():
                if k in obj and obj[k] == v:
                    matched_all = matched_all + 1

            if matched_all == len(attributes.items()):
                ret_list.append(obj)

    if attributes == None:
        result["attributes_len"] = 0
    else:
        result["attributes_len"] = len(attributes.items())
    result["ret_list"] = ret_list

    ret_dict[list_name] = ret_list

    result["ansible_facts"] = ret_dict

    logout(fos_ip_addr, https, auth, result, timeout)
    module.exit_json(**result)
Example #11
0
def main():
    """
    Main function
    """

    argument_spec = dict(
        credential=dict(required=True, type='dict', no_log=True),
        vfid=dict(required=False, type='int'),
        throttle=dict(required=False, type='float'),
        timeout=dict(required=False, type='float'),
        module_name=dict(required=True, type='str'),
        obj_name=dict(required=True, type='str'))

    module = AnsibleModule(
        argument_spec=argument_spec,
        supports_check_mode=False
    )

    input_params = module.params

    # Set up state variables
    fos_ip_addr = input_params['credential']['fos_ip_addr']
    fos_user_name = input_params['credential']['fos_user_name']
    fos_password = input_params['credential']['fos_password']
    https = input_params['credential']['https']
    ssh_hostkeymust = True
    if 'ssh_hostkeymust' in input_params['credential']:
        ssh_hostkeymust = input_params['credential']['ssh_hostkeymust']
    throttle = input_params['throttle']
    timeout = input_params['timeout']
    vfid = input_params['vfid']
    module_name = str_to_human(input_params['module_name'])
    obj_name = str_to_human(input_params['obj_name'])
    result = {"changed": False}

    if vfid is None:
        vfid = 128

    ret_code, auth, fos_version = login(fos_ip_addr,
                           fos_user_name, fos_password,
                           https, throttle, result, timeout)
    if ret_code != 0:
        module.exit_json(**result)

    facts = {}

    facts['ssh_hostkeymust'] = ssh_hostkeymust

    ret_code, response = singleton_get(fos_user_name, fos_password, fos_ip_addr,
                                  module_name, obj_name, fos_version,
                                  https, auth, vfid, result,
                                  ssh_hostkeymust, timeout)
    if ret_code != 0:
        result["singleton_get"] = ret_code
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    obj = response["Response"][str_to_yang(obj_name)]

    to_human_singleton(module_name, obj_name, obj)

    result["obj"] = obj

    ret_dict = {}
    ret_dict[obj_name] = obj

    result["ansible_facts"] = ret_dict

    logout(fos_ip_addr, https, auth, result, timeout)
    module.exit_json(**result)