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

Source Code for Module cm_api.endpoints.hosts

  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 datetime 
 18  try: 
 19    import json 
 20  except ImportError: 
 21    import simplejson as json 
 22   
 23  from cm_api.endpoints.types import * 
 24   
 25  __docformat__ = "epytext" 
 26   
 27  HOSTS_PATH = "/hosts" 
 28   
29 -def create_host(resource_root, host_id, name, ipaddr, rack_id=None):
30 """ 31 Create a host 32 @param resource_root: The root Resource object. 33 @param host_id: Host id 34 @param name: Host name 35 @param ipaddr: IP address 36 @param rack_id: Rack id. Default None 37 @return: An ApiHost object 38 """ 39 apihost = ApiHost(resource_root, host_id, name, ipaddr, rack_id) 40 apihost_list = ApiList([apihost]) 41 body = json.dumps(apihost_list.to_json_dict()) 42 resp = resource_root.post(HOSTS_PATH, data=body) 43 # The server returns a list of created hosts (with size 1) 44 return ApiList.from_json_dict(ApiHost, resp, resource_root)[0]
45
46 -def get_host(resource_root, host_id):
47 """ 48 Lookup a host by id 49 @param resource_root: The root Resource object. 50 @param host_id: Host id 51 @return: An ApiHost object 52 """ 53 dic = resource_root.get("%s/%s" % (HOSTS_PATH, host_id)) 54 return ApiHost.from_json_dict(dic, resource_root)
55
56 -def get_all_hosts(resource_root, view=None):
57 """ 58 Get all hosts 59 @param resource_root: The root Resource object. 60 @return: A list of ApiHost objects. 61 """ 62 dic = resource_root.get(HOSTS_PATH, 63 params=view and dict(view=view) or None) 64 return ApiList.from_json_dict(ApiHost, dic, resource_root)
65
66 -def delete_host(resource_root, host_id):
67 """ 68 Delete a host by id 69 @param resource_root: The root Resource object. 70 @param host_id: Host id 71 @return: The deleted ApiHost object 72 """ 73 resp = resource_root.delete("%s/%s" % (HOSTS_PATH, host_id)) 74 return ApiHost.from_json_dict(resp, resource_root)
75 76
77 -class ApiHost(BaseApiObject):
78 _ATTRIBUTES = { 79 'hostId' : None, 80 'hostname' : None, 81 'ipAddress' : None, 82 'rackId' : None, 83 'status' : ROAttr(), 84 'lastHeartbeat' : ROAttr(datetime.datetime), 85 'roleRefs' : ROAttr(ApiRoleRef), 86 'healthSummary' : ROAttr(), 87 'healthChecks' : ROAttr(), 88 'hostUrl' : ROAttr(), 89 'commissionState' : ROAttr(), 90 'maintenanceMode' : ROAttr(), 91 'maintenanceOwners' : ROAttr(), 92 'numCores' : ROAttr(), 93 'totalPhysMemBytes' : ROAttr(), 94 } 95
96 - def __init__(self, resource_root, hostId=None, hostname=None, 97 ipAddress=None, rackId=None):
98 BaseApiObject.init(self, resource_root, locals())
99
100 - def __str__(self):
101 return "<ApiHost>: %s (%s)" % (self.hostId, self.ipAddress)
102
103 - def _path(self):
104 return HOSTS_PATH + '/' + self.hostId
105
106 - def _cmd(self, cmd, data=None):
107 path = self._path() + '/commands/' + cmd 108 resp = self._get_resource_root().post(path, data=data) 109 return ApiCommand.from_json_dict(resp, self._get_resource_root())
110
111 - def _put(self):
112 """ 113 Update this resource. 114 @return: The updated object. 115 """ 116 return self._resource_root.put(self._path(), 117 data=json.dumps(self.to_json_dict()))
118
119 - def get_config(self, view=None):
120 """ 121 Retrieve the host's configuration. 122 123 The 'summary' view contains strings as the dictionary values. The full 124 view contains ApiConfig instances as the values. 125 126 @param view: View to materialize ('full' or 'summary') 127 @return Dictionary with configuration data. 128 """ 129 path = self._path() + '/config' 130 resp = self._get_resource_root().get(path, 131 params = view and dict(view=view) or None) 132 return json_to_config(resp, view == 'full')
133
134 - def update_config(self, config):
135 """ 136 Update the host's configuration. 137 138 @param config Dictionary with configuration to update. 139 @return Dictionary with updated configuration. 140 """ 141 path = self._path() + '/config' 142 resp = self._get_resource_root().put(path, data = config_to_json(config)) 143 return json_to_config(resp)
144
145 - def get_metrics(self, from_time=None, to_time=None, metrics=None, 146 ifs=[], storageIds=[], view=None):
147 """ 148 Retrieve metric readings for the host. 149 150 @param from_time: A datetime; start of the period to query (optional). 151 @param to_time: A datetime; end of the period to query (default = now). 152 @param metrics: List of metrics to query (default = all). 153 @param ifs: network interfaces to query. Default all, use None to disable. 154 @param storageIds: storage IDs to query. Default all, use None to disable. 155 @param view: View to materialize ('full' or 'summary') 156 @return List of metrics and their readings. 157 """ 158 params = { } 159 if ifs: 160 params['ifs'] = ifs 161 elif ifs is None: 162 params['queryNw'] = 'false' 163 if storageIds: 164 params['storageIds'] = storageIds 165 elif storageIds is None: 166 params['queryStorage'] = 'false' 167 return self._get_resource_root().get_metrics(self._path() + '/metrics', 168 from_time, to_time, metrics, view, params)
169
170 - def enter_maintenance_mode(self):
171 """ 172 Put the host in maintenance mode. 173 174 @return: Reference to the completed command. 175 @since: API v2 176 """ 177 cmd = self._cmd('enterMaintenanceMode') 178 if cmd.success: 179 self._update(get_host(self._get_resource_root(), self.hostId)) 180 return cmd
181
182 - def exit_maintenance_mode(self):
183 """ 184 Take the host out of maintenance mode. 185 186 @return: Reference to the completed command. 187 @since: API v2 188 """ 189 cmd = self._cmd('exitMaintenanceMode') 190 if cmd.success: 191 self._update(get_host(self._get_resource_root(), self.hostId)) 192 return cmd
193
194 - def set_rack_id(self, rackId):
195 """ 196 Update the rack ID of this host. 197 """ 198 self.rackId = rackId 199 self._put()
200