Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
        def do_images(self, args):
                try:
                        doParser = self.arg_images()
                        doArgs = doParser.parse_args(shlex.split(args))

                        printer.out("Getting images list...")
                        allImages = self.api.Users(doArgs.account).Appliances(doArgs.id).Images.Getall()

                        appliancesList = self.api.Users(doArgs.account).Appliances.Getall()
                        appliancesList = appliancesList.appliances.appliance

                        if allImages is None or len(allImages.images.image) == 0:
                                printer.out("No images found for user [" + doArgs.account + "].")
                                return 0

                        allImages = generics_utils.order_list_object_by(allImages.images.image, "name")

                        if doArgs.name is not None:
                                allImages = compare(allImages, doArgs.name, "name")
                        if doArgs.format is not None:
                                allImages = compare(allImages, doArgs.format, "format", "name")
                        if doArgs.os is not None:
                                allImages = compare(list=allImages, values=doArgs.os, attrName='distributionName', subattrName=None, otherList=appliancesList, linkProperties=['applianceUri', 'uri'])

                        if allImages is None or len(allImages) == 0:
                                printer.out("No images found for user [" + doArgs.account + "] with these filters.")
                                return 0

                        printer.out("Images list :")
                        table = Texttable(200)
                        table.set_cols_align(["l", "l", "l", "l", "l", "l", "l", "l", "l", "l"])
                        table.header(["ID", "Name", "Version", "Rev", "OS", "Format", "Created", "Size", "Compressed", "Status"])
                        for image in allImages:
                                created = image.created.strftime("%Y-%m-%d %H:%M:%S")
                                if image.compress:
                                        compressed = "X"
                                else:
                                        compressed = ""
                                if image.status.error:
                                        status = "Error"
                                elif image.status.cancelled:
                                        status = "Cancelled"
                                elif image.status.complete:
                                        status = "Done"
                                else:
                                        status = "Generating"
                                appliance = self.api.Users(doArgs.account).Appliances(doArgs.id).Get()
                                osImage = appliance.distributionName + " " + appliance.archName
                                table.add_row([image.dbId, image.name, image.version, image.revision, osImage, image.format.name, created, size(image.size), compressed, status])
                        print table.draw() + "\n"

                        printer.out("Found " + str(len(allImages)) + " images.")
                        return 0

                except ArgumentParserError as e:
                        printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
                        self.help_images()
                except Exception as e:
                        return handle_uforge_exception(e)
Exemplo n.º 3
0
        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)
Exemplo n.º 4
0
    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)
Exemplo n.º 5
0
        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_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)
Exemplo n.º 8
0
        def do_list(self, args):
                try:
                        doParser = self.arg_list()
                        doArgs = doParser.parse_args(shlex.split(args))

                        userAppliances = self.api.Users(doArgs.account).Appliances.Getall()
                        if userAppliances.total == 0:
                                printer.out("[" + doArgs.account + "] has no template.")
                                return 0
                        userAppliances = userAppliances.appliances.appliance

                        if doArgs.os is not None:
                                userAppliances = compare(userAppliances, doArgs.os, "distributionName")
                        if doArgs.name is not None:
                                userAppliances = compare(userAppliances, doArgs.name, "name")

                        if len(userAppliances) == 0:
                                printer.out("No match with this filters.")
                                return 0
                        printer.out("List of templates created by [" + doArgs.account + "]:")
                        table = Texttable(200)
                        table.set_cols_align(["l", "l", "l", "l", "l", "l", "l", "l", "l", "l"])
                        table.header(["Id", "Name", "Version", "OS", "Created", "Last Modified", "# Imgs", "Updates", "Imp", "Shd"])
                        for appliance in userAppliances:
                                created = appliance.created.strftime("%Y-%m-%d %H:%M:%S")
                                lastModified = appliance.lastModified.strftime("%Y-%m-%d %H:%M:%S")
                                if appliance.imported:
                                        imported = "X"
                                else:
                                        imported = ""
                                if appliance.shared:
                                        shared = "X"
                                else:
                                        shared = ""
                                table.add_row([appliance.dbId, appliance.name, appliance.version, appliance.distributionName + " " + appliance.archName, created, lastModified, str(len(appliance.images.image)), appliance.nbUpdates, imported, shared])
                        print table.draw() + "\n"

                        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)
