oc-cdtapi 3.9.1__py2-none-any.whl → 3.9.4__py2-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
oc_cdtapi/ForemanAPI.py CHANGED
@@ -9,14 +9,10 @@ from oc_cdtapi.API import HttpAPI, HttpAPIError
9
9
  from collections import namedtuple, defaultdict
10
10
  from datetime import datetime, timedelta
11
11
 
12
- if sys.version_info.major == 3:
13
- from urllib.parse import urljoin
14
- else:
15
- from urlparse import urljoin
16
-
17
12
  class ForemanAPIError(HttpAPIError):
18
13
  pass
19
14
 
15
+
20
16
  class ForemanAPI(HttpAPI):
21
17
  """
22
18
  A simple client for Foreman's REST API
@@ -37,74 +33,68 @@ class ForemanAPI(HttpAPI):
37
33
  location_id = 5
38
34
  hostgroup = 11
39
35
  deploy_on = 1
40
-
36
+ self.apiversion = int(os.getenv('FOREMAN_API_VERSION', "1") or "1")
41
37
  self.defs = class_defaults(exp_date, location_id, hostgroup, deploy_on)
42
38
 
43
- if os.environ.get ('FOREMAN_API_VERSION'):
44
- self.apiversion = int (os.environ.get ('FOREMAN_API_VERSION') )
45
- else:
46
- self.apiversion = 1
47
39
 
48
-
49
40
  def re(self, req):
50
- return urljoin (self.root, posixpath.join ("api", req) )
41
+ return posixpath.join(self.root, "api", req)
51
42
 
52
-
53
- def get_environment (self, env_name):
43
+ def get_environment(self, env_name):
54
44
  """
55
45
  wrapper for api v1/v2
56
46
  """
57
- logging.debug ('Reached get_environment')
58
- logging.debug ('env_name = [%s]' % env_name)
47
+ logging.debug('Reached get_environment')
48
+ logging.debug('env_name = [%s]' % env_name)
59
49
  if self.apiversion == 1:
60
- logging.error ('Not supported in v1, returning None')
50
+ logging.error('Not supported in v1, returning None')
61
51
  return None
62
52
  elif self.apiversion == 2:
63
- logging.debug ('Passing to get_environment_v2')
64
- return self.get_environment_v2 (env_name)
65
-
53
+ logging.debug('Passing to get_environment_v2')
54
+ return self.get_environment_v2(env_name)
66
55
 
67
- def get_environment_v2 (self, env_name):
56
+ def get_environment_v2(self, env_name):
68
57
  """
69
58
  returns environment id for env_name
70
59
  """
71
- logging.debug ('Reached get_environment_v2')
72
- logging.debug ('env_name = [%s]' % env_name)
60
+ logging.debug('Reached get_environment_v2')
61
+ logging.debug('env_name = [%s]' % env_name)
73
62
  params = {'search': 'name=%s' % env_name}
74
- response = self.get ('environments', params=params).json ()
75
- results = response.get ('results')
63
+ response = self.get('environments', params=params).json()
64
+ results = response.get('results')
76
65
  for result in results:
77
- if result.get ('name') == env_name:
78
- env_id = result.get ('id')
79
- logging.debug ('Found environment [%s]' % env_id)
80
- return env_id
81
- logging.error ('Could not find environment for [%s], returning None' % env_name)
82
- return None
66
+ if result.get('name') != env_name:
67
+ continue
83
68
 
69
+ env_id = result.get('id')
70
+ logging.debug('Found environment [%s]' % env_id)
71
+ return env_id
84
72
 
85
- def get_owner (self, user_login):
73
+ logging.error('Could not find environment for [%s], returning None' % env_name)
74
+ return None
75
+
76
+ def get_owner(self, user_login):
86
77
  """
87
78
  wrapper for api v1/v2
88
79
  """
89
- logging.debug ('Reached get_owner')
80
+ logging.debug('Reached get_owner')
90
81
  if self.apiversion == 1:
91
- logging.debug ('Passing to get_owner_v1')
92
- return self.get_owner_v1 (user_login)
82
+ logging.debug('Passing to get_owner_v1')
83
+ return self.get_owner_v1(user_login)
93
84
  elif self.apiversion == 2:
94
- logging.debug ('Passing to get_owner_v2')
95
- return self.get_owner_v2 (user_login)
96
-
85
+ logging.debug('Passing to get_owner_v2')
86
+ return self.get_owner_v2(user_login)
97
87
 
98
- def get_owner_v1 (self, user_login):
88
+ def get_owner_v1(self, user_login):
99
89
  """
100
90
  Looks for user id in the Foreman DB
101
91
  """
102
- logging.debug ('Reached get_owner_v1')
92
+ logging.debug('Reached get_owner_v1')
103
93
  try:
104
94
  params = {'search': 'login=%s' % user_login}
105
95
  response = self.get('users', params=params)
106
96
  except ForemanAPIError as err:
107
- raise(err)
97
+ raise (err)
108
98
 
109
99
  data = json.loads(response.text)
110
100
 
@@ -115,44 +105,40 @@ class ForemanAPI(HttpAPI):
115
105
 
116
106
  return user_id
117
107
 
118
-
119
- def get_owner_v2 (self, user_login):
108
+ def get_owner_v2(self, user_login):
120
109
  """
121
110
  did not change since v1
122
111
  """
123
- logging.debug ('Reached get_owner_v2')
124
- logging.debug ('Passing to get_owner_v1')
125
- return self.get_owner_v1 (user_login)
126
-
112
+ logging.debug('Reached get_owner_v2')
113
+ logging.debug('Passing to get_owner_v1')
114
+ return self.get_owner_v1(user_login)
127
115
 
128
- def get_usergroup_id (self, group_name):
116
+ def get_usergroup_id(self, group_name):
129
117
  """
130
118
  wrapper for api v1/v2
131
119
  """
132
- logging.debug ('Reached get_usergroup_id')
120
+ logging.debug('Reached get_usergroup_id')
133
121
  if self.apiversion == 1:
134
- logging.debug ('Passing to get_usergroup_id_v1')
135
- return self.get_usergroup_id_v1 (group_name)
122
+ logging.debug('Passing to get_usergroup_id_v1')
123
+ return self.get_usergroup_id_v1(group_name)
136
124
  if self.apiversion == 2:
137
- logging.debug ('Passing to get_usergroup_id_v2')
138
- return self.get_usergroup_id_v2 (group_name)
125
+ logging.debug('Passing to get_usergroup_id_v2')
126
+ return self.get_usergroup_id_v2(group_name)
139
127
 
140
-
141
- def get_usergroup_id_v1 (self, group_name):
128
+ def get_usergroup_id_v1(self, group_name):
142
129
  """
143
130
  Looks for usergroup id in the Foreman DB
144
131
  """
145
- logging.debug ('Reached get_usergroup_id_v1')
132
+ logging.debug('Reached get_usergroup_id_v1')
146
133
  if re.search("\s", group_name):
147
134
  group_name = "%22{}%22".format(re.sub("\s", "%20", group_name))
148
- try:
149
- params = {'search': 'name=%s' % group_name}
150
- response = self.get('usergroups', params=params)
151
- except ForemanAPIError as err:
152
- raise(err)
135
+
136
+ # removed catching error since it is raised again
137
+ params = {'search': 'name=%s' % group_name}
138
+ response = self.get('usergroups', params=params)
153
139
 
154
140
  data = response.json()
155
-
141
+
156
142
  try:
157
143
  group_id = data["results"][0]["id"]
158
144
  except IndexError:
@@ -160,58 +146,60 @@ class ForemanAPI(HttpAPI):
160
146
 
161
147
  return group_id
162
148
 
163
-
164
- def get_usergroup_id_v2 (self, group_name):
149
+ def get_usergroup_id_v2(self, group_name):
165
150
  """
166
151
  Looks for usergroup id in the Foreman DB
167
152
  """
168
- logging.debug ('Reached get_usergroup_id_v2')
169
- logging.debug ('Passing to get_usergroup_id_v1')
170
- return self.get_usergroup_id_v1 (group_name)
171
-
153
+ logging.debug('Reached get_usergroup_id_v2')
154
+ logging.debug('Passing to get_usergroup_id_v1')
155
+ return self.get_usergroup_id_v1(group_name)
172
156
 
173
157
  def _set_expiration(self):
174
158
  """
175
- A private method which sets the default expiration date (1 year from the current date)
159
+ A private method which sets the default expiration date (3 months from the current date)
176
160
  """
177
- logging.debug ('Reached _set_expiration')
161
+ logging.debug('Reached _set_expiration')
178
162
  return str((datetime.now() + timedelta(days=90)).strftime('%d/%m/%Y'))
179
163
 
180
-
181
- def create_host (self, hostname = None, cores = 1, memory = 4096, disk = 50, owner_id = None,
182
- exp_date = None, location_id = None, hostgroup = None,
183
- deploy_on = None, custom_json = None):
164
+ def create_host(self, hostname=None, cores=1, memory=4096, disk=50, owner_id=None,
165
+ exp_date=None, location_id=None, hostgroup=None,
166
+ deploy_on=None, custom_json=None):
184
167
  """
