Ejemplo n.º 1
0
def operation_helper(module, fos_ip_addr, fos_user_name, fos_password, https,
                     ssh_hostkeymust, throttle, vfid, op_name, in_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["input"] = attributes

    ret_code = to_fos_operation(op_name, in_name, 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, resp = operation_post(fos_user_name, fos_password,
                                        fos_ip_addr, op_name, in_name,
                                        fos_version, https, auth, vfid, result,
                                        attributes, ssh_hostkeymust, timeout)
        if ret_code != 0:
            exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    result["changed"] = True

    to_human_operation(op_name, in_name, resp["Response"])

    result["operation_resp"] = resp["Response"]

    logout(fos_ip_addr, https, auth, result, timeout)

    module.exit_json(**result)
Ejemplo n.º 2
0
def zoning_find_pair_common(module, fos_ip_addr, https, auth, vfid, type_str,
                            obj_name, new_name, result, timeout):
    type_get = None
    name = None
    if type_str == "alias":
        type_get = alias_get
        name = "alias-name"
    elif type_str == "zone":
        type_get = zone_get
        name = "zone-name"
    elif type_str == "cfg":
        type_get = cfg_get
        name = "cfg-name"
    else:
        result["failed"] = True
        result['msg'] = "invalid type string" + type_str
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    ret_code, get_resp = type_get(fos_ip_addr, https, auth, vfid, result,
                                  timeout)
    if ret_code != 0:
        result["failed"] = True
        result['msg'] = "failed to read from database"
        exit_after_login(fos_ip_addr, https, auth, result, module, timeout)

    r_list = resp_to_list(get_resp, type_str)

    obj_name_dict = {}
    new_name_dict = {}
    for obj in r_list:
        if obj[name] == obj_name:
            obj_name_dict = obj_to_yml(obj)
        if obj[name] == new_name:
            new_name_dict = obj_to_yml(obj)

    return obj_name_dict, new_name_dict
Ejemplo n.º 3
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'),
                         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']
    vfid = input_params['vfid']
    module_name = input_params['module_name']
    obj_name = 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)
    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)
    if ret_code != 0:
        result["singleton_get"] = ret_code
        exit_after_login(fos_ip_addr, https, auth, result, module)

    obj = response["Response"][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)
    module.exit_json(**result)
Ejemplo n.º 4
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)
Ejemplo n.º 5
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)
def main():
    """
    Main function
    """

    argument_spec = dict(credential=dict(required=True, type='dict'),
                         vfid=dict(required=False, type='int'),
                         throttle=dict(required=False, type='float'),
                         user_configs=dict(required=False, type='list'),
                         delete_user_configs=dict(required=False, type='list'))

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

    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']
    vfid = input_params['vfid']
    user_configs = input_params['user_configs']
    delete_user_configs = input_params['delete_user_configs']
    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)
    if ret_code != 0:
        module.exit_json(**result)

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

    resp_uc = response["Response"]["user-config"]

    if isinstance(resp_uc, list):
        c_user_configs = resp_uc
    else:
        c_user_configs = [resp_uc]

    for c_user_config in c_user_configs:
        if "virtual-fabric-role-id-list" in c_user_config and not isinstance(
                c_user_config["virtual-fabric-role-id-list"], list):
            c_user_config["virtual-fabric-role-id-list"] = [
                c_user_config["virtual-fabric-role-id-list"]
            ]

    # convert REST to human readable format first
    for c_user_config in c_user_configs:
        to_human_user_config(c_user_config)

    # if delete user config is not None, then we make sure
    # the user config is not present.
    # user config creation or update does not happen at the same
    # time
    if delete_user_configs != None:
        to_delete = []
        for delete_user_config in delete_user_configs:
            found = False
            for c_user_config in c_user_configs:
                if c_user_config["name"] == delete_user_config["name"]:
                    found = True
                    break
            if found:
                to_delete.append(delete_user_config)

        if len(to_delete) > 0:
            if not module.check_mode:
                ret_code = user_config_delete(fos_ip_addr, https, auth, vfid,
                                              result, to_delete)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

            result["changed"] = True

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

    diff_user_configs = []
    for new_uc in user_configs:
        for c_user_config in c_user_configs:
            if new_uc["name"] == c_user_config["name"]:
                diff_attributes = generate_diff(result, c_user_config, new_uc)
                # cannot change password using patch
                # so skip for diff identification
                if "password" in diff_attributes:
                    diff_attributes.pop("password")

                if len(diff_attributes) > 0:
                    result["c_user_config"] = c_user_config
                    diff_attributes["name"] = new_uc["name"]
                    ret_code = to_fos_user_config(diff_attributes, result)
                    if ret_code != 0:
                        exit_after_login(fos_ip_addr, https, auth, result,
                                         module)

                    diff_user_configs.append(diff_attributes)

    add_user_configs = []
    for new_uc in user_configs:
        found = False
        for c_user_config in c_user_configs:
            if new_uc["name"] == c_user_config["name"]:
                found = True
        if not found:
            new_user_config = {}
            for k, v in new_uc.items():
                new_user_config[k] = v
            ret_code = to_fos_user_config(new_user_config, result)
            result["retcode"] = ret_code
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

            add_user_configs.append(new_user_config)

    result["resp_uc"] = resp_uc
    result["user_configs"] = user_configs
    result["diff_user_configs"] = diff_user_configs
    result["add_user_configs"] = add_user_configs

    if len(diff_user_configs) > 0:
        if not module.check_mode:
            ret_code = user_config_patch(fos_user_name, fos_password,
                                         fos_ip_addr, fos_version, https, auth,
                                         vfid, result, diff_user_configs,
                                         ssh_hostkeymust)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    if len(add_user_configs) > 0:
        if not module.check_mode:
            ret_code = user_config_post(fos_ip_addr, https, auth, vfid, result,
                                        add_user_configs)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)
