diff --git a/README.md b/README.md index 8493842..453972c 100644 --- a/README.md +++ b/README.md @@ -1,58 +1,85 @@ # phpIPAMClient -This will be a nice api client for phpIPAM. It is in work, so just take a coffee chill some days :) +phpIPAM is an open-source IP address management web application which can be controlled by an api. +This is an api client for php which can be used to control phpIPAM. + +Learn more: + +* [phpIPAM Homepage](https://phpipam.net/) +* [phpIPAM Source](https://github.com/phpipam/phpipam) +* [phpIPAM API Docs](https://phpipam.net/api/api_documentation/) ## Installation You can install this package through composer: `composer require colq2/phpipam-client` -## Usage +## Basic Usage You can use this client in two ways. 1. Use the controller classes and let the client manage the most things for you -2. Use the call methode +2. Use the call method on the client object + +Notice: I only provide secure connections to the server. The API provides two ways to do that: +* SSL +* Own encryption + +Read more on [Security](#security) + ### Initialize connection +`use colq2\PhpIPAMClient;` -`$client = new colq2\PhpIPAMClient('1.2.3.4', 'myApp', 'name', 'password', 'apikey');` +`$client = new PhpIPAMClient('1.2.3.4', 'myApp', 'name', 'password', 'apikey', );` or -`colq2\Connection\Connection::initializeConnection('1.2.3.4', 'myApp', 'name', 'password', 'apikey');` +`use colq2\PhpIPAMClient\Connection\Connection;` + +`Connection::initializeConnection('1.2.3.4', 'myApp', 'name', 'password', 'apikey');` ### First Way I will use the sections controller as Example. -Every method from the api is implemented in the controller class. +It's important to understand, that this api ist strong object oriented. This means, that the controllers can be used as objects that represents the objects server side. The objects provides getter and setter for their attributes. And you can set client side objects to the attribute. This client handles to conversion between Object and IDs on it's own! +Import the controller with `use colq2\PhpIPAMClient\Controller\Section;` at the top. #### Get all sections To get all sections use the getAll method. This return an array with all sections as `colq2\Controller\Section` instances. -`$sections = colq2\Controller\Section::getAll()` +`$sections = Section::getAll();` #### Get section by id or name -`$section = colq2\Controller\Section::getByID('1')` +`$section = Section::getByID('1');` -`$section = colq2\Controller\Section::getByName('name')` +`$section = Section::getByName('name');` #### Create a section You can use the construct or the create method. With the construct: -`$section = new Section(['params_array'])` +`$section = new Section(['params_array']);` + +And use the getter and setter: + +`$section->setDescription();` -But you need to use the patch method to safe it: +You need to use the patch method to safe it: -`$section->patch()` +`$section->patch();` -With the create method: +#### Patch a section -`$section = colq2\Controller\Section::create(['params_array'])` +To patch a section use the setter on it or an array to the method. Or both! +But the array will override the given attributes. -Here you don't need to safe, this will create instantly a section on the server. +`$section->setDescription('A cool description!');` + +#### Delete a section + +`$section->delete();` ### Second Way `$response = $client->call('method', 'controller', ['identifier_array'], ['param_array']);` @@ -66,5 +93,59 @@ To get the whole response body use the getBody method. `$body = $response->getBody()` +Learn more on [Response Object](docs/response.md) + +## Controller + +#### Notice + +This docs are not ready yet. I'm working on it. Read the phpIPAM Api docs. I mapped the request url's to name. For example: +* GET /api/my_app/subnets/{id}/ => $subnet = Subnet::getById($id); +* GET /api/my_app/subnets/{id}/usage/ => $subnet->getUsage(); +* PATCH /api/my_app/subnets/{id}/ => Subnet::patch(); + +For more detailed information for all methods and controller look at [phpIPAM website](https://phpipam.net) and at the following controller documentation: + +* [Sections](docs/section.md) +* [Subnets](docs/subnet.md) +* [Folders](docs/folder.md) +* [VLAN](docs/vlan.md) +* [Address](docs/address.md) +* [L2 Domain](docs/L2Domain.md) +* [VRF](docs/vrf.md) +* [Device](docs/device.md) + +## Security + +The last parameter a of the construct is a value of the following constants: + +`use colq2\PhpIPAMClient\Connection\Connection` +* `Connection::SECURITY_METHOD_SSL` +* `Connection::SECURITY_METHOD_CRYPT` +* `Connection::SECURITY_METHOD_BOTH` + +I recommend to use the SLL method. For this you need a https connection to your Server! + +If you don't have the possibility to use SSL use crypt. You need to provide the API Key from the Backend. + +`$client = new PhpIPAMClient('www.example.com/phpipam', 'myApp','' , '', 'your app key, Connection::SECURITY_METHOD_CRYPT)` + +And also if you are paranoid security fanatics (like me) use the `Connection::SECURITY_METHOD_BOTH` + +`$client = new PhpIPAMClient('https://www.example.com/phpipam', 'myApp','' , '', 'your app key, Connection::SECURITY_METHOD_BOTH)` + +## Exceptions + +There are two types of Exceptions: +* `PhpIPAMException` + +This will be thrown when something went wrong with the api client. + +* `PhpIPAMRequestException` + +This will be thrown if a request fails. This Exception saves the response so you can lookup the error: +`$response = $e->getResponse();` + +Read more on [Response Object](docs/response.md) ## License MIT \ No newline at end of file diff --git a/docs/L2Domain.md b/docs/L2Domain.md new file mode 100644 index 0000000..d022bb0 --- /dev/null +++ b/docs/L2Domain.md @@ -0,0 +1,5 @@ +# L2Domain controller + +Please forgive me. + +I will write this docs soon! \ No newline at end of file diff --git a/docs/address.md b/docs/address.md new file mode 100644 index 0000000..d0de9b2 --- /dev/null +++ b/docs/address.md @@ -0,0 +1,5 @@ +# Address controller + +Please forgive me. + +I will write this docs soon! \ No newline at end of file diff --git a/docs/device.md b/docs/device.md new file mode 100644 index 0000000..3d50196 --- /dev/null +++ b/docs/device.md @@ -0,0 +1,5 @@ +# Device controller + +Please forgive me. + +I will write this docs soon! \ No newline at end of file diff --git a/docs/folder.md b/docs/folder.md new file mode 100644 index 0000000..4fe650a --- /dev/null +++ b/docs/folder.md @@ -0,0 +1,5 @@ +# Folder controller + +Please forgive me. + +I will write this docs soon! \ No newline at end of file diff --git a/docs/response.md b/docs/response.md new file mode 100644 index 0000000..ffa9752 --- /dev/null +++ b/docs/response.md @@ -0,0 +1,43 @@ +# Response + +The `colq2\PhpIPAMClient\Connection\Respose` Object is used to handle the Response from the server. +Also this is the return of the `call` function of the `PhpIPAMClient`. + +The object has 6 attributes: +* code +* success +* message +* data +* time +* body + +## Code +HTTP status code. + +Get: +`$response->getCode()` + +## Success +Determines if the request was successful. + +`$response->isSuccess()` + +## Message +Is only set when a request fails. It contains the error message. + +`$response->getMessage()` + +## Data +Contains the returned data as an array. + +`$response->getData()` + +## Time +Contains how long the server took to handle the request. + +`$response->getTime()` + +## Body +This is the raw body which was sent by the server. It is json encoded. + +`$response->getBody()` \ No newline at end of file diff --git a/docs/section.md b/docs/section.md index e69de29..8a12557 100644 --- a/docs/section.md +++ b/docs/section.md @@ -0,0 +1,5 @@ +# Section controller + +Please forgive me. + +I will write this docs soon! \ No newline at end of file diff --git a/docs/subnet.md b/docs/subnet.md new file mode 100644 index 0000000..cdb1213 --- /dev/null +++ b/docs/subnet.md @@ -0,0 +1,5 @@ +# Subnet controller + +Please forgive me. + +I will write this docs soon! \ No newline at end of file diff --git a/docs/vlan.md b/docs/vlan.md new file mode 100644 index 0000000..62ecac2 --- /dev/null +++ b/docs/vlan.md @@ -0,0 +1,5 @@ +# VLAN controller + +Please forgive me. + +I will write this docs soon! \ No newline at end of file diff --git a/docs/vrf.md b/docs/vrf.md new file mode 100644 index 0000000..024d7e4 --- /dev/null +++ b/docs/vrf.md @@ -0,0 +1,5 @@ +# VRF controller + +Please forgive me. + +I will write this docs soon! \ No newline at end of file