Exemplo n.º 9
0
        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)
Exemplo n.º 10
0
        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)
Exemplo n.º 11
0
    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)
Exemplo n.º 12
0
        def do_list(self, args):
                try:
                        doParser = self.arg_list()
                        doArgs = doParser.parse_args(shlex.split(args))

                        printer.out("Getting published images ...")
                        allPimages = self.api.Users(doArgs.account).Pimages.Get()
                        allPimages = allPimages.publishImages.publishImage

                        userAppliances = self.api.Users(doArgs.account).Appliances.Getall()
                        userAppliances = userAppliances.appliances.appliance

                        if allPimages is None or len(allPimages) == 0:
                                printer.out("No publish images for user [" + doArgs.account + "].")
                                return 0

                        if doArgs.name:
                                allPimages = compare(allPimages, doArgs.name, "name")

                        if doArgs.os is not None:
                                allPimages = compare(list=allPimages, values=doArgs.os, attrName='distributionName', subattrName=None, otherList=userAppliances, linkProperties=['applianceUri', 'uri'])

                        if doArgs.cloud is not None:
                                allPimages = compare(list=allPimages, values=doArgs.cloud, attrName='format', subattrName=None, otherList=userAppliances, linkProperties=['applianceUri', 'uri'])

                        table = Texttable(200)
                        table.set_cols_align(["l", "l", "l", "l", "l", "l", "l", "l", "l"])
                        table.header(["Id", "Name", "Version", "Rev", "OS", "Cloud", "Published", "Size", "Status"])
                        for item in allPimages:
                                for item2 in userAppliances:
                                        if item.applianceUri == item2.uri:
                                                os = item2.distributionName + " " + item2.archName
                                table.add_row([item.dbId, item.name, item.version, item.revision, os, item2.format, item.created.strftime("%Y-%m-%d %H:%M:%S"), size(item.size), item.status])
                        print table.draw() + "\n"
                        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_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)
Exemplo n.º 14
0
    def do_remove(self, args):
        try:
            # add arguments
            do_parser = self.arg_remove()
            doArgs = do_parser.parse_args(shlex.split(args))

            printer.out("Getting role [" + doArgs.name + "]...")
            org = org_utils.org_get(self.api, doArgs.org)
            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 0

            new_role = role()
            new_role.name = old_role.name
            new_role.description = old_role.description
            new_role.entitlements = pyxb.BIND()

            delete_roles = compare(r.entitlements.entitlement,
                                   doArgs.entitlements, "name")

            for entitlementItem in r.entitlements.entitlement:
                exist = False
                for deleterole in delete_roles:
                    if entitlementItem.name == deleterole.name:
                        exist = True
                if not exist:
                    already_entitlement = entitlement()
                    already_entitlement.name = entitlementItem.name
                    new_role.entitlements.append(already_entitlement)
                else:
                    printer.out("Removed " + entitlementItem.name +
                                " from role.")

            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_remove()
        except Exception as e:
            return handle_uforge_exception(e)
Exemplo n.º 15
0
    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)
