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  import copy 
 18  from cm_api.endpoints.types import * 
 19   
 20  __docformat__ = "epytext" 
 21   
 22  HOST_TEMPLATES_PATH = "/clusters/%s/hostTemplates" 
 23  HOST_TEMPLATE_PATH = "/clusters/%s/hostTemplates/%s" 
 24  APPLY_HOST_TEMPLATE_PATH = HOST_TEMPLATE_PATH + "/commands/applyHostTemplate" 
 25   
26 -def create_host_template(resource_root, name, cluster_name):
27 """ 28 Create a host template. 29 @param resource_root: The root Resource object. 30 @param name: Host template name 31 @param cluster_name: Cluster name 32 @return: An ApiHostTemplate object for the created host template. 33 @since: API v3 34 """ 35 apitemplate = ApiHostTemplate(resource_root, name, []) 36 return call(resource_root.post, 37 HOST_TEMPLATES_PATH % (cluster_name,), 38 ApiHostTemplate, True, data=[apitemplate], api_version=3)[0]
39
40 -def get_host_template(resource_root, name, cluster_name):
41 """ 42 Lookup a host template by name in the specified cluster. 43 @param resource_root: The root Resource object. 44 @param name: Host template name. 45 @param cluster_name: Cluster name. 46 @return: An ApiHostTemplate object. 47 @since: API v3 48 """ 49 return call(resource_root.get, 50 HOST_TEMPLATE_PATH % (cluster_name, name), 51 ApiHostTemplate, api_version=3)
52
53 -def get_all_host_templates(resource_root, cluster_name="default"):
54 """ 55 Get all host templates in a cluster. 56 @param cluster_name: Cluster name. 57 @return: ApiList of ApiHostTemplate objects for all host templates in a cluster. 58 @since: API v3 59 """ 60 return call(resource_root.get, 61 HOST_TEMPLATES_PATH % (cluster_name,), 62 ApiHostTemplate, True, api_version=3)
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 @since: API v3 72 """ 73 return call(resource_root.delete, 74 HOST_TEMPLATE_PATH % (cluster_name, name), 75 ApiHostTemplate, api_version=3)
76
77 -def update_host_template(resource_root, name, cluster_name, api_host_template):
78 """ 79 Update a host template identified by name in the specified cluster. 80 @param resource_root: The root Resource object. 81 @param name: Host template name. 82 @param cluster_name: Cluster name. 83 @param api_host_template: The updated host template. 84 @return: The updated ApiHostTemplate. 85 @since: API v3 86 """ 87 return call(resource_root.put, 88 HOST_TEMPLATE_PATH % (cluster_name, name), 89 ApiHostTemplate, data=api_host_template, api_version=3)
90
91 -def apply_host_template(resource_root, name, cluster_name, host_ids, start_roles):
92 """ 93 Apply a host template identified by name on the specified hosts and 94 optionally start them. 95 @param resource_root: The root Resource object. 96 @param name: Host template name. 97 @param cluster_name: Cluster name. 98 @param host_ids: List of host ids. 99 @param start_roles: Whether to start the created roles or not. 100 @return: An ApiCommand object. 101 @since: API v3 102 """ 103 host_refs = [] 104 for host_id in host_ids: 105 host_refs.append(ApiHostRef(resource_root, host_id)) 106 107 params = {"startRoles" : start_roles} 108 return call(resource_root.post, 109 APPLY_HOST_TEMPLATE_PATH % (cluster_name, name), 110 ApiCommand, data=host_refs, params=params, api_version=3)
111 112
113 -class ApiHostTemplate(BaseApiResource):
114 _ATTRIBUTES = { 115 'name' : None, 116 'roleConfigGroupRefs' : Attr(ApiRoleConfigGroupRef), 117 'clusterRef' : ROAttr(ApiClusterRef), 118 } 119
120 - def __init__(self, resource_root, name=None, roleConfigGroupRefs=None):
121 BaseApiObject.init(self, resource_root, locals())
122
123 - def __str__(self):
124 return "<ApiHostTemplate>: %s (cluster %s)" % (self.name, self.clusterRef.clusterName)
125
126 - def _api_version(self):
127 return 3
128
129 - def _path(self):
130 return HOST_TEMPLATE_PATH % (self.clusterRef.clusterName, self.name)
131
132 - def _do_update(self, update):
133 self._update(self._put('', ApiHostTemplate, data=update)) 134 return self
135
136 - def rename(self, new_name):
137 """ 138 Rename a host template. 139 @param new_name: New host template name. 140 @return: An ApiHostTemplate object. 141 """ 142 update = copy.copy(self) 143 update.name = new_name 144 return self._do_update(update)
145
146 - def set_role_config_groups(self, role_config_group_refs):
147 """ 148 Updates the role config groups in a host template. 149 @param role_config_group_refs: List of role config group refs. 150 @return: An ApiHostTemplate object. 151 """ 152 update = copy.copy(self) 153 update.roleConfigGroupRefs = role_config_group_refs 154 return self._do_update(update)
155
156 - def apply_host_template(self, host_ids, start_roles):
157 """ 158 Apply a host template identified by name on the specified hosts and 159 optionally start them. 160 @param host_ids: List of host ids. 161 @param start_roles: Whether to start the created roles or not. 162 @return: An ApiCommand object. 163 """ 164 return apply_host_template(self._get_resource_root(), self.name, self.clusterRef.clusterName, host_ids, start_roles)
165