Ejemplo n.º 7
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'),
                         wwn=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']
    vfid = input_params['vfid']
    wwn = input_params['wwn']
    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)
    if ret_code != 0:
        module.exit_json(**result)

    facts = {}

    facts['ssh_hostkeymust'] = ssh_hostkeymust

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

    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module)

    alias_list = []
    if "alias" in response["Response"]["defined-configuration"]:
        if isinstance(response["Response"]["defined-configuration"]["alias"],
                      list):
            alias_list = response["Response"]["defined-configuration"]["alias"]
        else:
            alias_list = [
                response["Response"]["defined-configuration"]["alias"]
            ]

    result["alias_list"] = alias_list

    ret_list = []
    for alias in alias_list:
        if "member-entry" in alias and "alias-entry-name" in alias[
                "member-entry"]:
            if isinstance(alias["member-entry"]["alias-entry-name"], list):
                for entry in alias["member-entry"]["alias-entry-name"]:
                    if entry == wwn.lower():
                        ret_list.append(alias)
                        break
            else:
                if alias["member-entry"]["alias-entry-name"] == wwn.lower():
                    ret_list.append(alias)

    ret_dict = {}
    if len(ret_list) > 0:
        ret_dict["alias"] = ret_list

    result["ansible_facts"] = ret_dict

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)
Ejemplo n.º 8
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)
Ejemplo n.º 9
0
def zoning_common(fos_ip_addr, https, auth, vfid, result, module, input_list,
                  members_add_only, members_remove_only,
                  to_delete_list, type_str, type_diff_processing,
                  type_diff_processing_to_delete, type_get,
                  type_post, type_delete, active_cfg):
    """
        common flow of zone database updates.

        :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 auth: dict
        :param result: dict to keep track of execution msgs
        :type result: dict
        :param module: AnsibleModule
        :type module: AnsibleModule
        :param input_list: list of zones, aliases or cfgs
        :type input_list: list
        :param to_delete_list: list of zones, aliases or cfgs to delete
        :type to_delete_list: list
        :param type_str: "zone", "alias", or "cfg"
        :type type_str: str
        :param type_diff_processing: function to compare expected & current
        :type type_diff_processing: func
        :param type_diff_processing_to_delete: function to compare to delete & current
        :type type_diff_processing_to_delete: func
        :param type_get: function to get the current db
        :type type_get: func
        :param type_post: function to post to FOS
        :type type_post: func
        :param type_delete: function to delete from FOS
        :type type_delete: func
        :param active_cfg: cfg to be enabled at the end. if None, only saved.
        :type active_cfg: str
        :return: code to indicate failure or success
        :rtype: int
    """

    ret_code, cfgname, checksum = cfgname_checksum_get(fos_ip_addr,
                                                       https, auth,
                                                       vfid, result)
    if ret_code != 0:
        result["failed"] = True
        result['msg'] = "failed to checksum"
        exit_after_login(fos_ip_addr, https, auth, result, module)

    ret_code, get_resp = type_get(fos_ip_addr,
                                  https, auth, vfid, result)
    if ret_code != 0:
        result["failed"] = True
        result['msg'] = "failed to read from database"
        exit_after_login(fos_ip_addr, https, auth, result, module)

    if get_resp is None:
        c_list = []
    else:
        if isinstance(get_resp["Response"][type_str], list):
            c_list = get_resp["Response"][type_str]
        else:
            if get_resp["Response"][type_str] is None:
                c_list = []
            else:
                c_list = [get_resp["Response"][type_str]]

