def test_get_app_by_name_expect_fullname(call_api):
    apps = responses.apps.copy()
    apps['total_results'] = 0
    apps['resources'] = []

    call_api.return_value = responses.FakeResponse(
        status_code=200, text=None, json=lambda: apps)

    with pytest.raises(FailedActivity) as ex:
        get_app_by_name("my-", config.config, secrets.secrets)
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 test_get_app_by_name_and_space(auth):
    auth.return_value = responses.auth_response

    q = "q=name:my-app&q=space_guid:{s}".format(
        s=responses.space["metadata"]["guid"])

    with requests_mock.mock() as m:
        m.get(
            "https://example.com/v2/apps?{q}".format(q=q), status_code=200,
            json=responses.apps, complete_qs=True)

        app = get_app_by_name(
            "my-app", config.config, secrets.secrets,
            space_guid=responses.space["metadata"]["guid"])
        assert app["entity"]["name"] == "my-app"
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 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 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
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 test_get_app_by_name_returns_the_app_with_exact_name(call_api):
    call_api.return_value = responses.FakeResponse(
        status_code=200, text=None, json=lambda: responses.apps)

    app = get_app_by_name("my-app", config.config, secrets.secrets)
    assert app["entity"]["name"] == "my-app"