Package cm_api :: Package endpoints :: Module host_templates
[hide private]
[frames] | no frames]

Source Code for Module cm_api.endpoints.host_templates

  1  # Licensed to Cloudera, Inc. under one 
  2  # or more contributor license agreements.  See the NOTICE file 
  3  # distributed with this work for additional information 
  4  # regarding copyright ownership.  Cloudera, Inc. licenses this file 
  5  # to you under the Apache License, Version 2.0 (the 
  6  # "License"); you may not use this file except in compliance 
  7  # with the License.  You may obtain a copy of the License at 
  8  # 
  9  #     http://www.apache.org/licenses/LICENSE-2.0 
 10  # 
 11  # Unless required by applicable law or agreed to in writing, software 
 12  # distributed under the License is distributed on an "AS IS" BASIS, 
 13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 14  # See the License for the specific language governing permissions and 
 15  # limitations under the License. 
 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   
30 -def create_host_template(resource_root, name, cluster_name):
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
44 -def get_host_template(resource_root, name, cluster_name):
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
55 -def get_all_host_templates(resource_root, cluster_name="default"):
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
64 -def delete_host_template(resource_root, name, cluster_name):
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
75 -def update_host_template(resource_root, name, cluster_name, api_host_template):
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
88 -def apply_host_template(resource_root, name, cluster_name, host_ids, start_roles):
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
109 -class ApiHostTemplate(BaseApiObject):
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):
117 BaseApiObject.init(self, resource_root, locals())
118
119 - def __str__(self):
120 return "<ApiHostTemplate>: %s (cluster %s)" % (self.name, self.clusterRef.clusterName)
121
122 - def _path(self):
123 return HOST_TEMPLATE_PATH % (self.clusterRef.clusterName, self.name)
124
125 - def _put(self, dic):
126 resp = self._get_resource_root().put(self._path(), data=json.dumps(dic)) 127 host_template = ApiHostTemplate.from_json_dict(resp, self._get_resource_root()) 128 129 self._update(host_template) 130 return self
131
132 - def rename(self, new_name):
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
142 - def set_role_config_groups(self, role_config_group_refs):
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
152 - def apply_host_template(self, host_ids, start_roles):
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