From 98f5a0259aa1b69bb2d54aa0a7855d909390427a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9sar=20Mu=C3=B1oz?= Date: Tue, 8 Jul 2014 21:24:36 -0500 Subject: [PATCH] Added one more test, improved docs and CONTRIBUTING.md --- CHANGELOG.md | 6 ++- CONTRIBUTING.md | 25 ++++++++++++ doc/core_concepts.md | 45 ++++++++++++++++++++++ src/Proxmox.php | 20 +++++++--- tests/CustomCredentials/BadCredentials.php | 18 +++++++++ tests/ProxmoxTest.php | 18 ++++----- 6 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 CONTRIBUTING.md create mode 100644 tests/CustomCredentials/BadCredentials.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 40cf713..3586de6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,10 @@ -v2.0.0 +v2.1.0 ------ - Now it is possible to specify the desired return type when creating Proxmox object, setters and getters for return type were added. -- Added docs about getters & setters of the Proxmox client object. +- Added docs about getters & setters and other tricks of the Proxmox client object. +- Now you can create a Proxmox API client object with a custom credentials object. + v2.0.0 ------ diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..8f88702 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,25 @@ +Welcome developers! +================== + +Before making any contribution you should now: + +- All code contribution should follow PSR-1 and PSR-2 coding style standards. (Thanks to [Alexey Ashurok](https://github.com/aotd1)) +- You should code the tests for any function you add, some times is not possible but try doing it. +- All functions need to be properly documented, all comments, variable names, function names only on english language. +- Variables and functions names should be self descriptive. + + +Installation +------------ + +Of course you should [fork the repo](https://github.com/ZzAntares/ProxmoxVE/fork), then after cloning your forked repo: + +```sh +$ composer install --dev # run cmd inside the project folder +``` + +What needs to be done? +---------------------- + +What ever you think will improve this library and also let's wait the people to open issues and then we'll see. + diff --git a/doc/core_concepts.md b/doc/core_concepts.md index 4573584..b298bd1 100644 --- a/doc/core_concepts.md +++ b/doc/core_concepts.md @@ -182,6 +182,51 @@ echo 'Hostname: ' . $credentialsB->getHostname(); // Hostname: hostB ``` +Using custom credentials object +------------------------------- + +You can pass your own custom credentials object when creating the API client object, for now this library internally will create a valid credentials object. The only thing you custom credentials object needs, is to have the required accesible properties: + +- `hostname` +- `username` +- `password` +- `realm` (optional defaults to `pam`) +- `port` (optional defaults to `8006`) + +If you feel using getters is better, the Proxmox API client object will search for the next getters if properties are not accesible: + +- `getHostname()` +- `getUsername()` +- `getPassword()` +- `getRealm()` (optional defaults to `pam`) +- `getPort()` (optional defaults to `8006`) + +```php +hostname = $host; + $this->username = $user; + $this->password = $pass; + } +} + +// Create an object of your custom credentials object +$customCredentials = new CustomCredentials('my.proxmox.tld', 'user', 'secret'); + +// Pass your custom credentials when creating the API client +$proxmox = new ProxmoxVE\Proxmox($customCredentials); + +// At this point you can use the $proxmox normally +``` + +> **Why is this useful?** Personally when dealing with Eloquent models I already have a Credentials object, so I want to use that object to login to a proxmox server. + Set and get response type ------------------------- diff --git a/src/Proxmox.php b/src/Proxmox.php index 6b31975..416993d 100644 --- a/src/Proxmox.php +++ b/src/Proxmox.php @@ -172,10 +172,20 @@ public function getCredentials() /** * Assign the passed Credentials object to the ProxmoxVE. * - * @param \ProxmoxVE\Credentials $credentials to assign. + * @param object $credentials A custom object holding credentials or a + * Credentials object to assign. */ - public function setCredentials(Credentials $credentials) + public function setCredentials($credentials) { + if (!$credentials instanceof Credentials) { + if (!$this->validCredentialsObject($credentials)) { + $errorMessage = 'setCredentials needs a valid object.'; + throw new \InvalidArgumentException($errorMessage); + } + + $credentials = $this->loginUsingCredentials($credentials); + } + $this->credentials = $credentials; $token = $credentials->login(); @@ -403,7 +413,7 @@ public function validCredentialsObject($credentials) * * @param object $credentials A custom object holding proxmox login data. */ - public function loginUsingCredentials($credentials) + protected function loginUsingCredentials($credentials) { if ($this->accessibleBy == 'properties') { @@ -411,8 +421,8 @@ public function loginUsingCredentials($credentials) $credentials->hostname, $credentials->username, $credentials->password, - isset($credentials->realm) ? $credentials->realm : 'pam', // Make it optional? - isset($credentials->port) ? $credentials->port : '8006' // Make it optional? + isset($credentials->realm) ? $credentials->realm : 'pam', + isset($credentials->port) ? $credentials->port : '8006' ); } diff --git a/tests/CustomCredentials/BadCredentials.php b/tests/CustomCredentials/BadCredentials.php new file mode 100644 index 0000000..0f64505 --- /dev/null +++ b/tests/CustomCredentials/BadCredentials.php @@ -0,0 +1,18 @@ +hostname = $host; + $this->username = $user; + $this->password = $pass; + } +} diff --git a/tests/ProxmoxTest.php b/tests/ProxmoxTest.php index 3a6b85b..ae1933f 100644 --- a/tests/ProxmoxTest.php +++ b/tests/ProxmoxTest.php @@ -212,23 +212,23 @@ public function testValidCredentialsObject() $credentials = $this->getMockCredentials(array('host', 'user', 'pass')); $proxmox = new Proxmox($credentials); - $propertiesCredentials = new CustomCredentials\PropertiesCredentials('host', 'user', 'pass', 'realm', 'port'); - $methodsCredentials = new CustomCredentials\MethodsCredentials('host', 'user', 'pass', 'realm', 'port'); - $this->assertFalse($proxmox->validCredentialsObject('not an object')); + + $propertiesCredentials = new CustomCredentials\PropertiesCredentials('host', 'user', 'pass', 'realm', 'port'); $this->assertTrue($proxmox->validCredentialsObject($propertiesCredentials)); + + $methodsCredentials = new CustomCredentials\MethodsCredentials('host', 'user', 'pass', 'realm', 'port'); $this->assertTrue($proxmox->validCredentialsObject($methodsCredentials)); $propertiesOptCredentials = new CustomCredentials\PropertiesOptCredentials('host', 'user', 'pass'); - $methodsOptCredentials = new CustomCredentials\MethodsOptCredentials('host', 'user', 'pass'); - $this->assertTrue($proxmox->validCredentialsObject($propertiesOptCredentials)); + + $methodsOptCredentials = new CustomCredentials\MethodsOptCredentials('host', 'user', 'pass'); $this->assertTrue($proxmox->validCredentialsObject($methodsOptCredentials)); - } + $badCredentials = new CustomCredentials\BadCredentials('bad', 'user', 'passwd'); + $this->assertFalse($proxmox->validCredentialsObject($badCredentials)); + } - /** - * Probar que se pueda usar CustomCredentials con campos realm y port optativos - */ }