#    result["input_list"] = input_list
#    result["c_list"] = c_list

    if input_list:
        ret_code, post_list, remove_list, common_list = type_diff_processing(result,
                                                                input_list,
                                                                c_list)

        result["post_list"] = post_list
        result["remove_list"] = remove_list
        result["common_list"] = common_list

        # scenarios to return no changes
        # to add list has nothing or
        # add list has something but members_remove_only is True
        # and
        # to remove list has nothing or
        # to remove list has something but members_add_only is True or
        # to remove list has something but member_remove_only is True
        # and
        # common_list has nothing and member_remove_only is True
        # and cfg is not enabled
        if (len(post_list) == 0 or (len(post_list) > 0 and members_remove_only == True)) and (len(remove_list) == 0 or (len(remove_list) > 0 and members_add_only == True) or (len(remove_list) > 0 and members_remove_only == True)) and (members_remove_only == None or (len(common_list) == 0 and members_remove_only == True)) and active_cfg is None:
            exit_after_login(fos_ip_addr, https, auth, result, module)

        need_to_save = False
        if len(post_list) > 0 and (members_remove_only == None or members_remove_only == False):
            if not module.check_mode:
                ret_code = type_post(fos_ip_addr, https, auth, vfid,
                                     result, post_list)
                if ret_code != 0:
                    ret_code = cfg_abort(fos_ip_addr, https,
                                         auth, vfid, result)
                    result["failed"] = True
                    result['msg'] = "HTTP POST failed"
                    exit_after_login(fos_ip_addr, https, auth, result, module)

            need_to_save = True

        if len(remove_list) > 0 and (members_add_only == False or members_add_only == None) and (members_remove_only == None or members_remove_only == False):
            if not module.check_mode:
                ret_code = type_delete(fos_ip_addr, https, auth, vfid,
                                       result, remove_list)
                if ret_code != 0:
                    ret_code = cfg_abort(fos_ip_addr, https,
                                         auth, vfid, result)
                    result["failed"] = True
                    result['msg'] = "HTTP DELETE failed"
                    exit_after_login(fos_ip_addr, https, auth, result, module)

            need_to_save = True

        if len(common_list) > 0 and (members_remove_only == True):
            if not module.check_mode:
                ret_code = type_delete(fos_ip_addr, https, auth, vfid,
                                       result, common_list)
                if ret_code != 0:
                    ret_code = cfg_abort(fos_ip_addr, https,
                                         auth, vfid, result)
                    result["failed"] = True
                    result['msg'] = "HTTP DELETE common failed"
                    exit_after_login(fos_ip_addr, https, auth, result, module)

            need_to_save = True

        if active_cfg is None:
            if need_to_save:
                if not module.check_mode:
                    # if something changed and there is already an active cfg
                    # reenable that cfg
                    ret_code = 0
                    failed_msg = ""
                    if cfgname is not None:
                        failed_msg = "CFG ENABLE failed"
                        ret_code = cfg_enable(fos_ip_addr, https, auth, vfid,
                                            result, checksum, cfgname)
                    else:
                        failed_msg = "CFG SAVE failed"
                        ret_code = cfg_save(fos_ip_addr, https, auth, vfid,
                                        result, checksum)
                    if ret_code != 0:
                        ret_code = cfg_abort(fos_ip_addr, https,
                                             auth, vfid, result)
                        result['msg'] = failed_msg
                        result["failed"] = True
                        exit_after_login(fos_ip_addr, https, auth,
                                         result, module)

                result["changed"] = True
        else:
            if need_to_save or cfgname != active_cfg:
                if not module.check_mode:
                    ret_code = cfg_enable(fos_ip_addr, https, auth, vfid,
                                        result, checksum, active_cfg)
                    if ret_code != 0:
                        ret_code = cfg_abort(fos_ip_addr, https,
                                            auth, vfid, result)
                        result['msg'] = "CFG ENABLE failed"
                        result["failed"] = True
                        exit_after_login(fos_ip_addr, https, auth, result, module)

                result["changed"] = True

    if to_delete_list:
        need_to_save = False

        ret_code, delete_list = type_diff_processing_to_delete(result,
                                                               to_delete_list,
                                                               c_list)

        result["delete_list"] = delete_list

        if len(delete_list) == 0:
            return 0

        if not module.check_mode:
            ret_code = type_delete(fos_ip_addr, https, auth, vfid,
                                   result, delete_list)
            if ret_code != 0:
                ret_code = cfg_abort(fos_ip_addr, https, auth, vfid, result)
                result["failed"] = True
                result['msg'] = "HTTP DELETE failed"
                exit_after_login(fos_ip_addr, https, auth, result, module)

            need_to_save = True

        if active_cfg is None:
            if need_to_save:
                if not module.check_mode:
                    ret_code = 0
                    failed_msg = ""
                    if cfgname is not None:
                        failed_msg = "CFG ENABLE failed"
                        ret_code = cfg_enable(fos_ip_addr, https, auth, vfid,
                                            result, checksum, cfgname)
                    else:
                        failed_msg = "CFG SAVE failed"
                        ret_code = cfg_save(fos_ip_addr, https, auth, vfid,
                                        result, checksum)
                    if ret_code != 0:
                        ret_code = cfg_abort(fos_ip_addr, https, auth,
                                             vfid, result)
                        result["failed"] = True
                        result['msg'] = failed_msg
                        exit_after_login(fos_ip_addr, https, auth,
                                         result, module)

                result["changed"] = True
        else:
            if not module.check_mode:
                ret_code = cfg_enable(fos_ip_addr, https, auth, vfid,
                                      result, checksum, active_cfg)
                if ret_code != 0:
                    ret_code = cfg_abort(fos_ip_addr, https,
                                         auth, result)
                    result["failed"] = True
                    result['msg'] = "CFG ENABLE failed"
                    exit_after_login(fos_ip_addr, https, auth, result, module)

            result["changed"] = True

    return 0