185
168
  wrapper for api v1/v2
186
169
  """
187
- logging.debug ('Reached create_host')
188
- logging.debug ('hostname = [%s]' % hostname)
189
- logging.debug ('cores = [%s]' % cores)
190
- logging.debug ('memory = [%s]' % memory)
170
+ logging.debug('Reached create_host')
171
+ logging.debug('hostname = [%s]' % hostname)
172
+ logging.debug('cores = [%s]' % cores)
173
+ logging.debug('memory = [%s]' % memory)
174
+
191
175
  if self.apiversion == 1:
192
- logging.debug ('Passing to create_host_v1')
193
- self.create_host_v1 (hostname, cores, memory, disk, owner_id,
194
- exp_date, location_id, hostgroup,
195
- deploy_on, custom_json)
176
+ logging.debug('Passing to create_host_v1')
177
+ self.create_host_v1(hostname, cores, memory, disk, owner_id,
178
+ exp_date, location_id, hostgroup,
179
+ deploy_on, custom_json)
196
180
  elif self.apiversion == 2:
197
- logging.debug ('Passing to create_host_v2')
198
- self.create_host_v2 (hostname, custom_json)
181
+ logging.debug('Passing to create_host_v2')
182
+ self.create_host_v2(hostname, custom_json)
199
183
 
200
-
201
- def create_host_v1 (self, hostname, cores, memory, disk, owner_id,
202
- exp_date, location_id, hostgroup,
203
- deploy_on, custom_json):
184
+ def create_host_v1(self, hostname, cores, memory, disk, owner_id,
185
+ exp_date, location_id, hostgroup,
186
+ deploy_on, custom_json):
204
187
  """
205
188
  Creates a host using the default parameters or the ones from an external json
206
- note that create_vm in engine actually sends db_task instead of hostname and custom_json, other parms are ignored
189
+ note that create_vm in engine actually sends db_task instead of hostname and custom_json,
190
+ other parms are ignored
207
191
  """
208
- logging.debug ('Reached create_host_v1')
209
- logging.debug ('hostname = [%s]' % hostname)
192
+ logging.debug('Reached create_host_v1')
193
+ logging.debug('hostname = [%s]' % hostname)
210
194
 
211
- if not exp_date: exp_date = self.defs.exp_date
212
- if not location_id: location_id = self.defs.location_id
213
- if not hostgroup: hostgroup = self.defs.hostgroup
214
- if not deploy_on: deploy_on = self.defs.deploy_on
195
+ if not exp_date:
196
+ exp_date = self.defs.exp_date
197
+ if not location_id:
198
+ location_id = self.defs.location_id
199
+ if not hostgroup:
200
+ hostgroup = self.defs.hostgroup
201
+ if not deploy_on:
202
+ deploy_on = self.defs.deploy_on
215
203
 
216
204
  default_params = {
217
205
  "name": hostname,
@@ -243,16 +231,15 @@ class ForemanAPI(HttpAPI):
243
231
  if not default_params["name"]:
244
232
  raise ForemanAPIError("The hostname is not specified")
245
233
 
246
- logging.debug ("ForemanAPI is about to send the following payload:")
247
- logging.debug (default_params)
248
- request = self.post("hosts", headers = self.headers, json = default_params)
249
-
234
+ logging.debug("ForemanAPI is about to send the following payload:")
235
+ logging.debug(default_params)
236
+ request = self.post("hosts", headers=self.headers, json=default_params)
250
237
 
251
- def create_host_v2 (self, task, custom_json):
238
+ def create_host_v2(self, task, custom_json):
252
239
 
253
- logging.debug ('Reached create_host_v2')
254
- logging.debug ('task = [%s]' % task)
255
- hostname = task ['task_content'] ['resources'] ['name']
240
+ logging.debug('Reached create_host_v2')
241
+ logging.debug('task = [%s]' % task)
242
+ hostname = task['task_content']['resources']['name']
256
243
  cores = 1
257
244
  memory = 4096
258
245
  disk = 50
@@ -262,10 +249,10 @@ class ForemanAPI(HttpAPI):
262
249
  location_id = self.defs.location_id
263
250
  hostgroup = self.defs.hostgroup
264
251
  deploy_on = self.defs.deploy_on
265
- domain_id = self.get_domain_id (hostname)
266
- arch_id = self.get_architecture_id ('x86_64')
267
- os_id = self.get_os_id ('CentOS Linux 7.9.2009')
268
- ptable_id = self.get_ptable_id (os_id, 'CDT LVM')
252
+ domain_id = self.get_domain_id(hostname)
253
+ arch_id = self.get_architecture_id('x86_64')
254
+ os_id = self.get_os_id('CentOS Linux 7.9.2009')
255
+ ptable_id = self.get_ptable_id(os_id, 'CDT LVM')
269
256
 
270
257
  default_params = {
271
258
  "name": hostname,
@@ -301,861 +288,823 @@ class ForemanAPI(HttpAPI):
301
288
  if not default_params["name"]:
302
289
  raise ForemanAPIError("The hostname is not specified")
303
290
 
304
- if not default_params.get ('hostgroup_id'):
305
- hostgroup = self.get_hostgroup_id ('stands')
306
- logging.debug ('houstgroup_id is not set, setting default [%s]' % hostgroup)
307
- default_params ['hostgroup_id'] = hostgroup
291
+ if not default_params.get('hostgroup_id'):
292
+ hostgroup = self.get_hostgroup_id('stands')
293
+ logging.debug('houstgroup_id is not set, setting default [%s]' % hostgroup)
294
+ default_params['hostgroup_id'] = hostgroup
308
295
 
309
- if not default_params.get ('environment_id'):
310
- env_id = self.get_environment ('development')
311
- logging.debug ('environment_id is not set, setting default [%s]' % env_id)
312
- default_params ['environment_id'] = env_id
296
+ if not default_params.get('environment_id'):
297
+ env_id = self.get_environment('development')
298
+ logging.debug('environment_id is not set, setting default [%s]' % env_id)
299
+ default_params['environment_id'] = env_id
313
300
 
314
- logging.debug ("ForemanAPI is about to send the following payload:")
315
- logging.debug (default_params)
316
- request = self.post("hosts", headers = self.headers, json = default_params)
301
+ logging.debug("ForemanAPI is about to send the following payload:")
302
+ logging.debug(default_params)
303
+ request = self.post("hosts", headers=self.headers, json=default_params)
317
304
 
318
-
319
- def get_architecture_id (self, arch_name):
305
+ def get_architecture_id(self, arch_name):
320
306
  """
321
307
  wrapper for api v1/v2
322
308
  """
323
- logging.debug ('Reached get_architecture_id')
324
- logging.debug ('arch_name = [%s]')
309
+ logging.debug('Reached get_architecture_id')
310
+ logging.debug('arch_name = [%s]')
325
311
  if self.apiversion == 1:
326
- logging.error ('Not supported in v1, returning None')
312
+ # message appneded since different logging level on above and here
313
+ logging.error('Get_Architecture_ID is not supported in v1, returning None')
327
314
  return None
328
315
  elif self.apiversion == 2:
329
- logging.debug ('Passing to get_architecture_id_v2')
330
- return self.get_architecture_id_v2 (arch_name)
331
-
316
+ logging.debug('Passing to get_architecture_id_v2')
317
+ return self.get_architecture_id_v2(arch_name)
332
318
 
333
- def get_architecture_id_v2 (self, arch_name):
319
+ def get_architecture_id_v2(self, arch_name):
334
320
  """
335
321
  returns architecture id
336
322
  """
337
- logging.debug ('Reached get_architecture_id_v2')
338
- logging.debug ('arch_name = [%s]' % arch_name)
323
+ logging.debug('Reached get_architecture_id_v2')
324
+ logging.debug('arch_name = [%s]' % arch_name)
339
325
  params = {'search': 'name=%s' % arch_name}
340
- response = self.get ('architectures', params=params).json ()
341
- logging.debug ('Received response:')
342
- logging.debug (response)
343
- results = response.get ('results')
326
+ response = self.get('architectures', params=params).json()
327
+ logging.debug('Received response: %s')
328
+ logging.debug(response)
329
+ results = response.get('results')
330
+
344
331
  for result in results:
345
- if result.get ('name') == arch_name:
346
- arch_id = result.get ('id')
347
- logging.debug ('Found architecture, id = [%s]' % id)
348
- return arch_id
349
- logging.error ('No architecture found, returning None')
332
+ if result.get('name') != arch_name:
333
+ continue
334
+
335
+ arch_id = result.get('id')
336
+ logging.debug('Found architecture, id = [%s]' % id)
337
+ return arch_id
338
+
339
+ logging.error('No architecture found, returning None')
350
340
  return None
351
-
352
341
 
353
- def get_domain_id (self, hostname):
342
+ def get_domain_id(self, hostname):
354
343
  """
