fwartner/linode

使用 Guzzle 的 Linode API 包装器(针对 spark 进行了分支更新和更新)

3.1.1 2015-05-23 12:42 UTC

This package is not auto-updated.

Last update: 2024-09-18 19:01:54 UTC


README

使用 Guzzle 实现的简单命令对象的 Linode API 包装器

作者:Simon Hampel.

感谢 Everett Griffiths 实现了额外的 API 调用。

安装

推荐通过 Composer 安装 Hampel Linode。

:::json
{
    "require": {
        "fwartner/linode": "~3.1"
    }
}

注意:如果您打算使用此包与 Laravel 一起使用,我们建议安装 hampel/linode-laravel 包,该包提供了一个简单的 Laravel 服务提供者和 Facade,用于与该 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

警告

运行可收费组测试将产生信用卡费用!尽管单元测试创建的服务器在清理过程中将被删除,但退款金额将不会等于已收费金额!如果单元测试失败,服务器(s)将保留在您的 Linode 账户中,并且您将为此付费:在这种情况下,您必须手动删除它们。