Ejemplo n.º 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'),
                         ipfilter_policies=dict(required=False, type='list'),
                         active_policy=dict(required=False, type='str'),
                         delete_policies=dict(required=False, type='list'))

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

    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']
    throttle = input_params['throttle']
    timeout = input_params['timeout']
    vfid = input_params['vfid']
    ipfilter_policies = input_params['ipfilter_policies']
    active_policy = input_params['active_policy']
    delete_policies = input_params['delete_policies']
    result = {"changed": False}

    # if delete policy is not None, then we make sure
    # the policy is not present.
    # policy creation or update does not happen at the same
    # time
    if delete_policies != None:
        return list_delete_helper(module, fos_ip_addr, fos_user_name,
                                  fos_password, https, True, throttle, vfid,
                                  "brocade_security", "ipfilter_policy",
                                  delete_policies, True, result, timeout)

    # if I am dealing with active_policy set, it must be policy list update
    if active_policy == None:
        return list_helper(module, fos_ip_addr, fos_user_name, fos_password,
                           https, True, throttle, vfid, "brocade_security",
                           "ipfilter_policy", ipfilter_policies, False, result,
                           timeout)

    if not is_full_human(ipfilter_policies, 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)

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

    resp_ir = response["Response"]["ipfilter-policy"]

    if isinstance(resp_ir, list):
        c_policies = resp_ir
    else:
        c_policies = [resp_ir]

    # convert REST to human readable format first
    for c_policy in c_policies:
        to_human_ipfilter_policy(c_policy)

    # if active policy is not None, then we make sure
    # the policy is active or activate. and return
    # policy creation or update does not happen at the same
    # time
    if active_policy != None:
        found_disabled_policy = False
        found_active_policy = False
        activate_list = []
        for c_policy in c_policies:
            if c_policy["name"] == active_policy:
                if c_policy["is_policy_active"] == False:
                    found_disabled_policy = True
                    activate_dict = {
                        "name": c_policy["name"],
                        "action": "activate"
                    }
                    activate_list.append(activate_dict)
                else:
                    found_active_policy = True
                    activate_dict = {
                        "name": c_policy["name"],
                    }
                    activate_list.append(activate_dict)

        if found_disabled_policy:
            if not module.check_mode:
                ret_code = ipfilter_policy_patch(fos_ip_addr, https, auth,
                                                 vfid, result, activate_list,
                                                 timeout)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module,
                                     timeout)

            result["changed"] = True
        elif found_active_policy:
            result["same active policy"] = activate_list
        else:
            result["failed"] = True
            result["msg"] = "could not find matching policy"

        logout(fos_ip_addr, https, auth, result, timeout)
        module.exit_json(**result)
Ejemplo n.º 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'),
        clock_server=dict(required=False, type='dict'))

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

    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']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    clock_server = input_params['clock_server']
    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)
    if ret_code != 0:
        module.exit_json(**result)

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

    resp_clock = response["Response"]["clock-server"]

    to_human_clock_server(resp_clock)

    if "ntp_server_address" in resp_clock and "server_address" in resp_clock["ntp_server_address"]:
        if not isinstance(resp_clock["ntp_server_address"]["server_address"], list):
            new_list = []
            new_list.append(resp_clock["ntp_server_address"]["server_address"])
            resp_clock["ntp_server_address"]["server_address"] = new_list

    diff_attributes = generate_diff(result, resp_clock, clock_server)

    result["diff_attributes"] = diff_attributes
    result["clock_server"] = clock_server
    result["resp_clock"] = resp_clock

    if len(diff_attributes) > 0:
        ret_code = to_fos_clock_server(diff_attributes, result)
        if ret_code != 0:
            exit_after_login(fos_ip_addr, https, auth, result, module)

        if not module.check_mode:
            ret_code = clock_server_patch(
                fos_ip_addr, https,
                auth, vfid, result, diff_attributes)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

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

    logout(fos_ip_addr, https, auth, result)
    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)
