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