355
344
  wrapper for api v1/v2
356
345
  """
357
- logging.debug ('Reached get_domain_id')
346
+ logging.debug('Reached get_domain_id')
347
+
358
348
  if self.apiversion == 1:
359
- logging.error ('Not supported in v1, returning None')
349
+ logging.error('Not supported in v1, returning None')
360
350
  return None
361
351
  elif self.apiversion == 2:
362
- logging.debug ('Passing to get_domain_id_v2')
363
- return self.get_domain_id_v2 (hostname)
364
-
352
+ logging.debug('Passing to get_domain_id_v2')
353
+ return self.get_domain_id_v2(hostname)
365
354
 
366
- def get_domain_id_v2 (self, hostname):
355
+ def get_domain_id_v2(self, hostname):
367
356
  """
368
357
  Returns domain id if found
369
358
  """
370
- logging.debug ('Reached get_domain_id_v2')
371
- logging.debug ('hostname = [%s]' % hostname)
372
- domain = '.'.join (hostname.split ('.') [1:] )
373
- response = self.get ('domains/')
374
- j = response.json ()
375
- domains = j ['results']
376
- logging.debug ('Searching domain [%s]' % domain)
377
- logging.debug ('Domains list:')
378
- logging.debug (domains)
359
+ logging.debug('Reached get_domain_id_v2')
360
+ logging.debug('hostname = [%s]' % hostname)
361
+ domain = '.'.join(hostname.split('.')[1:])
362
+ response = self.get('domains')
363
+ j = response.json()
364
+ domains = j['results']
365
+ logging.debug('Searching domain [%s]' % domain)
366
+ logging.debug('Domains list:')
367
+ logging.debug(domains)
368
+
379
369
  for d in domains:
380
- if d.get ('name') == domain:
381
- return d.get ('id')
382
- logging.error ('Cannot find domain for host [%s]' % hostname)
383
- return None
370
+ if d.get('name') == domain:
371
+ return d.get('id')
384
372
 
373
+ logging.error('Cannot find domain for host [%s]' % hostname)
374
+ return None
385
375
 
386
- def get_host_info (self, hostname):
376
+ def get_host_info(self, hostname):
387
377
  """
388
378
  wrapper for api v1/v2
389
379
  """
390
- logging.debug ('Reached get_host_info')
380
+ logging.debug('Reached get_host_info')
391
381
  if self.apiversion == 1:
392
- logging.debug ('Passing to get_host_info_v1')
393
- return self.get_host_info_v1 (hostname)
382
+ logging.debug('Passing to get_host_info_v1')
383
+ return self.get_host_info_v1(hostname)
394
384
  elif self.apiversion == 2:
395
- logging.debug ('Passing to get_host_info_v2')
396
- return self.get_host_info_v2 (hostname)
397
-
385
+ logging.debug('Passing to get_host_info_v2')
386
+ return self.get_host_info_v2(hostname)
398
387
 
399
- def get_host_info_v1 (self, hostname):
388
+ def get_host_info_v1(self, hostname):
400
389
  """
401
390
  Gathers all information about the host and returns it as a json
402
391
  """
403
- logging.debug ('Reached get_host_info_v1')
404
- logging.debug ('hostname = [%s]' % hostname)
405
- response = self.get("hosts/" + hostname)
392
+ logging.debug('Reached get_host_info_v1')
393
+ logging.debug('hostname = [%s]' % hostname)
394
+ response = self.get(posixpath.join("hosts", hostname))
406
395
  return response.json()
407
396
 
397
+ def get_host_info_v2(self, hostname):
398
+ logging.debug('Reached get_host_info_v2')
399
+ logging.debug('hostname = [%s]' % hostname)
400
+ logging.debug('passing to get_host_info_v1')
401
+ return self.get_host_info_v1(hostname)
408
402
 
409
- def get_host_info_v2 (self, hostname):
410
- logging.debug ('Reached get_host_info_v2')
411
- logging.debug ('hostname = [%s]' % hostname)
412
- logging.debug ('passing to get_host_info_v1')
413
- return self.get_host_info_v1 (hostname)
414
-
415
-
416
- def get_ptable_id (self, os_id, ptable_name):
403
+ def get_ptable_id(self, os_id, ptable_name):
417
404
  """
418
405
  wrapper for api v1/v2
419
406
  """
420
- logging.debug ('Reached get_ptable_id')
407
+ logging.debug('Reached get_ptable_id')
421
408
  if self.apiversion == 1:
422
- logging.error ('Not supported in v1, returning None')
409
+ logging.error('Get_Ptable_Id is not supported in v1, returning None')
423
410
  return None
424
411
  elif self.apiversion == 2:
425
- logging.debug ('Passing to get_ptable_id_v2')
426
- return self.get_ptable_id_v2 (os_id, ptable_name)
412
+ logging.debug('Passing to get_ptable_id_v2')
413
+ return self.get_ptable_id_v2(os_id, ptable_name)
427
414
 
428
-
429
- def get_ptable_id_v2 (self, os_id, ptable_name):
415
+ def get_ptable_id_v2(self, os_id, ptable_name):
430
416
  """
431
417
  returns partition table id
432
418
  """
433
- logging.debug ('Reached get_ptable_id_v2')
434
- logging.debug ('os_id = [%s]' % os_id )
435
- logging.debug ('ptable_name = [%s]' % ptable_name)
436
- response = self.get ('operatingsystems/%s/ptables' % os_id).json ()
437
- logging.debug ('response is:')
438
- logging.debug (response)
439
- results = response.get ('results')
419
+ logging.debug('Reached get_ptable_id_v2')
420
+ logging.debug('os_id = [%s]' % os_id)
421
+ logging.debug('ptable_name = [%s]' % ptable_name)
422
+ response = self.get(posixpath.join('operatingsystems', str(os_id), 'ptables')).json()
423
+ logging.debug('response is:')
424
+ logging.debug(response)
425
+ results = response.get('results')
440
426
  for result in results:
441
- if result.get ('name') == ptable_name:
442
- ptable_id = result.get ('id')
443
- logging.debug ('Found ptable id = [%s]' % ptable_id)
444
- return ptable_id
445
- logging.error ('No ptable found, returning None')
446
- return None
427
+ if result.get('name') != ptable_name:
428
+ continue
447
429
 
430
+ ptable_id = result.get('id')
431
+ logging.debug('Found ptable id = [%s]' % ptable_id)
432
+ return ptable_id
433
+
434
+ logging.error('No ptable found, returning None')
435
+ return None
448
436
 
449
- def update_host (self, hostname, payload):
437
+ def update_host(self, hostname, payload):
450
438
  """
451
439
  wrapper for api v1/v2
452
440
  """
453
- logging.debug ('Reached update_host')
441
+ logging.debug('Reached update_host')
454
442
  if self.apiversion == 1:
455
- logging.debug ('Passing to update_host_v1')
456
- self.update_host_v1 (hostname, payload)
443
+ logging.debug('Passing to update_host_v1')
444
+ self.update_host_v1(hostname, payload)
457
445
  elif self.apiversion == 2:
458
- logging.debug ('Passing to update_host_v2')
459
- self.update_host_v2 (hostname, payload)
446
+ logging.debug('Passing to update_host_v2')
447
+ self.update_host_v2(hostname, payload)
460
448
 
461
-
462
- def update_host_v1 (self, hostname, payload):
449
+ def update_host_v1(self, hostname, payload):
463
450
  """
464
451
  Updates the host using the payload
465
452
  """
466
- logging.debug ('Reached update_host_v1')
467
- request = self.put("hosts/" + hostname, headers = self.headers, json = payload)
468
-
453
+ logging.debug('Reached update_host_v1')
454
+ request = self.put(posixpath.join("hosts", hostname), headers=self.headers, json=payload)
469
455
 
470
- def update_host_v2 (self, hostname, payload):
456
+ def update_host_v2(self, hostname, payload):
471
457
  """
472
458
  Updates the host using the payload
473
459
  """
474
- logging.debug ('Reached update_host_v2')
475
- logging.debug ('Passing to update_host_v1')
476
- self.update_host_v1 (hostname, payload)
477
-
460
+ logging.debug('Reached update_host_v2')
461
+ logging.debug('Passing to update_host_v1')
462
+ self.update_host_v1(hostname, payload)
478
463
 
479
- def delete_host (self, hostname):
464
+ def delete_host(self, hostname):
480
465
  """
481
466
  wrapper for api v1/v2
482
467
  """
483
- logging.debug ('Reached delete_host')
468
+ logging.debug('Reached delete_host')
484
469
  if self.apiversion == 1:
485
- logging.debug ('Passing to delete_host_v1')
486
- self.delete_host_v1 (hostname)
470
+ logging.debug('Passing to delete_host_v1')
471
+ self.delete_host_v1(hostname)
487
472
  elif self.apiversion == 2:
488
- logging.debug ('Passing to delete_host_v2')
489
- self.delete_host_v2 (hostname)
473
+ logging.debug('Passing to delete_host_v2')
474
+ self.delete_host_v2(hostname)
490
475
 
491
-
492
- def delete_host_v1 (self, hostname):
476
+ def delete_host_v1(self, hostname):
493
477
  """