Ejemplo n.º 13
0
def main():
    """
    Main function
    """

    argument_spec = dict(credential=dict(required=True, type='dict'),
                         vfid=dict(required=False, type='int'),
                         throttle=dict(required=False, type='float'),
                         switch=dict(required=True, type='dict'))

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

    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']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    switch = input_params['switch']
    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)
    if ret_code != 0:
        module.exit_json(**result)

    ret_code, response = fc_switch_get(fos_user_name, fos_password,
                                       fos_ip_addr, fos_version, https, auth,
                                       vfid, result)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module)

    resp_switch = response["Response"]["fibrechannel-switch"]

    to_human_switch(resp_switch)

    if "dns_servers" in resp_switch:
        if resp_switch[
                "dns_servers"] is not None and "dns_server" in resp_switch[
                    "dns_servers"]:
            if not isinstance(resp_switch["dns_servers"]["dns_server"], list):
                new_list = []
                new_list.append(resp_switch["dns_servers"]["dns_server"])
                resp_switch["dns_servers"]["dns_server"] = new_list

    if "ip_address" in resp_switch:
        if resp_switch[
                "ip_address"] is not None and "ip_address" in resp_switch[
                    "ip_address"]:
            if not isinstance(resp_switch["ip_address"]["ip_address"], list):
                new_list = []
                new_list.append(resp_switch["ip_address"]["ip_address"])
                resp_switch["ip_address"]["ip_address"] = new_list

    if "ip_static_gateway_list" in resp_switch:
        if resp_switch[
                "ip_static_gateway_list"] is not None and "ip_static_gateway" in resp_switch[
                    "ip_static_gateway_list"]:
            if not isinstance(
                    resp_switch["ip_static_gateway_list"]["ip_static_gateway"],
                    list):
                new_list = []
                new_list.append(
                    resp_switch["ip_static_gateway_list"]["ip_static_gateway"])
                resp_switch["ip_static_gateway_list"][
                    "ip_static_gateway"] = new_list

    diff_attributes = generate_diff(result, resp_switch, switch)

    result["diff_attributes"] = diff_attributes
    result["resp_switch"] = resp_switch
    result["switch"] = switch

    if len(diff_attributes) > 0:
        # let's add name key to it
        diff_attributes["name"] = resp_switch["name"]
        ret_code = to_fos_switch(diff_attributes, result)
        if ret_code != 0:
            exit_after_login(fos_ip_addr, https, auth, result, module)

        if not module.check_mode:
            ret_code = fc_switch_patch(fos_user_name, fos_password,
                                       fos_ip_addr, fos_version, https, auth,
                                       vfid, result, diff_attributes)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

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

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)
Ejemplo n.º 14
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)
Ejemplo n.º 15
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'),
                         module_name=dict(required=True, type='str'),
                         list_name=dict(required=True, type='str'),
                         all_entries=dict(required=False, type='bool'),
                         longer_timeout=dict(required=False, type='int'),
                         entries=dict(required=True, type='list'))

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

    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']
    vfid = input_params['vfid']
    module_name = input_params['module_name']
    list_name = input_params['list_name']
    entries = input_params['entries']
    all_entries = input_params['all_entries']
    longer_timeout = input_params['longer_timeout']
    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)
    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)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module)

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

    to_human_list(module_name, list_name, current_entries, result)

    result["current_entries"] = current_entries

    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)

    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)

    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)

    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)

    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)

    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
            if longer_timeout != None:
                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, longer_timeout)
            else:
                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)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    if len(add_entries) > 0 and all_entries:
        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)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        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)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

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

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

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

    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']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    syslog_servers = input_params['syslog_servers']
    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)
    if ret_code != 0:
        module.exit_json(**result)

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

    resp_ss = response["Response"]["syslog-server"]

    if isinstance(resp_ss, list):
        current_servers = resp_ss
    else:
        current_servers = [resp_ss]

    diff_servers = []
    for new_server in syslog_servers:
        for current_server in current_servers:
            if new_server["server"] == current_server["server"]:
                to_human_syslog_server(current_server)
                diff_attributes = generate_diff(result, current_server,
                                                new_server)
                if len(diff_attributes) > 0:
                    result["current_server"] = current_server
                    diff_attributes["server"] = new_server["server"]
                    ret_code = to_fos_syslog_server(diff_attributes, result)
                    result["retcode"] = ret_code
                    if ret_code != 0:
                        exit_after_login(fos_ip_addr, https, auth, result,
                                         module)

                    diff_servers.append(diff_attributes)

    add_servers = []
    for new_server in syslog_servers:
        found = False
        for current_server in current_servers:
            if new_server["server"] == current_server["server"]:
                found = True
        if not found:
            new_yang_server = {}
            secured = ("secured_mode" in new_server
                       and new_serer["secured_mode"] == True)
            if secured:
                for k, v in new_server.items():
                    new_yang_server[k] = v
            else:
                for k, v in new_server.items():
                    if k == "server":
                        new_yang_server[k] = v
            ret_code = to_fos_syslog_server(new_yang_server, result)
            result["retcode"] = ret_code
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

            add_servers.append(new_yang_server)

    delete_servers = []
    for current_server in current_servers:
        found = False
        for new_server in syslog_servers:
            if new_server["server"] == current_server["server"]:
                found = True
        if not found:
            delete_server = {}
            delete_server["server"] = current_server["server"]
            delete_servers.append(delete_server)

    result["resp_ss"] = resp_ss
    result["syslog_servers"] = syslog_servers
    result["diff_servers"] = diff_servers
    result["add_servers"] = add_servers
    result["delete_servers"] = delete_servers

    if len(diff_servers) > 0:
        if not module.check_mode:
            ret_code = syslog_server_patch(fos_ip_addr, https, auth, vfid,
                                           result, diff_servers)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    if len(add_servers) > 0:
        if not module.check_mode:
            ret_code = syslog_server_post(fos_ip_addr, https, auth, vfid,
                                          result, add_servers)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    if len(delete_servers) > 0:
        if not module.check_mode:
            ret_code = syslog_server_delete(fos_ip_addr, https, auth, vfid,
                                            result, delete_servers)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)
