Skip to content
This repository has been archived by the owner on May 9, 2020. It is now read-only.

Add cookbook support #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions chef/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 62 additions & 0 deletions chef/cookbook.py
Original file line number Diff line number Diff line change
@@ -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])
7 changes: 7 additions & 0 deletions docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ Environments
:members:
:inherited-members:

Cookbooks
---------

.. autoclass :: Cookbooks
:members:
:inherited-members:

Search
------

Expand Down