class ShowSubscriptions(OrganizationAction): description = _('show subscriptions') def __init__(self): super(ShowSubscriptions, self).__init__() self.productApi = ProductAPI() def setup_parser(self, parser): parser.add_option('--name', dest='name', help=_("organization name eg: foo.example.com (required)")) def check_options(self, validator): validator.require('name') def run(self): name = self.get_option('name') org = self.api.organization(name) pools = self.api.pools(org["label"]) updated_pool_info = [self.displayable_pool(pool) for pool in pools] # by default use verbose mode if not self.has_option('grep'): self.printer.set_strategy(VerboseStrategy()) self.printer.add_column('productName', name='Subscription') self.printer.add_column('consumed') self.printer.add_column('contractNumber', show_with=printer.VerboseStrategy) self.printer.add_column('sla', show_with=printer.VerboseStrategy) self.printer.add_column('id') self.printer.add_column('startDate', show_with=printer.VerboseStrategy) self.printer.add_column('endDate', show_with=printer.VerboseStrategy) self.printer.set_header(_("Organization's Subscriptions")) self.printer.print_items(updated_pool_info) return os.EX_OK def sla(self, pool): return {'sla': self.extract_sla_from_product(self.productApi.show(self.get_option('name'), pool['productId']))} @classmethod def convert_timestamp(cls, timestamp_field): offset = int(timestamp_field[-5:]) delta = timedelta(hours = offset / 100) t = datetime.strptime(timestamp_field[:-9], "%Y-%m-%dT%H:%M:%S") - delta return datetime.strftime(t, "%Y/%m/%d %H:%M:%S") @classmethod def extract_sla_from_product(cls, p): sla_attr = [attr.get("value", "") for attr in p["attributes"] if attr.get("name", "") == "sla"] return sla_attr[0] if len(sla_attr) > 0 else "" def displayable_pool(self, pool): p = dict(list(pool.items()) + list(self.sla(pool).items())) p['startDate'] = self.convert_timestamp(pool['startDate']) p['endDate'] = self.convert_timestamp(pool['endDate']) return p
class ShowSubscriptions(OrganizationAction): description = _('show subscriptions') def __init__(self): super(ShowSubscriptions, self).__init__() self.productApi = ProductAPI() def setup_parser(self, parser): parser.add_option('--name', dest='name', help=_("organization name eg: foo.example.com (required)")) def check_options(self, validator): validator.require('name') def run(self): name = self.get_option('name') org = self.api.organization(name) pools = self.api.pools(org["label"]) updated_pool_info = [self.displayable_pool(pool) for pool in pools] yank = ['cores', 'sockets', 'ram'] prints = {'cores': _("Cores"), 'sockets': _("Sockets"), 'ram': _("RAM")} for sub in updated_pool_info: limits = [] for attr in sub['productAttributes']: if attr['name'] in yank: limits.append("%(name)s: %(value)s" % {'name': prints[attr['name']], 'value': attr['value']}) sub['limits'] = "[ %s ]" % ", ".join(limits) # by default use verbose mode if not self.has_option('grep'): self.printer.set_strategy(VerboseStrategy()) self.printer.add_column('productName', _("Subscription")) self.printer.add_column('consumed', _("Consumed")) self.printer.add_column('contractNumber', _("Contract Number"), show_with=printer.VerboseStrategy) self.printer.add_column('sla', _("SLA"), show_with=printer.VerboseStrategy) self.printer.add_column('id', _("ID")) self.printer.add_column('limits', _("Limits"), show_with=printer.VerboseStrategy, multiline=True) self.printer.add_column('startDate', _("Start Date"), show_with=printer.VerboseStrategy) self.printer.add_column('endDate', _("End Date"), show_with=printer.VerboseStrategy) self.printer.set_header(_("Organization's Subscriptions")) self.printer.print_items(updated_pool_info) return os.EX_OK def sla(self, pool): return {'sla': self.extract_sla_from_product(self.productApi.show(self.get_option('name'), pool['productId']))} @classmethod def convert_timestamp(cls, timestamp_field): offset = int(timestamp_field[-5:]) delta = timedelta(hours = offset / 100) t = datetime.strptime(timestamp_field[:-9], "%Y-%m-%dT%H:%M:%S") - delta return datetime.strftime(t, "%Y/%m/%d %H:%M:%S") @classmethod def extract_sla_from_product(cls, p): sla_attr = [attr.get("value", "") for attr in p["attributes"] if attr.get("name", "") == "sla"] return sla_attr[0] if len(sla_attr) > 0 else "" def displayable_pool(self, pool): p = dict(list(pool.items()) + list(self.sla(pool).items())) p['startDate'] = self.convert_timestamp(pool['startDate']) p['endDate'] = self.convert_timestamp(pool['endDate']) return p
class ShowSubscriptions(OrganizationAction): description = _('show subscriptions') def __init__(self): super(ShowSubscriptions, self).__init__() self.productApi = ProductAPI() def setup_parser(self, parser): parser.add_option( '--name', dest='name', help=_("organization name eg: foo.example.com (required)")) def check_options(self, validator): validator.require('name') def run(self): name = self.get_option('name') org = self.api.organization(name) pools = self.api.pools(org["cp_key"]) updated_pool_info = [self.displayable_pool(pool) for pool in pools] # by default use verbose mode if not self.has_option('grep'): self.printer.set_strategy(VerboseStrategy()) self.printer.add_column('productName', name='Subscription') self.printer.add_column('consumed') self.printer.add_column('contractNumber', show_with=printer.VerboseStrategy) self.printer.add_column('sla', show_with=printer.VerboseStrategy) self.printer.add_column('id') self.printer.add_column('startDate', show_with=printer.VerboseStrategy) self.printer.add_column('endDate', show_with=printer.VerboseStrategy) self.printer.set_header(_("Organization's Subscriptions")) self.printer.print_items(updated_pool_info) return os.EX_OK def sla(self, pool): return { 'sla': self.extract_sla_from_product( self.productApi.show(self.get_option('name'), pool['productId'])) } def convert_timestamp(self, timestamp_field): offset = int(timestamp_field[-5:]) delta = timedelta(hours=offset / 100) t = datetime.strptime(timestamp_field[:-9], "%Y-%m-%dT%H:%M:%S") - delta return datetime.strftime(t, "%Y/%m/%d %H:%M:%S") def extract_sla_from_product(self, p): sla_attr = [ attr.get("value", "") for attr in p["attributes"] if attr.get("name", "") == "sla" ] return sla_attr[0] if len(sla_attr) > 0 else "" def displayable_pool(self, pool): p = dict(list(pool.items()) + list(self.sla(pool).items())) p['startDate'] = self.convert_timestamp(pool['startDate']) p['endDate'] = self.convert_timestamp(pool['endDate']) return p