Skip to content

Commit

Permalink
[4.1] Add App Platform functions (DigitalOceanPHP#263)
Browse files Browse the repository at this point in the history

Co-authored-by: Graham Campbell <[email protected]>
  • Loading branch information
shihjay2 and GrahamCampbell authored Jan 10, 2021
1 parent f1a48d4 commit 1535563
Show file tree
Hide file tree
Showing 10 changed files with 763 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ CHANGE LOG
==========


## 4.1.0 (UPCOMING)

* Added app platform API support


## 4.0.1 (02/01/2021)

* Fixed issues with headers
Expand Down
85 changes: 81 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,83 @@ $actions = $action->getAll();
$action123 = $action->getById(123);
```

### App

```php
// return the app api
$app = $client->app();

// return a collection of Apps
$apps = $app->getAll();

// return the App entity 123
$app123 = $app->getById(123);

// create a new App
$spec = [
"name" => "sample-golang",
"services" => [
[
"name" => "web",
"github" => [
"repo" => "digitalocean/sample-golang",
"branch" => "branch"
],
"run_command" => "bin/sample-golang",
"environment_slug" => "go",
"instance_size_slug" => "basic-xxs",
"instance_count" => 2,
"routes" => [
[
"path" => "/"
]
]
]
],
"region" => "ams"
];
$app = $app->create($spec);

// update an App with App entity 123
$app = $app->update(123, $spec);

// delete an App with App entity 123
$app->remove(123);

// retrieve App deployments with App entity 123
$deployments = $app->getAppDeployments(123);

// return an App deployment with App entity 123, deployment ID of 456
$deployment = $app->getAppDeployment(123,456);

// create an App deployment with App entity 123
$deployment = $app->createAppDeployment(123);

// cancel an App deployment
$deployment = $app->cancelAppDeployment(123,456);

// retrieve deployment logs by component for entity 123, deployment ID of 456, component name of "test_component"
$logs = $app->getDeploymentLogs(123,456,"test_component");

// retrieve aggregate deployment logs for entity 123, deployment ID of 456
$logs = $app->getAggregateDeploymentLogs(123,456);

// retrieve App regions
$regions = $app->getRegions();

// retrieve App tiers
$tiers = $app->getTiers();

// return an App tier by slug with slug name of "test_slug"
$tier = $app->getTierBySlug("test_slug");

// retrieve App instance sizes
$instance_sizes = $app->getInstanceSizes();

// return App instance size by slug with slug name of "test_slug"
$instance_size = $app->getInstanceSizeBySlug("test_slug");
```

### Database

```php
Expand Down Expand Up @@ -512,19 +589,19 @@ $volume->remove('506f78a4-e098-11e5-ad9f-000f53306ae1');
// removes a volume by name and region
$volume->remove('example', 'nyc1');

// attach a volume to a Droplet
// attach a volume to a Droplet
$volume->attach('506f78a4-e098-11e5-ad9f-000f53306ae1', 123, 'nyc1');

// detach a volume from a Droplet
// detach a volume from a Droplet
$volume->detach('506f78a4-e098-11e5-ad9f-000f53306ae1', 123, 'nyc1');

// resize a volume
// resize a volume
$volume->resize('506f78a4-e098-11e5-ad9f-000f53306ae1', 20, 'nyc1');

// take a snapshot of volume and name it 'my-snapshot'. Returns the Snapshot entity
$snapshot = $volume->snapshot('506f78a4-e098-11e5-ad9f-000f53306ae1', 'my-snapshot');

// get a volume action by its id
// get a volume action by its id
$volume->getActionById(123, '506f78a4-e098-11e5-ad9f-000f53306ae1');

// get all actions related to a volume
Expand Down
266 changes: 266 additions & 0 deletions src/Api/App.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,266 @@
<?php

declare(strict_types=1);

/*
* This file is part of the DigitalOcean API library.
*
* (c) Antoine Corcy <[email protected]>
* (c) Graham Campbell <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DigitalOceanV2\Api;

use DigitalOceanV2\Entity\App as AppEntity;
use DigitalOceanV2\Entity\AppDeployment as AppDeploymentEntity;
use DigitalOceanV2\Entity\AppDeploymentLog as AppDeploymentLogEntity;
use DigitalOceanV2\Entity\AppInstanceSize as AppInstanceSizeEntity;
use DigitalOceanV2\Entity\AppRegion as AppRegionEntity;
use DigitalOceanV2\Entity\AppTier as AppTierEntity;
use DigitalOceanV2\Exception\ExceptionInterface;

/**
* @author Michael Shihjay Chen <[email protected]>
* @author Graham Campbell <[email protected]>
*/
class App extends AbstractApi
{
/**
* @throws ExceptionInterface
*
* @return AppEntity[]
*/
public function getAll()
{
$apps = $this->get('apps');

return \array_map(function ($app) {
return new AppEntity($app);
}, $apps->apps);
}

/**
* @param string $appID
*
* @throws ExceptionInterface
*
* @return AppEntity
*/
public function getByID(string $appID)
{
$app = $this->get(\sprintf('apps/%s', $appID));

return new AppEntity($app->app);
}

/**
* @param array $spec
*
* @throws ExceptionInterface
*
* @return AppEntity
*/
public function create(array $spec)
{
$app = $this->post('apps', [
'spec' => $spec,
]);

return new AppEntity($app->app);
}

/**
* @param string $appID
* @param array $spec
*
* @throws ExceptionInterface
*
* @return AppEntity
*/
public function update(string $appID, array $spec)
{
$result = $this->put(\sprintf('apps/%s', $appID), [
'spec' => $spec,
]);

return new AppEntity($result->app);
}

/**
* @param string $appID
*
* @throws ExceptionInterface
*
* @return void
*/
public function remove(string $appID): void
{
$this->delete(\sprintf('apps/%s', $appID));
}

/**
* @param string $appID
*
* @throws ExceptionInterface
*
* @return AppDeploymentEntity[]
*/
public function getAppDeployments(string $appID)
{
$deployments = $this->get(\sprintf('apps/%s/deployments', $appID));

return \array_map(function ($deployment) {
return new AppDeploymentEntity($deployment);
}, $deployments->deployments);
}

/**
* @param string $deploymentID
*
* @throws ExceptionInterface
*
* @return AppDeploymentEntity
*/
public function getAppDeployment(string $appID, string $deploymentID)
{
$deployment = $this->get(\sprintf('apps/%s/deployments/%s', $appID, $deploymentID));

return new AppDeploymentEntity($deployment->deployment);
}

/**
* @param string $appID
* @param bool $force_build
*
* @throws ExceptionInterface
*
* @return AppDeploymentEntity
*/
public function createAppDeployment(string $appID, $force_build = true)
{
$deployment = $this->post(\sprintf('apps/%s/deployments', $appID), [
'force_build' => $force_build,
]);

return new AppDeploymentEntity($deployment->deployment);
}

/**
* @param string $appID
* @param string $deploymentID
*
* @throws ExceptionInterface
*
* @return AppDeploymentEntity
*/
public function cancelAppDeployment(string $appID, string $deploymentID)
{
$deployment = $this->post(\sprintf('apps/%s/deployments/%s/cancel', $appID, $deploymentID));

return new AppDeploymentEntity($deployment->deployment);
}

/**
* @param string $appID
* @param string $deploymentID
* @param string $componentName
*
* @throws ExceptionInterface
*
* @return AppDeploymentLogEntity
*/
public function getDeploymentLogs(string $appID, string $deploymentID, string $componentName)
{
$logs = $this->get(\sprintf('apps/%s/deployments/%s/components/%s/logs', $appID, $deploymentID, $componentName));

return new AppDeploymentLogEntity($logs);
}

/**
* @param string $appID
* @param string $deploymentID
*
* @throws ExceptionInterface
*
* @return AppDeploymentLogEntity
*/
public function getAggregateDeploymentLogs(string $appID, string $deploymentID)
{
$logs = $this->get(\sprintf('apps/%s/deployments/%s/logs', $appID, $deploymentID));

return new AppDeploymentLogEntity($logs);
}

/**
* @throws ExceptionInterface
*
* @return AppRegionEntity[]
*/
public function getRegions()
{
$regions = $this->get('apps/regions');

return \array_map(function ($region) {
return new AppRegionEntity($region);
}, $regions->regions);
}

/**
* @throws ExceptionInterface
*
* @return AppTierEntity[]
*/
public function getTiers()
{
$tiers = $this->get('apps/tiers');

return \array_map(function ($tier) {
return new AppTierEntity($tier);
}, $tiers->tiers);
}

/**
* @param string $slug
*
* @throws ExceptionInterface
*
* @return AppTierEntity
*/
public function getTierBySlug(string $slug)
{
$tier = $this->get(\sprintf('apps/tiers/%s', $slug));

return new AppTierEntity($tier);
}

/**
* @throws ExceptionInterface
*
* @return AppInstanceSizeEntity[]
*/
public function getInstanceSizes()
{
$instance_sizes = $this->get('apps/tiers/instance_sizes');

return \array_map(function ($instance_size) {
return new AppInstanceSizeEntity($instance_size);
}, $instance_sizes->instance_sizes);
}

/**
* @param string $slug
*
* @throws ExceptionInterface
*
* @return AppInstanceSizeEntity
*/
public function getInstanceSizeBySlug(string $slug)
{
$instance_size = $this->get(\sprintf('apps/tiers/instance_sizes/%s', $slug));

return new AppInstanceSizeEntity($instance_size);
}
}
Loading

0 comments on commit 1535563

Please sign in to comment.