494
478
  Deletes the specified host
495
479
  """
496
- logging.debug ('Reached delete_host_v1')
497
- request = self.delete("hosts/" + hostname, headers = self.headers)
498
-
480
+ logging.debug('Reached delete_host_v1')
481
+ request = self.delete(posixpath.join("hosts", hostname), headers=self.headers)
499
482
 
500
- def delete_host_v2 (self, hostname):
483
+ def delete_host_v2(self, hostname):
501
484
  """
502
485
  Deletes the specified host
503
486
  """
504
- logging.debug ('Reached delete_host_v2')
505
- logging.debug ('Passing to delete_host_v1')
506
- self.delete_host_v1 (hostname)
487
+ logging.debug('Reached delete_host_v2')
488
+ logging.debug('Passing to delete_host_v1')
489
+ self.delete_host_v1(hostname)
507
490
 
508
-
509
- def puppet_class_info (self, classname):
491
+ def puppet_class_info(self, classname):
510
492
  """
511
493
  wrapper for api v1/v2
512
494
  """
513
- logging.debug ('Reached puppet_class_info')
495
+ logging.debug('Reached puppet_class_info')
514
496
  if self.apiversion == 1:
515
- logging.debug ('Passing to puppet_class_info_v1')
516
- return self.puppet_class_info_v1 (classname)
497
+ logging.debug('Passing to puppet_class_info_v1')
498
+ return self.puppet_class_info_v1(classname)
517
499
  elif self.apiversion == 2:
518
- logging.debug ('Passing to puppet_class_info_v2')
519
- return self.puppet_class_info_v1 (classname)
520
-
500
+ logging.debug('Passing to puppet_class_info_v2')
501
+ return self.puppet_class_info_v1(classname)
521
502
 
522
- def puppet_class_info_v1 (self, classname):
503
+ def puppet_class_info_v1(self, classname):
523
504
  """
524
505
  Returns puppet class info
525
506
  """
526
- logging.debug ('Reached puppet_class_info_v1')
527
- response = self.get("puppetclasses/" + classname, headers = self.headers)
507
+ logging.debug('Reached puppet_class_info_v1')
508
+ response = self.get(posixpath.join("puppetclasses", classname), headers=self.headers)
528
509
  return response.json()
529
510
 
530
-
531
- def puppet_class_info_v2 (self, classname):
511
+ def puppet_class_info_v2(self, classname):
532
512
  """
533
513
  Returns puppet class info
534
514
  """
535
- logging.debug ('Reached puppet_class_info_v2')
536
- logging.debug ('Passing to puppet_class_info_v1')
537
- return self.puppet_class_info_v1 (classname)
538
-
515
+ logging.debug('Reached puppet_class_info_v2')
516
+ logging.debug('Passing to puppet_class_info_v1')
517
+ return self.puppet_class_info_v1(classname)
539
518
 
540
- def smart_class_info (self, scid):
519
+ def smart_class_info(self, scid):
541
520
  """
542
521
  wrapper for api v1/v2
543
522
  """
544
- logging.debug ('Reached smart_class_info')
523
+ logging.debug('Reached smart_class_info')
524
+
545
525
  if self.apiversion == 1:
546
- logging.debug ('Passing to smart_class_info_v1')
547
- return self.smart_class_info_v1 (scid)
526
+ logging.debug('Passing to smart_class_info_v1')
527
+ return self.smart_class_info_v1(scid)
548
528
  if self.apiversion == 2:
549
- logging.debug ('Passing to smart_class_info_v2')
550
- return self.smart_class_info_v2 (scid)
529
+ logging.debug('Passing to smart_class_info_v2')
530
+ return self.smart_class_info_v2(scid)
551
531
 
552
-
553
- def smart_class_info_v1 (self, scid):
532
+ def smart_class_info_v1(self, scid):
554
533
  """
555
534
  Returns smart class info
556
535
  """
557
- logging.debug ('Reached smart_class_info_v1')
558
- response = self.get("smart_class_parameters/" + str(scid), headers = self.headers)
536
+ logging.debug('Reached smart_class_info_v1')
537
+ response = self.get(posixpath.join("smart_class_parameters", str(scid)), headers=self.headers)
559
538
  return response.json()
560
539
 
561
-
562
- def smart_class_info_v2 (self, scid):
540
+ def smart_class_info_v2(self, scid):
563
541
  """
564
542
  Returns smart class info
565
543
  """
566
- logging.debug ('Reached smart_class_info_v2')
567
- logging.debug ('Passing to smart_class_info_v1')
568
- return self.smart_class_info_v1 (scid)
544
+ logging.debug('Reached smart_class_info_v2')
545
+ logging.debug('Passing to smart_class_info_v1')
546
+ return self.smart_class_info_v1(scid)
569
547
 
570
-
571
- def override_smart_class (self, scid, params):
548
+ def override_smart_class(self, scid, params):
572
549
  """
573
550
  wrapper for api v1/v2
574
551
  """
575
- logging.debug ('Reached mverride_smart_class')
552
+ logging.debug('Reached mverride_smart_class')
576
553
  if self.apiversion == 1:
577
- logging.debug ('Passing to override_smart_class_v1')
578
- self.override_smart_class_v1 (scid, params)
554
+ logging.debug('Passing to override_smart_class_v1')
555
+ self.override_smart_class_v1(scid, params)
579
556
  elif self.apiversion == 2:
580
- logging.debug ('Passing to override_smart_class_v2')
581
- self.override_smart_class_v1 (scid, params)
582
-
557
+ logging.debug('Passing to override_smart_class_v2')
558
+ self.override_smart_class_v1(scid, params)
583
559
 
584
- def override_smart_class_v1 (self, scid, params):
560
+ def override_smart_class_v1(self, scid, params):
585
561
  """
586
562
  Overrides smart class parameters
587
563
  """
588
- logging.debug ('Reached override_smart_class_v1')
589
- request = self.post("smart_class_parameters/" + str(scid) + "/override_values", headers = self.headers, data = params)
564
+ logging.debug('Reached override_smart_class_v1')
565
+ request = self.post(posixpath.join("smart_class_parameters", str(scid),
566
+ "override_values"), headers=self.headers, data=params)
590
567
 
591
-
592
- def override_smart_class_v2 (self, scid, params):
568
+ def override_smart_class_v2(self, scid, params):
593
569
  """
594
570
  Overrides smart class parameters
595
571
  """
596
- logging.debug ('Reached override_smart_class_v2')
597
- logging.debug ('Passing to override_smart_class_v1')
598
- self.override_smart_class_v1 (scid, params)
599
-
572
+ logging.debug('Reached override_smart_class_v2')
573
+ logging.debug('Passing to override_smart_class_v1')
574
+ self.override_smart_class_v1(scid, params)
600
575
 
601
- def get_hostgroup_puppetclasses (self, hostgroup_id):
576
+ def get_hostgroup_puppetclasses(self, hostgroup_id):
602
577
  """
603
578
  wrapper for api v1/v2
604
579
  """
605
- logging.debug ('Reached get_hostgroup_puppetclasses')
580
+ logging.debug('Reached get_hostgroup_puppetclasses')
606
581
  if self.apiversion == 1:
607
- logging.debug ('Passing to get_hostgroup_puppetclasses_v1')
608
- return self.get_hostgroup_puppetclasses_v1 (hostgroup_id)
582
+ logging.debug('Passing to get_hostgroup_puppetclasses_v1')
583
+ return self.get_hostgroup_puppetclasses_v1(hostgroup_id)
609
584
  elif self.apiversion == 2:
610
- logging.debug ('Passing to get_hostgroup_puppetclasses_v2')
611
- return self.get_hostgroup_puppetclasses_v2 (hostgroup_id)
612
-
585
+ logging.debug('Passing to get_hostgroup_puppetclasses_v2')
586
+ return self.get_hostgroup_puppetclasses_v2(hostgroup_id)
613
587
 
614
- def get_hostgroup_puppetclasses_v1 (self, hostgroup_id):
588
+ def get_hostgroup_puppetclasses_v1(self, hostgroup_id):
615
589
  """
616
590
  Returns info about all hostgroup's puppetclasses
617
591
  """
618
- logging.debug ('Reached get_hostgroup_puppetclasses_v1')
619
- response = self.get("hostgroups/" + str(hostgroup_id) + "/puppetclasses", headers = self.headers)
592
+ logging.debug('Reached get_hostgroup_puppetclasses_v1')
593
+ response = self.get(posixpath.join("hostgroups", str(hostgroup_id), "puppetclasses"), headers=self.headers)
620
594
  return response.json()
621
595
 
622
-
623
- def get_hostgroup_puppetclasses_v2 (self, hostgroup_id):
596
+ def get_hostgroup_puppetclasses_v2(self, hostgroup_id):
624
597
  """