Ejemplo n.º 17
0
def main():
    """
    Main function
    """

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

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

    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']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    default_zone_access = input_params['default_zone_access']
    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)
    if ret_code != 0:
        module.exit_json(**result)

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

    resp_effective = response["Response"]["effective-configuration"]

    to_human_zoning(resp_effective)

    diff_attributes = {}
    if (default_zone_access is not None and
        default_zone_access != resp_effective["default_zone_access"]):
        diff_attributes["default_zone_access"] = default_zone_access

    if len(diff_attributes) > 0:
        ret_code = to_fos_zoning(diff_attributes, result)
        if ret_code != 0:
            exit_after_login(fos_ip_addr, https, auth, result, module)

        if not module.check_mode:
            ret_code = effective_patch(fos_ip_addr, https,
                                       auth, vfid, result, diff_attributes)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

            checksum = resp_effective["checksum"]
            ret_code = cfg_save(fos_ip_addr, https, auth, vfid,
                                result, checksum)
            if ret_code != 0:
                ret_code = cfg_abort(fos_ip_addr, https, auth, vfid, result)
                exit_after_login(fos_ip_addr, https, auth, result, module)

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

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

    argument_spec = dict(credential=dict(required=True, type='dict'),
                         vfid=dict(required=False, type='int'),
                         throttle=dict(required=False, type='float'),
                         ipfilter_policies=dict(required=False, type='list'),
                         active_policy=dict(required=False, type='str'),
                         delete_policies=dict(required=False, type='list'))

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

    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']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    ipfilter_policies = input_params['ipfilter_policies']
    active_policy = input_params['active_policy']
    delete_policies = input_params['delete_policies']
    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)
    if ret_code != 0:
        module.exit_json(**result)

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

    resp_ir = response["Response"]["ipfilter-policy"]

    if isinstance(resp_ir, list):
        c_policies = resp_ir
    else:
        c_policies = [resp_ir]

    # convert REST to human readable format first
    for c_policy in c_policies:
        to_human_ipfilter_policy(c_policy)

    # if active policy is not None, then we make sure
    # the policy is active or activate. and return
    # policy creation or update does not happen at the same
    # time
    if active_policy != None:
        found_disabled_policy = False
        found_active_policy = False
        activate_list = []
        for c_policy in c_policies:
            if c_policy["name"] == active_policy:
                if c_policy["is_policy_active"] == False:
                    found_disabled_policy = True
                    activate_dict = {
                        "name": c_policy["name"],
                        "action": "activate"
                    }
                    activate_list.append(activate_dict)
                else:
                    found_active_policy = True
                    activate_dict = {
                        "name": c_policy["name"],
                    }
                    activate_list.append(activate_dict)

        if found_disabled_policy:
            if not module.check_mode:
                ret_code = ipfilter_policy_patch(fos_ip_addr, https, auth,
                                                 vfid, result, activate_list)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

            result["changed"] = True
        elif found_active_policy:
            result["same active policy"] = activate_list
        else:
            result["failed"] = True
            result["msg"] = "could not find matching policy"

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

    # if delete policy is not None, then we make sure
    # the policy is not present.
    # policy creation or update does not happen at the same
    # time
    if delete_policies != None:
        to_delete = []
        for delete_policy in delete_policies:
            found = False
            for c_policy in c_policies:
                if c_policy["name"] == delete_policy["name"]:
                    found = True
                    break
            if found:
                to_delete.append(delete_policy)

        if len(to_delete) > 0:
            if not module.check_mode:
                ret_code = ipfilter_policy_delete(fos_ip_addr, https, auth,
                                                  vfid, result, to_delete)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

            result["changed"] = True

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

    diff_policies = []
    for new_ip in ipfilter_policies:
        for c_policy in c_policies:
            if new_ip["name"] == c_policy["name"]:
                diff_attributes = generate_diff(result, c_policy, new_ip)
                if len(diff_attributes) > 0:
                    result["c_policy"] = c_policy
                    diff_attributes["name"] = new_ip["name"]
                    ret_code = to_fos_ipfilter_policy(diff_attributes, result)
                    if ret_code != 0:
                        exit_after_login(fos_ip_addr, https, auth, result,
                                         module)

                    diff_policies.append(diff_attributes)

    add_policies = []
    for new_ip in ipfilter_policies:
        found = False
        for c_policy in c_policies:
            if new_ip["name"] == c_policy["name"]:
                found = True
        if not found:
            new_policy = {}
            for k, v in new_ip.items():
                new_policy[k] = v
            ret_code = to_fos_ipfilter_policy(new_policy, result)
            result["retcode"] = ret_code
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

            add_policies.append(new_policy)

    result["resp_ir"] = resp_ir
    result["ipfilter_policies"] = ipfilter_policies
    result["diff_policies"] = diff_policies
    result["add_policies"] = add_policies
    result["delete_policies"] = delete_policies

    if len(diff_policies) > 0:
        if not module.check_mode:
            ret_code = ipfilter_policy_patch(fos_ip_addr, https, auth, vfid,
                                             result, diff_policies)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    if len(add_policies) > 0:
        if not module.check_mode:
            ret_code = ipfilter_policy_post(fos_ip_addr, https, auth, vfid,
                                            result, add_policies)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

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

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

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

    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']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    ports = input_params['ports']
    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)
    if ret_code != 0:
        module.exit_json(**result)

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

    resp_ports = response["Response"]["fibrechannel"]
    if isinstance(resp_ports, list):
        current_ports = resp_ports
    else:
        current_ports = [resp_ports]

    diff_ports = []
    for port in ports:
        for current_port in current_ports:
            if port["name"] == current_port["name"]:
                to_human_fc(current_port)
                diff_attributes = generate_diff(result, current_port, port)
                if len(diff_attributes) > 0:
                    result["current_port"] = current_port
                    diff_attributes["name"] = port["name"]
                    ret_code = to_fos_fc(diff_attributes, result)
                    if ret_code != 0:
                        exit_after_login(fos_ip_addr, https, auth, result,
                                         module)
                    diff_ports.append(diff_attributes)

    result["diff_ports"] = diff_ports

    if len(diff_ports) > 0:
        if not module.check_mode:
            ret_code = fc_port_patch(fos_ip_addr, https, auth, vfid, result,
                                     diff_ports)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

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

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

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

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

    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']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    ipfilter_rules = input_params['ipfilter_rules']
    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)
    if ret_code != 0:
        module.exit_json(**result)

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

    resp_ir = response["Response"]["ipfilter-rule"]

    if isinstance(resp_ir, list):
        c_rules = resp_ir
    else:
        c_rules = [resp_ir]

    # convert everything to human readable from REST
    for c_rule in c_rules:
        to_human_ipfilter_rule(c_rule)

    diff_rules = []
    for new_ir in ipfilter_rules:
        for c_rule in c_rules:
            if new_ir["policy_name"] == c_rule["policy_name"] and str(new_ir["index"]) == c_rule["index"]:
                diff_attributes = generate_diff(result, c_rule, new_ir)
                if len(diff_attributes) > 0:
                    result["c_rule"] = c_rule
                    diff_attributes["policy_name"] = new_ir["policy_name"]
                    diff_attributes["index"] = new_ir["index"]
                    ret_code = to_fos_ipfilter_rule(diff_attributes, result)
                    result["retcode"] = ret_code
                    if ret_code != 0:
                        exit_after_login(fos_ip_addr, https, auth, result, module)

                    diff_rules.append(diff_attributes)

    add_rules = []
    for new_ir in ipfilter_rules:
        found = False
        for c_rule in c_rules:
            if new_ir["policy_name"] == c_rule["policy_name"] and str(new_ir["index"]) == c_rule["index"]:
                found = True
        if not found:
            new_yang_rule = {}
            for k, v in new_ir.items():
                new_yang_rule[k] = v
            ret_code = to_fos_ipfilter_rule(new_yang_rule, result)
            result["retcode"] = ret_code
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

            add_rules.append(new_yang_rule)

    delete_rules = []
    for c_rule in c_rules:
        found = False
        for new_ir in ipfilter_rules:
            if new_ir["policy_name"] == c_rule["policy_name"] and str(new_ir["index"]) == c_rule["index"]:
                found = True
        if not found:
            delete_rule = {}
            delete_rule["policy-name"] = c_rule["policy_name"]
            delete_rule["index"] = c_rule["index"]
            delete_rules.append(delete_rule)

    result["resp_ir"] = resp_ir
    result["ipfilter_rules"] = ipfilter_rules
    result["diff_rules"] = diff_rules
    result["add_rules"] = add_rules
    result["delete_rules"] = delete_rules

    if len(diff_rules) > 0:
        if not module.check_mode:
            ret_code = ipfilter_rule_patch(
                fos_ip_addr, https,
                auth, vfid, result, diff_rules)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

    if len(add_rules) > 0:
        if not module.check_mode:
            ret_code = ipfilter_rule_post(
                fos_ip_addr, https,
                auth, vfid, result, add_rules)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

        result["changed"] = True

