david-caro / python-foreman

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

Creating interfaces #79

Closed Mandorath closed 7 years ago

Mandorath commented 7 years ago

How would I create interfaces for hosts with the API via /api/hosts/:host_id/interfaces? I've tried several things but every object that is requires an ID of an object declarated before its own I can't get to work. Is this possible?

david-caro commented 7 years ago

If you give me the version of foreman you are running I might give you a more accurate answer, but looking to the 1.14 api (https://theforeman.org/api/1.14/apidoc/v2/interfaces/create.html) you should be able to create a new one with:

cli = Foreman('http://localhost', api_version=2, version='1.9.2')
cli.hosts.interfaces_create(
    host_id=myhostid,
    interface=myinterfacedef,
)

Where the myinterfacedef is a dict with the params required by the api call. If you want to check which params you need dynamically, I recommend using ipython, and then hitting:

ipython> cli.hosts.interfaces_create?

That will show the docs, where a copy of that info is shown, something like:

Signature: cli.hosts.interfaces_create(interface, host_id)
Docstring:
Create an interface on a host

:param interface: interface information; Must be a Hash (REQUIRED)
:param interface[mac]: MAC address of interface. Required for managed interfaces on bare metal.; Must be String (OPTIONAL)
:param interface[ip]: IP address of interface; Must be String (OPTIONAL)
:param interface[type]: Interface type, e.g. bmc. Default is interface; Must be one of: interface, bmc, bond. (OPTIONAL)
:param interface[name]: Interface's DNS name; Must be String (OPTIONAL)
:param interface[subnet_id]: Foreman subnet ID of interface; Must be Fixnum (OPTIONAL)
:param interface[domain_id]: Foreman domain ID of interface. Required for primary interfaces on managed
hosts.; Must be Fixnum (OPTIONAL)
:param interface[identifier]: Device identifier, e.g. eth0 or eth1.1; Must be String (OPTIONAL)
:param interface[managed]: Should this interface be managed via DHCP and DNS smart proxy and should it
be configured during provisioning?; Must be 'true' or 'false' (OPTIONAL)
:param interface[primary]: Should this interface be used for constructing the FQDN of the host? Each
managed hosts needs to have one primary interface.; Must be 'true' or 'false' (OPTIONAL)
:param interface[provision]: Should this interface be used for TFTP of PXELinux (or SSH for image-based
hosts)? Each managed hosts needs to have one provision interface.; Must be 'true' or 'false' (OPTIONAL)
:param interface[username]: Only for BMC interfaces.; Must be String (OPTIONAL)
:param interface[password]: Only for BMC interfaces.; Must be String (OPTIONAL)
:param interface[provider]: Interface provider, e.g. IPMI. Only for BMC interfaces.; Must be one of: IPMI. (OPTIONAL)
:param interface[virtual]: Alias or VLAN device; Must be 'true' or 'false' (OPTIONAL)
:param interface[tag]: VLAN tag, this attribute has precedence over the subnet VLAN ID. Only for
virtual interfaces.; Must be String (OPTIONAL)
:param interface[attached_to]: Identifier of the interface to which this interface belongs, e.g. eth1.
Only for virtual interfaces.; Must be String (OPTIONAL)
:param interface[mode]: Bond mode of the interface, e.g. balance-rr. Only for bond interfaces.; Must be one of: balance-rr, active-backup, balance-xor, broadcast, 802.3ad, balance-tlb, balance-alb. (OPTIONAL)
:param interface[attached_devices]: Identifiers of slave interfaces, e.g. `['eth1', 'eth2']`. Only for bond
interfaces.; Must be an array of any type (OPTIONAL)
:param interface[bond_options]: Space separated options, e.g. miimon=100. Only for bond interfaces.; Must be String (OPTIONAL)
:param interface[compute_attributes]: Additional compute resource specific attributes for the interface.; Must be Hash (OPTIONAL)
:param host_id: ID or name of host; Must be String (REQUIRED)

From there, any param like interface[something]' means that the interface dict should have a key calledsomething`.

If you need more help, please reopen with some extra info or questions, though if is about how the foreman api internally works, you might have to reask to the foreman guys.

Cheers!

Mandorath commented 7 years ago

Thanks, I missed the _create, got the interface creation working now. Much appreciated!!!