625
598
  Returns info about all hostgroup's puppetclasses
626
599
  """
627
- logging.debug ('Reached get_hostgroup_puppetclasses_v2')
628
- params = {'hostgroup_id': str (hostgroup_id) }
629
- response = self.get ('puppetclasses/', params=params)
630
- return response.json ()
631
-
600
+ logging.debug('Reached get_hostgroup_puppetclasses_v2')
601
+ params = {'hostgroup_id': str(hostgroup_id)}
602
+ response = self.get('puppetclasses', params=params)
603
+ return response.json()
632
604
 
633
- def add_puppet_class_to_host (self, hostname, params):
605
+ def add_puppet_class_to_host(self, hostname, params):
634
606
  """
635
607
  wrapper for api v1/v2
636
608
  """
637
- logging.debug ('Reached add_puppet_class_to_host')
609
+ logging.debug('Reached add_puppet_class_to_host')
638
610
  if self.apiversion == 1:
639
- logging.debug ('Passing to add_puppet_class_to_host_v1')
640
- self.add_puppet_class_to_host_v1 (hostname, params)
611
+ logging.debug('Passing to add_puppet_class_to_host_v1')
612
+ self.add_puppet_class_to_host_v1(hostname, params)
641
613
  if self.apiversion == 2:
642
- logging.debug ('Passing to add_puppet_class_to_host_v2')
643
- self.add_puppet_class_to_host_v2 (hostname, params)
644
-
614
+ logging.debug('Passing to add_puppet_class_to_host_v2')
615
+ self.add_puppet_class_to_host_v2(hostname, params)
645
616
 
646
- def add_puppet_class_to_host_v1 (self, hostname, params):
617
+ def add_puppet_class_to_host_v1(self, hostname, params):
647
618
  """
648
619
  Adds the required puppet class to the host
649
620
  """
650
- logging.debug ('Reached add_puppet_class_to_host_v1')
651
- request = self.post("hosts/" + hostname + "/puppetclass_ids", headers = self.headers, data = params)
621
+ logging.debug('Reached add_puppet_class_to_host_v1')
622
+ request = self.post(posixpath.join("hosts", hostname, "puppetclass_ids"), headers=self.headers, data=params)
652
623
 
653
-
654
- def add_puppet_class_to_host_v2 (self, hostname, params):
624
+ def add_puppet_class_to_host_v2(self, hostname, params):
655
625
  """
656
626
  Adds the required puppet class to the host
657
627
  """
658
- logging.debug ('Reached add_puppet_class_to_host_v2')
659
- logging.debug ('Passing to add_puppet_class_to_host_v1')
660
- self.add_puppet_class_to_host_v1 (hostname, params)
661
-
628
+ logging.debug('Reached add_puppet_class_to_host_v2')
629
+ logging.debug('Passing to add_puppet_class_to_host_v1')
630
+ self.add_puppet_class_to_host_v1(hostname, params)
662
631
 
663
- def get_subnets (self):
632
+ def get_subnets(self):
664
633
  """
665
634
  wrapper for api v1/v2
666
635
  """
667
- logging.debug ('Reached get_subnets')
636
+ logging.debug('Reached get_subnets')
668
637
  if self.apiversion == 1:
669
- logging.debug ('Passing to get_subnets_v1')
670
- return self.get_subnets_v1 ()
638
+ logging.debug('Passing to get_subnets_v1')
639
+ return self.get_subnets_v1()
671
640
  elif self.apiversion == 2:
672
- logging.debug ('Passing to get_subnets_v2')
673
- return self.get_subnets_v2 ()
641
+ logging.debug('Passing to get_subnets_v2')
642
+ return self.get_subnets_v2()
674
643
 
675
-
676
- def get_subnets_v1 (self):
644
+ def get_subnets_v1(self):
677
645
  """
678
646
  Returns all available subnets
679
647
  """
680
- logging.debug ('Reached get_subnets_v1')
681
- subnets_count = self.get("subnets", headers = self.headers).json()["total"]
682
- response = self.get("subnets?per_page={}".format(subnets_count), headers = self.headers)
648
+ logging.debug('Reached get_subnets_v1')
649
+ subnets_count = self.get("subnets", headers=self.headers).json()["total"]
650
+ response = self.get("subnets?per_page={}".format(subnets_count), headers=self.headers)
683
651
  return response.json()
684
652
 
685
-
686
- def get_subnets_v2 (self):
653
+ def get_subnets_v2(self):
687
654
  """
688
655
  Return all available subnets
689
656
  """
690
- logging.debug ('Reached get_subnets_v2')
691
- logging.debug ('Passing to get_subnets_v1')
692
- return self.get_subnets_v1 ()
657
+ logging.debug('Reached get_subnets_v2')
658
+ logging.debug('Passing to get_subnets_v1')
659
+ return self.get_subnets_v1()
693
660
 
694
-
695
- def get_host_reports (self, hostname, last=False):
661
+ def get_host_reports(self, hostname, last=False):
696
662
  """
697
663
  wrapper for api v1/v2
698
664
  """
699
- logging.debug ('Reached get_host_reports')
665
+ logging.debug('Reached get_host_reports')
700
666
  if self.apiversion == 1:
701
- logging.debug ('Passing to get_host_reports_v1')
702
- return self.get_host_reports_v1 (hostname, last)
667
+ logging.debug('Passing to get_host_reports_v1')
668
+ return self.get_host_reports_v1(hostname, last)
703
669
  elif self.apiversion == 2:
704
- logging.debug ('Passing to get_host_reports_v2')
705
- return self.get_host_reports_v2 (hostname, last)
706
-
670
+ logging.debug('Passing to get_host_reports_v2')
671
+ return self.get_host_reports_v2(hostname, last)
707
672
 
708
- def get_host_reports_v1 (self, hostname, last):
673
+ def get_host_reports_v1(self, hostname, last):
709
674
  """
710
675
  Returns all reports (or only the last one) for the host
711
676
  """
712
- if not last:
713
- response = self.get("hosts/" + hostname + "/config_reports", headers = self.headers)
714
- else:
715
- response = self.get("hosts/" + hostname + "/config_reports/last", headers = self.headers)
716
- return response.json()
677
+ _rq = posixpath.join("hosts", hostname, "config_reports")
678
+
679
+ if last:
680
+ _rq = posixpath.join(_rq, "last")
717
681
 
682
+ response = self.get(_rq, headers=self.headers)
718
683
 
719
- def get_host_reports_v2 (self, hostname, last):
684
+ return response.json()
685
+
686
+ def get_host_reports_v2(self, hostname, last):
720
687
  """
721
688
  Returns all reports (or only the last one) for the host
722
689
  """
723
- logging.debug ('Reached get_host_reports_v2')
724
- logging.debug ('hostname = [%s]' % hostname)
725
- logging.debug ('last = [%s]' % last)
690
+ logging.debug('Reached get_host_reports_v2')
691
+ logging.debug('hostname = [%s]' % hostname)
692
+ logging.debug('last = [%s]' % last)
726
693
  if not last:
727
694
  params = {'search': 'host=%s' % hostname}
728
- response = self.get ('config_reports', params=params).json ()
729
- logging.debug ('Received response:')
730
- logging.debug (response)
695
+ response = self.get('config_reports', params=params).json()
696
+ logging.debug('Received response:')
697
+ logging.debug(response)
731
698
  else:
732
- host_id = self.get_host_info (hostname).get ('id')
733
- logging.debug ('host_id = [%s]' % host_id)
734
- response = self.get ('hosts/%s/config_reports/last' % host_id).json ()
735
- logging.debug ('Received response:')
736
- logging.debug (response)
699
+ host_id = self.get_host_info(hostname).get('id')
700
+ logging.debug('host_id = [%s]' % host_id)
701
+ response = self.get(posixpath.join('hosts', str(host_id), "config_reports", "last")).json()
702
+ logging.debug('Received response:')
703
+ logging.debug(response)
737
704
  return response
738
705
 
739
-
740
- def host_power (self, hostname, action):
706
+ def host_power(self, hostname, action):
741
707
  """
742
708
  wrapper for api v1/v2
743
709
  NB currently (2023-02-08) host power is managed via vsphere api
744
710
  """
745
- logging.debug ('Reached host_power')
711
+ logging.debug('Reached host_power')
746
712
  if self.apiversion == 1:
747
- logging.debug ('Passing to host_power_v1')
748
- self.host_power_v1 (hostname, action)
713
+ logging.debug('Passing to host_power_v1')
714
+ self.host_power_v1(hostname, action)
749
715
  elif self.apiversion == 2:
750
- logging.debug ('Passing to host_power_v2')
751
- self.host_power_v2 (hostname, action)
716
+ logging.debug('Passing to host_power_v2')
717
+ self.host_power_v2(hostname, action)
752
718
 
753
-
754
- def host_power_v1 (self, hostname, action):
719
+ def host_power_v1(self, hostname, action):
755
720
  """
