def do_delete(self, args): try: doParser = self.arg_delete() doArgs = doParser.parse_args(shlex.split(args)) org = org_get(self.api, doArgs.org) allUsergrp = self.api.Usergroups.Getall(Name=org.name) if allUsergrp is None: printer.out("No user groups found in [" + org.name + "].") return 0 allUsergrp = allUsergrp.userGroups.userGroup for item in allUsergrp: if item.admin.name == doArgs.name: result = self.api.Usergroups(item.dbId).Delete() printer.out("[" + item.admin.name + "] has been successfully deleted.", printer.OK) return 0 printer.out("[" + doArgs.name + "] was not found in [" + org.name + "].") return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_delete() except Exception as e: return handle_uforge_exception(e)
def do_delete(self, args): try: doParser = self.arg_delete() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out("There is no organization matching ["+doArgs.org+"].", printer.OK) return 0 printer.out("Getting target platform with id ["+doArgs.id+"] for ["+org.name+"] . . .") targetPlatform = self.api.Orgs(org.dbId).Targetplatforms(doArgs.id).Get() if targetPlatform is None: printer.out("targetPlatform with id "+ doArgs.id +" does not exist", printer.ERROR) return 2 else: result = self.api.Orgs(org.dbId).Targetplatforms(doArgs.id).Delete() printer.out("Target Platform ["+targetPlatform.name+"] has successfully been deleted.", printer.OK) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_delete() except Exception as e: return handle_uforge_exception(e)
def do_delete(self, args): try: # add arguments doParser = self.arg_delete() doArgs = doParser.parse_args(shlex.split(args)) printer.out("Deleting subscription profile [" + doArgs.name + "] ...") org = org_utils.org_get(self.api, doArgs.org) # call UForge API subscriptions = self.api.Orgs(org.dbId).Subscriptions().Getall(Search=None) exist = False for item in subscriptions.subscriptionProfiles.subscriptionProfile: if item.name == doArgs.name: exist = True subscription = subscriptions.subscriptionProfiles.subscriptionProfile[0] self.api.Orgs(org.dbId).Subscriptions(subscription.dbId).Remove(None) printer.out("Subscription profile [" + doArgs.name + "] deleted", printer.OK) if not exist: printer.out("Subscription profile requested don't exist in [" + org.name + "]") return 0 return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_delete() except Exception as e: return handle_uforge_exception(e)
def do_create(self, args): try: doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) org = org_get(self.api, doArgs.org) newUsergrp = userGroup() newUser = user() newUser.loginName = doArgs.name newUser.email = doArgs.email newUser.password = doArgs.usergrpPassword newUsergrp.admin = newUser newUsergrp.members = pyxb.BIND() if doArgs.accounts is not None: for item in doArgs.accounts: addNewUser = user() addNewUser.loginName = item newUsergrp.members.append(addNewUser) printer.out("[" + addNewUser.loginName + "] has been added to user group.") result = self.api.Usergroups.Create(Org=org.name,body=newUsergrp) printer.out("User group [" + newUser.loginName + "] has been successfully created", printer.OK) return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_create() except Exception as e: return handle_uforge_exception(e)
def do_disable(self, args): try: doParser = self.arg_enable() doArgs = doParser.parse_args(shlex.split(args)) printer.out("Getting subscription profile with name [" + doArgs.name + "]...") org = org_utils.org_get(self.api, doArgs.org) subscriptions = self.api.Orgs(org.dbId).Subscriptions().Getall(Search=doArgs.name) exist = False for item in subscriptions.subscriptionProfiles.subscriptionProfile: if item.name == doArgs.name: exist = True updated_subscription = subscriptionProfile() updated_subscription.name = item.name updated_subscription.code = item.code if item.active: updated_subscription.active = False printer.out("Disabling subscription profile with name [" + doArgs.name + "] ...") # call UForge API self.api.Orgs(org.dbId).Subscriptions(item.dbId).Update(updated_subscription) printer.out("Subscription [" + doArgs.name + "] is disabled.", printer.OK) else: printer.out("Subscription [" + doArgs.name + "] is already disabled", printer.WARNING) if not exist: printer.out("Subscription profile requested don't exist in [" + org.name + "]") return 0 return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_enable() except Exception as e: return handle_uforge_exception(e)
def do_create(self, args): try: doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) allRepo = self.api.Orgs(org.dbId).Repositories.Getall() allRepo = allRepo.repositories.repository for item in allRepo: if doArgs.repoUrl == item.url: printer.out("The repository with URL [" + item.url + "] already exist in [" + org.name + "].", printer.ERROR) return 0 newRepository = repository() newRepository.url = doArgs.repoUrl newRepository.packagingType = doArgs.type newRepository.name = doArgs.name newRepository.officiallySupported = doArgs.officiallySupported result = self.api.Orgs(org.dbId).Repositories.Create(body=newRepository) printer.out("Successfully created repository with URL [" + doArgs.repoUrl + "] in [" + org.name + "].", printer.OK) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_create() except Exception as e: return handle_uforge_exception(e)
def do_list(self, args): try: doParser = self.arg_list() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) printer.out("Getting category list for ["+org.name+"] . . .") allCategory = self.api.Orgs(org.dbId).Categories.Getall() allCategory = order_list_object_by(allCategory.categories.category, "name") if len(allCategory) is 0: printer.out("["+org.name+"] has no categories.") return 0 table = Texttable(200) table.set_cols_align(["l", "l", "l"]) table.header(["Id", "Category", "type"]) for item in allCategory: table.add_row([item.dbId, item.name, item.type]) print table.draw() + "\n" printer.out("Found " + str(len(allCategory)) + " categories.") return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def do_create(self, args): try: doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) allCategory = self.api.Orgs(org.dbId).Categories.Getall() allCategory = allCategory.categories.category Exist = False for item in allCategory: if doArgs.name == item.name: Exist = True printer.out("A category already have the name ["+doArgs.name+"].", printer.ERROR) return 0 if not Exist: newCategory = category() newCategory.name = doArgs.name newCategory.type = doArgs.type result = self.api.Orgs(org.dbId).Categories.Create(body=newCategory) printer.out("Category ["+newCategory.name+"] has successfully been created.", printer.OK) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_create() except Exception as e: return handle_uforge_exception(e)
def do_list(self, args): try: doParser = self.arg_list() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) # call UForge API printer.out("Getting all the subscription profiles for organization ...") subscriptions = self.api.Orgs(org.dbId).Subscriptions().Getall(Search=None) subscriptions = generics_utils.order_list_object_by(subscriptions.subscriptionProfiles.subscriptionProfile, "name") if subscriptions is None or len(subscriptions) == 0: printer.out("There is no subscriptions in [" + org.name + "] ") return 0 printer.out("List of subscription profiles in [" + org.name + "] :") table = Texttable(200) table.set_cols_align(["c", "c", "c", "c"]) table.header(["Name", "Code", "Active", "description"]) for subscription in subscriptions: if subscription.active: active = "X" else: active = "" table.add_row([subscription.name, subscription.code, active, subscription.description]) print table.draw() + "\n" printer.out("Foumd " + str(len(subscriptions)) + " subscription profile(s).") return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def do_delete(self, args): try: doParser = self.arg_delete() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) allCategory = self.api.Orgs(org.dbId).Categories.Getall() allCategory = allCategory.categories.category deleteList = [] if doArgs.name is not None: for arg1 in doArgs.name: for item in allCategory: if arg1 == item.name: deleteList.append(item) break if doArgs.ids is not None: for arg2 in doArgs.ids: for item2 in allCategory: if long(arg2) == item2.dbId: deleteList.append(item2) break for item3 in deleteList: result = self.api.Orgs(org.dbId).Categories.Delete(Id=item3.dbId) printer.out("Category ["+item3.name+"] has been deleted.", printer.OK) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_delete() except Exception as e: return handle_uforge_exception(e)
def do_create(self, args): try: # add arguments doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) printer.out("Creating role [" + doArgs.name + "] ...") org = org_utils.org_get(self.api, doArgs.org) new_role = role() new_role.name = doArgs.name if doArgs.description: new_role.description = doArgs.description if doArgs.entitlements: if doArgs.entitlements is not None: new_role.entitlements = pyxb.BIND() entList = self.api.Entitlements.Getall() entList = entList.entitlements.entitlement entList = compare(entList, doArgs.entitlements, "name") for ent in entList: add_entitlement = entitlement() add_entitlement.name = ent.name add_entitlement.description = ent.description new_role.entitlements.append(add_entitlement) printer.out("Entitlement " + ent.name + " added to the role") self.api.Orgs(org.dbId).Roles().Create(new_role) printer.out("Role [" + new_role.name + "] was correctly created", printer.OK) return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_create() except Exception as e: return handle_uforge_exception(e)
def do_list(self, args): try: doParser = self.arg_list() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) printer.out("Getting user list for ["+org.name+"] . . .") allUsers = self.api.Orgs(org.dbId).Members.Getall() allUsers = order_list_object_by(allUsers.users.user, "loginName") table = Texttable(200) table.set_cols_align(["l", "l", "c"]) table.header(["Login", "Email", "Active"]) for item in allUsers: if item.active: active = "X" else: active = "" table.add_row([item.loginName, item.email, active]) print table.draw() + "\n" return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def do_create(self, args): try: doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) allDist = self.api.Orgs(org.dbId).Distributions.Getall() allDist = allDist.distributions.distribution goldenId = None for distrib in allDist: if distrib.name == doArgs.name and distrib.version == doArgs.version and distrib.arch == doArgs.arch: goldenId = distrib.dbId if goldenId is None: printer.out("No distributions found with the arguments entered.") return 0 result = self.api.Orgs(org.dbId).Distributions(goldenId).Goldens.Create(Language=doArgs.language, Edition=doArgs.edition, Type=doArgs.type, Goldendate=doArgs.goldenDate, Filename=doArgs.goldenName, body=None) printer.out("Golden image [" + result.name + "] has been created", printer.OK) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_create() except Exception as e: return handle_uforge_exception(e)
def do_list(self, args): try: doParser = self.arg_list() doArgs = doParser.parse_args(shlex.split(args)) org = org_get(self.api, doArgs.org) allUsergrp = self.api.Usergroups.Getall(Name=org.name) if allUsergrp is None: printer.out("No user groups found.") return 0 allUsergrp = allUsergrp.userGroups.userGroup table = Texttable(200) table.set_cols_align(["l", "r"]) table.header(["Name", "# Members"]) for item in allUsergrp: table.add_row([item.admin.name, str(len(item.members.member))]) print table.draw() + "\n" printer.out("Found " + str(len(allUsergrp)) + " user group in [" + org.name + "].") return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def do_enable(self, args): try: doParser = self.arg_enable() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out("There is no organization matching ["+doArgs.org+"].", printer.OK) return 0 targetPlatformsOrg = self.api.Orgs(org.dbId).Targetplatforms.Getall() if targetPlatformsOrg is None or len(targetPlatformsOrg.targetPlatforms.targetPlatform) == 0: printer.out("There is no target platform for the user \""+doArgs.account+"\" in [" + org.name + "].") return 0 else: targetPlatformsOrg = targetPlatformsOrg.targetPlatforms.targetPlatform targetPlatformsList = targetPlatforms() targetPlatformsList.targetPlatforms = pyxb.BIND() targetPlatformsOrg = compare(targetPlatformsOrg, doArgs.targetPlatforms, "name") if len(targetPlatformsOrg) == 0: listName = "" for tpname in doArgs.targetPlatforms: listName = listName + tpname + " " printer.out("There is no target platforms matching ["+listName+"].") return 0 for item in targetPlatformsOrg: targetPlatformToEnable = targetPlatform() targetPlatformToEnable = item targetPlatformToEnable.active = True targetPlatformToEnable.access = True printer.out("Enabling ["+item.name+"].") targetPlatformsList.targetPlatforms.append(targetPlatformToEnable) result = self.api.Users(doArgs.account).Targetplatforms.Update(Org=org.name,body=targetPlatformsList) result =generics_utils.order_list_object_by(result.targetPlatforms.targetPlatform, "name") table = Texttable(200) table.set_cols_align(["c", "c", "c", "c"]) table.header(["Id", "Name", "Type", "Access"]) for item in result: if item.access: access = "X" else: access = "" table.add_row([item.dbId, item.name, item.type, access]) printer.out("Target Platform list for user \""+doArgs.account+"\" :") print table.draw() + "\n" return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_enable() except Exception as e: return handle_uforge_exception(e)
def do_remove(self, args): try: # add arguments doParser = self.arg_remove() doArgs = doParser.parse_args(shlex.split(args)) printer.out("Getting subscription profile with name [" + doArgs.name + "]...") org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out("There is no organization matching ["+doArgs.org+"].", printer.OK) return 0 subscriptions = self.api.Orgs(org.dbId).Subscriptions().Getall(Search=None) exist = False subProfileSelected = None for item in subscriptions.subscriptionProfiles.subscriptionProfile: if item.name == doArgs.name: subProfileSelected = item exist = True all_targetFormats = targetFormats() all_targetFormats.targetFormats = pyxb.BIND() newTargetFormats = compare(item.targetFormats.targetFormat, doArgs.targetFormats, "name") if len(newTargetFormats) == 0: listName = "" for tfname in doArgs.targetFormats: listName = listName + tfname + " " printer.out("There is no target formats matching ["+listName+"].") return 0 for targetFormatItem in item.targetFormats.targetFormat: for deleteList in newTargetFormats: if targetFormatItem.name == deleteList.name: already_targetFormat = targetFormat() already_targetFormat.access = targetFormatItem.access already_targetFormat.active = False already_targetFormat.preselected = targetFormatItem.preselected already_targetFormat.name = targetFormatItem.name already_targetFormat.uri = targetFormatItem.uri all_targetFormats.targetFormats.append(already_targetFormat) printer.out("Removed target format " + targetFormatItem.name + " for subscription.") # call UForge API self.api.Orgs(org.dbId).Subscriptions(subProfileSelected.dbId).Targetformats.Orgsubscriptionprofiletargetformatupdate(Allusers=doArgs.allusers, body=all_targetFormats) printer.out("Somes target formats removed from subscription profile [" + doArgs.name + "]...", printer.OK) if not exist: printer.out("Subscription profile requested don't exist in [" + org.name + "]") return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_remove() except Exception as e: return handle_uforge_exception(e)
def do_create(self, args): try: doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out("There is no organization matching ["+doArgs.org+"].", printer.OK) return 0 newTargetFormat = targetFormat() newTargetFormat.name = doArgs.name field = fieldImageFormat format = imageFormat() format.name = doArgs.format newTargetFormat.format = format targetFormatCategory = category() targetFormatCategory.name = doArgs.category targetFormatCategory.type = categoryImageFormat newTargetFormat.category = targetFormatCategory field = fieldType newTargetFormat.type = doArgs.type if doArgs.credAccountType is not None: field = fieldCredAccountType newTargetFormat.credAccountType = doArgs.credAccountType if doArgs.credInfos is not None: newTargetFormat.credInfos = doArgs.credInfos if doArgs.imageInfos is not None: newTargetFormat.imageInfos = doArgs.imageInfos if doArgs.publishInfos is not None: newTargetFormat.publishInfos = doArgs.publishInfos result = self.api.Orgs(org.dbId).Targetformats.Create(body=newTargetFormat) if doArgs.file is not None and result is not None: fileName = os.path.basename(doArgs.file) file = open(doArgs.file, "r") self.api.Orgs(org.dbId).Targetformats(result.dbId).Logo(Logoid=result.logo.dbId, Filename=fileName).Upload(body=file) printer.out("Target Format ["+newTargetFormat.name+"] has successfully been created.", printer.OK) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_create() except pyxb.SimpleFacetValueError as e: printer.out("Unknown " + field + " ["+str(e.value)+"] for target format." , printer.ERROR) values = None for value in e.type.values(): if values is None: values = value else: values = values + ", " + value printer.out("Please change for one of these values: " + values, printer.ERROR) return 2 except Exception as e: return handle_uforge_exception(e)
def do_disable(self, args): try: doParser = self.arg_disable() doArgs = doParser.parse_args(shlex.split(args)) org = org_get(api=self.api,name=doArgs.org) printer.out("Getting distributions list for user \""+doArgs.account+"\".") distrosUser = self.api.Users(doArgs.account).Distros.Getall() distrosUser = distrosUser.distributions.distribution distrosUser = compare(distrosUser, doArgs.name, "name") if doArgs.version is not None: distrosUser = compare(distrosUser, doArgs.version, "version") if doArgs.arch is not None: distrosUser = compare(distrosUser, doArgs.arch, "arch") distribs = distributions() distribs.distributions = pyxb.BIND() for item in distrosUser: newDistro = distribution() newDistro = item newDistro.active = False distribs.distributions.append(newDistro) printer.out("Updating distributions list for user \""+doArgs.account+"\" :") returnList = self.api.Users(doArgs.account).Distros.Update(Org=org.name,body=distribs) printer.out("List for user \""+doArgs.account+"\" updated :", printer.OK) table = Texttable(200) table.set_cols_align(["c", "c", "c", "c", "c", "c"]) table.header(["Distribution", "Version", "Architecture", "Access", "Visible", "Release Date"]) for item in returnList.distributions.distribution: if item.active: active = "X" else: active = "" if item.visible: visible = "X" else: visible = "" if item.releaseDate is None: releaseDate = "Unknown" else: releaseDate = item.releaseDate table.add_row([item.name, item.version, item.arch, active, visible, releaseDate]) print table.draw() + "\n" return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_disable() except Exception as e: return handle_uforge_exception(e)
def do_list(self, args): try: doParser = self.arg_list() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) allRepo = self.api.Orgs(org.dbId).Repositories.Getall() if allRepo is None: printer.out("No repositories found in [" + org.name + "].") return 0 if doArgs.sort is not None: if doArgs.sort.lower() == "name": printer.out("Repository list ordered by [name] :") allRepo = order_list_object_by(allRepo.repositories.repository, "name") elif doArgs.sort.lower() == "id": printer.out("Repository list ordered by [id] :") allRepo = sorted(allRepo.repositories.repository, key=lambda x: getattr(x, "dbId"), reverse=False) elif doArgs.sort.lower() == "url": printer.out("Repository list ordered by [url] :") allRepo = order_list_object_by(allRepo.repositories.repository, "url") elif doArgs.sort.lower() == "type": printer.out("Repository list ordered by [type] :") allRepo = order_list_object_by(allRepo.repositories.repository, "packagingType") else: printer.out("Sorting parameter filled don't exist.", printer.WARNING) printer.out("Repository list :") allRepo = sorted(allRepo.repositories.repository, key=lambda x: getattr(x, "dbId"), reverse=False) else: printer.out("Repository list :") allRepo = sorted(allRepo.repositories.repository, key=lambda x: getattr(x, "dbId"), reverse=False) table = Texttable(200) table.set_cols_align(["c", "c", "l", "l", "c"]) table.set_cols_width([5,5,30,80,8]) table.header(["Id", "Off. Supported", "Name", "URL", "Type"]) for item in allRepo: if item.officiallySupported: officiallySupported = "X" else: officiallySupported = "" table.add_row([item.dbId, officiallySupported, item.name, item.url, item.packagingType]) print table.draw() + "\n" printer.out("Found " + str(len(allRepo)) + " repositories.") return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_list() except Exception as e: return handle_uforge_exception(e)
def do_disable(self, args): try: doParser = self.arg_disable() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out("There is no organization matching ["+doArgs.org+"].", printer.OK) return 0 printer.out("Getting target platform list for ["+org.name+"] . . .") allTargetPlatforms = self.api.Orgs(org.dbId).Targetplatforms.Getall() allTargetPlatforms = allTargetPlatforms.targetPlatforms.targetPlatform if len(allTargetPlatforms) == 0: printer.out("There is no target platforms in ["+org.name+"].", printer.WARNING) return 0 all_targetplatforms = targetPlatforms() all_targetplatforms.targetPlatforms = pyxb.BIND() allTargetPlatforms = compare(allTargetPlatforms, doArgs.targetPlatforms, "name") if len(allTargetPlatforms) == 0: listName = "" for tpname in doArgs.targetPlatforms: listName = listName + tpname + " " printer.out("There is no target platforms matching ["+listName+"].", printer.ERROR) return 2 for item in allTargetPlatforms: targetPlatformToDisable = targetPlatform() targetPlatformToDisable = item targetPlatformToDisable.active = False targetPlatformToDisable.access = False printer.out("Disabling ["+item.name+"].") all_targetplatforms.targetPlatforms.append(targetPlatformToDisable) result = self.api.Orgs(org.dbId).Targetplatforms.Updateaccess(body=all_targetplatforms) if result is None or len(result.targetPlatforms.targetPlatform) == 0: printer.out("The target platforms(s) selected are already disabled", printer.WARNING) return 0 sentenceReturn = "Target Platform(s)" for tp in result.targetPlatforms.targetPlatform: sentenceReturn = sentenceReturn + " " + tp.name + " " printer.out(sentenceReturn + "has/have been disabled.", printer.OK) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_disable() except Exception as e: return handle_uforge_exception(e)
def do_add(self, args): try: doParser = self.arg_add() doArgs = doParser.parse_args(shlex.split(args)) # call UForge API printer.out("Getting user [" + doArgs.account + "] ...") user = self.api.Users(doArgs.account).Get() if user is None: printer.out("user " + self.login + "does not exist", printer.ERROR) return 0 org = org_get(self.api, doArgs.org) orgRoles = self.api.Orgs(org.dbId).Roles().Getall(body=None) # Check if the given roles really exist in the organization if doArgs.roles != None: checkedRoleList = [] for r in doArgs.roles: found = False for existing_role in orgRoles.roles.role: if existing_role.name == r: found = True checkedRoleList.append(r) break if found == False: printer.out("Unable to find role with name [" + r + "] in organization [" + org.name + "]", printer.ERROR) return 0 # Create a new roles structures new_roles = roles() new_roles.roles = pyxb.BIND() # Add the new roles for new_role in checkedRoleList: r = role() r.name = new_role new_roles.roles.append(r) # Add the user existing roles for existing_user_role in user.roles.role: new_roles.roles.append(existing_user_role) self.api.Users(doArgs.account).Roles.Update(new_roles) printer.out("User [" + doArgs.account + "] updated with new roles.", printer.OK) else: printer.out("You need to specify roles to add.", printer.ERROR) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_add() except Exception as e: return handle_uforge_exception(e)
def do_enable(self, args): try: doParser = self.arg_enable() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out("There is no organization matching ["+doArgs.org+"].", printer.OK) return 0 printer.out("Getting target format list for ["+org.name+"] . . .") allTargetFormats = self.api.Orgs(org.dbId).Targetformats.Getall() allTargetFormats = allTargetFormats.targetFormats.targetFormat if len(allTargetFormats) == 0: printer.out("There is no target formats in ["+org.name+"].", printer.WARNING) return 0 all_targetformats = targetFormats() all_targetformats.targetFormats = pyxb.BIND() allTargetFormats = compare(allTargetFormats, doArgs.targetFormats, "name") if len(allTargetFormats) == 0: listName = "" for tfname in doArgs.targetFormats: listName = listName + tfname + " " printer.out("There is no target formats matching ["+listName+"].", printer.ERROR) return 2 for item in allTargetFormats: newTargetFormat = targetFormat() newTargetFormat = item newTargetFormat.active = True newTargetFormat.access = True printer.out("Enabling ["+item.name+"].") all_targetformats.targetFormats.append(newTargetFormat) result = self.api.Orgs(org.dbId).Targetformats.Updateaccess(body=all_targetformats) if result is None or len(result.targetFormats.targetFormat) == 0: printer.out("The target format(s) selected are already enabled", printer.WARNING) return 0 sentenceReturn = "Target Format(s)" for tf in result.targetFormats.targetFormat: sentenceReturn = sentenceReturn + " " + tf.name + " " printer.out(sentenceReturn + "has/have been enabled.", printer.OK) return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_enable() except Exception as e: return handle_uforge_exception(e)
def do_create(self, args): try: # add arguments doParser = self.arg_create() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) # call UForge API printer.out("Creating subscription profile [" + doArgs.name + "] ...") # create a user manually new_subscription_profile = subscriptionProfile() new_subscription_profile.name = doArgs.name new_subscription_profile.code = doArgs.code if doArgs.description: new_subscription_profile.description = doArgs.description if doArgs.active: new_subscription_profile.active = doArgs.active new_subscription_profile.admins = pyxb.BIND() if doArgs.admins: for a in doArgs.admins: new_admin = user() new_admin.loginName = a new_subscription_profile.admins.append(new_admin) new_subscription_profile.roles = pyxb.BIND() if doArgs.roles: for a in doArgs.roles: new_role = role() new_role.name = a new_subscription_profile.roles.append(new_role) # Send the create user request to the server new_subscription_profile = self.api.Orgs(org.dbId).Subscriptions().Add(new_subscription_profile) if new_subscription_profile is None: printer.out("No information about the new subscription profile available", printer.ERROR) else: printer.out("New subscription profile [" + new_subscription_profile.name + "] created.", printer.OK) table = Texttable(200) table.set_cols_align(["c", "c", "c"]) table.header( ["Name", "Code", "Active"]) table.add_row([new_subscription_profile.name, new_subscription_profile.code, "X" if new_subscription_profile.active else ""]) print table.draw() + "\n" return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_create() except Exception as e: return handle_uforge_exception(e)
def do_remove(self, args): try: doParser = self.arg_remove() doArgs = doParser.parse_args(shlex.split(args)) printer.out("Getting subscription profile with name [" + doArgs.name + "]...") org = org_utils.org_get(self.api, doArgs.org) subscriptions = self.api.Orgs(org.dbId).Subscriptions().Getall(Search=None) osList = self.api.Orgs(org.dbId).Distributions.Getall() osList = osList.distributions.distribution if osList == None: printer.out("The organization as no OS available") if doArgs.os is not None: osList = compare(osList, doArgs.os, "name") if doArgs.version is not None: osList = compare(osList, doArgs.version, "version") if doArgs.arch is not None: osList = compare(osList, doArgs.arch, "arch") if len(osList) == 0: printer.out("There is no distribution matching the request.") return 0 subProfileSelected = None exist = False for subProfile in subscriptions.subscriptionProfiles.subscriptionProfile: if subProfile.name == doArgs.name: subProfileSelected = subProfile exist = True all_distros = distributions() all_distros.distributions = pyxb.BIND() for distribItem in subProfile.distributions.distribution: for nr in osList: if distribItem.name == nr.name and distribItem.version == nr.version and distribItem.arch == nr.arch: distro = distribution() distro = nr distro.active = False all_distros.distributions.append(distro) if not exist: printer.out("Subscription profile requested don't exist in [" + org.name + "]") return 0 self.api.Orgs(org.dbId).Subscriptions(subProfileSelected.dbId).Distros.Update(Allusers=doArgs.allusers, body=all_distros) printer.out("Some OS removed for subscription profile [" + doArgs.name + "]...", printer.OK) return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_remove() except Exception as e: return handle_uforge_exception(e)
def do_add(self, args): try: doParser = self.arg_add() doArgs = doParser.parse_args(shlex.split(args)) printer.out("Getting subscription profile with name [" + doArgs.name + "]...") org = org_utils.org_get(self.api, doArgs.org) subscriptions = self.api.Orgs(org.dbId).Subscriptions().Getall(Search=None) osList = self.api.Orgs(org.dbId).Distributions.Getall() osList = osList.distributions.distribution if osList == None: printer.out("The organisation as no OS available") if doArgs.os is not None: osList = compare(osList, doArgs.os, "name") if doArgs.version is not None: osList = compare(osList, doArgs.version, "version") if doArgs.arch is not None: osList = compare(osList, doArgs.arch, "arch") subProfileSelected = None exist = False for subProfile in subscriptions.subscriptionProfiles.subscriptionProfile: if subProfile.name == doArgs.name: subProfileSelected = subProfile exist = True all_distros = distributions() all_distros.distributions = pyxb.BIND() for f in subProfile.distributions.distribution: all_distros.distributions.append(f) for nr in osList: existRole = False for item in all_distros.distributions.distribution: if item.name == nr.name: existRole = True if not existRole: all_distros.distributions.append(nr) if not exist: printer.out("Subscription profile requested don't exist in [" + org.name + "]") return 0 self.api.Orgs(org.dbId).Subscriptions(subProfileSelected.dbId).Distros.Update(all_distros) printer.out("Some OS added for subscription profile [" + doArgs.name + "]...", printer.OK) return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_add() except Exception as e: return handle_uforge_exception(e)
def do_add(self, args): try: # add arguments do_parser = self.arg_add() doArgs = do_parser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) printer.out("Getting role [" + doArgs.name + "]...") all_roles = self.api.Orgs(org.dbId).Roles().Getall(None) old_role = None for r in all_roles.roles.role: if r.name == doArgs.name: old_role = r break if old_role is None: printer.out("No role [" + doArgs.name + "]...") return 1 new_role = role() new_role.name = old_role.name new_role.description = old_role.description new_role.entitlements = pyxb.BIND() for r in old_role.entitlements.entitlement: already_entitlement = entitlement() already_entitlement.name = r.name new_role.entitlements.append(already_entitlement) entitlementsList = self.api.Entitlements.Getall() entitlementsList = compare(entitlementsList.entitlements.entitlement, doArgs.entitlements, "name") for e in entitlementsList: new_entitlement = entitlement() new_entitlement.name = e.name new_role.entitlements.append(new_entitlement) printer.out("Added " + new_entitlement.name + " to role.") if len(entitlementsList) == 0: printer.out("No entitlements found.", printer.ERROR) return 0 self.api.Orgs(org.dbId).Roles().Update(new_role) printer.out("Role [" + doArgs.name + "] updated with new entitlements.", printer.OK) return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_add() except Exception as e: return handle_uforge_exception(e)
def do_add(self, args): try: # add arguments doParser = self.arg_add() doArgs = doParser.parse_args(shlex.split(args)) printer.out("Getting subscription profile with name [" + doArgs.name + "]...") org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out("There is no organization matching ["+doArgs.org+"].", printer.OK) return 0 subscriptions = self.api.Orgs(org.dbId).Subscriptions().Getall(Search=None) orgTargetPlatforms = self.api.Orgs(org.dbId).Targetplatforms.Getall() if orgTargetPlatforms is None: printer.out("The organization as no target platform available.") return 0 exist = False subProfileSelected = None for item in subscriptions.subscriptionProfiles.subscriptionProfile: if item.name == doArgs.name: exist = True subProfileSelected = item all_targetPlatforms = targetPlatforms() all_targetPlatforms.targetPlatforms = pyxb.BIND() newTargetPlatforms = compare(orgTargetPlatforms.targetPlatforms.targetPlatform, doArgs.targetPlatforms, "name") if len(newTargetPlatforms) == 0: listName = "" for tpname in doArgs.targetPlatforms: listName = listName + tpname + " " printer.out("There is no target platforms matching ["+listName+"].") return 0 for nr in newTargetPlatforms: all_targetPlatforms.targetPlatforms.append(nr) printer.out("Added target platform " + nr.name + " for subscription.") self.api.Orgs(org.dbId).Subscriptions(subProfileSelected.dbId).Targetplatforms.Orgsubscriptionprofiletargetplatformupdate(Allusers=doArgs.allusers, body=all_targetPlatforms) printer.out("Some target platforms added for subscription profile [" + doArgs.name + "]...", printer.OK) if not exist: printer.out("Subscription profile requested don't exist in [" + org.name + "]") return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_add() except Exception as e: return handle_uforge_exception(e)
def do_listTargetFormat(self, args): try: doParser = self.arg_listTargetFormat() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out("There is no organization matching ["+doArgs.org+"].", printer.OK) return 0 printer.out("Getting target platform with id ["+doArgs.id+"] for ["+org.name+"] . . .") targetPlatform = self.api.Orgs(org.dbId).Targetplatforms(doArgs.id).Get() if targetPlatform is None: printer.out("TargetPlatform with id "+ doArgs.id +" does not exist", printer.ERROR) return 2 else: printer.out("Getting target format list for target platform ["+targetPlatform.name+"] . . .") allTargetFormats = self.api.Orgs(org.dbId).Targetplatforms(targetPlatform.dbId).Targetformats.Getalltargetformats() allTargetFormats = order_list_object_by(allTargetFormats.targetFormats.targetFormat, "name") if len(allTargetFormats) == 0: printer.out("There is no target formats in target platform ["+targetPlatform.name+"].", printer.OK) return 0 printer.out("Found "+str(len(allTargetFormats))+" target formats in target platform ["+targetPlatform.name+"].", printer.OK) table = Texttable(200) table.set_cols_align(["c", "l", "l", "l", "l", "l", "c"]) table.header(["Id", "Name", "Format", "Category", "Type", "CredAccountType", "Access"]) for item in allTargetFormats: if item.access: access = "X" else: access = "" if item.credAccountType is None: credAccountType = "" else: credAccountType = item.credAccountType table.add_row([item.dbId, item.name, item.format.name, item.category.name, item.type, credAccountType, access]) print table.draw() + "\n" return 0 except ArgumentParserError as e: printer.out("In Arguments: "+str(e), printer.ERROR) self.help_listTargetFormat() except Exception as e: return handle_uforge_exception(e)
def do_promote(self, args): try: doParser = self.arg_promote() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) adminUser = self.api.Users(doArgs.account).Get() if adminUser == None: printer.out("User [" + doArgs.account + "] doesn't exist.", printer.ERROR) return 0 self.api.Orgs(org.dbId).Members(adminUser.loginName).Change(Admin=True, body=adminUser) printer.out("User [" + doArgs.account + "] has been promoted in [" + org.name + "] :", printer.OK) # Second API call to get latest infos adminUser = self.api.Users(doArgs.account).Get() if adminUser.active: active = "X" else: active = "" printer.out("Informations about [" + adminUser.loginName + "] :") table = Texttable(200) table.set_cols_align(["c", "l", "c", "c", "c", "c", "c", "c"]) table.header( ["Login", "Email", "Lastname", "Firstname", "Created", "Active", "Promo Code", "Creation Code"] ) table.add_row( [ adminUser.loginName, adminUser.email, adminUser.surname, adminUser.firstName, adminUser.created.strftime("%Y-%m-%d %H:%M:%S"), active, adminUser.promoCode, adminUser.creationCode, ] ) print table.draw() + "\n" return 0 except ArgumentParserError as e: printer.out("In Arguments: " + str(e), printer.ERROR) self.help_promote() except Exception as e: return handle_uforge_exception(e)
def do_add(self, args): try: doParser = self.arg_add() doArgs = doParser.parse_args(shlex.split(args)) org = org_get(self.api, doArgs.org) allUsergrp = self.api.Usergroups.Getall(Name=org.name) if allUsergrp.userGroups.userGroup is None or len(allUsergrp.userGroups.userGroup) == 0: printer.out("There is no user group in organization [" + org.name + "].") return 0 Exist = False for item in allUsergrp.userGroups.userGroup: if item.admin.name == doArgs.name: Exist = True userGroupId = item.dbId if not Exist: printer.out("The user group [" + doArgs.name + "] doesn't exist in [" + org.name + "].") return 0 allMembers = self.api.Usergroups(userGroupId).Members.Get() newUsers = users() newUsers.users = pyxb.BIND() for item2 in allMembers.users.user: newUser = user() newUser.loginName = item2.loginName newUsers.users.append(newUser) for item3 in doArgs.accounts: userExist = False for item4 in allMembers.users.user: if item3 == item4.loginName: userExist = True printer.out("User [" + item3 + "] is already in the user group.", printer.WARNING) break if not userExist: newUser = user() newUser.loginName = item3 newUsers.users.append(newUser) printer.out("User " + newUser.loginName + " has been added to the user group.", printer.OK) result = self.api.Usergroups(userGroupId).Members.Add(body=newUsers) return 0 except ArgumentParserError as e: printer.out("ERROR: In Arguments: " + str(e), printer.ERROR) self.help_add() except Exception as e: return handle_uforge_exception(e)
def do_disable(self, args): try: doParser = self.arg_disable() doArgs = doParser.parse_args(shlex.split(args)) org = org_utils.org_get(self.api, doArgs.org) if org is None: printer.out( "There is no organization matching [" + doArgs.org + "].", printer.OK) return 0 if doArgs.org is not None: targetPlatformsUser = self.api.Users( doArgs.account).Targetplatforms.Getall(org=org.dbId) else: targetPlatformsUser = self.api.Users( doArgs.account).Targetplatforms.Getall() if targetPlatformsUser is None or len( targetPlatformsUser.targetPlatforms.targetPlatform) == 0: printer.out("There is no target platform for the user \"" + doArgs.account + "\" in [" + org.name + "].") return 0 else: targetPlatformsUser = targetPlatformsUser.targetPlatforms.targetPlatform targetPlatformsList = targetPlatforms() targetPlatformsList.targetPlatforms = pyxb.BIND() targetPlatformsUser = compare(targetPlatformsUser, doArgs.targetPlatforms, "name") if len(targetPlatformsUser) == 0: listName = "" for tpname in doArgs.targetPlatforms: listName = listName + tpname + " " printer.out("There is no target platforms matching [" + listName + "].") return 0 for item in targetPlatformsUser: targetPlatformToDisable = targetPlatform() targetPlatformToDisable = item targetPlatformToDisable.active = False targetPlatformToDisable.access = False printer.out("Disabling [" + item.name + "].") targetPlatformsList.targetPlatforms.append( targetPlatformToDisable) result = self.api.Users(doArgs.account).Targetplatforms.Update( Org=org.name, body=targetPlatformsList) result = generics_utils.order_list_object_by( result.targetPlatforms.targetPlatform, "name") table = Texttable(200) table.set_cols_align(["c", "c", "c", "c"]) table.header(["Id", "Name", "Type", "Access"]) for item in result: if item.access: access = "X" else: access = "" table.add_row([item.dbId, item.name, item.type, access]) printer.out("Target Platform list for user \"" + doArgs.account + "\" :") print table.draw() + "\n" return 0 except ArgumentParserError as e: printer.out("In Arguments: " + str(e), printer.ERROR) self.help_disable() except Exception as e: return handle_uforge_exception(e)