Package: phpypam
phpypam: Python API client library for phpIPAM installationAs we started to develop phpipam-ansible-modules we used an existing python library for phpIPAM API. As we needed a good error handling and we don't expect a quick fix of existing project we started to develop our own library.installationThis library is hosted onpypi.org, so you can simply usepipto install it.pipinstallphpypamAlternatively you can install it from source. You need to do the following:$gitclonehttps://github.com/codeaffen/phpypam.git
Cloninginto'phpypam'...
remote:Enumeratingobjects:1,done.
remote:Countingobjects:100%(1/1),done.
remote:Total366(delta0),reused0(delta0),pack-reused365Receivingobjects:100%(366/366),88.57KiB|521.00KiB/s,done.
Resolvingdeltas:100%(187/187),done.
$cdphpypam/
$pythonsetup.pyinstallquick startTo start usingphpypamyou simply have to write some lines of code.importphpypampi=phpypam.api(url='https://ipam.example.com',app_id='ansible',username='apiuser',password='apiP455wd',ssl_verify=True)pi.get_entity(controller='sections')making api connectionTo connect to phpIPAM API you need some parameters to authenticate against the phpIPAM instance.ParameterDescriptionDefaulturlThe URL to a phpIPAM instance. It includes the protocol (http or https).app_idThe app_id which is used for the API operations.usernameTheusernamewhich is used to connect to API.NonepasswordThepasswordto authenticateusernameagainst API.Nonessl_verifyShould certificate of endpoint verified or not. Useful if you use a self signed certificate.TrueExampleconnect to api and request current token:connection_params=dict(url='https://ipam.example.com',app_id='ansible',username='apiuser',password='apiP455wd',ssl_verify=True)pi=phpypam.api(**connection_params)token=pi.get_token()First of all you create a dictionary with the connection data. This dictionary will unpacked for creating aphpypam.apiobject.If all went well you can use theget_tokento get the currently valid token from API.get available controllersTo work with the phpIPAM api it is useful if you know all available controllers. To achieve this you can either read the api documentation or you can use thecontrollersmethod.controllers=pi.controllers()The method returns a set with all supported controllers.get an entityTo get an entity theget_entitymethod has to be used.get_entity(controller,controller_path=None,params=None)Exampleget asectionby name:entity=pi.get_entity(controller='sections',controller_path='foobar')This call returns a dictionary for the entity with the namefoobar.create an entityTo create an entity thecreate_entitymethod has to be used.create_entity(controller,controller_path=None,data=None,params=None)Examplecreate asectionif it does not exists:my_section=dict(name='foobar',description='new section',permissions='{"3":"1","2":"2"}')try:entity=pi.get_entity(controller='sections',controller_path=my_section['name'])exceptPHPyPAMEntityNotFoundException:print('create entity')entity=pi.create_entity(controller='sections',data=my_section)In this example first we check if the section we work on already exists. If the PHPyPAMEntityNotFoundException is raised we create the entity.update an entityTo update an entity you have to use theupdate_entitymethod.update_entity(controller,controller_path=None,data=None,params=None)Exampleupdate asectionif it exists:my_section['description']='new description'entity=pi.get_entity(controller='sections',controller_path=my_section['name'])pi.update_entity(controller='sections',controller_path=entity['id'],data=my_section)To change data you have to modify the value of the desired key to the value you want. You can see the data is changed in the dict from the former example.
Then you get the entity to obtain its id to work on.Note:All modifying operations need the id of an entity not the name.In the last step you callupdate_entityand put the entity id in parametercontroller_pathwith thedataparameter you provide the fully entity description dictionary.delete an entityTo delete an entity you have to use thedelete_entitymethod.delete_entity(controller,controller_path,params=None)Exampledelete a existing section:entity=pi.get_entity(controller='sections',controller_path=my_section['name'])pi.delete_entity(controller='sections',controller_path=entity['id'])In this example you request the entity you had created/updated in the above examples.
After that you calldelete_entitywith the entity id from the request before.possible exceptionsPHPyPAMInvalidCredentials- will be raised if something goes wrong with the authenticationPHPyPAMEntityNotFoundException- will be raised if an entity does not existsPHPyPAMInvalidSyntax- will be raised for requests which will be answered with status code 400 from APIPHPyPAMException- for any errors which we catch but no specific exception exists this exception wil be raised