1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17  import datetime 
 18   
 19  from cm_api.endpoints.types import * 
 20   
 21  __docformat__ = "epytext" 
 22   
 23  HOSTS_PATH = "/hosts" 
 24   
 25 -def create_host(resource_root, host_id, name, ipaddr, rack_id=None): 
  26    """ 
 27    Create a host 
 28    @param resource_root: The root Resource object. 
 29    @param host_id: Host id 
 30    @param name: Host name 
 31    @param ipaddr: IP address 
 32    @param rack_id: Rack id. Default None 
 33    @return: An ApiHost object 
 34    """ 
 35    apihost = ApiHost(resource_root, host_id, name, ipaddr, rack_id) 
 36    return call(resource_root.post, HOSTS_PATH, ApiHost, True, data=[apihost])[0] 
  37   
 39    """ 
 40    Lookup a host by id 
 41    @param resource_root: The root Resource object. 
 42    @param host_id: Host id 
 43    @return: An ApiHost object 
 44    """ 
 45    return call(resource_root.get, "%s/%s" % (HOSTS_PATH, host_id), ApiHost) 
  46   
 48    """ 
 49    Get all hosts 
 50    @param resource_root: The root Resource object. 
 51    @return: A list of ApiHost objects. 
 52    """ 
 53    return call(resource_root.get, HOSTS_PATH, ApiHost, True, 
 54            params=view and dict(view=view) or None) 
  55   
 57    """ 
 58    Delete a host by id 
 59    @param resource_root: The root Resource object. 
 60    @param host_id: Host id 
 61    @return: The deleted ApiHost object 
 62    """ 
 63    return call(resource_root.delete, "%s/%s" % (HOSTS_PATH, host_id), ApiHost) 
  64   
 65   
 67    _ATTRIBUTES = { 
 68      'hostId'            : None, 
 69      'hostname'          : None, 
 70      'ipAddress'         : None, 
 71      'rackId'            : None, 
 72      'status'            : ROAttr(), 
 73      'lastHeartbeat'     : ROAttr(datetime.datetime), 
 74      'roleRefs'          : ROAttr(ApiRoleRef), 
 75      'healthSummary'     : ROAttr(), 
 76      'healthChecks'      : ROAttr(), 
 77      'hostUrl'           : ROAttr(), 
 78      'commissionState'   : ROAttr(), 
 79      'maintenanceMode'   : ROAttr(), 
 80      'maintenanceOwners' : ROAttr(), 
 81      'numCores'          : ROAttr(), 
 82      'totalPhysMemBytes' : ROAttr(), 
 83    } 
 84   
 85 -  def __init__(self, resource_root, hostId=None, hostname=None, 
 86        ipAddress=None, rackId=None): 
  88   
 90      return "<ApiHost>: %s (%s)" % (self.hostId, self.ipAddress) 
  91   
 94   
 96      """ 
 97      Update this resource. 
 98      @return: The updated object. 
 99      """ 
100      return self._put('', ApiHost, data=self) 
 101   
103      """ 
104      Retrieve the host's configuration. 
105   
106      The 'summary' view contains strings as the dictionary values. The full 
107      view contains ApiConfig instances as the values. 
108   
109      @param view: View to materialize ('full' or 'summary') 
110      @return: Dictionary with configuration data. 
111      """ 
112      return self._get_config("config", view) 
 113   
115      """ 
116      Update the host's configuration. 
117   
118      @param config: Dictionary with configuration to update. 
119      @return: Dictionary with updated configuration. 
120      """ 
121      return self._update_config("config", config) 
 122   
123 -  def get_metrics(self, from_time=None, to_time=None, metrics=None, 
124        ifs=[], storageIds=[], view=None): 
 125      """ 
126      This endpoint is not supported as of v6. Use the timeseries API 
127      instead. To get all metrics for a host with the timeseries API use 
128      the query: 
129   
130      'select * where hostId = $HOST_ID'. 
131   
132      To get specific metrics for a host use a comma-separated list of 
133      the metric names as follows: 
134   
135      'select $METRIC_NAME1, $METRIC_NAME2 where hostId = $HOST_ID'. 
136   
137      For more information see http://tiny.cloudera.com/tsquery_doc 
138      @param from_time: A datetime; start of the period to query (optional). 
139      @param to_time: A datetime; end of the period to query (default = now). 
140      @param metrics: List of metrics to query (default = all). 
141      @param ifs: network interfaces to query. Default all, use None to disable. 
142      @param storageIds: storage IDs to query. Default all, use None to disable. 
143      @param view: View to materialize ('full' or 'summary') 
144      @return: List of metrics and their readings. 
145      """ 
146      params = { } 
147      if ifs: 
148        params['ifs'] = ifs 
149      elif ifs is None: 
150        params['queryNw'] = 'false' 
151      if storageIds: 
152        params['storageIds'] = storageIds 
153      elif storageIds is None: 
154        params['queryStorage'] = 'false' 
155      return self._get_resource_root().get_metrics(self._path() + '/metrics', 
156          from_time, to_time, metrics, view, params) 
 157   
159      """ 
160      Put the host in maintenance mode. 
161   
162      @return: Reference to the completed command. 
163      @since: API v2 
164      """ 
165      cmd = self._cmd('enterMaintenanceMode') 
166      if cmd.success: 
167        self._update(get_host(self._get_resource_root(), self.hostId)) 
168      return cmd 
 169   
171      """ 
172      Take the host out of maintenance mode. 
173   
174      @return: Reference to the completed command. 
175      @since: API v2 
176      """ 
177      cmd = self._cmd('exitMaintenanceMode') 
178      if cmd.success: 
179        self._update(get_host(self._get_resource_root(), self.hostId)) 
180      return cmd 
 181   
183      """ 
184      Update the rack ID of this host. 
185      """ 
186      self.rackId = rackId 
187      self._put() 
  188