From d91cdda9e6745b58683a4c23f00f5cb7e3b4aaa5 Mon Sep 17 00:00:00 2001 From: "Gavin M. Roy" Date: Sat, 20 Jul 2013 03:04:45 -0400 Subject: [PATCH] Add cookbook support An annoying nit is that due to the class level attributes attribute, we can not use the attributes attribute for cookbook attributes. --- chef/__init__.py | 1 + chef/cookbook.py | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ docs/api.rst | 7 ++++++ 3 files changed, 70 insertions(+) create mode 100644 chef/cookbook.py diff --git a/chef/__init__.py b/chef/__init__.py index fccae67..a8afad5 100644 --- a/chef/__init__.py +++ b/chef/__init__.py @@ -6,6 +6,7 @@ from chef.client import Client from chef.data_bag import DataBag, DataBagItem from chef.exceptions import ChefError +from chef.cookbook import Cookbook from chef.node import Node from chef.role import Role from chef.environment import Environment diff --git a/chef/cookbook.py b/chef/cookbook.py new file mode 100644 index 0000000..af76397 --- /dev/null +++ b/chef/cookbook.py @@ -0,0 +1,62 @@ +from chef.base import ChefObject +from chef.api import ChefAPI +from chef.exceptions import * + + +class Cookbook(ChefObject): + """A Chef cookbook object. + + .. versionadded:: 0.2.3 + """ + url = '/cookbooks' + api_version = '0.10' + attributes = { + 'url': str, + 'definitions': list, + 'files': list, + 'libraries': list, + 'metadata': dict, + 'providers': list, + 'recipes': list, + 'resources': list, + 'root_files': list, + 'templates': list, + 'version': str} + + def __init__(self, name, version=None, api=None, skip_load=False): + self.name = name + self.api = api or ChefAPI.get_global() + self._check_api_version(self.api) + + self._versions = Cookbook.versions(name, + self.api) if not skip_load else [] + + self.url = (self.__class__.url + '/' + self.name + '/' + + (version or self.latest_version)) + self.exists = False + data = {} + if not skip_load: + try: + data = self.api[self.url] + except ChefServerNotFoundError: + pass + else: + self.exists = True + self._populate(data) + + @property + def latest_version(self): + return self._versions[-1] if self._versions else None + + @classmethod + def versions(cls, name, api=None): + """Return a :list: of versions for the specified cookbook + """ + api = api or ChefAPI.get_global() + cls._check_api_version(api) + try: + data = api[Cookbook.url + '/' + name].get(name) + except ChefServerNotFoundError: + return [] + items = data.get('versions', []) if data else [] + return sorted([item['version'] for item in items]) diff --git a/docs/api.rst b/docs/api.rst index bdd6449..f9835e8 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -47,6 +47,13 @@ Environments :members: :inherited-members: +Cookbooks +--------- + +.. autoclass :: Cookbooks + :members: + :inherited-members: + Search ------