Exemplo n.º 16
0
    def do_remove(self, args):
        try:
            # add arguments
            do_parser = self.arg_remove()
            doArgs = do_parser.parse_args(shlex.split(args))

            printer.out("Getting role [" + doArgs.name + "]...")
            org = org_utils.org_get(self.api, doArgs.org)
            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 0

            new_role = role()
            new_role.name = old_role.name
            new_role.description = old_role.description
            new_role.entitlements = pyxb.BIND()

            delete_roles = compare(r.entitlements.entitlement, doArgs.entitlements, "name")

            for entitlementItem in r.entitlements.entitlement:
                exist = False
                for deleterole in delete_roles:
                    if entitlementItem.name == deleterole.name:
                        exist = True
                if not exist:
                    already_entitlement = entitlement()
                    already_entitlement.name = entitlementItem.name
                    new_role.entitlements.append(already_entitlement)
                else:
                    printer.out("Removed " + entitlementItem.name + " from role.")

            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_remove()
        except Exception as e:
            return handle_uforge_exception(e)
Exemplo n.º 17
0
        def do_addTargetFormat(self, args):
                try:
                        doParser = self.arg_addTargetFormat()
                        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 ["+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

                                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:
                                        targetFormatToAdd = targetFormat()
                                        targetFormatToAdd.name = item.name
                                        result = self.api.Orgs(org.dbId).Targetplatforms(targetPlatform.dbId).Targetformats.Addtargetformat(body=targetFormatToAdd)
                                        printer.out("Target Format ["+targetFormatToAdd.name+"] has successfully been added to Target platform ["+targetPlatform.name+"].", printer.OK)

                        return 0

                except ArgumentParserError as e:
                        printer.out("In Arguments: "+str(e), printer.ERROR)
                        self.help_addTargetFormat()
                except Exception as e:
                        return handle_uforge_exception(e)
Exemplo n.º 18
0
        def do_removeTargetPlatform(self, args):
                try:
                        doParser = self.arg_removeTargetPlatform()
                        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 with id ["+doArgs.id+"] for ["+org.name+"] . . .")
                        targetFormat = self.api.Orgs(org.dbId).Targetformats(doArgs.id).Get()
                        if targetFormat is None:
                                printer.out("Format with id "+ doArgs.id +" does not exist", printer.ERROR)
                                return 2
                        else:
                                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

                                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:
                                        result = self.api.Orgs(org.dbId).Targetformats(doArgs.id).Targetplatforms(item.dbId).Removetargetplatform()
                                        printer.out("Target platform ["+item.name+"] has successfully been removed from Target format ["+targetFormat.name+"].", printer.OK)

                        return 0

                except ArgumentParserError as e:
                        printer.out("In Arguments: "+str(e), printer.ERROR)
                        self.help_removeTargetPlatform()
                except Exception as e:
                        return handle_uforge_exception(e)
Exemplo n.º 19
0
        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)
                        printer.out("Getting format list for ["+org.name+"] . . .")
                        allFormats = self.api.Orgs(org.dbId).Formats.Getall()
                        allFormats = allFormats.imageFormats.imageFormat

                        if len(allFormats) == 0:
                                printer.out("There is no formats in ["+org.name+"].")
                                return 0

                        all_formats = imageFormats()
                        all_formats.imageFormats = pyxb.BIND()

                        allFormats = compare(allFormats, doArgs.format, "name")

                        for item in allFormats:
                                newFormat = imageFormat()
                                newFormat = item
                                newFormat.access = True
                                printer.out("Enabling ["+item.name+"].")
                                all_formats.imageFormats.append(newFormat)

                        result = self.api.Orgs(org.dbId).Formats.Update(body=all_formats)

                        if result is None or len(result.imageFormats.imageFormat) == 0:
                                printer.out("The format(s) selected are already enabled", printer.WARNING)
                                return 0
                        sentenceReturn = "Format(s)"
                        for format in result.imageFormats.imageFormat:
                                sentenceReturn = sentenceReturn + " " + format.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)
Exemplo n.º 20
0
 def test_compare_should_support_wide_card(self):
     candidates = [centos , debian, fedora]
     filtered_elements = compare_utils.compare(candidates, "Deb*", "name")
     self.assertListEqual(filtered_elements, [debian])
