Skip to content

Commit

Permalink
Allow VM packages to be changed, add first() method to resource contr…
Browse files Browse the repository at this point in the history
…oller
  • Loading branch information
mewejo committed Jul 15, 2020
1 parent 1ea83e5 commit 7671c2a
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/API/ResourceControllerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ interface ResourceControllerInterface
* @return array
*/
public function all();

/**
* @return ResourceInterface|null
*/
public function first();
}
7 changes: 7 additions & 0 deletions src/API/RestfulKatapultApiV1.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ public function post($url, array $body = [])
]);
}

public function put($url, array $body = [])
{
return $this->getClient()->put($url, [
'json' => $body
]);
}

public function delete($url)
{
return $this->getClient()->delete($url);
Expand Down
15 changes: 13 additions & 2 deletions src/API/RestfulKatapultApiV1/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,21 @@ public function createApiUrl($resourceId = null, $contextualArguments = null)
return $mappedResourceClass::getUrl($resourceId, array_merge($this->arguments, $contextualArguments));
}


/**
* @return ResourceInterface|null
*/
public function first()
{
$resources = $this->all(false);
return count($resources) > 0 ? reset($resources) : null;
}

/**
* @param bool $fetchAllPages
* @return array
*/
public function all()
public function all($fetchAllPages = true)
{
$lastFetchedPage = null;
$nextPage = 1;
Expand All @@ -121,7 +132,7 @@ public function all()
}

// If this route doesn't support pagination, break out
if(!isset($responseBody->pagination)) break;
if(!$fetchAllPages || !isset($responseBody->pagination)) break;

$lastFetchedPage = $nextPage;
$totalPages = $responseBody->pagination->total_pages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class VirtualMachine extends \Krystal\Katapult\Resources\Organization\VirtualMac
const ACTION_STOP = 'stop';
const ACTION_SHUTDOWN = 'shutdown';
const ACTION_RESET = 'reset';
const ACTION_CHANGE_PACKAGE = 'change_package';

public static function getUrl($resourceId = null, $arguments = null)
{
Expand All @@ -49,6 +50,10 @@ public static function getUrl($resourceId = null, $arguments = null)
case self::ACTION_RESET:
$url .= "/reset";
break;

case self::ACTION_CHANGE_PACKAGE:
$url .= "/package";
break;
}
}
}
Expand Down Expand Up @@ -122,4 +127,11 @@ public function reset()
'action' => self::ACTION_RESET
]));
}

public function changePackage($virtualMachinePackageLookup)
{
return $this->resourceController->api->put($this->resourceController->createApiUrl($this->id, [
'action' => self::ACTION_CHANGE_PACKAGE
]), ['virtual_machine_package' => $virtualMachinePackageLookup]);
}
}
48 changes: 47 additions & 1 deletion tests/RestApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ public function can_get_single_resources()
}
}

/** @test */
public function can_get_first_of_resource()
{
foreach($this->resourceClasses as $resourceClass)
{
$resource = $this->katapult->resource($resourceClass)->first();
$this->assertInstanceOf($resourceClass, $resource);
}
}

/** @test */
public function can_create_dns_zone()
{
Expand All @@ -229,7 +239,6 @@ public function can_create_dns_zone()
]);

$this->assertInstanceOf(Organization\DNS\DnsZone::class, $zone);

}

/** @test */
Expand Down Expand Up @@ -447,6 +456,43 @@ public function can_create_console_sessions_for_virtual_machines()
$this->assertLessThanOrEqual(0, $failed);
}

/** @test */
public function can_change_virtual_machine_packages()
{
if(!self::TEST_COMPUTE) return;

$totalToCreate = 1;

// First we need to fetch an org, so we can fetch it's resources
$firstOrg = self::getFirstOrganization($this->katapult);

// Create some VMs
$resources = $this->createVmsAndWaitUntilReady($firstOrg, $totalToCreate);
$this->assertCount($totalToCreate, $resources);

$success = 0;
$failed = 0;

foreach($resources as $resource)
{
try
{
// Start the VM and wait for it to come online
if($resource->state != 'started') $this->executeVmPowerOperationAndWaitUntilCompleted($resource, RestfulKatapultApiV1\Resources\Organization\VirtualMachine::ACTION_START);

$resource->changePackage(['id' => 'vmpkg_NZ9vnAhUyUlanf65']);
$success++;
}
catch(\Exception $e)
{
$failed++;
}
}

$this->assertEquals(count($resources), $success);
$this->assertLessThanOrEqual(0, $failed);
}

/** @test */
public function can_delete_dns_zones()
{
Expand Down

0 comments on commit 7671c2a

Please sign in to comment.