1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
44 return ApiList.from_json_dict(ApiHost, resp, resource_root)[0]
45
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
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
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
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):
99
101 return "<ApiHost>: %s (%s)" % (self.hostId, self.ipAddress)
102
105
106 - def _cmd(self, cmd, data=None):
110
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
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
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
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
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
195 """
196 Update the rack ID of this host.
197 """
198 self.rackId = rackId
199 self._put()
200