예제 #1
0
    # custom controller
    sdk = cloudgenix.API(controller=CLOUDGENIX_CONTROLLER)
elif CLOUDGENIX_CONTROLLER is None:
    # custom SSL Verification setting.
    sdk = cloudgenix.API(ssl_verify=CLOUDGENIX_SSL_VERIFY)
else:
    # custom everything.
    sdk = cloudgenix.API(controller=CLOUDGENIX_CONTROLLER,
                         ssl_verify=CLOUDGENIX_SSL_VERIFY)

# actual api token use
token_login = sdk.interactive.use_token(CLOUDGENIX_AUTH_TOKEN)
# if login fails, sdk.tenant_id will be None.

# start cloudgenix_idname instance to cache responses.
idname = cloudgenix_idname.CloudGenixIDName(sdk)

logger.warning(
    "Loading CloudGenix ID->Name cache at startup, this may take several minutes.."
)
global_id2n = update_id2n_dicts_slow(idname)
logger.warning("ID->Name cache successfully updated.")


class CgxParseforRaw(object):
    """
    Class to allow using raw
    """
    Slacker = None
    SlackClient = None
    self_data = None
예제 #2
0
def servicelinks(sdk=None,
                 idname_obj=None,
                 controller="https://api.elcapitan.cloudgenix.com",
                 ssl_verify=True,
                 output_csv=False):

    if sdk is None:
        # need to instantiate
        sdk = cloudgenix.API(controller=controller, ssl_verify=ssl_verify)

    if not sdk.tenant_id:
        # need to login.
        if AUTH_TOKEN is None:
            sdk.interactive.login()
        else:
            sdk.interactive.use_token(AUTH_TOKEN)

    # gen id_name maps
    if idname_obj is None:
        id2n_obj = cloudgenix_idname.CloudGenixIDName(sdk)
    else:
        id2n_obj = idname_obj

    id2n_obj.update_sites_cache()
    id2n = id2n_obj.generate_sites_map()

    id2n_obj.update_elements_cache()
    id2n.update(id2n_obj.generate_elements_map())
    element_id2site = id2n_obj.generate_elements_map(key_val='id',
                                                     value_val='site_id')
    element_id2connected = id2n_obj.generate_elements_map(
        key_val='id', value_val='connected')
    # cloudgenix.jd(element_id2connected)
    id2n_obj.update_interfaces_cache()
    id2n.update(id2n_obj.generate_interfaces_map())

    prisma_servicelinks = []
    for interface in id2n_obj.interfaces_cache:
        tags = interface.get('tags', [])
        if isinstance(tags, list) and 'AUTO-PRISMA_MANAGED' in tags:
            prisma_servicelinks.append(interface)

    servicelink_status_list = []

    for sl in prisma_servicelinks:
        description = sl.get('description', '')
        prisma_rno_list = remotenet_re.findall(
            description if description is not None else "")
        if len(prisma_rno_list) >= 1:
            prisma_rno = ";".join(prisma_rno_list)
        else:
            prisma_rno = "Unknown"

        element_id = sl.get('element_id')
        site_id = element_id2site.get(element_id, "Could not get site")
        element_connected = element_id2connected.get(element_id, False)
        interface_id = sl.get('id')
        parent_if_id = sl.get('parent')
        admin_state = sl.get('admin_up')

        resp = sdk.get.interfaces_status(site_id, element_id, interface_id)
        if not element_connected:
            # if element is not connected, status is stale.
            operational_state = "Unknown_Offline"
            extended_state = "Unknown_Offline"
        elif resp.cgx_status:
            operational_state = resp.cgx_content.get("operational_state")
            extended_state = resp.cgx_content.get("extended_state")
        else:
            operational_state = "Unknown"
            extended_state = "Unknown"

        site_name = id2n.get(site_id, site_id)
        element_name = id2n.get(element_id, element_id)
        interface_name = id2n.get(interface_id, interface_id)
        parent_if_name = id2n.get(parent_if_id, parent_if_id)

        servicelink_status_list.append({
            "Site":
            site_name,
            "Element":
            element_name,
            "Interface":
            interface_name,
            "Element Online":
            "Online" if element_connected else "Offline",
            "Admin State":
            str(admin_state),
            "Operational State":
            operational_state,
            "Extended State":
            extended_state,
            "Prisma Remote On-boarding":
            prisma_rno,
            "Parent Interface":
            parent_if_name
        })

    return servicelink_status_list
