Skip to content

Commit 6a87a92

Browse files
author
Jamie Hannaford
committed
Merge pull request #576 from ycombinator/gh-575
Adding support for availability zones in Compute servers.
2 parents 3fdf01b + d2d3329 commit 6a87a92

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

lib/OpenCloud/Compute/Resource/Server.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ class Server extends NovaResource implements HasPtrRecordsInterface
172172
*/
173173
public $powerStatus;
174174

175+
/**
176+
* @link http://developer.openstack.org/api-ref-compute-v2-ext.html#ext-os-ext-az
177+
* @var string Availability zone of the VM
178+
*/
179+
public $availabilityZone;
180+
175181
protected static $json_name = 'server';
176182
protected static $url_resource = 'servers';
177183

@@ -206,6 +212,7 @@ class Server extends NovaResource implements HasPtrRecordsInterface
206212
'OS-EXT-STS:vm_state' => 'extendedStatus',
207213
'OS-EXT-STS:task_state' => 'taskStatus',
208214
'OS-EXT-STS:power_state' => 'powerStatus',
215+
'OS-EXT-AZ:availability_zone' => 'availabilityZone'
209216
);
210217

211218
/**
@@ -729,6 +736,12 @@ protected function createJson()
729736
$server->user_data = $this->user_data;
730737
}
731738

739+
// Availability zone
740+
if (!empty($this->availabilityZone)) {
741+
$this->checkExtension('OS-EXT-AZ');
742+
$server->availability_zone = $this->availabilityZone;
743+
}
744+
732745
return (object) array('server' => $server);
733746
}
734747

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2014 Rackspace US, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
require dirname(__DIR__) . '/../vendor/autoload.php';
20+
21+
use OpenCloud\OpenStack;
22+
use Guzzle\Http\Exception\BadResponseException;
23+
24+
// 1. Instantiate a OpenStack client. You can replace {authUrl} with
25+
// OpenStack::US_IDENTITY_ENDPOINT or similar
26+
$client = new OpenStack('{authUrl}', array(
27+
'username' => '{username}',
28+
'password' => '{password}',
29+
));
30+
31+
// 2. Create Compute service
32+
$service = $client->computeService('nova', '{region}');
33+
34+
// 3. Get empty server
35+
$server = $service->server();
36+
37+
// 4. Create the server. If you do not know what imageId or flavorId to use,
38+
// please run the list_flavors.php and list_images.php scripts.
39+
try {
40+
$response = $server->create(array(
41+
'name' => '{serverName}',
42+
'imageId' => '{imageId}',
43+
'flavorId' => '{flavorId}',
44+
'availabilityZone' => '{availabilityZone}'
45+
));
46+
} catch (BadResponseException $e) {
47+
echo $e->getResponse();
48+
}

tests/OpenCloud/Tests/Compute/Resource/ServerTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,4 +403,13 @@ public function test_Stop()
403403
$this->addMockSubscriber(new \Guzzle\Http\Message\Response(202));
404404
$this->assertEquals(202, $this->server->stop()->getStatusCode());
405405
}
406+
407+
public function test_Create_Availability_Zone()
408+
{
409+
$new = new PublicServer($this->service);
410+
$new->setAvailabilityZone('AZ1');
411+
$obj = $new->CreateJson();
412+
413+
$this->assertEquals('AZ1', $obj->server->availability_zone);
414+
}
406415
}

tests/OpenCloud/Tests/Compute/_response/Extensions.resp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ Server: Jetty(8.0.y.z-SNAPSHOT)
1515
"alias": "OS-DCF",
1616
"description": "Disk Management Extension."
1717
},
18+
{
19+
"updated": "2013-01-30T00:00:00Z",
20+
"name": "ExtendedAvailabilityZone",
21+
"links": [],
22+
"namespace": "http://docs.openstack.org/compute/ext/extended_availability_zone/api/v2",
23+
"alias": "OS-EXT-AZ",
24+
"description": "Extended Availability Zone support."
25+
},
1826
{
1927
"updated": "2013-02-19T00:00:00Z",
2028
"name": "ImageSize",

0 commit comments

Comments
 (0)