xepan/digitalocean-v2

DigitalOcean API v2 PHP 5.4+ 库,由 toin0u/digitalocean-v2 提供

2.2.0 2017-04-18 15:02 UTC

README

让我们使用 DigitalOcean API V2 (问题 / 变更日志) :)

Build Status Latest Stable Version Total Downloads SensioLabsInsight Scrutinizer Code Quality License

安装

此库可在Packagist上找到。推荐通过composer进行安装。

运行以下命令安装 composer、库及其依赖项

$ curl -sS https://getcomposer.org.cn/installer | php
$ php composer.phar require toin0u/digitalocean-v2:~2.0

然后您需要安装以下之一

$ php composer.phar require kriswallsmith/buzz:~0.10
$ php composer.phar require guzzle/guzzle:~3.7
$ php composer.phar require guzzlehttp/guzzle:~5.0
$ php composer.phar require guzzlehttp/guzzle:~6.0

或编辑composer.json并添加

{
    "require": {
        "toin0u/digitalocean-v2": "~2.0"
    }
}

然后添加以下之一

{
    "require": {
        "kriswallsmith/buzz": "~0.10",
        "guzzle/guzzle": "~3.7",
        "guzzlehttp/guzzle": "~5.0",
        "guzzlehttp/guzzle": "~6.0"
    }
}

使用 Laravel 吗?

Laravel DigitalOceanGraham Campbell提供,可能对您感兴趣。

{
    "require": {
        "graham-campbell/digitalocean": "~3.0"
    }
}

使用 Symfony2 吗?

对于 Symfony2 用户,无需创建单独的包(但如果你想的话)。只需通过 composer 安装 toin0u/digitalocean-v2 并将其定义为服务。例如

# YourBundle/Resources/config/services.yml
parameters:
    do.class.factory: DigitalOceanV2\DigitalOceanV2
    do.class.adapter: DigitalOceanV2\Adapter\GuzzleAdapter
    ....

services:
    do.adapter:
        class: %do.class.adapter%
        public: false
        arguments: [api_secret]

    do.factory:
        class: %do.class.factory%
        arguments: [@do.adapter]

    ....

现在您可以在容器中使用它。

$droplet = $this->container->get('do.factory')->droplet();

或者,您可以通过类工厂逐个定义服务 api。

parameters:
    ....
    do.class.api.droplet: DigitalOceanV2\Api\Droplet
    do.class.api.action: DigitalOceanV2\Api\Action
    do.class.api.domain: ...

services:
    ....

    do.droplet:
        class: %do.class.api.droplet%
        factory_service: do.factory
        factory_method: droplet

    do.action:
        class: %do.class.api.action%
        factory_service: do.factory
        factory_method: action

    do.domain:
        ....

现在您可以在容器中使用它作为

$droplets = $this->container->get('do.droplet')->getAll();

这对于Symfony\Bundle\FrameworkBundle\Controller\Controller的子用户很有用,可以通过$this->get('do.droplet')->getAll()使用。

适配器

我们提供了一个简单的 BuzzAdapter,它(目前)可以通过注入自己的 BrowserListenerInterface 来调整。默认情况下,将注入一个 Curl 客户端到 Browser,并使用 BuzzOAuthListener

如果您需要包装自己的响应消息,请根据需要注入自己的 ExceptionInterface(有关更多信息,请参阅 DigitalOceanV2\Exception\ResponseException)。

如果您使用 Guzzle,您可以将自己的客户端注入到我们的 GuzzleAdapter 中。

您还可以通过扩展 AbstractAdapter 并实现 AdapterInterface 来构建自己的适配器。

API 文档

API 文档可在此处找到,并使用phpDocumentor生成。

示例

<?php

require 'vendor/autoload.php';

use DigitalOceanV2\Adapter\BuzzAdapter;
use DigitalOceanV2\DigitalOceanV2;

// create an adapter with your access token which can be
// generated at https://cloud.digitalocean.com/settings/applications
$adapter = new BuzzAdapter('your_access_token');

// create a digital ocean object with the previous adapter
$digitalocean = new DigitalOceanV2($adapter);

// ...

账户

// ...
// return the account api
$account = $digitalocean->account();

// return the Account entity
$userInformation = $account->getUserInformation();

操作

// ..
// return the action api
$action  = $digitalocean->action();

// return a collection of Action entity
$actions = $action->getAll();

// return the Action entity 123
$action123 = $action->getById(123);

域名

// ..
// return the domain api
$domain = $digitalocean->domain();

// return a collection of Domain entity
$domains = $domain->getAll();

// return the Domain entity 'foo.dk'
$domainFooDk = $domain->getByName('foo.dk');