Exemplo n.º 21
0
    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 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 = False
                newTargetFormat.access = False
                printer.out("Disabling [" + 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 disabled",
                    printer.WARNING)
                return 0
            sentenceReturn = "Target Format(s)"
            for tf in result.targetFormats.targetFormat:
                sentenceReturn = sentenceReturn + " " + tf.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)
Exemplo n.º 22
0
    def do_removeTargetPlatform(self, args):
        try:
            doParser = self.arg_removeTargetPlatform()
            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 with id [" + doArgs.id +
                        "] for [" + org.name + "] . . .")
            targetFormat = self.api.Orgs(org.dbId).Targetformats(
                doArgs.id).Get()
            if targetFormat is None:
                printer.out("Format with id " + doArgs.id + " does not exist",
                            printer.ERROR)
                return 2
            else:
                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

                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:
                    result = self.api.Orgs(org.dbId).Targetformats(
                        doArgs.id).Targetplatforms(
                            item.dbId).Removetargetplatform()
                    printer.out(
                        "Target platform [" + item.name +
                        "] has successfully been removed from Target format ["
                        + targetFormat.name + "].", printer.OK)

            return 0

        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_removeTargetPlatform()
        except Exception as e:
            return handle_uforge_exception(e)
Exemplo n.º 23
0
    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)
            allDist = self.api.Orgs(org.dbId).Distributions.Getall()

            if allDist is None:
                printer.out(
                    "No distribution found with the currents arguments.")
                return 0

            allDist = allDist.distributions.distribution

            allDist = compare(allDist, doArgs.name, "name")
            if doArgs.version is not None:
                allDist = compare(allDist, doArgs.version, "version")
            if doArgs.arch is not None:
                allDist = compare(allDist, doArgs.arch, "arch")

            newAllDists = distributions()
            newAllDists.distributions = pyxb.BIND()

            for item in allDist:
                newDist = distribution()
                newDist = item
                newDist.active = False
                newAllDists.distributions.append(newDist)

            result = self.api.Orgs(
                org.dbId).Distributions.Update(body=newAllDists)
            if result is None:
                printer.out(
                    "No distribution was updated. It could be that the distributions are already disabled."
                )
                return 0

            table = Texttable(200)
            table.set_cols_align(["l", "l", "l", "l", "l", "l", "l"])
            table.header([
                "Distribution", "Version", "Architecture", "Access", "Visible",
                "Default", "Release Date"
            ])
            printer.out("Distribution(s) has been disabled.", printer.OK)
            for dist in result.distributions.distribution:
                if dist.active:
                    access = "X"
                else:
                    access = ""
                if dist.visible:
                    visible = "X"
                else:
                    visible = ""
                if dist.preselected:
                    default = "X"
                else:
                    default = ""
                if dist.releaseDate is None:
                    date = "Unknown"
                else:
                    date = dist.releaseDate.strftime("%Y-%m-%d %H:%M:%S")
                table.add_row([
                    dist.name, dist.version, dist.arch, access, visible,
                    default, date
                ])

            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)
Exemplo n.º 24
0
 def test_compare_should_support_several_values_as_second_parameter(self):
     candidates = [centos , debian, fedora]
     filtered_elements = compare_utils.compare(candidates, ["CentOS", "Fedora"], "name")
     self.assertListEqual(filtered_elements, [centos, fedora])
Exemplo n.º 25
0
    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 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:
                targetPlatformToEnable = targetPlatform()
                targetPlatformToEnable = item
                targetPlatformToEnable.active = True
                targetPlatformToEnable.access = True
                printer.out("Enabling [" + item.name + "].")
                all_targetplatforms.targetPlatforms.append(
                    targetPlatformToEnable)

            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 enabled",
                    printer.WARNING)
                return 0
            sentenceReturn = "Target Platform(s)"
            for tp in result.targetPlatforms.targetPlatform:
                sentenceReturn = sentenceReturn + " " + tp.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)
