Example #1
0
def get_tenant_metrics(tenant_id, scaling_groups, grouped_servers,
                       _print=False):
    """
    Produce per-group metrics for all the groups of a tenant

    :param list scaling_groups: Tenant's scaling groups as dict from CASS
    :param dict grouped_servers: Servers from Nova grouped based on
        scaling group ID.
    :return: generator of (tenantId, groupId, desired, actual) GroupMetrics
    """
    if _print:
        print('processing tenant {} with groups {} and servers {}'.format(
              tenant_id, len(scaling_groups), len(grouped_servers)))

    groups = {g['groupId']: g for g in scaling_groups}

    for group_id in set(groups.keys() + grouped_servers.keys()):
        servers = grouped_servers.get(group_id, [])
        if group_id in groups:
            group = groups[group_id]
        else:
            group = {'groupId': group_id_from_metadata(servers[0]['metadata']),
                     'desired': 0}
        servers = map(NovaServer.from_server_details_json, servers)
        _len = compose(len, list, flip(filter, servers))
        active = _len(lambda s: s.state == ServerState.ACTIVE)
        bad = _len(lambda s: s.state in (ServerState.SHUTOFF,
                                         ServerState.ERROR,
                                         ServerState.DELETED))
        yield GroupMetrics(tenant_id, group['groupId'], group['desired'],
                           active, len(servers) - bad - active)
Example #2
0
def test_flip():
    def f(a, b):
        return a, b

    assert flip(f, 'a', 'b') == ('b', 'a')