Skip to content

Instantly share code, notes, and snippets.

@michalc
Created November 14, 2023 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michalc/d8c794c0e80a259d7353ba9f8476fce7 to your computer and use it in GitHub Desktop.
Save michalc/d8c794c0e80a259d7353ba9f8476fce7 to your computer and use it in GitHub Desktop.
Check the stack of apps in CloudFoundry
import json
import os
from urllib.parse import urlparse, parse_qsl
import requests
from rich import box
from rich.console import Console
from rich.table import Table
with open(f'{os.environ["HOME"]}/.cf/config.json') as f:
token = json.loads(f.read())['AccessToken']
def api_request(method, path, params=(), json=None):
response = requests.request(
method=method,
url=f'https://api.london.cloud.service.gov.uk{path}',
headers={'authorization': token},
params=params,
json=json,
)
response.raise_for_status()
return response.json()
def paginated_api_request(method, path, params=(), json=None):
while path:
response = api_request(method, path, params, json)
yield from response['resources']
pagination_obj = response.get("pagination", {}) or {}
next_obj = pagination_obj.get("next", {}) or {}
href_str = next_obj.get("href", "")
parsed_url = urlparse(href_str)
path, params = parsed_url.path, parse_qsl(parsed_url.query)
our_space_names = {
'my-space-1',
'my-space-2,
}
spaces_by_guid = {
space['guid']: space
for space in paginated_api_request('GET', '/v3/spaces')
}
our_apps = sorted((
app for app in paginated_api_request('GET', '/v3/apps')
if spaces_by_guid[app['relationships']['space']['data']['guid']]['name'] in our_space_names
), key=lambda app: app['name'])
table = Table(box=box.ASCII, header_style='not bold')
table.add_column("Space")
table.add_column("App")
table.add_column("Stack")
for app in our_apps:
table.add_row(
spaces_by_guid[app['relationships']['space']['data']['guid']]['name'],
app['name'],
app['lifecycle']['data'].get('stack', 'Unknown'),
style='bold bright_red' if app['lifecycle']['data'].get('stack', 'Unknown') != 'cflinuxfs4' else 'bold bright_white'
)
console = Console()
console.print(table)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment