Skip to content

Commit

Permalink
Merge pull request #15 from HD-CMS/feature/namespace-resource
Browse files Browse the repository at this point in the history
Add Namespace Resource
  • Loading branch information
maclof authored Dec 5, 2017
2 parents c8d9646 + f293ce0 commit 29ca75e
Show file tree
Hide file tree
Showing 11 changed files with 137 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
/coverage
/clover.xml
/composer.lock
/composer.lock
/.idea
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ $ composer require maclof/kubernetes-client
## Supported API Features
### v1
* Nodes
* Namespaces
* Pods
* Replica Sets
* Replication Controllers
Expand Down
5 changes: 4 additions & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Maclof\Kubernetes\Repositories\ReplicationControllerRepository;
use Maclof\Kubernetes\Repositories\SecretRepository;
use Maclof\Kubernetes\Repositories\ServiceRepository;
use Maclof\Kubernetes\Repositories\NamespaceRepository;

/**
* @method NodeRepository nodes()
Expand All @@ -39,6 +40,7 @@
* @method DaemonSetRepository daemonSets()
* @method DeploymentRepository deployments()
* @method IngressRepository ingresses()
* @method NamespaceRepository namespaces()
*/
class Client
{
Expand Down Expand Up @@ -129,7 +131,8 @@ class Client
'configMaps' => 'Repositories\ConfigMapRepository',
'endpoints' => 'Repositories\EndpointRepository',
'persistentVolumeClaims' => 'Repositories\PersistentVolumeClaimRepository',

'namespaces' => 'Repositories\NamespaceRepository',

// batch/v1
'jobs' => 'Repositories\JobRepository',

Expand Down
31 changes: 31 additions & 0 deletions src/Collections/NamespaceCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php namespace Maclof\Kubernetes\Collections;

use Maclof\Kubernetes\Models\NamespaceModel;

class NamespaceCollection extends Collection
{
/**
* The constructor.
*
* @param array $data
*/
public function __construct(array $data)
{
parent::__construct($this->getNamespaces(isset($data['items']) ? $data['items'] : []));
}

/**
* Get an array of Namespaces.
*
* @param array $items
* @return array
*/
protected function getNamespaces(array $items)
{
foreach ($items as &$item) {
$item = new NamespaceModel($item);
}

return $items;
}
}
12 changes: 8 additions & 4 deletions src/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ public function getMetadata($key)
*/
public function getSchema()
{
$this->schema['kind'] = basename(str_replace('\\', '/', get_class($this)));
if ($this->pluralKind) {
$this->schema['kind'] .= 's';
if (!isset($this->schema['kind'])) {
$this->schema['kind'] = basename(str_replace('\\', '/', get_class($this)));
if ($this->pluralKind) {
$this->schema['kind'] .= 's';
}
}

$this->schema['apiVersion'] = $this->apiVersion;
if (!isset($this->schema['apiVersion'])) {
$this->schema['apiVersion'] = $this->apiVersion;
}

$schema = array_merge($this->schema, $this->toArray());

Expand Down
11 changes: 11 additions & 0 deletions src/Models/NamespaceModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace Maclof\Kubernetes\Models;

class NamespaceModel extends Model
{
/**
* The schema.
*
* @var array
*/
protected $schema = ['kind' => 'Namespace'];
}
14 changes: 14 additions & 0 deletions src/Repositories/NamespaceRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php namespace Maclof\Kubernetes\Repositories;

use Maclof\Kubernetes\Collections\NamespaceCollection;

class NamespaceRepository extends Repository
{
protected $uri = 'namespaces';
protected $namespace = false;

protected function createCollection($response)
{
return new NamespaceCollection($response);
}
}
6 changes: 3 additions & 3 deletions src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ protected function getApiVersion()
*/
public function create(Model $model)
{
$this->sendRequest('POST', '/' . $this->uri, null, $model->getSchema());
$this->sendRequest('POST', '/' . $this->uri, null, $model->getSchema(), $this->namespace);
return true;
}

Expand All @@ -113,7 +113,7 @@ public function create(Model $model)
*/
public function update(Model $model)
{
$this->sendRequest('PUT', '/' . $this->uri . '/' . $model->getMetadata('name'), null, $model->getSchema());
$this->sendRequest('PUT', '/' . $this->uri . '/' . $model->getMetadata('name'), null, $model->getSchema(), $this->namespace);
return true;
}

Expand All @@ -139,7 +139,7 @@ public function delete(Model $model, DeleteOptions $options = null)
public function deleteByName($name, DeleteOptions $options = null)
{
$body = $options ? $options->getSchema() : null;
$this->sendRequest('DELETE', '/' . $this->uri . '/' . $name, null, $body);
$this->sendRequest('DELETE', '/' . $this->uri . '/' . $name, null, $body, $this->namespace);
return true;
}

Expand Down
30 changes: 30 additions & 0 deletions tests/collections/NamespaceCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Maclof\Kubernetes\Collections\NamespaceCollection;

class NamespaceCollectionTest extends TestCase
{
protected $items = [
[],
[],
[],
];

protected function getNamespaceCollection()
{
$namespaceCollection = new NamespaceCollection([
'items' => $this->items,
]);

return $namespaceCollection;
}

public function test_get_items()
{
$namespaceCollection = $this->getNamespaceCollection();
$items = $namespaceCollection->toArray();

$this->assertTrue(is_array($items));
$this->assertEquals(3, count($items));
}
}
4 changes: 4 additions & 0 deletions tests/fixtures/namespaces/empty.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"kind": "Namespace",
"apiVersion": "v1"
}
29 changes: 29 additions & 0 deletions tests/models/NamespaceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

use Maclof\Kubernetes\Models\NamespaceModel;

class NamespaceTest extends TestCase
{
public function test_get_schema()
{
$namespace = new NamespaceModel;

$schema = $namespace->getSchema();
$fixture = $this->getFixture('namespaces/empty.json');

$this->assertEquals($schema, $fixture);
}

public function test_get_metadata()
{
$node = new NamespaceModel([
'metadata' => [
'name' => 'test',
],
]);

$metadata = $node->getMetadata('name');

$this->assertEquals($metadata, 'test');
}
}

0 comments on commit 29ca75e

Please sign in to comment.