#    if len(delete_rules) > 0:
#        if not module.check_mode:
#            ret_code = ipfilter_rule_delete(
#                fos_ip_addr, https,
#                auth, vfid, result, delete_rules)
#            if ret_code != 0:
#                exit_after_login(fos_ip_addr, https, auth, result, module)
#
#        result["changed"] = True

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)
Ejemplo n.º 21
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'),
                         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']
    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)
    if ret_code != 0:
        module.exit_json(**result)

    facts = {}

    facts['ssh_hostkeymust'] = ssh_hostkeymust

    for area in valid_areas:
        if (gather_subset is None or area in gather_subset
                or "all" in gather_subset):
            if area == "brocade_interface_fibrechannel":
                ret_code, response = fc_port_get(fos_ip_addr, https, auth,
                                                 vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                interface = {}
                interface["fibrechannel"] = (
                    response["Response"]["fibrechannel"])

                for port in interface["fibrechannel"]:
                    to_human_fc(port)

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

                interface["fibrechannel-statistics"] = (
                    response["Response"]["fibrechannel-statistics"])

                facts[area] = interface

            if area == "brocade_zoning":
                ret_code, response = defined_get(fos_ip_addr, https, auth,
                                                 vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

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

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

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

                to_human_zoning(zoning["effective-configuration"])

                facts[area] = zoning

            if area == "brocade_chassis":
                ret_code, response = chassis_get(fos_user_name, fos_password,
                                                 fos_ip_addr, fos_version,
                                                 https, auth, vfid, result,
                                                 ssh_hostkeymust)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_chassis(response["Response"]["chassis"])

                facts[area] = response["Response"]["chassis"]

            if area == "brocade_fibrechannel_configuration_fabric":
                ret_code, response = fabric_get(fos_user_name, fos_password,
                                                fos_ip_addr, fos_version,
                                                https, auth, vfid, result,
                                                ssh_hostkeymust)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_fabric(response["Response"]["fabric"])

                facts[area] = response["Response"]["fabric"]

            if area == "brocade_fibrechannel_configuration_port_configuration":
                ret_code, response = port_configuration_get(
                    fos_user_name, fos_password, fos_ip_addr, fos_version,
                    https, auth, vfid, result, ssh_hostkeymust)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_port_configuration(
                    response["Response"]["port-configuration"])

                facts[area] = response["Response"]["port-configuration"]

            if area == "brocade_fibrechannel_switch":
                ret_code, response = fc_switch_get(fos_user_name, fos_password,
                                                   fos_ip_addr, fos_version,
                                                   https, auth, vfid, result,
                                                   ssh_hostkeymust)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_switch(response["Response"]["fibrechannel-switch"])

                facts[area] = response["Response"]["fibrechannel-switch"]

            if area == "brocade_time_clock_server":
                ret_code, response = clock_server_get(fos_ip_addr, https, auth,
                                                      vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_clock_server(response["Response"]["clock-server"])

                facts[area] = response["Response"]["clock-server"]

            if area == "brocade_time_time_zone":
                ret_code, response = time_zone_get(fos_ip_addr, https, auth,
                                                   vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_time_zone(response["Response"]["time-zone"])

                facts[area] = response["Response"]["time-zone"]

            if area == "brocade_logging_syslog_server":
                ret_code, response = syslog_server_get(fos_ip_addr, https,
                                                       auth, vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                if isinstance(response["Response"]["syslog-server"], list):
                    servers = response["Response"]["syslog-server"]
                else:
                    servers = [response["Response"]["syslog-server"]]

                for server in servers:
                    to_human_syslog_server(server)

                facts[area] = servers

            if area == "brocade_logging_audit":
                ret_code, response = audit_get(fos_ip_addr, https, auth, vfid,
                                               result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_audit(response["Response"]["audit"])

                facts[area] = response["Response"]["audit"]

            if area == "brocade_snmp_system":
                ret_code, response = system_get(fos_user_name, fos_password,
                                                fos_ip_addr, fos_version,
                                                https, auth, vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                to_human_system(response["Response"]["system"])

                facts[area] = response["Response"]["system"]

            if area == "brocade_security_ipfilter_rule":
                ret_code, response = ipfilter_rule_get(fos_ip_addr, https,
                                                       auth, vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                for rule in response["Response"]["ipfilter-rule"]:
                    to_human_ipfilter_rule(rule)

                facts[area] = response["Response"]["ipfilter-rule"]

            if area == "brocade_security_ipfilter_policy":
                ret_code, response = ipfilter_policy_get(
                    fos_ip_addr, https, auth, vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                for rule in response["Response"]["ipfilter-policy"]:
                    to_human_ipfilter_policy(rule)

                facts[area] = response["Response"]["ipfilter-policy"]

            if area == "brocade_security_user_config":
                ret_code, response = user_config_get(fos_ip_addr, https, auth,
                                                     vfid, result)
                if ret_code != 0:
                    exit_after_login(fos_ip_addr, https, auth, result, module)

                for rule in response["Response"]["user-config"]:
                    to_human_user_config(rule)

                facts[area] = response["Response"]["user-config"]

    result["ansible_facts"] = facts

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)
Ejemplo n.º 22
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)
Ejemplo n.º 23
0
def main():
    """
    Main function
    """

    argument_spec = dict(
        credential=dict(required=True, type='dict'),
        vfid=dict(required=False, type='int'),
        throttle=dict(required=False, type='float'),
        chassis=dict(required=True, type='dict'))

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

    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']
    throttle = input_params['throttle']
    vfid = input_params['vfid']
    chassis = input_params['chassis']
    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)
    if ret_code != 0:
        module.exit_json(**result)

    ret_code, response = chassis_get(fos_user_name, fos_password, fos_ip_addr,
                                     fos_version, https, auth, vfid, result)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module)

    resp_chassis = response["Response"]["chassis"]

    to_human_chassis(resp_chassis)

    diff_attributes = generate_diff(result, resp_chassis, chassis)

    result["diff_attributes"] = diff_attributes
    result["resp_chassis"] = resp_chassis
    result["chassis"] = chassis

    if len(diff_attributes) > 0:
        ret_code = to_fos_chassis(diff_attributes, result)
        if ret_code != 0:
            exit_after_login(fos_ip_addr, https, auth, result, module)

        if not module.check_mode:
            ret_code = chassis_patch(fos_user_name, fos_password, fos_ip_addr,
                                     fos_version, https,
                                     auth, vfid, result, diff_attributes)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

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

    logout(fos_ip_addr, https, auth, result)
    module.exit_json(**result)
Ejemplo n.º 24
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'),
        module_name=dict(required=True, type='str'),
        obj_name=dict(required=True, type='str'),
        attributes=dict(required=True, type='dict'))

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

    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']
    vfid = input_params['vfid']
    module_name = input_params['module_name']
    obj_name = input_params['obj_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)
    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)
    if ret_code != 0:
        exit_after_login(fos_ip_addr, https, auth, result, module)

    resp_attributes = response["Response"][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)
        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)

    result["diff_attributes"] = diff_attributes
    result["resp_attributes"] = resp_attributes
    result["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)

        if not module.check_mode:
            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)
            if ret_code != 0:
                exit_after_login(fos_ip_addr, https, auth, result, module)

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

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