示例#1
0
    def handle_category(self, category_type, category_name=None):
        """Ensure a category is present."""
        if self.env.get(category_type):
            category_name = self.env.get(category_type)

        if category_name is not None:
            try:
                category = self.jss.Category(category_name)
                category_name = category.name
                self.output(
                    "Category type: %s-'%s' already exists according to JSS, "
                    "moving on..." % (category_type, category_name))
            except jss.GetError:
                # Category doesn't exist
                category = jss.Category(self.jss, category_name)
                category.save()
                self.output(
                    "Category type: %s-'%s' created." % (category_type,
                                                         category_name))
                self.env["jss_changed_objects"]["jss_category_added"].append(
                    category_name)
        else:
            category = None

        return category
示例#2
0
def ensure_categories(j, categories):
    """Given a list of categories, ensure that they all exist on the
    JSS.

    """
    print("Ensuring categories exist...")
    category_set = set(categories)
    for category in category_set:
        try:
            j.Category(category)
            print("Category %s exists..." % category)
        except jss.exceptions.JSSGetError:
            jss.Category(j, category).save()
            print("Category %s created." % category)