File: event_detail.py

package info (click to toggle)
python-softlayer 6.2.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,508 kB
  • sloc: python: 57,195; makefile: 133; xml: 97; sh: 59
file content (74 lines) | stat: -rw-r--r-- 2,436 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""Details of a specific event, and ability to acknowledge event."""
# :license: MIT, see LICENSE for more details.

import click

from SoftLayer.CLI.command import SLCommand as SLCommand
from SoftLayer.CLI import environment
from SoftLayer.CLI import formatting
from SoftLayer.managers.account import AccountManager as AccountManager
from SoftLayer import utils


@click.command(cls=SLCommand)
@click.argument('identifier')
@click.option('--ack', is_flag=True, default=False,
              help="Acknowledge Event. Doing so will turn off the popup in the control portal")
@environment.pass_env
def cli(env, identifier, ack):
    """Details of a specific event, and ability to acknowledge event."""

    # Print a list of all on going maintenance
    manager = AccountManager(env.client)
    event = manager.get_event(identifier)

    if ack:
        manager.ack_event(identifier)

    env.fout(basic_event_table(event))
    env.fout(impacted_table(event))
    env.fout(update_table(event))


def basic_event_table(event):
    """Formats a basic event table"""
    table = formatting.Table(["Id", "Status", "Type", "Start", "End"],
                             title=utils.clean_splitlines(event.get('subject')))

    table.add_row([
        event.get('id'),
        utils.lookup(event, 'statusCode', 'name'),
        utils.lookup(event, 'notificationOccurrenceEventType', 'keyName'),
        utils.clean_time(event.get('startDate')),
        utils.clean_time(event.get('endDate'))
    ])

    return table


def impacted_table(event):
    """Formats a basic impacted resources table"""
    table = formatting.Table([
        "Type", "Id", "Hostname", "PrivateIp", "Label"
    ])
    for item in event.get('impactedResources', []):
        table.add_row([
            item.get('resourceType'),
            item.get('resourceTableId'),
            item.get('hostname'),
            item.get('privateIp'),
            item.get('filterLabel')
        ])
    return table


def update_table(event):
    """Formats a basic event update table"""
    update_number = 0
    for update in event.get('updates', []):
        update_number = update_number + 1
        header = "======= Update #%s on %s =======" % (update_number, utils.clean_time(update.get('startDate')))
        click.secho(header, fg='green')
        text = update.get('contents')
        # deals with all the \r\n from the API
        click.secho(utils.clean_splitlines(text))