756
721
  Turns on/off power on the host
757
722
  """
758
- logging.debug ('Reached host_power_v1')
723
+ logging.debug('Reached host_power_v1')
759
724
  actions = ["start", "stop"]
725
+
760
726
  if action not in actions:
761
727
  raise ForemanAPIError("500 - Incorrect power action was provided")
762
- params = json.dumps({"power_action": action})
763
- request = self.put("hosts/" + hostname + "/power", headers = self.headers, data = params)
764
728
 
729
+ params = json.dumps({"power_action": action})
730
+ request = self.put(posixpath.join("hosts", hostname, "power"), headers=self.headers, data=params)
765
731
 
766
- def host_power_v2 (self, hostname, action):
732
+ def host_power_v2(self, hostname, action):
767
733
  """
768
734
  Turns on/off power on the host
769
735
  """
770
- logging.debug ('Reached host_power_v2')
771
- logging.debug ('Passing to host_power_v1')
772
- logging.debug ('host = [%s]' % host)
773
- logging.debug ('action = [%s]' % host)
774
- self.host_power_v1 (hostname, action)
775
-
736
+ logging.debug('Reached host_power_v2')
737
+ logging.debug('Passing to host_power_v1')
738
+ logging.debug('host = [%s]' % hostname)
739
+ logging.debug('action = [%s]' % action)
740
+ self.host_power_v1(hostname, action)
776
741
 
777
- def get_report (self, id):
742
+ def get_report(self, id):
778
743
  """
779
744
  wrapper for api v1/v2
780
745
  """
781
- logging.debug ('Reached get_report')
746
+ logging.debug('Reached get_report')
782
747
  if self.apiversion == 1:
783
- logging.debug ('Passing to get_report_v1')
784
- return self.get_report_v1 (id)
785
- elif self.apiversion == 2:
786
- logging.debug ('Passing to get_report_v2')
787
- return self.get_report_v2 (id)
788
-
748
+ logging.debug('Passing to get_report_v1')
749
+ return self.get_report_v1(id)
750
+ elif self.apiversion == 2:
751
+ logging.debug('Passing to get_report_v2')
752
+ return self.get_report_v2(id)
789
753
 
790
- def get_report_v1 (self, id):
754
+ def get_report_v1(self, id):
791
755
  """
792
756
  Returns a foreman report with specified id
793
757
  :param id: int, id of the required report
794
758
  :return: response in json format
795
759
  """
796
- response = self.get("config_reports/" + str(id), headers = self.headers)
760
+ response = self.get(posixpath.join("config_reports", str(id)), headers=self.headers)
797
761
  return response.json()
798
762
 
799
-
800
- def get_report_v2 (self, id):
763
+ def get_report_v2(self, id):
801
764
  """
802
765
  Returns a foreman report with specified id
803
766
  :param id: int, id of the required report
804
767
  :return: response in json format
805
768
  """
806
- logging.debug ('Reached get_report_v2')
807
- logging.debug ('Passing to get_report_v1')
808
- return self.get_report_v1 (id)
769
+ logging.debug('Reached get_report_v2')
770
+ logging.debug('Passing to get_report_v1')
771
+ return self.get_report_v1(id)
809
772
 
810
-
811
- def get_hostgroup_id (self, hostgroup_name):
773
+ def get_hostgroup_id(self, hostgroup_name):
812
774
  """
813
775
  wrapper for api v1/v2
814
776
  """
815
- logging.debug ('Reached get_hostgroup_id')
777
+ logging.debug('Reached get_hostgroup_id')
816
778
  if self.apiversion == 1:
817
- logging.debug ('Passing to get_hostgroup_id_v1')
818
- return self.get_hostgroup_id_v1 (hostgroup_name)
779
+ logging.debug('Passing to get_hostgroup_id_v1')
780
+ return self.get_hostgroup_id_v1(hostgroup_name)
819
781
  elif self.apiversion == 2:
820
- logging.debug ('Passing to get_hostgroup_id_v2')
821
- return self.get_hostgroup_id_v2 (hostgroup_name)
822
-
782
+ logging.debug('Passing to get_hostgroup_id_v2')
783
+ return self.get_hostgroup_id_v2(hostgroup_name)
823
784
 
824
- def get_hostgroup_id_v1 (self, hostgroup_name):
785
+ def get_hostgroup_id_v1(self, hostgroup_name):
825
786
  """
826
787
  Returns id of the required hostgroup
827
788
  :param hostgroup_name: str
828
789
  :return: int
829
790
  """
830
- logging.debug ('Reached get_hostgroup_id_v1')
831
- hostgroups = self.get("hostgroups", headers = self.headers).json()["results"]
791
+ logging.debug('Reached get_hostgroup_id_v1')
792
+ hostgroups = self.get("hostgroups", headers=self.headers).json()["results"]
793
+
832
794
  for hostgroup in hostgroups:
833
- if hostgroup["name"] == hostgroup_name:
795
+ if hostgroup.get("name") == hostgroup_name:
834
796
  return hostgroup.get ('id')
797
+
798
+ logging.debug("Hostgroup [%s] not found, returning None" % hostgroup_name)
799
+ return None
835
800
 
836
-
837
- def get_hostgroup_id_v2 (self, hostgroup_name):
801
+ def get_hostgroup_id_v2(self, hostgroup_name):
838
802
  """
839
803
  Returns id of the required hostgroup
840
804
  :param hostgroup_name: str
841
805
  :return: int
842
806
  """
843
- logging.debug ('Reached get_hostgroup_id_v2')
844
- logging.debug ('Passing to get_hostgroup_id_v1')
845
- return self.get_hostgroup_id_v1 (hostgroup_name)
846
-
807
+ logging.debug('Reached get_hostgroup_id_v2')
808
+ logging.debug('Passing to get_hostgroup_id_v1')
809
+ return self.get_hostgroup_id_v1(hostgroup_name)
847
810
 
848
- def get_organization_id (self, organization_name):
811
+ def get_organization_id(self, organization_name):
849
812
  """
850
813
  wrapper for api v1/v2
851
814
  """
852
- logging.debug ('Reached get_organization_id')
815
+ logging.debug('Reached get_organization_id')
853
816
  if self.apiversion == 1:
854
- logging.debug ('Passing to get_organization_id_v1')
855
- return self.get_organization_id_v1 (organization_name)
817
+ logging.debug('Passing to get_organization_id_v1')
818
+ return self.get_organization_id_v1(organization_name)
856
819
  elif self.apiversion == 2:
857
- logging.debug ('Passing to get_organization_id_v2')
858
- return self.get_organization_id_v2 (organization_name)
820
+ logging.debug('Passing to get_organization_id_v2')
821
+ return self.get_organization_id_v2(organization_name)
859
822
 
860
-
861
- def get_organization_id_v1 (self, organization_name):
823
+ def get_organization_id_v1(self, organization_name):
862
824
  """
863
825
  Returns id of the required organization
864
826
  :param organization_name: str
865
827
  :return: int
866
828
  """
867
- logging.debug ('Reached get_organization_id_v1')
829
+ logging.debug('Reached get_organization_id_v1')
868
830
  organization_id = None
869
- organizations_count = self.get("organizations", headers = self.headers).json()["total"]
870
- organizations = self.get("organizations?per_page={}".format(organizations_count), headers = self.headers).json()["results"]
871
-
831
+ organizations_count = self.get("organizations", headers=self.headers).json()["total"]
832
+ organizations = self.get("organizations?per_page={}".format(
833
+ organizations_count), headers=self.headers).json()["results"]
834
+
872
835
  try:
873
- organization_id = next(organization["id"] for organization in organizations if organization["name"] == organization_name)
836
+ organization_id = next(organization["id"]
837
+ for organization in organizations if organization["name"] == organization_name)
874
838
  return organization_id
875
839
  except StopIteration:
876
840
  return None
877
841
 
878
-
879
- def get_organization_id_v2 (self, organization_name):
842
+ def get_organization_id_v2(self, organization_name):
880
843
  """
881
844
  Returns id of the required organization
882
845
  :param organization_name: str
883
846
  :return: int
884
847
  """
885
- logging.debug ('Reached get_organization_id_v2')
886
- logging.debug ('Passing to get_organization_id_v1')
887
- return self.get_organization_id_v1 (organization_name)
888
-
848
+ logging.debug('Reached get_organization_id_v2')
849
+ logging.debug('Passing to get_organization_id_v1')
850
+ return self.get_organization_id_v1(organization_name)
889
851
 
890
- def get_os_id (self, os_name):
852
+ def get_os_id(self, os_name):
891
853
  """
892
854
  Wrapper for api v1/v2