예제 #3
0
def servicelinks(sdk=None,
                 idname_obj=None,
                 controller="https://api.elcapitan.cloudgenix.com",
                 ssl_verify=True,
                 prisma_all_endpoints=False):

    prisma_name2ip = {}
    prisma_status = False

    if sdk is None:
        # need to instantiate
        sdk = cloudgenix.API(controller=controller, ssl_verify=ssl_verify)

    if not sdk.tenant_id:
        # need to login.
        if AUTH_TOKEN is None:
            sdk.interactive.login()
        else:
            sdk.interactive.use_token(AUTH_TOKEN)

    # Check Prisma API key if specified
    if PRISMA_API_KEY is not None:
        gpcs = GPCloudServiceOperations(PRISMA_API_KEY)
        print("Checking Prisma API Key.. ", end="", flush=True)
        prisma_status, prisma_name2ip, _, message = gpcs.remote_gw_addr_dicts()
        if not prisma_status:
            print("ERROR, message: {0}.".format(message), flush=True)
            sys.exit(1)
        else:
            print("Success." if message is None else "{0}.".format(message),
                  flush=True)

    if sdk.tenant_id is None:
        print(
            "ERROR: CloudGenix API Login failed, please check credentials or AUTH_TOKEN."
        )
        sys.exit(1)
    else:
        print("Logged into CloudGenix Tenant {0}.".format(sdk.tenant_name),
              flush=True)

    # gen id_name maps
    if idname_obj is None:
        id2n_obj = cloudgenix_idname.CloudGenixIDName(sdk)
    else:
        id2n_obj = idname_obj

    id2n_obj.update_sites_cache()
    id2n = id2n_obj.generate_sites_map()

    id2n_obj.update_elements_cache()
    id2n.update(id2n_obj.generate_elements_map())
    element_id2site = id2n_obj.generate_elements_map(key_val='id',
                                                     value_val='site_id')
    element_id2connected = id2n_obj.generate_elements_map(
        key_val='id', value_val='connected')
    # cloudgenix.jd(element_id2connected)
    id2n_obj.update_interfaces_cache()
    id2n.update(id2n_obj.generate_interfaces_map())

    prisma_servicelinks = []
    for interface in id2n_obj.interfaces_cache:
        tags = interface.get('tags', [])
        if isinstance(tags, list) and 'AUTO-PRISMA_MANAGED' in tags:
            prisma_servicelinks.append(interface)

    servicelink_status_list = []

    for sl in prisma_servicelinks:
        description = sl.get('description', '')
        prisma_rno_list = remotenet_re.findall(
            description if description is not None else "")
        if len(prisma_rno_list) >= 1:
            prisma_rno = ";".join(prisma_rno_list)
        else:
            prisma_rno = "Unknown"

        element_id = sl.get('element_id')
        site_id = element_id2site.get(element_id, "Could not get site")
        element_connected = element_id2connected.get(element_id, False)
        interface_id = sl.get('id')
        parent_if_id = sl.get('parent')
        admin_state = sl.get('admin_up')

        # check for endpoint ip config
        sl_config = sl.get('service_link_config', {})
        if sl_config is not None:
            peer_config = sl_config.get('peer', {})
        else:
            peer_config = {}

        if peer_config is not None:
            conf_ip_list = peer_config.get('ip_addresses', [])
        else:
            conf_ip_list = []

        if len(conf_ip_list) == 0:
            conf_ip = "No IP"
        else:
            conf_ip = ";".join(conf_ip_list)

        resp = sdk.get.interfaces_status(site_id, element_id, interface_id)
        if not element_connected:
            # if element is not connected, status is stale.
            operational_state = "Unknown_Offline"
            extended_state = "Unknown_Offline"
        elif resp.cgx_status:
            operational_state = resp.cgx_content.get("operational_state")
            extended_state = resp.cgx_content.get("extended_state")
        else:
            operational_state = "Unknown"
            extended_state = "Unknown"

        site_name = id2n.get(site_id, site_id)
        element_name = id2n.get(element_id, element_id)
        interface_name = id2n.get(interface_id, interface_id)
        parent_if_name = id2n.get(parent_if_id, parent_if_id)

        if not prisma_status:
            servicelink_status_list.append({
                "Site":
                site_name,
                "Element":
                element_name,
                "Interface":
                interface_name,
                "Element Online":
                "Online" if element_connected else "Offline",
                "Admin State":
                str(admin_state),
                "Operational State":
                operational_state,
                "Extended State":
                extended_state,
                "Prisma Remote On-boarding":
                prisma_rno,
                "Parent Interface":
                parent_if_name,
                "Configured Endpoint":
                conf_ip
            })
        else:
            # in prisma endpoint IP mode. check if we want all tunnels or just admin down ones.
            if prisma_all_endpoints or admin_state is False:
                servicelink_status_list.append({
                    "Site":
                    site_name,
                    "Element":
                    element_name,
                    "Interface":
                    interface_name,
                    "Element Online":
                    "Online" if element_connected else "Offline",
                    "Admin State":
                    str(admin_state),
                    "Operational State":
                    operational_state,
                    "Extended State":
                    extended_state,
                    "Prisma Remote On-boarding":
                    prisma_rno,
                    "Parent Interface":
                    parent_if_name,
                    "Configured Endpoint":
                    conf_ip,
                    "Prisma API Endpoint IP":
                    prisma_name2ip.get(prisma_rno)
                })

    return servicelink_status_list