def get_product(orgName, prodName): product_api = ProductAPI() prod = product_api.product_by_name(orgName, prodName) if prod == None: print >> sys.stderr, _("Could not find product [ %s ] within organization [ %s ]") % (prodName, orgName) return prod
def get_product(orgName, prodName): product_api = ProductAPI() prod = product_api.product_by_name(orgName, prodName) if prod == None: raise ApiDataError(_("Could not find product [ %s ] within organization [ %s ]") % (prodName, orgName)) return prod
def get_product(orgName, prodName): product_api = ProductAPI() prod = product_api.product_by_name(orgName, prodName) if prod == None: raise ApiDataError( _("Could not find product [ %s ] within organization [ %s ]") % (prodName, orgName)) return prod
class Product(): api = ProductAPI() def create_product(self, prv, name=None, label=None, description='Built by API', gpgkey=None): if name is None: name = generate_name(8) if label is None: label = "label-%s" % name.lower() return self.api.create(prv['id'], name, label, description, gpgkey) def delete_product(self, org, pId): return self.api.delete(org['label'], pId) def product(self, org, pId): return self.api.show(org['label'], pId) def products_by_org(self, org, name=None): return self.api.products_by_org(org['label'], name) def sync(self, org, pId): task = self.api.sync(org['label'], pId) while task['sync_status'] != 'finished': task = self.api.last_sync_status(org['label'], pId) return task
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
def get_product(orgName, prodName=None, prodLabel=None, prodId=None): """ Retrieve product by name, label or id. """ product_api = ProductAPI() products = product_api.product_by_name_or_label_or_id(orgName, prodName, prodLabel, prodId) if len(products) > 1: raise ApiDataError(_("More than 1 product found with the name or label provided, "\ "recommend using product id. The product id may be retrieved "\ "using the 'product list' command.")) elif len(products) == 0: raise ApiDataError(_("Could not find product [ %(prodName)s ] within organization [ %(orgName)s ]") % {'prodName':prodName, 'orgName':orgName}) return products[0]
def get_product(orgName, prodName=None, prodLabel=None, prodId=None): """ Retrieve product by name, label or id. """ product_api = ProductAPI() products = product_api.product_by_name_or_label_or_id( orgName, prodName, prodLabel, prodId) if len(products) > 1: raise ApiDataError(_("More than 1 product found with the name or label provided, "\ "recommend using product id. The product id may be retrieved "\ "using the 'product list' command.")) elif len(products) == 0: raise ApiDataError( _("Could not find product [ %(prodName)s ] within organization [ %(orgName)s ]" ) % { 'prodName': prodName or prodLabel or prodId, 'orgName': orgName }) return products[0]
def __init__(self): super(ShowSubscriptions, self).__init__() self.productApi = ProductAPI()
def create_data(numorgs, numsystems, numproviders, numproducts, numrepos, singleorg): # Setup connection to Katello admin = AdminCLI() admin.setup_parser() admin.opts, admin.args = admin.parser.parse_args([]) admin.setup_server() admin._username = "******" admin._password = "******" org_names = [] if (singleorg): # If we pass in a single org name # we just load all the data into that. org_names.append(singleorg) else: # Otherwise just create fake orgs orgapi = OrganizationAPI() print "Creating [%s] Orgs" % numorgs for i in range(numorgs): name = "Org-%s" % randomString() org_names.append(name) print "[%s] Creating org with name [%s]" % (i, name) orgapi.create(name, "description") # create envs envapi = EnvironmentAPI() for i in range(len(org_names)): print "[%s] Creating DEV/TEST/STAGE in org: [%s]" % (i, org_names[i]) libraryId = get_environment(org_names[i], "Library")["id"] print "Library ID: %s" % libraryId envids = [libraryId] for x in range(len(ENVIRONMENTS)): existing_env = get_environment(org_names[i], ENVIRONMENTS[x]) if not existing_env: e = envapi.create(org_names[i], ENVIRONMENTS[x], "Desc", envids[x]) envids.append(e["id"]) else: envids.append(existing_env["id"]) ## create providers, products and repos print "Creating [%s] providers in each org" % numproviders for i in range(len(org_names)): for y in range(numproviders): provider_name = "Provider-%s" % randomString() print "[%s] Creating Provider with name: [%s] in org: [%s] and products + repos" % ( y, provider_name, org_names[i]) providerapi = ProviderAPI() provider = providerapi.create(provider_name, org_names[i], "Desc", "Custom", None) print " Creating [%s] Products in each provider" % numproducts for z in range(numproducts): product_name = "P-%s" % randomString() print " [%s] Creating product with name: [%s]" % ( z, product_name) productapi = ProductAPI() product = productapi.create(provider["id"], product_name, "Desc", None) print " Creating [%s] Products in each product" % numproducts for x in range(numrepos): repo_name = "Repo-%s" % randomString() print " [%s] Creating repo with name: [%s]" % ( x, repo_name) repoapi = RepoAPI() url = "http://repos.example.com/%s" % repo_name repoapi.create(org_names[i], product["id"], repo_name, url, None, True) ## Create systems print "Creating [%s] Systems in each org and assigning to random envs" % numsystems for i in range(len(org_names)): systemapi = SystemAPI() for x in range(numsystems): system_name = "System-%s" % randomString() randenv = random.choice(ENVIRONMENTS) print "Registering system: [%s] in environment: [%s]" % ( system_name, randenv) system = systemapi.register(system_name, org_names[i], randenv, [], 'system') print "[%s] Created system: %s" % (x, system["name"])
def __init__(self): super(ProductAction, self).__init__() self.api = ProductAPI() self.repoapi = RepoAPI() self.csapi = ChangesetAPI()
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
def create_data(numorgs, numsystems, numproviders, numproducts, numrepos, singleorg): # Setup connection to Katello admin = AdminCLI() admin.setup_parser() admin.opts, admin.args = admin.parser.parse_args([]) admin.setup_server() admin._username = "******" admin._password = "******" org_names = [] if (singleorg): # If we pass in a single org name # we just load all the data into that. org_names.append(singleorg) else: # Otherwise just create fake orgs orgapi = OrganizationAPI() print "Creating [%s] Orgs" % numorgs for i in range(numorgs): name = "Org-%s" % randomString() org_names.append(name) print "[%s] Creating org with name [%s]" % (i, name) orgapi.create(name, "description") # create envs envapi = EnvironmentAPI() for i in range(len(org_names)): print "[%s] Creating DEV/TEST/STAGE in org: [%s]" % (i, org_names[i]) libraryId = get_environment(org_names[i], "Library")["id"] print "Library ID: %s" % libraryId envids = [libraryId] for x in range(len(ENVIRONMENTS)): existing_env = get_environment(org_names[i], ENVIRONMENTS[x]) if not existing_env: e = envapi.create(org_names[i], ENVIRONMENTS[x], "Desc", envids[x]) envids.append(e["id"]) else: envids.append(existing_env["id"]) ## create providers, products and repos print "Creating [%s] providers in each org" % numproviders for i in range(len(org_names)): for y in range(numproviders): provider_name = "Provider-%s" % randomString() print "[%s] Creating Provider with name: [%s] in org: [%s] and products + repos" % (y, provider_name, org_names[i]) providerapi = ProviderAPI() provider = providerapi.create(provider_name, org_names[i], "Desc", "Custom", None) print " Creating [%s] Products in each provider" % numproducts for z in range(numproducts): product_name = "P-%s" % randomString() print " [%s] Creating product with name: [%s]" % (z, product_name) productapi = ProductAPI() product = productapi.create(provider["id"], product_name, "Desc", None) print " Creating [%s] Products in each product" % numproducts for x in range(numrepos): repo_name = "Repo-%s" % randomString() print " [%s] Creating repo with name: [%s]" % (x, repo_name) repoapi = RepoAPI() url = "http://repos.example.com/%s" % repo_name repoapi.create(org_names[i], product["id"], repo_name, url, None, True) ## Create systems print "Creating [%s] Systems in each org and assigning to random envs" % numsystems for i in range(len(org_names)): systemapi = SystemAPI() for x in range(numsystems): system_name = "System-%s" % randomString() randenv = random.choice(ENVIRONMENTS) print "Registering system: [%s] in environment: [%s]" % (system_name, randenv) system = systemapi.register(system_name, org_names[i], randenv, [], 'system') print "[%s] Created system: %s" % (x, system["name"])