Exemplo n.º 26
0
    def do_addTargetFormat(self, args):
        try:
            doParser = self.arg_addTargetFormat()
            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 [" + 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

                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:
                    targetFormatToAdd = targetFormat()
                    targetFormatToAdd.name = item.name
                    result = self.api.Orgs(org.dbId).Targetplatforms(
                        targetPlatform.dbId).Targetformats.Addtargetformat(
                            body=targetFormatToAdd)
                    printer.out(
                        "Target Format [" + targetFormatToAdd.name +
                        "] has successfully been added to Target platform [" +
                        targetPlatform.name + "].", printer.OK)

            return 0

        except ArgumentParserError as e:
            printer.out("In Arguments: " + str(e), printer.ERROR)
            self.help_addTargetFormat()
        except Exception as e:
            return handle_uforge_exception(e)
Exemplo n.º 27
0
    def do_list(self, args):
        try:
            doParser = self.arg_list()
            doArgs = doParser.parse_args(shlex.split(args))

            id = doArgs.account
            imagesList = self.api.Users(id).Images.Get()
            imagesList = imagesList.images.image

            appliancesList = self.api.Users(id).Appliances.Getall()
            appliancesList = appliancesList.appliances.appliance

            if imagesList is None or len(imagesList) == 0:
                printer.out("There is no images for user [" + doArgs.account +
                            "].")
                return 0

            if doArgs.name is not None:
                imagesList = compare(imagesList, doArgs.name, "name")
            if doArgs.format is not None:
                imagesList = compare(imagesList, doArgs.format, "format",
                                     "name")
            if doArgs.os is not None:
                imagesList = compare(list=imagesList,
                                     values=doArgs.os,
                                     attrName='distributionName',
                                     subattrName=None,
                                     otherList=appliancesList,
                                     linkProperties=['applianceUri', 'uri'])

            if len(imagesList) == 0:
                printer.out("There is no images for user [" + doArgs.account +
                            "] with these filters.")
                return 0

            imagesListSorted = order_list_object_by(imagesList, "name")
            printer.out("List of images :", printer.OK)

            table = Texttable(200)
            table.set_cols_align(
                ["c", "c", "c", "c", "c", "c", "c", "c", "c", "c"])
            table.header([
                "ID", "Name", "Version", "Rev", "OS", "Format", "Created",
                "Size", "Compressed", "Status"
            ])
            count = 0
            error = 0
            for item in imagesListSorted:
                count = count + 1
                if not item.compress:
                    compressed = "No"
                else:
                    compressed = "Yes"
                if item.status.error:
                    status = "Error"
                    error = error + 1
                else:
                    status = "Done"
                timeCreated = item.created.strftime("%Y-%m-%d %H:%M:%S")
                for item3 in imagesList:
                    for item2 in appliancesList:
                        if item3.applianceUri == item2.uri:
                            os = "" + item2.distributionName + "  " + item2.archName
                            break
                table.add_row([
                    item.dbId, item.name, item.version, item.revision, os,
                    item.format.name, timeCreated,
                    size(item.size), compressed, status
                ])
            print table.draw() + "\n"

            printer.out(str(count) + " images found.")
            if error != 0:
                printer.out(str(error) + " images with error status.")
            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)