893
855
  """
894
- logging.debug ('Reached get_os_id')
895
- logging.debug ('os_name = [%s]')
856
+ logging.debug('Reached get_os_id')
857
+ logging.debug('os_name = [%s]')
896
858
  if self.apiversion == 1:
897
- logging.error ('Not supported in v1, returning None')
859
+ logging.error('Not supported in v1, returning None')
898
860
  return None
899
861
  elif self.apiversion == 2:
900
- logging.debug ('Passing to get_os_id_v2')
901
- return self.get_os_id_v2 (os_name)
862
+ logging.debug('Passing to get_os_id_v2')
863
+ return self.get_os_id_v2(os_name)
902
864
 
903
-
904
- def get_os_id_v2 (self, os_name):
865
+ def get_os_id_v2(self, os_name):
905
866
  """
906
867
  returns operating system id
907
868
  """
908
- logging.debug ('Reached get_os_id_v2')
909
- logging.debug ('os_name = [%s]' % os_name)
869
+ logging.debug('Reached get_os_id_v2')
870
+ logging.debug('os_name = [%s]' % os_name)
910
871
  params = {'search': 'name=%s' % os_name}
911
- response = self.get ('operatingsystems', params=params).json ()
912
- logging.debug ('response is:')
913
- logging.debug (response)
914
- results = response.get ('results')
872
+ response = self.get('operatingsystems', params=params).json()
873
+ logging.debug('response is:')
874
+ logging.debug(response)
875
+ results = response.get('results')
915
876
  for result in results:
916
- if result.get ('description') == os_name:
917
- os_id = result.get ('id')
918
- logging.debug ('Found os, id = [%s]' % os_id)
919
- return os_id
920
- logging.error ('OS not found, returning None')
921
- return None
877
+ if result.get('description') != os_name:
878
+ continue
922
879
 
880
+ os_id = result.get('id')
881
+ logging.debug('Found os, id = [%s]' % os_id)
882
+ return os_id
883
+
884
+ logging.error('OS not found, returning None')
885
+ return None
923
886
 
924
- def set_host_expiry (self, hostname, expiry):
887
+ def set_host_expiry(self, hostname, expiry):
925
888
  """
926
889
  wrapper for api v1/v2
927
890
  """
928
- logging.debug ('Reached set_host_expiry')
891
+ logging.debug('Reached set_host_expiry')
929
892
  if self.apiversion == 1:
930
- logging.debug ('Passing to set_host_expiry_v1')
931
- self.set_host_expiry_v1 (hostname, expiry)
893
+ logging.debug('Passing to set_host_expiry_v1')
894
+ self.set_host_expiry_v1(hostname, expiry)
932
895
  elif self.apiversion == 2:
933
- logging.debug ('Passing to set_host_expiry_v2')
934
- self.set_host_expiry_v2 (hostname, expiry)
896
+ logging.debug('Passing to set_host_expiry_v2')
897
+ self.set_host_expiry_v2(hostname, expiry)
935
898
 
936
-
937
- def set_host_expiry_v1 (self, hostname, expiry):
899
+ def set_host_expiry_v1(self, hostname, expiry):
938
900
  """
939
901
  Attempts to set host expiry date
940
902
  :param hostname: full hostname
941
903
  :param expiry: expiry date in format yyyy-mm-dd
942
904
  """
943
- logging.debug ('Reached set_host_expiry_v1')
944
- pl={}
945
- pl['host']={}
946
- pl['host']['expired_on']=expiry
947
- try:
948
- self.update_host(hostname,pl)
949
- except ForemanAPIError as err:
950
- raise(err)
951
-
905
+ logging.debug('Reached set_host_expiry_v1')
906
+ pl = {}
907
+ pl['host'] = {}
908
+ pl['host']['expired_on'] = expiry
909
+ self.update_host(hostname, pl)
952
910
 
953
- def set_host_expiry_v2 (self, hostname, expiry):
911
+ def set_host_expiry_v2(self, hostname, expiry):
954
912
  """
955
913
  Attempts to set host expiry date
956
914
  :param hostname: full hostname
957
915
  :param expiry: expiry date in format yyyy-mm-dd
958
916
  """
959
- logging.debug ('Reached set_host_expiry_v2')
960
- logging.debug ('Passing to set_host_expiry_v1')
961
- self.set_host_expiry_v1 (hostname, expiry)
917
+ logging.debug('Reached set_host_expiry_v2')
918
+ logging.debug('Passing to set_host_expiry_v1')
919
+ self.set_host_expiry_v1(hostname, expiry)
962
920
 
963
-
964
- def get_image_uuid (self, os_name, image_name):
921
+ def get_image_uuid(self, os_name, image_name):
965
922
  """
966
923
  wrapper api v1/v2
967
924
  """
968
- logging.debug ('Reached get_image_uuid')
925
+ logging.debug('Reached get_image_uuid')
969
926
  if self.apiversion == 1:
970
- logging.debug ('Passing to get_image_uuid_v1')
971
- return self.get_image_uuid_v1 (os_name, image_name)
927
+ logging.debug('Passing to get_image_uuid_v1')
928
+ return self.get_image_uuid_v1(os_name, image_name)
972
929
  elif self.apiversion == 2:
973
- logging.debug ('Passing to get_image_uuid_v2')
974
- return self.get_image_uuid_v2 (os_name, image_name)
975
-
930
+ logging.debug('Passing to get_image_uuid_v2')
931
+ return self.get_image_uuid_v2(os_name, image_name)
976
932
 
977
- def get_image_uuid_v1 (self, os_name, image_name):
933
+ def get_image_uuid_v1(self, os_name, image_name):
978
934
  """
979
935
  Returns OS image uuid
980
936
  :param os_name: str
981
937
  :param image_name: str
982
938
  :return: str
983
939
  """
984
- logging.debug ('Reached get_image_uuid_v1')
940
+ logging.debug('Reached get_image_uuid_v1')
985
941
  os_list = self.get("operatingsystems", headers=self.headers).json()["results"]
986
942
  try:
987
943
  os_id = next(os["id"] for os in os_list if os["description"] == os_name)
988
- images_list = self.get("operatingsystems/{}/images".format(os_id)).json()["results"]
944
+ images_list = self.get(posixpath.join("operatingsystems", str(os_id), "images")).json()["results"]
989
945
  image_uuid = next(image["uuid"] for image in images_list if image["name"] == image_name)
990
946
  return image_uuid
991
947
  except StopIteration:
992
948
  return None
993
949
 
994
-
995
- def get_image_uuid_v2 (self, os_name, image_name):
950
+ def get_image_uuid_v2(self, os_name, image_name):
996
951
  """
997
952
  Returns OS image uuid
998
953
  :param os_name: str
999
954
  :param image_name: str
1000
955
  :return: str
1001
956
  """
1002
- logging.debug ('Reached get_image_uuid_v2')
1003
- logging.debug ('Passing to get_image_uuid_v1')
1004
- return self.get_image_uuid_v1 (os_name, image_name)
957
+ logging.debug('Reached get_image_uuid_v2')
958
+ logging.debug('Passing to get_image_uuid_v1')
959
+ return self.get_image_uuid_v1(os_name, image_name)
1005
960
 
1006
-
1007
- def get_flavor_id (self, compute_resource_id, flavor_name):
961
+ def get_flavor_id(self, compute_resource_id, flavor_name):
1008
962
  """
1009
963
  wrapper api v1/v2
1010
964
  """
1011
- logging.debug ('Reached get_flavor_id')
965
+ logging.debug('Reached get_flavor_id')
1012
966
  if self.apiversion == 1:
1013
- logging.debug ('Passing to get_flavor_id_v1')
1014
- return self.get_flavor_id_v1 (compute_resource_id, flavor_name)
967
+ logging.debug('Passing to get_flavor_id_v1')
968
+ return self.get_flavor_id_v1(compute_resource_id, flavor_name)
1015
969
  elif self.apiversion == 2:
1016
- logging.debug ('Passing to get_flavor_id_v2')
1017
- return self.get_flavor_id_v2 (compute_resource_id, flavor_name)
1018
-
970
+ logging.debug('Passing to get_flavor_id_v2')
971
+ return self.get_flavor_id_v2(compute_resource_id, flavor_name)
1019
972
 
1020
- def get_flavor_id_v1 (self, compute_resource_id, flavor_name):
973
+ def get_flavor_id_v1(self, compute_resource_id, flavor_name):
1021
974
  """
1022
975
  :param compute_resource_id: int
1023
976
  :param flavor_name: str
1024
977
  :return: int
1025
978
  """
1026
- logging.debug ('Reached get_flavor_id_v1')
1027
- flavors_list = self.get("compute_resources/{}/available_flavors".format(compute_resource_id)).json()["results"]
979
+ logging.debug('Reached get_flavor_id_v1')
980
+ flavors_list = self.get(posixpath.join("compute_resources", str(compute_resource_id),
981
+ "available_flavors")).json()["results"]
982
+
1028
983
  try:
1029
984
  flavor_id = next(flavor["id"] for flavor in flavors_list if flavor["name"] == flavor_name)
1030
985
  return flavor_id
1031
986
  except StopIteration:
1032
987
  return None
1033
-
1034
988
 
1035
- def get_flavor_id_v2 (self, compute_resource_id, flavor_name):
989
+ def get_flavor_id_v2(self, compute_resource_id, flavor_name):
1036
990
  """
