Skip to content

Commit

Permalink
Implement reimageRoleInstance API
Browse files Browse the repository at this point in the history
  • Loading branch information
ogail committed Aug 10, 2012
1 parent 47813ad commit 3b14524
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 25 deletions.
1 change: 1 addition & 0 deletions WindowsAzure/Common/Internal/Resources.php
Expand Up @@ -268,6 +268,7 @@ class Resources
const QPV_UPGRADE = 'upgrade';
const QPV_WALK_UPGRADE_DOMAIN = 'walkupgradedomain';
const QPV_REBOOT = 'reboot';
const QPV_REIMAGE = 'reimage';

// Request body content types
const URL_ENCODED_CONTENT_TYPE = 'application/x-www-form-urlencoded';
Expand Down
Expand Up @@ -514,9 +514,9 @@ public function rebootRoleInstance($name, $roleName, $options);
* environment (staging or production), or by specifying the deployment's unique
* name.
*
* @param string $name The hosted service name.
* @param string $roleName The role instance name.
* @param ReimageRoleInstanceOptions $options The optional parameters.
* @param string $name The hosted service name.
* @param string $roleName The role instance name.
* @param GetDeploymentOptions $options The optional parameters.
*
* @return AsynchronousOperationResult
*
Expand Down
68 changes: 47 additions & 21 deletions WindowsAzure/ServiceManagement/ServiceManagementRestProxy.php
Expand Up @@ -72,6 +72,38 @@ class ServiceManagementRestProxy extends RestProxy
*/
private $_subscriptionId;

/**
* Sends an order request for the specified role instance.
*
* @param string $name The hosted service name.
* @param string $roleName The role instance name.
* @param GetDeploymentOptions $options The optional parameters.
* @param string $order The order name which is used as value
* for query parameter 'comp'.
*
* @return AsynchronousOperationResult
*/
private function _sendRoleInstanceOrder($name, $roleName, $options, $order)
{
Validate::isString($name, 'name');
Validate::notNullOrEmpty($name, 'name');
Validate::isString($roleName, 'roleName');
Validate::notNullOrEmpty($roleName, 'roleName');
Validate::notNullOrEmpty($options, 'options');

$context = new HttpCallContext();
$context->setMethod(Resources::HTTP_POST);
$context->setPath($this->_getRoleInstancePath($name, $options, $roleName));
$context->addStatusCode(Resources::STATUS_ACCEPTED);
$context->addQueryParameter(Resources::QP_COMP, $order);
$context->addHeader(Resources::CONTENT_TYPE, Resources::XML_CONTENT_TYPE);
$context->addHeader(Resources::CONTENT_LENGTH_NO_SPACE, 0);

$response = $this->sendContext($context);

return AsynchronousOperationResult::create($response->getHeader());
}

/**
* Constructs URI path for given service management resource.
*
Expand Down Expand Up @@ -1375,23 +1407,12 @@ public function walkUpgradeDomain($name, $upgradeDomain, $options)
*/
public function rebootRoleInstance($name, $roleName, $options)
{
Validate::isString($name, 'name');
Validate::notNullOrEmpty($name, 'name');
Validate::isString($roleName, 'roleName');
Validate::notNullOrEmpty($roleName, 'roleName');
Validate::notNullOrEmpty($options, 'options');

$context = new HttpCallContext();
$context->setMethod(Resources::HTTP_POST);
$context->setPath($this->_getRoleInstancePath($name, $options, $roleName));
$context->addStatusCode(Resources::STATUS_ACCEPTED);
$context->addQueryParameter(Resources::QP_COMP, Resources::QPV_REBOOT);
$context->addHeader(Resources::CONTENT_TYPE, Resources::XML_CONTENT_TYPE);
$context->addHeader(Resources::CONTENT_LENGTH_NO_SPACE, 0);

$response = $this->sendContext($context);

return AsynchronousOperationResult::create($response->getHeader());
return $this->_sendRoleInstanceOrder(
$name,
$roleName,
$options,
Resources::QPV_REBOOT
);
}