Exemplo n.º 28
0
    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)
            allDist = self.api.Orgs(org.dbId).Distributions.Getall()

            if allDist is None:
                printer.out("No distribution found with the currents arguments.")
                return 0

            allDist = allDist.distributions.distribution

            allDist = compare(allDist, doArgs.name, "name")
            if doArgs.version is not None:
                allDist = compare(allDist, doArgs.version, "version")
            if doArgs.arch is not None:
                allDist = compare(allDist, doArgs.arch, "arch")

            newAllDists = distributions()
            newAllDists.distributions = pyxb.BIND()

            for item in allDist:
                newDist = distribution()
                newDist = item
                newDist.active = False
                newAllDists.distributions.append(newDist)

            result = self.api.Orgs(org.dbId).Distributions.Update(body=newAllDists)
            if result is None:
                printer.out("No distribution was updated. It could be that the distributions are already disabled.")
                return 0

            table = Texttable(200)
            table.set_cols_align(["l", "l", "l", "l", "l", "l", "l"])
            table.header(["Distribution", "Version", "Architecture", "Access", "Visible", "Default", "Release Date"])
            printer.out("Distribution(s) has been disabled.", printer.OK)
            for dist in result.distributions.distribution:
                if dist.active:
                    access = "X"
                else:
                    access = ""
                if dist.visible:
                    visible = "X"
                else:
                    visible = ""
                if dist.preselected:
                    default = "X"
                else:
                    default = ""
                if dist.releaseDate is None:
                    date = "Unknown"
                else:
                    date = dist.releaseDate.strftime("%Y-%m-%d %H:%M:%S")
                table.add_row([dist.name, dist.version, dist.arch, access, visible, default, date])

            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)
Exemplo n.º 29
0
        def do_list(self, args):
                try:
                        doParser = self.arg_list()
                        doArgs = doParser.parse_args(shlex.split(args))

                        id = doArgs.account
                        imagesList = self.api.Users(id).Images.Get()
                        imagesList = imagesList.images.image

                        appliancesList = self.api.Users(id).Appliances.Getall()
                        appliancesList = appliancesList.appliances.appliance

                        if imagesList is None or len(imagesList) == 0:
                                printer.out("There is no images for user [" + doArgs.account + "].")
                                return 0

                        if doArgs.name is not None:
                                imagesList = compare(imagesList, doArgs.name, "name")
                        if doArgs.format is not None:
                                imagesList = compare(imagesList, doArgs.format, "format", "name")
                        if doArgs.os is not None:
                                imagesList = compare(list=imagesList, values=doArgs.os, attrName='distributionName', subattrName=None, otherList=appliancesList, linkProperties=['applianceUri', 'uri'])

                        if len(imagesList) == 0:
                                printer.out("There is no images for user [" + doArgs.account + "] with these filters.")
                                return 0

                        imagesListSorted = order_list_object_by(imagesList, "name")
                        printer.out("List of images :", printer.OK)

                        table = Texttable(200)
                        table.set_cols_align(["c", "c", "c", "c", "c", "c", "c", "c", "c", "c"])
                        table.header(["ID", "Name", "Version", "Rev", "OS", "Format", "Created", "Size", "Compressed", "Status"])
                        count = 0
                        error = 0
                        for item in imagesListSorted:
                                count = count + 1
                                if not item.compress:
                                        compressed = "No"
                                else:
                                        compressed = "Yes"
                                if item.status.error:
                                        status = "Error"
                                        error = error + 1
                                else:
                                        status = "Done"
                                timeCreated = item.created.strftime("%Y-%m-%d %H:%M:%S")
                                for item3 in imagesList:
                                        for item2 in appliancesList:
                                                if item3.applianceUri == item2.uri:
                                                        os = "" + item2.distributionName + "  " + item2.archName
                                                        break
                                table.add_row([item.dbId, item.name, item.version, item.revision, os, item.targetFormat.name, timeCreated, size(item.size), compressed, status])
                        print table.draw() + "\n"

                        printer.out(str(count)+" images found.")
                        if error != 0:
                                printer.out(str(error)+" images with error status.")
                        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)
