This repository was archived by the owner on May 9, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathcookbook.py
More file actions
62 lines (54 loc) · 1.78 KB
/
cookbook.py
File metadata and controls
62 lines (54 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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])