Skip to content

Commit

Permalink
Merge pull request #96 from nexcess/os-host-readonly
Browse files Browse the repository at this point in the history
[rtr] Hypervisor RO Access
  • Loading branch information
haphan authored Apr 27, 2017
2 parents 38325c0 + abfc7d8 commit 3451ef4
Show file tree
Hide file tree
Showing 13 changed files with 434 additions and 28 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
.test/
coverage/
vendor/

*.pyc

phpunit.xml
coverage.xml
composer.lock
env_test.sh
26 changes: 26 additions & 0 deletions samples/compute/v2/hypervisors/get_hypervisor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use OpenStack\Compute\v2\Models\Hypervisor;

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
'authUrl' => '{authUrl}',
'region' => '{region}',
'user' => [
'id' => '{userId}',
'password' => '{password}'
],
'scope' => ['project' => ['id' => '{projectId}']]
]);

$compute = $openstack->computeV2(['region' => '{region}']);

$hypervisor = $compute->getHypervisor(['id' => '{hypervisorId}']);

// By default, this will return an empty Server object and NOT hit the API.
// This is convenient for when you want to use the object for operations
// that do not require an initial GET request. To retrieve the server's details,
// run the following, which *will* call the API with a GET request:

$hypervisor->retrieve();
23 changes: 23 additions & 0 deletions samples/compute/v2/hypervisors/list_hypervisors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use OpenStack\Compute\v2\Models\Hypervisor;

require 'vendor/autoload.php';

$openstack = new OpenStack\OpenStack([
'authUrl' => '{authUrl}',
'region' => '{region}',
'user' => [
'id' => '{userId}',
'password' => '{password}'
],
'scope' => ['project' => ['id' => '{projectId}']]
]);

$compute = $openstack->computeV2(['region' => '{region}']);

$hypervisors = $compute->listHypervisors();

foreach ($hypervisors as $hypervisor) {
/**@var Hypervisor $hypervisor*/
}
27 changes: 27 additions & 0 deletions src/Compute/v2/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,31 @@ public function getHypervisorStatistics(): array
]
];
}

public function getHypervisors(): array
{
return [
'method' => 'GET',
'path' => 'os-hypervisors',
'jsonKey' => 'hypervisors',
'params' => [
],
];
}

public function getHypervisorsDetail(): array
{
$definition = $this->getHypervisors();
$definition['path'] .= '/detail';
return $definition;
}

public function getHypervisor(): array
{
return [
'method' => 'GET',
'path' => 'os-hypervisors/{id}',
'params' => ['id' => $this->params->urlId('hypervisor')]
];
}
}
78 changes: 78 additions & 0 deletions src/Compute/v2/Models/Hypervisor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php declare(strict_types=1);

namespace OpenStack\Compute\v2\Models;

use OpenStack\Common\Resource\Listable;
use OpenStack\Common\Resource\Retrievable;
use OpenStack\Common\Resource\OperatorResource;

/**
* @property \OpenStack\Compute\v2\Api $api
*/
class Hypervisor extends OperatorResource implements
Retrievable,
Listable
{
/** @var int */
public $id;

/** @var string */
public $status;

/** @var string */
public $state;

/** @var string */
public $host_ip;

/** @var int */
public $free_disk_gb;

/** @var int */
public $free_ram_mb;

/** @var string */
public $hypervisor_hostname;

/** @var string */
public $hypervisor_type;

/** @var string */
public $hypervisor_version;

/** @var int */
public $local_gb;

/** @var int */
public $local_gb_used;

/** @var int */
public $memory_mb;

/** @var int */
public $memory_mb_used;

/** @var int */
public $running_vms;

/** @var int */
public $vcpus;

/** @var int */
public $vcpus_used;

/** @var array */
public $service;

protected $resourceKey = 'hypervisor';
protected $resourcesKey = 'hypervisors';

/**
* {@inheritDoc}
*/
public function retrieve()
{
$response = $this->execute($this->api->getHypervisor(), ['id' => (string) $this->id]);
$this->populateFromResponse($response);
}
}
18 changes: 11 additions & 7 deletions src/Compute/v2/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class Server extends OperatorResource implements
/** @var string */
public $hostId;

/** @var string */
public $hypervisorHostname;

/** @var Image */
public $image;

Expand Down Expand Up @@ -86,13 +89,14 @@ class Server extends OperatorResource implements
protected $markerKey = 'id';

protected $aliases = [
'block_device_mapping_v2' => 'blockDeviceMapping',
'accessIPv4' => 'ipv4',
'accessIPv6' => 'ipv6',
'tenant_id' => 'tenantId',
'user_id' => 'userId',
'security_groups' => 'securityGroups',
'OS-EXT-STS:task_state' => 'taskState',
'block_device_mapping_v2' => 'blockDeviceMapping',
'accessIPv4' => 'ipv4',
'accessIPv6' => 'ipv6',
'tenant_id' => 'tenantId',
'user_id' => 'userId',
'security_groups' => 'securityGroups',
'OS-EXT-STS:task_state' => 'taskState',
'OS-EXT-SRV-ATTR:hypervisor_hostname' => 'hypervisorHostname',
];

/**
Expand Down
30 changes: 30 additions & 0 deletions src/Compute/v2/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenStack\Compute\v2\Models\Keypair;
use OpenStack\Compute\v2\Models\Limit;
use OpenStack\Compute\v2\Models\Server;
use OpenStack\Compute\v2\Models\Hypervisor;

/**
* Compute v2 service for OpenStack.
Expand Down Expand Up @@ -199,4 +200,33 @@ public function getHypervisorStatistics(): HypervisorStatistic
$statistics->populateFromResponse($this->execute($this->api->getHypervisorStatistics(), []));
return $statistics;
}

/**
* List hypervisors.
*
* @param bool $detailed Determines whether detailed information will be returned. If FALSE is specified, only
* the ID, name and links attributes are returned, saving bandwidth.
* @param array $options {@see \OpenStack\Compute\v2\Api::getHypervisors}
* @param callable $mapFn A callable function that will be invoked on every iteration of the list.
*
* @return \Generator
*/
public function listHypervisors(bool $detailed = false, array $options = [], callable $mapFn = null): \Generator
{
$def = ($detailed === true) ? $this->api->getHypervisorsDetail() : $this->api->getHypervisors();
return $this->model(Hypervisor::class)->enumerate($def, $options, $mapFn);
}

/**
* Shows details for a given hypervisor.
*
* @param array $options
*
* @return Hypervisor
*/
public function getHypervisor(array $options = []): Hypervisor
{
$hypervisor = $this->model(Hypervisor::class);
return $hypervisor->populateFromArray($options);
}
}
31 changes: 29 additions & 2 deletions tests/integration/Compute/v2/CoreTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use OpenStack\BlockStorage\v2\Models\Volume;
use OpenStack\Compute\v2\Models\Flavor;
use OpenStack\Compute\v2\Models\HypervisorStatistic;
use OpenStack\Compute\v2\Models\Hypervisor;
use OpenStack\Compute\v2\Models\Image;
use OpenStack\Compute\v2\Models\Keypair;
use OpenStack\Compute\v2\Models\Limit;
Expand Down Expand Up @@ -66,7 +67,7 @@ private function getNetworkService()

return $this->networkService;
}