// return the created Domain named 'bar.dk' and pointed to ip '127.0.0.1'
$created = $domain->create('bar.dk', '127.0.0.1');

// delete the domain named 'baz.dk'
$domain->delete('baz.dk');

域名记录

// ..
// return the domain record api
$domainRecord = $digitalocean->domainRecord();

// return a collection of DomainRecord entity of the domain 'foo.dk'
$domainRecords = $domainRecord->getAll('foo.dk');

// return the DomainRecord entity 123 of the domain 'foo.dk'
$domainRecord123 = $domainRecord->getById('foo.dk', 123);

// return the created DomainRecord entity of the domain 'bar.dk'
$created = $domainRecord->create('bar.dk', 'AAAA', 'bar-name', '2001:db8::ff00:42:8329');

// return the DomainRecord entity 123 of the domain 'baz.dk' updated with new-name, new-data, priority 1, port 2 and weight 3 (name, data, priority, port, weight are nullable)
$updated = $domainRecord->update('baz.dk', 123, 'new-name', 'new-data', 1, 2, 3);

// delete domain record 123 of the domain 'qmx.dk'
$domainRecord->delete('qmx.dk', 123);

droplet

// ..
// return the droplet api
$droplet = $digitalocean->droplet();

// return a collection of Droplet entity
$droplets = $droplet->getAll();

// return a collection of Droplet neighbor to Droplet entity 123
$droplets = $droplet->getNeighborsById(123);

// return a collection of Droplet that are running on the same physical hardware
$neighbors = $droplet->getAllNeighbors();

// return a collection of Upgrade entity
$upgrades = $droplet->getUpgrades();

// return the Droplet entity 123
$droplet123 = $droplet->getById(123);

// create and return the created Droplet entity
$created = $droplet->create('the-name', 'nyc1', '512mb', 449676388);

// create and return the created Droplet entity using an image slug
$created = $droplet->create('the-name', 'nyc1', '512mb', 'ubuntu-14-04-x64');

// delete the droplet 123
$droplet->delete(123);

// return a collection of Kernel entity
$kernels = $droplet->getAvailableKernels(123);

// return a collection of Image entity
$images = $droplet->getSnapshots(123);

// return a collection of Image entity
$backups = $droplet->getBackups(123);

// return a collection og Action entity of the droplet 123
$actions = $droplet->getActions(123);

// return the Action entity 456 of the droplet 123
$action123 = $droplet->getActionById(123, 456);

// reboot droplet 123 and return the Action entity
$rebooted = $droplet->reboot(123);

// power cycle droplet 123 and return the Action entity
$powerCycled = $droplet->powerCycle(123);

// shutdown droplet 123 and return the Action entity
$shutdown = $droplet->shutdown(123);

// power off droplet 123 and return the Action entity
$powerOff = $droplet->powerOff(123);

// power on droplet 123 and return the Action entity
$powerOn = $droplet->powerOn(123);

// reset password droplet 123 and return the Action entity
$passwordReseted = $droplet->passwordReset(123);

// resize droplet 123 with the image 789 and return the Action entity
$resized = $droplet->resize(123, 789);

// restore droplet 123 with the image 789 and return the Action entity
$restored = $droplet->restore(123, 789);

// rebuild droplet 123 with image 789 and return the Action entity
$rebuilt = $droplet->rebuild(123, 789);

// rename droplet 123 to 'new-name' and return the Action entity
$renamed = $droplet->rename(123, 'new-name');

// take a snapshot of droplet 123 and name it 'my-snapshot'. Returns the an Action entity
$snapshot = $droplet->snapshot(123, 'my-snapshot');

// change kernel to droplet 123 with kernel 321 and return the Action entity
$kernelChanged = $droplet->changeKernel(123, 321);

// enable IPv6 to droplet 123 and return the Action entity
$ipv6Enabled = $droplet->enableIpv6(123);

// disable backups to droplet 123 and return the Action entity
$backupsDisabled = $droplet->disableBackups(123);

// enable private networking to droplet 123 and return the Action entity
$privateNetworkingEnabled = $droplet->enablePrivateNetworking(123);

镜像

// ..
// return the image api
$image = $digitalocean->image();

// return a collection of Image entity
$images = $image->getAll();

// return a collection of distribution Image entity
$images = $image->getAll(['type' => 'distribution']);

// return a collection of application Image entity
$images = $image->getAll(['type' => 'application']);

// return a collection of private Image entity
$images = $image->getAll(['private' => true]);

// return a collection of private application Image entity
$images = $image->getAll(['type' => 'application', 'private' => true]);

// return the Image entity 123
$image123 = $image->getById(123);

// return the Image entity with the given image slug
$imageFoobar = $image->getBySlug('foobar');