1037
991
  :param compute_resource_id: int
1038
992
  :param flavor_name: str
1039
993
  :return: int
1040
994
  """
1041
- logging.debug ('Reached get_flavor_id_v2')
1042
- logging.debug ('Passing to get_flavor_id_v1')
1043
- return self.get_flavor_id_v1 (compute_resource_id, flavor_name)
1044
-
995
+ logging.debug('Reached get_flavor_id_v2')
996
+ logging.debug('Passing to get_flavor_id_v1')
997
+ return self.get_flavor_id_v1(compute_resource_id, flavor_name)
1045
998
 
1046
- def get_tenant_id (self, compute_resource_id):
999
+ def get_tenant_id(self, compute_resource_id):
1047
1000
  """
1048
1001
  wrapper api v1/v2
1049
1002
  """
1050
- logging.debug ('Reached get_tenant_id')
1003
+ logging.debug('Reached get_tenant_id')
1051
1004
  if self.apiversion == 1:
1052
- logging.debug ('Passing to get_tenant_id_v1')
1053
- return self.get_tenant_id_v1 (compute_resource_id)
1005
+ logging.debug('Passing to get_tenant_id_v1')
1006
+ return self.get_tenant_id_v1(compute_resource_id)
1054
1007
  elif self.apiversion == 2:
1055
- logging.debug ('Passing to get_tenant_id_v2')
1056
- return self.get_tenant_id_v2 (compute_resource_id)
1057
-
1008
+ logging.debug('Passing to get_tenant_id_v2')
1009
+ return self.get_tenant_id_v2(compute_resource_id)
1058
1010
 
1059
- def get_tenant_id_v1 (self, compute_resource_id):
1011
+ def get_tenant_id_v1(self, compute_resource_id):
1060
1012
  """
1061
1013
  :param compute_resource_id: int
1062
1014
  :return: str
1063
1015
  """
1064
- logging.debug ('Reached get_tenant_id_v1')
1065
- compute_resource_info = self.get("compute_resources/{}".format(compute_resource_id)).json()
1016
+ logging.debug('Reached get_tenant_id_v1')
1017
+ compute_resource_info = self.get(posixpath.join("compute_resources", str(compute_resource_id))).json()
1018
+
1066
1019
  try:
1067
1020
  tenant_id = compute_resource_info["compute_attributes"][0]["attributes"]["tenant_id"]
1068
1021
  return tenant_id
1069
1022
  except KeyError:
1070
1023
  return None
1071
-
1072
1024
 
1073
- def get_tenant_id_v2 (self, compute_resource_id):
1025
+ def get_tenant_id_v2(self, compute_resource_id):
1074
1026
  """
1075
1027
  :param compute_resource_id: int
1076
1028
  :return: str
1077
1029
  """
1078
- logging.debug ('Reached get_tenant_id_v2')
1079
- logging.debug ('Passing to get_tenant_id_v1')
1080
- return self.get_tenant_id_v1 (compute_resource_id)
1081
-
1030
+ logging.debug('Reached get_tenant_id_v2')
1031
+ logging.debug('Passing to get_tenant_id_v1')
1032
+ return self.get_tenant_id_v1(compute_resource_id)
1082
1033
 
1083
- def get_hosts_uuids (self, hostnames):
1034
+ def get_hosts_uuids(self, hostnames):
1084
1035
  """
1085
1036
  wrapper api v1/v2
1086
1037
  """
1087
- logging.debug ('Reached get_hosts_uuids')
1038
+ logging.debug('Reached get_hosts_uuids')
1088
1039
  if self.apiversion == 1:
1089
- logging.debug ('Passing to get_hosts_uuids_v1')
1090
- return self.get_hosts_uuids_v1 (hostnames)
1040
+ logging.debug('Passing to get_hosts_uuids_v1')
1041
+ return self.get_hosts_uuids_v1(hostnames)
1091
1042
  elif self.apiversion == 2:
1092
- logging.debug ('Passing to get_hosts_uuids_v2')
1093
- return self.get_hosts_uuids_v2 (hostnames)
1043
+ logging.debug('Passing to get_hosts_uuids_v2')
1044
+ return self.get_hosts_uuids_v2(hostnames)
1094
1045
 
1095
-
1096
- def get_hosts_uuids_v1 (self, hostnames):
1046
+ def get_hosts_uuids_v1(self, hostnames):
1097
1047
  """
1098
1048
  :param hostnames: list
1099
1049
  :return: dict
1100
1050
  """
1101
- logging.debug ('Reached get_hosts_uuids_v1')
1051
+ logging.debug('Reached get_hosts_uuids_v1')
1102
1052
  hosts_total_qty = self.get("hosts", params={"per_page": 1}).json()["total"]
1103
- hostnames_uuids = {host["name"]: host["uuid"] for host in self.get("hosts", params={"per_page": hosts_total_qty}).json()["results"]}
1053
+ hostnames_uuids = {host["name"]: host["uuid"]
1054
+ for host in self.get("hosts", params={"per_page": hosts_total_qty}).json()["results"]}
1104
1055
  uuids = {hostname: uuid for hostname, uuid in hostnames_uuids.items() if hostname in hostnames}
1105
1056
  return uuids
1106
-
1107
1057
 
1108
- def get_hosts_uuids_v2 (self, hostnames):
1058
+ def get_hosts_uuids_v2(self, hostnames):
1109
1059
  """
1110
1060
  :param hostnames: list
1111
1061
  :return: dict
1112
1062
  """
1113
- logging.debug ('Reached get_hosts_uuids_v2')
1114
- logging.debug ('Passing to get_hosts_uuids_v1')
1115
- return self.get_hosts_uuids_v1 (hostnames)
1116
-
1063
+ logging.debug('Reached get_hosts_uuids_v2')
1064
+ logging.debug('Passing to get_hosts_uuids_v1')
1065
+ return self.get_hosts_uuids_v1(hostnames)
1117
1066
 
1118
- def get_host_uuid (self, hostname):
1067
+ def get_host_uuid(self, hostname):
1119
1068
  """
1120
1069
  wrapper api v1/v2
1121
1070
  """
1122
- logging.debug ('Reached get_host_uuid')
1071
+ logging.debug('Reached get_host_uuid')
1123
1072
  if self.apiversion == 1:
1124
- logging.debug ('Passing to get_host_uuid_v1')
1125
- return self.get_host_uuid_v1 (hostname)
1073
+ logging.debug('Passing to get_host_uuid_v1')
1074
+ return self.get_host_uuid_v1(hostname)
1126
1075
  elif self.apiversion == 2:
1127
- logging.debug ('Passing to get_host_uuid_v2')
1128
- return self.get_host_uuid_v2 (hostname)
1076
+ logging.debug('Passing to get_host_uuid_v2')
1077
+ return self.get_host_uuid_v2(hostname)
1129
1078
 
1130
-
1131
- def get_host_uuid_v1 (self, hostname):
1079
+ def get_host_uuid_v1(self, hostname):
1132
1080
  """
1133
1081
  :param hostname: str
1134
1082
  :return: str
1135
1083
  """
1136
- logging.debug ('Reached get_host_uuid_v1')
1084
+ logging.debug('Reached get_host_uuid_v1')
1137
1085
  try:
1138
- return self.get("hosts/{}".format(hostname)).json()["uuid"]
1086
+ return self.get(posixpath.join("hosts", hostname)).json()["uuid"]
1139
1087
  except KeyError:
1140
1088
  return None
1141
1089
 
1142
- def get_host_uuid_v2 (self, hostname):
1090
+ def get_host_uuid_v2(self, hostname):
1143
1091
  """
1144
1092
  :param hostname: str
1145
1093
  :return: str
1146
1094
  """
1147
- logging.debug ('Reached get_host_uuid_v2')
1148
- logging.debug ('Passing to get_host_uuid_v1')
1149
- return self.get_host_uuid_v1 (hostname)
1150
-
1095
+ logging.debug('Reached get_host_uuid_v2')
1096
+ logging.debug('Passing to get_host_uuid_v1')
1097
+ return self.get_host_uuid_v1(hostname)
1098
+
1151
1099
  def get_all_users(self):
1152
1100
  """
1153
1101
  :return: list
1154
1102
  """
1155
1103
  users_qty = self.get("users", params={"per_page": 1}).json()["total"]
1156
- users = [{"firstname": user["firstname"], "lastname": user["lastname"], "login": user["login"]} for user in self.get("users", params={"per_page": users_qty}).json()["results"]]
1104
+ users = [{"firstname": user["firstname"], "lastname": user["lastname"], "login": user["login"]}
1105
+ for user in self.get("users", params={"per_page": users_qty}).json()["results"]]
1157
1106
  return users
1158
-
1107
+
1159
1108
  def get_all_usergroups(self):
1160
1109
  """
1161
1110
  :return: list