Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Merge c40ef52 into 498f598
Browse files Browse the repository at this point in the history
  • Loading branch information
djoproject committed Jan 15, 2020
2 parents 498f598 + c40ef52 commit f3e1ee8
Show file tree
Hide file tree
Showing 5 changed files with 1,220 additions and 243 deletions.
98 changes: 59 additions & 39 deletions gandi/cli/commands/root.py
Expand Up @@ -65,44 +65,64 @@ def help(ctx, command):
def status(gandi, service):
"""Display current status from status.gandi.net."""

if not service:
global_status = gandi.status.status()
if global_status['status'] == 'FOGGY':
# something is going on but not affecting services
filters = {
'category': 'Incident',
'current': True,
}
events = gandi.status.events(filters)
for event in events:
if event['services']:
# do not process services
continue
event_url = gandi.status.event_timeline(event)
service_detail = '%s - %s' % (event['title'], event_url)
gandi.echo(service_detail)

# then check other services
descs = gandi.status.descriptions()
needed = services = gandi.status.services()
if service:
needed = [serv for serv in services
if serv['name'].lower() == service.lower()]

for serv in needed:
if serv['status'] != 'STORMY':
output_service(gandi, serv['name'], descs[serv['status']])
summary = gandi.status.summary()

# * create a dict with service id as key
# * collect leaf services
serv_by_id = {}
leaf_serv = []
for serv in summary[u"components"]:
serv_by_id[serv[u"id"]] = serv

if not serv[u"group"]:
leaf_serv.append(serv)

# * compute long service name (parent groupe name + service name)
# * filter wanted service
serv_by_name = {}
for serv in leaf_serv:
if serv[u"group_id"] is None:
group_name = None
service_name = serv['name']
else:
group_name = serv_by_id[serv[u"group_id"]][u"name"]
service_name = '%s - %s' % (group_name, serv['name'],)

# does the user picked a specific services ?
if service is not None:
if service not in (serv['name'], group_name, service_name):
continue

serv_by_name[service_name] = serv

# process each service leaf
services_report = []
for service_name in sorted(serv_by_name.keys()):
serv = serv_by_name[service_name]

# this is a relevant service, add it to report
services_report.append({
u'name': serv[u'name'],
u'status': serv[u'status'],
u'description': serv[u'description'],
})

# no major outage for this service, just print service status
if serv[u'status'] != 'major_outage':
output_service(gandi, service_name, serv['status'], justify=30)
continue

filters = {
'category': 'Incident',
'services': serv['name'],
'current': True,
}
events = gandi.status.events(filters)
for event in events:
event_url = gandi.status.event_timeline(event)
service_detail = '%s - %s' % (event['title'], event_url)
output_service(gandi, serv['name'], service_detail)

return services
# a major outage is in progress for that service
# print every related incidents
for incident in summary[u'incidents']:
for incident_component in incident[u"components"]:
if serv[u"id"] == incident_component[u"id"]:
break
else:
continue

service_detail = '%s - %s'
service_detail %= (incident[u'name'], incident[u'shortlink'])
output_service(gandi, service_name, service_detail, justify=30)

return services_report
50 changes: 5 additions & 45 deletions gandi/cli/modules/status.py
@@ -1,10 +1,5 @@
""" Status commands module. """

try:
import urllib.parse as uparse
except ImportError:
import urllib as uparse

from gandi.cli.core.base import GandiModule


Expand All @@ -16,45 +11,10 @@ class Status(GandiModule):
"""

base_url = 'https://status.gandi.net'
api_url = 'https://status.gandi.net/api'

@classmethod
def descriptions(cls):
""" Retrieve status descriptions from status.gandi.net. """
schema = cls.json_get('%s/status/schema' % cls.api_url, empty_key=True,
send_key=False)
descs = {}
for val in schema['fields']['status']['value']:
descs.update(val)
return descs

@classmethod
def services(cls):
"""Retrieve services statuses from status.gandi.net."""
return cls.json_get('%s/services' % cls.api_url, empty_key=True,
send_key=False)

@classmethod
def status(cls):
"""Retrieve global status from status.gandi.net."""
return cls.json_get('%s/status' % cls.api_url, empty_key=True,
send_key=False)

@classmethod
def events(cls, filters):
"""Retrieve events details from status.gandi.net."""
current = filters.pop('current', False)
current_params = []
if current:
current_params = [('current', 'true')]

filter_url = uparse.urlencode(sorted(list(filters.items())) + current_params) # noqa
events = cls.json_get('%s/events?%s' % (cls.api_url, filter_url),
empty_key=True, send_key=False)
return events
statuspage_api_url = 'https://gandi.statuspage.io/api/v2'

@classmethod
def event_timeline(cls, event):
"""Retrieve event timeline url for status.gandi.net."""
return '%s/timeline/events/%s' % (cls.base_url, event['id'])
def summary(cls):
"""Retrieve summary from gandi.statuspage.io."""
return cls.json_get('%s/summary.json' % cls.statuspage_api_url,
empty_key=True, send_key=False)

0 comments on commit f3e1ee8

Please sign in to comment.