hampel / linode
3.2.0
2016-02-26 01:00 UTC
Requires
- php: >=5.4.0
- guzzlehttp/guzzle: ~4.0
Requires (Dev)
- mockery/mockery: ^0.9
- phpunit/phpunit: ~4.0
README
使用Guzzle实现的简单命令对象,用于封装Linode API
由Simon Hampel编写。
感谢Everett Griffiths实现额外的API调用。
安装
推荐通过Composer安装Hampel Linode
:::json
{
"require": {
"hampel/linode": "~3.1"
}
}
注意:如果您打算与Laravel一起使用此包,我们建议安装hampel/linode-laravel包,该包提供简单的Laravel服务提供商和门面,用于处理此API包装器。
使用方法
注意:此包装器的v3.0版本与v1.0或v2.0版本不兼容 - 使用此包装器的应用程序需要重新编写以利用此新接口。
:::php
<?php
use Hampel\Linode\Linode;
use Hampel\Linode\Commands\TestCommand;
use Hampel\Linode\Commands\UserCommand;
use Hampel\Linode\Commands\DomainCommand;
// long-hand initialisation method
$client = new GuzzleHttp\Client();
$linode = new Linode($client, 'your api key here');
// alternative static factory
$linode = Linode::make('your api key here');
// create a command object for the Linode service to execute
// in this case, a simple echo test, API echos back parameters passed
$command = new TestCommand('echo', ['foo' => 'bar']);
// pass the command to Linode service
$response = $linode->execute($command);
var_dump($response);
// get api key
$command = new UserCommand('getapikey', ['username' => 'foo', 'password' => 'bar']);
$response = $linode->execute($command);
var_dump($response);
// create a new domain entry
$options = array("soa_email" => "soa_email@domain-name.com", "ttl_sec" => 3600);
$command = new DomainCommand('create', $options);
$response = $linode->execute($command);
var_dump($response);
// retrieve details about a domain
$response = $linode->execute(new DomainCommand('list', ['domainid' => 12345])); // specify domain id as parameter
var_dump($response);
?>
示例:创建新的Linode
有时并不清楚单个API命令如何组合在一起以启动新的服务器。通常过程如下
- 创建Linode(此作为容器使用)
- 创建主磁盘(例如,从堆栈脚本中创建)
- 创建交换磁盘
- 创建配置配置文件
- 启动Linode
以下是代码概述。对于生产使用,请考虑将命令执行包装在try/catch块中。
:::php
<?php
use Hampel\Linode\Linode;
use Hampel\Linode\Commands\LinodeCommand;
use Hampel\Linode\Commands\LinodeDiskCommand;
use Hampel\Linode\Commands\LinodeConfigCommand;
$linode = Linode::make('your api key here');
// Create the Linode
$response = $linode->execute(new LinodeCommand('create', [
'datacenterid' => 2, // from avail.datacenters()
'planid' => 2, // from avail.LinodePlans()
'paymentterm' => 1
]));
$linodeid = $response['LinodeID'];
// Create the Primary Disk
$response = $linode->execute(new LinodeDiskCommand('createfromstackscript', [
'linodeid' => $linodeid, // the one we just created
'stackscriptid' => 123,
'stackscriptudfresponses' => '{}',
'distributionid' => 89,
'label' => 'PrimaryDisk',
'size' => 24576
'rootpass' => 'xxxxxxxx'
]));
$primarydiskid = $response['DiskID'];
// Create the Swap Disk
$response = $linode->execute(new LinodeDiskCommand('create', [
'linodeid' => $linodeid,
'label' => 'SwapDisk',
'type' => 'swap',
'size' => 256
]));
$swapdiskid = $response['DiskID'];
// Create the Configuration Profile
$response = $linode->execute(new LinodeConfigCommand('create', [
'linodeid' => $linodeid,
'kernelid' => 138, // from avail.kernels()
'label' => 'StandardProfile',
'disklist' => "$primarydiskid,$swapdiskid,,,,,,,",
'rootdevicenum' => 1
]));
$configid = $response['ConfigID'];
// Boot the Linode
$response = $linode->execute(new LinodeCommand('boot', [
'linodeid' => $linodeid,
'configid' => $configid
]));
?>
说明
以下调用已实现
- account.*
- api.*
- avail.*
- domain.*
- domain.resource.*
- linode.*
- linode.config.*
- linode.disk.*
- linode.ip.*
- linode.job.*
- nodebalancer.*
- nodebalancer.config.*
- nodebalancer.node.*
- stackscript.*
- test.*
- user.*
待办事项
为所有命令编写网络测试(这可能会导致某些测试产生信用卡费用!)
单元测试
将phpunit.xml.dist重命名为phpunit.xml以设置单元测试,在php部分配置您的API密钥
:::xml
<php>
<const name="API_KEY" value="Linode API Key goes here" />
</php>
要仅运行模拟测试并忽略网络测试,请运行:phpunit --exclude-group network
有一些额外的测试将导致您的Linode账户产生信用卡费用,因此默认情况下被禁用。您可以通过运行以下命令来运行这些特定测试:phpunit --group chargeable
警告
运行可收费组测试将导致您的信用卡产生费用!尽管单元测试创建的服务器在清理过程中将被删除,但退款金额将与账单金额不匹配!如果单元测试失败,服务器将保留在您的Linode账户中,您将为此付费:在这种情况下,您必须手动删除它们。