Пример #1
0
def connect_to_cloudservers(region=None, context=None, **kwargs):
    """Creates a client for working with cloud servers."""
    context = context or identity
    _cs_auth_plugin.discover_auth_systems()
    id_type = get_setting("identity_type")
    if id_type != "keystone":
        auth_plugin = _cs_auth_plugin.load_plugin(id_type)
    else:
        auth_plugin = None
    region = _safe_region(region, context=context)
    mgt_url = _get_service_endpoint(context, "compute", region)
    cloudservers = None
    if not mgt_url:
        # Service is not available
        return
    insecure = not get_setting("verify_ssl")
    cs_shell = _cs_shell()
    extensions = cs_shell._discover_extensions("1.1")
    cloudservers = _cs_client.Client(context.username,
                                     context.password,
                                     project_id=context.tenant_id,
                                     auth_url=context.auth_endpoint,
                                     auth_system=id_type,
                                     region_name=region,
                                     service_type="compute",
                                     auth_plugin=auth_plugin,
                                     insecure=insecure,
                                     extensions=extensions,
                                     http_log_debug=_http_debug,
                                     **kwargs)
    agt = cloudservers.client.USER_AGENT
    cloudservers.client.USER_AGENT = _make_agent_name(agt)
    cloudservers.client.management_url = mgt_url
    cloudservers.client.auth_token = context.token
    cloudservers.exceptions = _cs_exceptions
    # Add some convenience methods
    cloudservers.list_images = cloudservers.images.list
    cloudservers.list_flavors = cloudservers.flavors.list
    cloudservers.list = cloudservers.servers.list

    def list_base_images():
        """
        Returns a list of all base images; excludes any images created
        by this account.
        """
        return [
            image for image in cloudservers.images.list()
            if not hasattr(image, "server")
        ]

    def list_snapshots():
        """
        Returns a list of all images created by this account; in other words, it
        excludes all the base images.
        """
        return [
            image for image in cloudservers.images.list()
            if hasattr(image, "server")
        ]

    def find_images_by_name(expr):
        """
        Returns a list of images whose name contains the specified expression.
        The value passed is treated as a regular expression, allowing for more
        specific searches than simple wildcards. The matching is done in a
        case-insensitive manner.
        """
        return [
            image for image in cloudservers.images.list()
            if re.search(expr, image.name, re.I)
        ]

    cloudservers.list_base_images = list_base_images
    cloudservers.list_snapshots = list_snapshots
    cloudservers.find_images_by_name = find_images_by_name
    cloudservers.identity = identity
    return cloudservers
Пример #2
0
def connect_to_cloudservers(region=None, context=None, verify_ssl=None, **kwargs):
    """Creates a client for working with cloud servers."""
    context = context or identity
    _cs_auth_plugin.discover_auth_systems()
    id_type = get_setting("identity_type")
    if id_type != "keystone":
        auth_plugin = _cs_auth_plugin.load_plugin(id_type)
    else:
        auth_plugin = None
    region = _safe_region(region, context=context)
    mgt_url = _get_service_endpoint(context, "compute", region)
    cloudservers = None
    if not mgt_url:
        # Service is not available
        return
    if verify_ssl is None:
        insecure = not get_setting("verify_ssl")
    else:
        insecure = not verify_ssl
    cs_shell = _cs_shell()
    extensions = cs_shell._discover_extensions("1.1")
    cloudservers = _cs_client.Client(context.username, context.password,
            project_id=context.tenant_id, auth_url=context.auth_endpoint,
            auth_system=id_type, region_name=region, service_type="compute",
            auth_plugin=auth_plugin, insecure=insecure, extensions=extensions,
            http_log_debug=_http_debug, **kwargs)
    agt = cloudservers.client.USER_AGENT
    cloudservers.client.USER_AGENT = _make_agent_name(agt)
    cloudservers.client.management_url = mgt_url
    cloudservers.client.auth_token = context.token
    cloudservers.exceptions = _cs_exceptions
    # Add some convenience methods
    cloudservers.list_images = cloudservers.images.list
    cloudservers.list_flavors = cloudservers.flavors.list
    cloudservers.list = cloudservers.servers.list

    def list_base_images():
        """
        Returns a list of all base images; excludes any images created
        by this account.
        """
        return [image for image in cloudservers.images.list()
                if not hasattr(image, "server")]

    def list_snapshots():
        """
        Returns a list of all images created by this account; in other words, it
        excludes all the base images.
        """
        return [image for image in cloudservers.images.list()
                if hasattr(image, "server")]

    def find_images_by_name(expr):
        """
        Returns a list of images whose name contains the specified expression.
        The value passed is treated as a regular expression, allowing for more
        specific searches than simple wildcards. The matching is done in a
        case-insensitive manner.
        """
        return [image for image in cloudservers.images.list()
                if re.search(expr, image.name, re.I)]

    cloudservers.list_base_images = list_base_images
    cloudservers.list_snapshots = list_snapshots
    cloudservers.find_images_by_name = find_images_by_name
    cloudservers.identity = identity
    return cloudservers