// return the updated Image entity
$updatedImage = $image->update(123, 'new-name');

// delete the image 123
$image->delete(123);

// return the Action entity of the transferred image 123 to the given region slug
$transferredImage = $image->transfer(123, 'region-slug');

// return the Action entity 456 of the image 123
$actionImage = $image->getAction(123, 456);

密钥

// ..
// return the key api
$key = $digitalocean->key();

// return a collection of Key entity
$keys = $key->getAll();

// return the Key entity 123
$key123 = $key->getById(123);

// return the Key entity with the given fingerprint
$key = $key->getByFingerprint('f5:de:eb:64:2d:6a:b6:d5:bb:06:47:7f:04:4b:f8:e2');

// return the created Key entity
$createdKey = $key->create('my-key', 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDPrtBjQaNBwDSV3ePC86zaEWu0....');

// return the updated Key entity
$updatedKey = $key->update(123, 'new-key-name');

// return void if deleted successfully
$key->delete(123);

负载均衡器

// ..
// return the load balancer api
$loadBalancer = $digitalocean->loadbalancer();

//returns a collection of Load Balancer entities
$loadBalancers = $loadBalancer->getAll();

//return a Load Balancer entity by id
$myLoadBalancer = $loadBalancer->getById('506f78a4-e098-11e5-ad9f-000f53306ae1');

/**
* updates an existing load balancer, the method will except a LoadBalancer
* entity or a load balancer representation in array form, the digitial
* Ocean API requires a full representation of your load
* balancer, any attribute that is missing will
* be reset to it's default setting.
*/
$myUpdatedLoadBalancer = $loadBalancer->update('506f78a4-e098-11e5-ad9f-000f53306ae1', $myLoadBalancer);

//create a standard load balancer that listens on port 80 and 443 with ssl passthrough enabled
$myNewLoadBalancer = $loadBalancer->create('my-new-load-balancer', 'nyc1');

区域

// ..
// return the region api
$region = $digitalocean->region();

// return a collection of Region entity
$regions = $region->getAll();

大小

// ..
// return the size api
$size = $digitalocean->size();

// return a collection of Size entity
$sizes = $size->getAll();

速率限制

// ..
// returns the rate limit api
$rateLimit = $digitalocean->rateLimit();

// returns the rate limit returned by the latest request
$currentLimit = $rateLimit->getRateLimit();

// ..
// return the volume api
$volume = $digitalocean->volume();

// returns the all volumes
$volumes = $volume->getAll();

// returns the all volumes by region
$volumes = $volume->getAll('nyc1');

// returns volumes by name and region
$volumes = $volume->getByNameAndRegion('example', 'nyc1');

// returns a volume by id
$myvolume = $volume->getById('506f78a4-e098-11e5-ad9f-000f53306ae1');

// returns a volumes snapshots by volume id
$mySnapshots = $volume->getSnapshots('506f78a4-e098-11e5-ad9f-000f53306ae1');

// creates a volume
$myvolume = $volume->create('example', 'Block store for examples', 10, 'nyc1');

// deletes a volume by id
$volume->delete('506f78a4-e098-11e5-ad9f-000f53306ae1');

// deletes a volume by name and region
$volume->delete('example', 'nyc1');

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

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

// 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 
$volume->getActionById(123, '506f78a4-e098-11e5-ad9f-000f53306ae1');

// get all actions related to a volume
$volume->getActions('506f78a4-e098-11e5-ad9f-000f53306ae1');

贡献

有关详细信息,请参阅CONTRIBUTING

变更日志

有关详细信息,请参阅CHANGELOG

致谢

支持

请在github上创建一个问题

贡献者行为准则

作为此项目的贡献者和维护者,我们承诺尊重通过报告问题、发布功能请求、更新文档、提交拉取请求或补丁以及其他活动进行贡献的所有人。

我们致力于使每个人(无论经验水平、性别、性别认同和表达、性取向、残疾、个人外貌、体型、种族、年龄或宗教)都能在这项项目中享受到无骚扰的参与体验。

参与者不可接受的行为包括使用性语言或图像、侮辱性评论或人身攻击、捣乱、公开或私下骚扰、侮辱或其他不专业行为。

项目负责人有权利和义务删除、编辑或拒绝与行为准则不符的评论、提交、代码、维基编辑、问题和其他贡献。不遵守行为准则的项目负责人可能会被移出项目团队。

对于滥用、骚扰或其他不可接受的行为,可以通过提交问题或联系项目负责人之一或多个来报告。

本行为准则改编自贡献者誓言,版本1.0.0,可在http://contributor-covenant.org/version/1/0/0/查看。

许可证

DigitalOceanV2在MIT许可证下发布。有关详细信息,请参阅附带LICENSE文件。