david-caro / python-foreman

Small low level python wrapper around Foreman API
GNU General Public License v2.0
57 stars 37 forks source link

Foreman upgrade to 1.19 Issue #93

Closed iahmad-khan closed 5 years ago

iahmad-khan commented 5 years ago

After upgrating to foreman 1.19 , host creation doesnt work.

There is no error on client side.

Server logs:

2018-09-04T14:03:01 [I|app|] Started POST "/api/hosts" for IP_address at 2018-09-04 14:03:01 +0100
2018-09-04T14:03:01 [I|app|a93f4] Processing by Api::V2::HostsController#create as JSON
2018-09-04T14:03:01 [I|app|a93f4]   Parameters: {"host"=>{"name"=>"test-vm", "hostgroup_id"=>8}, "apiv"=>"v2"}
2018-09-04T14:03:01 [I|app|a93f4] Redirected to https:/formean-url/api/hosts
2018-09-04T14:03:01 [I|app|a93f4] Filter chain halted as #<Proc:0x000000000a500460@/opt/theforeman/tfm-ror51/root/usr/share/gems/gems/actionpack-5.1.6/lib/action_controller/metal/force_ssl.rb:65> rendered or redirected
2018-09-04T14:03:01 [I|app|a93f4] Completed 301 Moved Permanently in 1ms (ActiveRecord: 0.0ms)
2018-09-04T14:03:01 [I|app|] Started GET "/api/hosts" for 172.22.69.245 at 2018-09-04 14:03:01 +0100
2018-09-04T14:03:01 [I|app|ee986] Processing by Api::V2::HostsController#index as JSON

Python Version 2.7

Code ( it was working before foreman upgrade )

#!/usr/bin/env python2.7

import sys
import json
import urllib3
from foreman.client import Foreman
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# prepare the info
password = sys.argv[1]
user_foreman = sys.argv[2]
vmname = sys.argv[3]
host_group = int(sys.argv[4])
mac = sys.argv[5]

# get the foreman connection
try:
  f = Foreman('http://foreman-host', (user_foreman, password),api_version=2,verify=False)
  info = {}
  info["name"] = vmname
  info["mac"] = mac
  info["domain_id"] = 1
  info["hostgroup_id"] = host_group
  info["build"] = 1
  f.create_hosts(host=info)
except Exception as e:
  print("\n .... cloud not register the VM  ..." , e)
david-caro commented 5 years ago

It looks like it's trying to force you to go to https, can you try using https for the url when instantiating the Foreman client?

david-caro commented 5 years ago

It's strange though if the client itself would not follow the redirect :/

iahmad-khan commented 5 years ago

@david-caro putting https in there did the trick. thanks alot. cheers