Skip to content

Commit 98f5a02

Browse files
committed
Added one more test, improved docs and CONTRIBUTING.md
1 parent a3ecc2b commit 98f5a02

File tree

6 files changed

+116
-16
lines changed

6 files changed

+116
-16
lines changed

CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
v2.0.0
1+
v2.1.0
22
------
33

44
- Now it is possible to specify the desired return type when creating Proxmox object, setters and getters for return type were added.
5-
- Added docs about getters & setters of the Proxmox client object.
5+
- Added docs about getters & setters and other tricks of the Proxmox client object.
6+
- Now you can create a Proxmox API client object with a custom credentials object.
7+
68

79
v2.0.0
810
------

CONTRIBUTING.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Welcome developers!
2+
==================
3+
4+
Before making any contribution you should now:
5+
6+
- All code contribution should follow PSR-1 and PSR-2 coding style standards. (Thanks to [Alexey Ashurok](https://github.com/aotd1))
7+
- You should code the tests for any function you add, some times is not possible but try doing it.
8+
- All functions need to be properly documented, all comments, variable names, function names only on english language.
9+
- Variables and functions names should be self descriptive.
10+
11+
12+
Installation
13+
------------
14+
15+
Of course you should [fork the repo](https://github.com/ZzAntares/ProxmoxVE/fork), then after cloning your forked repo:
16+
17+
```sh
18+
$ composer install --dev # run cmd inside the project folder
19+
```
20+
21+
What needs to be done?
22+
----------------------
23+
24+
What ever you think will improve this library and also let's wait the people to open issues and then we'll see.
25+

doc/core_concepts.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,51 @@ echo 'Hostname: ' . $credentialsB->getHostname(); // Hostname: hostB
182182
```
183183

184184

185+
Using custom credentials object
186+
-------------------------------
187+
188+
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:
189+
190+
- `hostname`
191+
- `username`
192+
- `password`
193+
- `realm` (optional defaults to `pam`)
194+
- `port` (optional defaults to `8006`)
195+
196+
If you feel using getters is better, the Proxmox API client object will search for the next getters if properties are not accesible:
197+
198+
- `getHostname()`
199+
- `getUsername()`
200+
- `getPassword()`
201+
- `getRealm()` (optional defaults to `pam`)
202+
- `getPort()` (optional defaults to `8006`)
203+
204+
```php
205+
<?
206+
require_once 'vendor/autoload.php';
207+
208+
// Example of custom credentials class
209+
class CustomCredentials
210+
{
211+
public function __construct($host, $user, $pass)
212+
{
213+
$this->hostname = $host;
214+
$this->username = $user;
215+
$this->password = $pass;
216+
}
217+
}
218+
219+
// Create an object of your custom credentials object
220+
$customCredentials = new CustomCredentials('my.proxmox.tld', 'user', 'secret');
221+
222+
// Pass your custom credentials when creating the API client
223+
$proxmox = new ProxmoxVE\Proxmox($customCredentials);
224+
225+
// At this point you can use the $proxmox normally
226+
```
227+
228+
> **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.
229+
185230
Set and get response type
186231
-------------------------
187232

src/Proxmox.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,20 @@ public function getCredentials()
172172
/**
173173
* Assign the passed Credentials object to the ProxmoxVE.
174174
*
175-
* @param \ProxmoxVE\Credentials $credentials to assign.
175+
* @param object $credentials A custom object holding credentials or a
176+
* Credentials object to assign.
176177
*/
177-
public function setCredentials(Credentials $credentials)
178+
public function setCredentials($credentials)
178179
{
180+
if (!$credentials instanceof Credentials) {
181+
if (!$this->validCredentialsObject($credentials)) {
182+
$errorMessage = 'setCredentials needs a valid object.';
183+
throw new \InvalidArgumentException($errorMessage);
184+
}
185+
186+
$credentials = $this->loginUsingCredentials($credentials);
187+
}
188+
179189
$this->credentials = $credentials;
180190
$token = $credentials->login();
181191

@@ -403,16 +413,16 @@ public function validCredentialsObject($credentials)
403413
*
404414
* @param object $credentials A custom object holding proxmox login data.
405415
*/
406-
public function loginUsingCredentials($credentials)
416+
protected function loginUsingCredentials($credentials)
407417
{
408418

409419
if ($this->accessibleBy == 'properties') {
410420
return new Credentials(
411421
$credentials->hostname,
412422
$credentials->username,
413423
$credentials->password,
414-
isset($credentials->realm) ? $credentials->realm : 'pam', // Make it optional?
415-
isset($credentials->port) ? $credentials->port : '8006' // Make it optional?
424+
isset($credentials->realm) ? $credentials->realm : 'pam',
425+
isset($credentials->port) ? $credentials->port : '8006'
416426
);
417427
}
418428

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace ProxmoxVE\CustomCredentials;
4+
5+
class BadCredentials
6+
{
7+
private $hostname;
8+
private $username;
9+
private $password;
10+
11+
12+
public function __construct($host, $user, $pass)
13+
{
14+
$this->hostname = $host;
15+
$this->username = $user;
16+
$this->password = $pass;
17+
}
18+
}

tests/ProxmoxTest.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,23 @@ public function testValidCredentialsObject()
212212
$credentials = $this->getMockCredentials(array('host', 'user', 'pass'));
213213
$proxmox = new Proxmox($credentials);
214214

215-
$propertiesCredentials = new CustomCredentials\PropertiesCredentials('host', 'user', 'pass', 'realm', 'port');
216-
$methodsCredentials = new CustomCredentials\MethodsCredentials('host', 'user', 'pass', 'realm', 'port');
217-
218215
$this->assertFalse($proxmox->validCredentialsObject('not an object'));
216+
217+
$propertiesCredentials = new CustomCredentials\PropertiesCredentials('host', 'user', 'pass', 'realm', 'port');
219218
$this->assertTrue($proxmox->validCredentialsObject($propertiesCredentials));
219+
220+
$methodsCredentials = new CustomCredentials\MethodsCredentials('host', 'user', 'pass', 'realm', 'port');
220221
$this->assertTrue($proxmox->validCredentialsObject($methodsCredentials));
221222

222223
$propertiesOptCredentials = new CustomCredentials\PropertiesOptCredentials('host', 'user', 'pass');
223-
$methodsOptCredentials = new CustomCredentials\MethodsOptCredentials('host', 'user', 'pass');
224-
225224
$this->assertTrue($proxmox->validCredentialsObject($propertiesOptCredentials));
225+
226+
$methodsOptCredentials = new CustomCredentials\MethodsOptCredentials('host', 'user', 'pass');
226227
$this->assertTrue($proxmox->validCredentialsObject($methodsOptCredentials));
227-
}
228228

229+
$badCredentials = new CustomCredentials\BadCredentials('bad', 'user', 'passwd');
230+
$this->assertFalse($proxmox->validCredentialsObject($badCredentials));
231+
}
229232

230-
/**
231-
* Probar que se pueda usar CustomCredentials con campos realm y port optativos
232-
*/
233233
}
234234

0 commit comments

Comments
 (0)