private function getBlockStorageService()
{
if (!$this->blockStorageService) {
Expand Down Expand Up @@ -179,7 +180,11 @@ public function runTests()

// Limits
$this->getLimits();

// Hypervisors
$this->listHypervisors();
$this->getHypervisorsStatistics();
$this->getHypervisor();

// Console
$this->getVncConsole();
Expand Down Expand Up @@ -549,6 +554,28 @@ private function deleteKeypair()
$this->logStep('Deleted keypair name {name}', ['{name}' => $this->keypairName]);
}

private function listHypervisors()
{
require_once $this->sampleFile([], 'hypervisors/list_hypervisors.php');

$this->logStep('Listed all available hypervisors');
}

private function getHypervisor()
{
$replacements = [
'{hypervisorId}' => '1',
];

require_once $this->sampleFile($replacements, 'hypervisors/get_hypervisor.php');

/**@var Hypervisor $hypervisor */
$this->assertInstanceOf(Hypervisor::class, $hypervisor);
$this->assertEquals($replacements['{hypervisorId}'], $hypervisor->id);

$this->logStep('Retrieved details for hypervisor id {hypervisorId}', $replacements);
}

private function getHypervisorsStatistics()
{
require_once $this->sampleFile([], 'hypervisors/get_hypervisors_statistics.php');
Expand All @@ -575,7 +602,7 @@ private function addSecurityGroupToServer()
'{serverId}' => $this->serverId,
'{secGroupName}' => 'default'
];

require_once $this->sampleFile($replacements, 'servers/add_security_group.php');

/**@var Server $server*/
Expand Down
42 changes: 27 additions & 15 deletions tests/unit/Compute/v2/Fixtures/hypervisor-get.resp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,30 @@ HTTP/1.1 200 OK
Content-Type: application/json

{
"hypervisor_statistics": {
"count": 1,
"vcpus_used": 0,
"local_gb_used": 0,
"memory_mb": 7980,
"current_workload": 0,
"vcpus": 8,
"running_vms": 0,
"free_disk_gb": 157,
"disk_available_least": 140,
"local_gb": 157,
"free_ram_mb": 7468,
"memory_mb_used": 512
}
}
"hypervisor":{
"status":"enabled",
"service":{
"host":"localhost.localdomain",
"disabled_reason":null,
"id":8
},
"vcpus_used":10,
"hypervisor_type":"QEMU",
"local_gb_used":120,
"vcpus":56,
"hypervisor_hostname":"localhost.localdomain",
"memory_mb_used":20992,
"memory_mb":97909,
"current_workload":0,
"state":"up",
"host_ip":"1.2.3.4",
"cpu_info":"{\"vendor\": \"Intel\", \"model\": \"Haswell-noTSX\", \"arch\": \"x86_64\", \"features\": [\"pge\", \"avx\", \"xsaveopt\", \"clflush\", \"sep\", \"syscall\", \"tsc_adjust\", \"vme\", \"dtes64\", \"invpcid\", \"msr\", \"sse\", \"xsave\", \"vmx\", \"erms\", \"xtpr\", \"cmov\", \"smep\", \"pcid\", \"est\", \"pat\", \"arat\", \"smx\", \"pbe\", \"lm\", \"tsc\", \"nx\", \"fxsr\", \"tm\", \"sse4.1\", \"pae\", \"sse4.2\", \"pclmuldq\", \"acpi\", \"fma\", \"tsc-deadline\", \"mmx\", \"osxsave\", \"cx8\", \"mce\", \"de\", \"tm2\", \"ht\", \"dca\", \"pni\", \"abm\", \"popcnt\", \"mca\", \"pdpe1gb\", \"apic\", \"fsgsbase\", \"f16c\", \"pse\", \"ds\", \"invtsc\", \"lahf_lm\", \"aes\", \"avx2\", \"sse2\", \"ss\", \"ds_cpl\", \"bmi1\", \"bmi2\", \"ssse3\", \"rdtscp\", \"fpu\", \"cx16\", \"pse36\", \"mtrr\", \"movbe\", \"pdcm\", \"cmt\", \"rdrand\", \"x2apic\"], \"topology\": {\"cores\": 14, \"cells\": 2, \"threads\": 2, \"sockets\": 1}}",
"running_vms":4,
"free_disk_gb":146,
"hypervisor_version":2006000,
"disk_available_least":2,
"local_gb":266,
"free_ram_mb":76917,
"id":1
}
}
19 changes: 19 additions & 0 deletions tests/unit/Compute/v2/Fixtures/hypervisor-statistic-get.resp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
HTTP/1.1 200 OK
Content-Type: application/json

{
"hypervisor_statistics": {
"count": 1,
"vcpus_used": 0,
"local_gb_used": 0,
"memory_mb": 7980,
"current_workload": 0,
"vcpus": 8,
"running_vms": 0,
"free_disk_gb": 157,
"disk_available_least": 140,
"local_gb": 157,
"free_ram_mb": 7468,
"memory_mb_used": 512
}
}
Loading

0 comments on commit 3451ef4

Please sign in to comment.