Exemplo n.º 30
0
        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:
                                targetFormatsUser = self.api.Users(doArgs.account).Targetformats.Getall(org=org.dbId)
                        else:
                                targetFormatsUser = self.api.Users(doArgs.account).Targetformats.Getall()
                        if targetFormatsUser is None or len(targetFormatsUser.targetFormats.targetFormat) == 0:
                                printer.out("There is no target format for the user \""+doArgs.account+"\" in [" + org.name + "].")
                                return 0
                        else:
                                targetFormatsUser = targetFormatsUser.targetFormats.targetFormat

                                targetFormatsList = targetFormats()
                                targetFormatsList.targetFormats = pyxb.BIND()

                                targetFormatsUser = compare(targetFormatsUser, doArgs.targetFormats, "name")

                                if len(targetFormatsUser) == 0:
                                        listName = ""
                                        for tfname in doArgs.targetFormats:
                                                listName = listName + tfname + " "
                                        printer.out("There is no target formats matching ["+listName+"].")
                                        return 0

                                for item in targetFormatsUser:
                                        targetFormatToDisable = targetFormat()
                                        targetFormatToDisable = item
                                        targetFormatToDisable.active = False
                                        targetFormatToDisable.access = False
                                        printer.out("Disabling ["+item.name+"].")
                                        targetFormatsList.targetFormats.append(targetFormatToDisable)

                                result = self.api.Users(doArgs.account).Targetformats.Update(Org=org.name,body=targetFormatsList)
                                result =generics_utils.order_list_object_by(result.targetFormats.targetFormat, "name")

                        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 result:
                                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])

                        printer.out("Target Format 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)
Exemplo n.º 31
0
        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

                        targetFormatsOrg = self.api.Orgs(org.dbId).Targetformats.Getall()
                        if targetFormatsOrg is None or len(targetFormatsOrg.targetFormats.targetFormat) == 0:
                                printer.out("There is no target format for the user \""+doArgs.account+"\" in [" + org.name + "].")
                                return 0
                        else:
                                targetFormatsOrg = targetFormatsOrg.targetFormats.targetFormat

                                targetFormatsList = targetFormats()
                                targetFormatsList.targetFormats = pyxb.BIND()

                                targetFormatsOrg = compare(targetFormatsOrg, doArgs.targetFormats, "name")

                                if len(targetFormatsOrg) == 0:
                                        listName = ""
                                        for tfname in doArgs.targetFormats:
                                                listName = listName + tfname + " "
                                        printer.out("There is no target formats matching ["+listName+"].")
                                        return 0

                                for item in targetFormatsOrg:
                                        targetFormatToEnable = targetFormat()
                                        targetFormatToEnable = item
                                        targetFormatToEnable.active = True
                                        targetFormatToEnable.access = True
                                        printer.out("Enabling ["+item.name+"].")
                                        targetFormatsList.targetFormats.append(targetFormatToEnable)

                                result = self.api.Users(doArgs.account).Targetformats.Update(Org=org.name,body=targetFormatsList)
                                result =generics_utils.order_list_object_by(result.targetFormats.targetFormat, "name")

                                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 result:
                                        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])

                                printer.out("Target Format 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)
Exemplo n.º 33
0
    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)
    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)
            orgTargetFormats = self.api.Orgs(org.dbId).Targetformats.Getall()
            if orgTargetFormats is None:
                printer.out("The organization as no target format available.")
                return 0

            exist = False
            subProfileSelected = None
            for item in subscriptions.subscriptionProfiles.subscriptionProfile:
                if item.name == doArgs.name:
                    exist = True
                    subProfileSelected = item
                    all_targetFormats = targetFormats()
                    all_targetFormats.targetFormats = pyxb.BIND()

                    newTargetFormats = compare(
                        orgTargetFormats.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 nr in newTargetFormats:
                        all_targetFormats.targetFormats.append(nr)
                        printer.out("Added target format " + nr.name +
                                    " for subscription.")

                    self.api.Orgs(org.dbId).Subscriptions(
                        subProfileSelected.dbId
                    ).Targetformats.Orgsubscriptionprofiletargetformatupdate(
                        Allusers=doArgs.allusers, body=all_targetFormats)
                    printer.out(
                        "Some target formats 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)
Exemplo n.º 35
0
 def test_compare_should_retain_only_elements_matching_with_given_value(self):
     candidates = [centos , debian, fedora]
     filtered_elements = compare_utils.compare(candidates, "CentOS", "name")
     self.assertListEqual(filtered_elements, [centos])