1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 try:
18 import json
19 except ImportError:
20 import simplejson as json
21
22 from cm_api.endpoints.types import *
23
24 __docformat__ = "epytext"
25
26 HOST_TEMPLATES_PATH = "/clusters/%s/hostTemplates"
27 HOST_TEMPLATE_PATH = "/clusters/%s/hostTemplates/%s"
28 APPLY_HOST_TEMPLATE_PATH = HOST_TEMPLATE_PATH + "/commands/applyHostTemplate"
29
31 """
32 Create a host template.
33 @param resource_root: The root Resource object.
34 @param name: Host template name
35 @param cluster_name: Cluster name
36 @return: An ApiHostTemplate object for the created host template.
37 """
38 apitemplate = ApiHostTemplate(resource_root, name, [])
39 apitemplate_list = ApiList([apitemplate])
40 body = json.dumps(apitemplate_list.to_json_dict())
41 resp = resource_root.post(HOST_TEMPLATES_PATH % (cluster_name,), data=body)
42 return ApiList.from_json_dict(ApiHostTemplate, resp, resource_root)[0]
43
45 """
46 Lookup a host template by name in the specified cluster.
47 @param resource_root: The root Resource object.
48 @param name: Host template name.
49 @param cluster_name: Cluster name.
50 @return An ApiHostTemplate object.
51 """
52 resp = resource_root.get(HOST_TEMPLATE_PATH % (cluster_name, name))
53 return ApiHostTemplate.from_json_dict(resp, resource_root)
54
56 """
57 Get all host templates in a cluster.
58 @param cluster_name: Cluster name.
59 @return ApiList of ApiHostTemplate objects for all host templates in a cluster.
60 """
61 resp = resource_root.get(HOST_TEMPLATES_PATH % (cluster_name,))
62 return ApiList.from_json_dict(ApiHostTemplate, resp, resource_root)
63
65 """
66 Delete a host template identified by name in the specified cluster.
67 @param resource_root: The root Resource object.
68 @param name: Host template name.
69 @param cluster_name: Cluster name.
70 @return The deleted ApiHostTemplate object.
71 """
72 resp = resource_root.delete(HOST_TEMPLATE_PATH % (cluster_name, name))
73 return ApiHostTemplate.from_json_dict(resp, resource_root)
74
76 """
77 Update a host template identified by name in the specified cluster.
78 @param resource_root: The root Resource object.
79 @param name: Host template name.
80 @param cluster_name: Cluster name.
81 @param api_host_template: The updated host template.
82 @return: The updated ApiHostTemplate.
83 """
84 body = json.dumps(api_host_template.to_json_dict())
85 resp = resource_root.put(HOST_TEMPLATE_PATH % (cluster_name, name), body)
86 return ApiHostTemplate.from_json_dict(resp, resource_root)
87
89 """
90 Apply a host template identified by name on the specified hosts and
91 optionally start them.
92 @param resource_root: The root Resource object.
93 @param name: Host template name.
94 @param cluster_name: Cluster name.
95 @param host_ids: List of host ids.
96 @param start_roles: Whether to start the created roles or not.
97 @return: An ApiCommand object.
98 """
99 host_refs = []
100 for host_id in host_ids:
101 host_refs.append(ApiHostRef(resource_root, host_id))
102
103 params = {"startRoles" : start_roles}
104 body = json.dumps(ApiList(host_refs).to_json_dict())
105 resp = resource_root.post(APPLY_HOST_TEMPLATE_PATH % (cluster_name, name), params=params, data=body)
106 return ApiCommand.from_json_dict(resp, resource_root)
107
108
110 _ATTRIBUTES = {
111 'name' : None,
112 'roleConfigGroupRefs' : Attr(ApiRoleConfigGroupRef),
113 'clusterRef' : ROAttr(ApiClusterRef),
114 }
115
116 - def __init__(self, resource_root, name=None, roleConfigGroupRefs=None):
118
120 return "<ApiHostTemplate>: %s (cluster %s)" % (self.name, self.clusterRef.clusterName)
121
124
125 - def _put(self, dic):
131
133 """
134 Rename a host template.
135 @param new_name: New host template name.
136 @return: An ApiHostTemplate object.
137 """
138 dic = self.to_json_dict()
139 dic['name'] = new_name
140 return self._put(dic)
141
143 """
144 Updates the role config groups in a host template.
145 @param role_config_group_refs: List of role config group refs.
146 @return: An ApiHostTemplate object.
147 """
148 dic = self.to_json_dict()
149 dic['roleConfigGroupRefs'] = [ x.to_json_dict() for x in role_config_group_refs ]
150 return self._put(dic)
151
153 """
154 Apply a host template identified by name on the specified hosts and
155 optionally start them.
156 @param host_ids: List of host ids.
157 @param start_roles: Whether to start the created roles or not.
158 @return: An ApiCommand object.
159 """
160 return apply_host_template(self._get_resource_root(), self.name, self.clusterRef.clusterName, host_ids, start_roles)
161