def unbind_service_from_app(app_name: str,
                            bind_name: str,
                            configuration: Configuration,
                            secrets: Secrets,
                            org_name: str = None,
                            space_name: str = None):
    """
    Unbind the service from the given application.

    See
    https://apidocs.cloudfoundry.org/280/service_bindings/delete_a_particular_service_binding.html
    """  # noqa: E501
    service_bind = get_bind_by_name(bind_name,
                                    configuration,
                                    secrets,
                                    app_name=app_name,
                                    space_name=space_name,
                                    org_name=org_name)

    logger.debug("Ubinding service {s} from application {a}".format(
        s=bind_name, a=app_name))

    path = "/v2/service_bindings/{s}".format(
        s=service_bind["metadata"]["guid"])
    call_api(path, configuration, secrets, method="DELETE")
def unmap_route_from_app(app_name: str,
                         host_name: str,
                         configuration: Configuration,
                         secrets: Secrets,
                         org_name: str = None,
                         space_name: str = None):
    """
    Unmap a specific route from a given application.

    As Domains are deprecated in the Cloud Foundry API, they are not
    specified here.
    See
    https://apidocs.cloudfoundry.org/280/#domains--deprecated-
    See
    https://www.cloudfoundry.org/blog/coming-changes-app-manifest-simplification/

    See
    https://apidocs.cloudfoundry.org/280/apps/remove_route_from_the_app.html
    """
    app = get_app_by_name(app_name,
                          configuration,
                          secrets,
                          org_name=org_name,
                          space_name=space_name)

    routes_path = app["entity"]["routes_url"]
    routes = call_api(routes_path, configuration, secrets).json()

    for route in routes["resources"]:
        if route["entity"]["host"] == host_name:
            call_api("{a}/{r}".format(a=routes_path,
                                      r=route["metadata"]["guid"]),
                     configuration,
                     secrets,
                     method="DELETE")
def remove_routes_from_app(app_name: str,
                           route_host: str,
                           configuration: Configuration,
                           secrets: Secrets,
                           org_name: str = None,
                           space_name: str = None):
    """
    Remove routes from a given application.

    See
    https://apidocs.cloudfoundry.org/280/apps/remove_route_from_the_app.html
    """
    app = get_app_by_name(app_name,
                          configuration,
                          secrets,
                          org_name=org_name,
                          space_name=space_name)

    routes = get_app_routes_by_host(app_name,
                                    route_host,
                                    configuration,
                                    secrets,
                                    org_name=org_name,
                                    space_name=space_name)

    app_guid = app["metadata"]["guid"]
    for route in routes:
        route_guid = route["metadata"]["guid"]
        path = "/v2/apps/{a}/routes/{r}".format(a=app_guid, r=route_guid)
        call_api(path, configuration, secrets, method="DELETE")
def list_apps(configuration: Configuration,
              secrets: Secrets) -> Dict[str, Any]:
    """
    List all applications available to the authorized user.

    See https://apidocs.cloudfoundry.org/280/apps/list_all_apps.html to
    understand the content of the response.
    """
    return call_api("/v2/apps", configuration, secrets).json()
def delete_app(app_name: str,
               configuration: Configuration,
               secrets: Secrets,
               org_name: str = None,
               space_name: str = None):
    """
    Delete application.

    See https://apidocs.cloudfoundry.org/280/apps/delete_a_particular_app.html
    """
    app = get_app_by_name(app_name,
                          configuration,
                          secrets,
                          org_name=org_name,
                          space_name=space_name)

    path = "/v2/apps/{a}".format(a=app['metadata']['guid'])
    call_api(path, configuration, secrets, method="DELETE")
def stop_app(app_name: str,
             configuration: Configuration,
             secrets: Secrets,
             org_name: str = None,
             space_name: str = None):
    """
    Stop application

    See https://apidocs.cloudfoundry.org/280/apps/updating_an_app.html
    """
    app = get_app_by_name(app_name,
                          configuration,
                          secrets,
                          org_name=org_name,
                          space_name=space_name)

    path = "/v2/apps/{a}".format(a=app['metadata']['guid'])
    call_api(path,
             configuration,
             secrets,
             method="PUT",
             body={"state": "STOPPED"})
def terminate_app_instance(app_name: str,
                           instance_index: int,
                           configuration: Configuration,
                           secrets: Secrets,
                           org_name: str = None,
                           space_name: str = None):
    """
    Terminate the application's instance at the given index.

    See
    https://apidocs.cloudfoundry.org/280/apps/terminate_the_running_app_instance_at_the_given_index.html
    """  # noqa: E501
    app = get_app_by_name(app_name,
                          configuration,
                          secrets,
                          org_name=org_name,
                          space_name=space_name)

    logger.debug("Terminating instance {i} of application {a}".format(
        i=instance_index, a=app_name))

    path = "/v2/apps/{a}/instances/{i}".format(a=app["metadata"]["guid"],
                                               i=instance_index)
    call_api(path, configuration, secrets, method="DELETE")
def get_app_summary(app_name: str, configuration: Configuration,
                    secrets: Secrets, org_name: str = None,
                    space_name: str = None) -> Dict[str, Any]:
    """
    Fetch the application summary.

    See https://apidocs.cloudfoundry.org/280/apps/get_app_summary.html
    for more information.
    """
    app = get_app_by_name(
        app_name, configuration, secrets, org_name=org_name,
        space_name=space_name)

    return call_api(
        "/v2/apps/{a}/summary".format(a=app["metadata"]["guid"]),
        configuration, secrets).json()
def get_app_stats(app_name: str, configuration: Configuration,
                  secrets: Secrets, org_name: str = None,
                  space_name: str = None) -> Dict[str, Any]:
    """
    Fetch the metrics of the given application.

    See https://apidocs.cloudfoundry.org/280/apps/get_detailed_stats_for_a_started_app.html
    for more information.
    """  # noqa: E501
    app = get_app_by_name(
        app_name, configuration, secrets, org_name=org_name,
        space_name=space_name)

    return call_api(
        "/v2/apps/{a}/stats".format(a=app["metadata"]["guid"]),
        configuration, secrets).json()
def map_route_to_app(app_name: str,
                     host_name: str,
                     configuration: Configuration,
                     secrets: Secrets,
                     org_name: str = None,
                     space_name: str = None) -> List[Dict[str, Any]]:
    """
    Map a specific route to a given application.

    As Domains are deprecated in the Cloud Foundry API, they are not
    specified here.
    See
    https://apidocs.cloudfoundry.org/280/#domains--deprecated-
    See
    https://www.cloudfoundry.org/blog/coming-changes-app-manifest-simplification/

    See
    https://apidocs.cloudfoundry.org/280/apps/remove_route_from_the_app.html
    """
    app = get_app_by_name(app_name,
                          configuration,
                          secrets,
                          org_name=org_name,
                          space_name=space_name)

    routes_for_host = get_routes_by_host(host_name,
                                         configuration,
                                         secrets,
                                         org_name=org_name)

    routes_path = app["entity"]["routes_url"]

    results = []
    for route in routes_for_host["resources"]:
        results.append(
            call_api("{a}/{r}".format(a=routes_path,
                                      r=route["metadata"]["guid"]),
                     configuration,
                     secrets,
                     method="PUT").json())
    return results