/**
Expand All @@ -1401,17 +1422,22 @@ public function rebootRoleInstance($name, $roleName, $options)
* environment (staging or production), or by specifying the deployment's unique
* name.
*
* @param string $name The hosted service name.
* @param string $roleName The role instance name.
* @param ReimageRoleInstanceOptions $options The optional parameters.
* @param string $name The hosted service name.
* @param string $roleName The role instance name.
* @param GetDeploymentOptions $options The optional parameters.
*
* @return AsynchronousOperationResult
*
* @see http://msdn.microsoft.com/en-us/library/windowsazure/gg441292.aspx
*/
public function reimageRoleInstance($name, $roleName, $options)
{
throw new \Exception(Resources::NOT_IMPLEMENTED_MSG);
return $this->_sendRoleInstanceOrder(
$name,
$roleName,
$options,
Resources::QPV_REIMAGE
);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/framework/ServiceManagementRestProxyTestBase.php
Expand Up @@ -257,6 +257,26 @@ public function waitUntilDeploymentReachStatus($name, $status)
} while($currentStatus != $status);
}

public function waitUntilRoleInstanceReachStatus($name, $state, $roleInstanceName)
{
$options = new GetDeploymentOptions();
$options->setSlot($this->defaultSlot);
$currentStatus = null;

do {
$result = $this->restProxy->getDeployment($name, $options);
$deployment = $result->getDeployment();
$roleInstanceList = $deployment->getRoleInstanceList();

foreach ($roleInstanceList as $roleInstance) {
if ($roleInstance->getInstanceName() == $roleInstanceName) {
$currentStatus = $roleInstance->getInstanceStatus();
break;
}
}
} while($currentStatus != $state);
}

public function createDeployment($name, $slot = null, $deploymentName = null, $options = null)
{
$deploymentName = is_null($deploymentName) ? $name : $deploymentName;
Expand Down
Expand Up @@ -1103,6 +1103,7 @@ public function testWalkUpgradeDomain()
/**
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::rebootRoleInstance
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_getRoleInstancePath
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_sendRoleInstanceOrder
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_getDeploymentPath
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_getPath
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_createRequestXml
Expand All @@ -1126,6 +1127,7 @@ public function testRebootRoleInstance()
$options->setDeploymentName($name);

$this->waitUntilDeploymentReachStatus($name, DeploymentStatus::RUNNING);
$this->waitUntilRoleInstanceReachStatus($name, 'ReadyRole', $roleName);

// Test
$result = $this->restProxy->rebootRoleInstance($name, $roleName, $options);
Expand All @@ -1138,6 +1140,49 @@ public function testRebootRoleInstance()
$deployment = $result->getDeployment();
$roleInstanceList = $deployment->getRoleInstanceList();
$webRoleInstance = $roleInstanceList[0];
$this->assertEquals('StartingVM', $webRoleInstance->getInstanceStatus());
$this->assertEquals('StoppedVM', $webRoleInstance->getInstanceStatus());
}

/**
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::reimageRoleInstance
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_getRoleInstancePath
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_sendRoleInstanceOrder
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_getDeploymentPath
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_getPath
* @covers WindowsAzure\ServiceManagement\ServiceManagementRestProxy::_createRequestXml
* @covers WindowsAzure\ServiceManagement\Models\AsynchronousOperationResult::create
* @group Deployment
*/
public function testReimageRoleInstance()
{
// Setup
$name = 'testReimageRoleInstance';
$roleName = 'WebRole1_IN_0';
$options = new CreateDeploymentOptions();
$options->setStartDeployment(true);
$this->createDeployment(
$name,
$this->defaultSlot,
$name,
$options
);
$options = new GetDeploymentOptions();
$options->setDeploymentName($name);

$this->waitUntilDeploymentReachStatus($name, DeploymentStatus::RUNNING);
$this->waitUntilRoleInstanceReachStatus($name, 'ReadyRole', $roleName);

// Test
$result = $this->restProxy->reimageRoleInstance($name, $roleName, $options);

// Block until reboot request is completed
$this->blockUntilAsyncSucceed($result);

// Assert
$result = $this->restProxy->getDeployment($name, $options);
$deployment = $result->getDeployment();
$roleInstanceList = $deployment->getRoleInstanceList();
$webRoleInstance = $roleInstanceList[0];
$this->assertEquals('StoppedVM', $webRoleInstance->getInstanceStatus());
}
}

0 comments on commit 3b14524

Please sign in to comment.