sandwave-io/cloud-sdk-php

PCextreme API 云 SDK

0.4.6 2022-01-06 09:22 UTC

README

PCextreme 云 API - PHP SDK

Codecov GitHub Workflow Status Packagist PHP Version Support Packagist PHP Version Support Packagist Downloads

如何贡献

如有改进想法,请随意创建一个 PR。或者创建一个问题。

  • 添加代码时,请确保为其添加测试(phpunit)。
  • 确保代码符合我们的编码标准(使用 php-cs-fixer 检查/修复)。
  • 同时确保 PHPStan 不会发现任何错误。
vendor/bin/php-cs-fixer fix

vendor/bin/phpstan analyze

vendor/bin/phpunit --coverage-text

这些工具也会在 GitHub actions 的 PR 和 master 推送上运行。

用法

use PCextreme\CloudSdkPhp\CloudSdk;

$cloudSdk = new CloudSdk('your-pcextreme-api-key', 'your-pcextreme-account-uuid');

$cloudSdk->listServers(); // list all servers under your account

可用方法

目前实现的端点并非全部现有端点。如果您需要实现其他端点,请创建一个问题或打开一个 PR。😊

部署服务器

$cloudSdk->deployServer(
    'example.com',
    'SuperSecretPassword',
    'pcexterme-offer-uuid', // check the listOffers method to get this id.
    'pcexterme-template-uuid', // check the listTemplates method to get this id.
    'pcexterme-datacenter-uuid' // check the listDatacenters method to get this id.
);

这将返回创建的服务器 ID

[
    'id' => 'aaaa-bbbb-cccc-dddd-eeee'
]

列出服务器

$cloudSdk->listServers();

这将返回一个包含您账户中所有服务器的数组。

[
    [
        'id'                    => 'aaaa-bbbb-cccc-dddd-eeee',
        'display_name'          => 'display_name',
        'status'                => 'Running',
        'rescue_iso_attached'   => false,
        'has_security_group'    => true,
    
        'created_at'            => '2020-04-02T09:50:13+00:00',
        'updated_at'            => '2020-04-02T09:50:13+00:00',
    ],
];

显示服务器

您也可以查看特定服务器。

$cloudSdk->showServer('aaaa-bbbb-cccc-dddd-eeee');
[
    'id'                    => 'aaaa-bbbb-cccc-dddd-eeee',
    'display_name'          => 'display_name',
    'status'                => 'Running',
    'rescue_iso_attached'   => false,
    'has_security_group'    => true,

    'created_at'            => '2020-04-02T09:50:13+00:00',
    'updated_at'            => '2020-04-02T09:50:13+00:00',
];

服务器详情

您可以使用此调用从底层 Cloudstack 系统获取数据。

$cloudSdk->showDetails('aaaa-bbbb-cccc-dddd-eeee');

查看 cloudstack 文档 以查看所有可用信息。

[
    "id"        => "aaaa-bbbb-cccc-dddd-eeee",
    "name"      => "aaaa-bbbb-cccc-dddd-eeee",
    "displayname" => "Name",
    "account" => "3",
    "userid" => "ad09ecc3-0356-4fva-ab42-d3dee2b08d82",
    "username" => "3",
    "domainid" => "6f243d98-7f2f-4276-9381-68e2a76f8b33",
    "domain" => "3",
    "created" => "2019-12-04T10:10:28+0100",
    "state" => "Running",
    "haenable" => false,
    "zoneid" => "10c85e3a-b499-4b73-a78d-f2f48ca2a3ba",
    "zonename" => "zone03.ams02.cldin.net",
    "templateid" => "50ea3730-6cf4-47c2-9663-387d16fa8c72",
    "templatename" => "Ubuntu 18.04.2",
    "templatedisplaytext" => "Ubuntu 18.04.2 20GB",
    "passwordenabled" => true,
    "serviceofferingid" => "53a3b7ab-8f9f-4ee1-b289-db70a29168aa",
    "serviceofferingname" => "pcx_standard_custom",
    "cpunumber" => 16,
    "cpuspeed" => 2000,
    "memory" => 65536,
    "cpuused" => "0.11%",
    "networkkbsread" => 898116,
    "networkkbswrite" => 40097,
    "diskkbsread" => 672,
    "diskkbswrite" => 4583328,
    "memorykbs" => 67108864,
    "memoryintfreekbs" => 67005920,
    "memorytargetkbs" => 67108864,
    "diskioread" => 168,
    "diskiowrite" => 346239,
    "guestosid" => "4ba6aae8-a4a1-11e9-814e-5254000588f7",
    "rootdeviceid" => 0,
    "rootdevicetype" => "ROOT",
    "securitygroup" => [
        [
            "id" => "a259a05b-8ea8-4fb0-97fb-9735261de5cb",
            "name" => "epic-vm","account" => "3",
            "ingressrule" => [],
            "egressrule" => [],
            "tags" => [],
            "virtualmachineids" => [],
        ],
    ],
    "nic" => [
        [
            "id" => "b707be76-7f82-44d0-b900-9fbdaba591bc",
            "networkid" => "a0234ec3-bdb6-46b8-80a1-37144d7c3928",
            "networkname" => "Guest Network 1",
            "netmask" => "255.255.255.0",
            "gateway" => "185.109.216.3",
            "ipaddress" => "185.109.216.118",
            "isolationuri" => "vxlan:\/\/500",
            "broadcasturi" => "vxlan:\/\/500",
            "traffictype" => "Guest",
            "type" => "Shared",
            "isdefault" => true,
            "macaddress" => "1e:00:1f:00:00:72",
            "ip6gateway" => "2a05:1500:600::1",
            "ip6cidr" => "2a05:1500:600::\/64",
            "ip6address" => "2a05:1500:600:0:1c00:1fff:fe00:72",
            "secondaryip" => [],
            "extradhcpoption" => [],
        ]
    ],
    "hypervisor" => "KVM",
    "details" => [
        "cpuNumber" => "16",
        "memory" => "65536",
        "cpuSpeed" => "2000",
        "memoryOvercommitRatio" => "1.0",
        "cpuOvercommitRatio" => "4.0",
        "rootdisksize" => "1280",
        "rootDiskController" => "scsi"
    ],
    "affinitygroup" => [],
    "isdynamicallyscalable" => true,
    "ostypeid" => "4ba6aae8-a4a1-11e9-814e-5254000588f7",
    "tags" => []
];

升级服务器

如果您想升级现有服务器,可以使用 upgradeServer。

请注意,在运行此操作时必须停止 VM。

$cloudSdk->upgradeServer(
    'aaaa-bbbb-cccc-dddd-eeee',
    'pcexterme-offer-uuid' // check the listOffers method to get this id.
);

重置服务器

$cloudSdk->resetServer('aaaa-bbbb-cccc-dddd-eeee');

解除救援 ISO 服务器

救援 ISO 可以用于恢复损坏的服务器。

$cloudSdk->detachRescueIso('aaaa-bbbb-cccc-dddd-eeee');

附加救援 ISO 服务器

救援 ISO 可以用于恢复损坏的服务器。

$cloudSdk->detachRescueIso('aaaa-bbbb-cccc-dddd-eeee');

编辑显示名称服务器

$cloudSdk->updateServerHostname(
    'aaaa-bbbb-cccc-dddd-eeee',
    'hostname',
);

重启服务器

$cloudSdk->rebootServer('aaaa-bbbb-cccc-dddd-eeee');

启动服务器

$cloudSdk->startServer('aaaa-bbbb-cccc-dddd-eeee');

停止服务器

$cloudSdk->stopServer('aaaa-bbbb-cccc-dddd-eeee');

删除服务器

$cloudSdk->deleteServer('aaaa-bbbb-cccc-dddd-eeee');

注意:在 Cloudstack 中存在一个宽限期。因此,数据不会立即丢失。

获取账户当前 RAM 使用情况

默认情况下,账户资源有限。您可以使用此方法检索当前资源使用情况。

如果您希望扩大限制,请联系 PCextreme。

$cloudSdk->getUsage();
[
    'ram' => 1 // in GB's
];

获取账户报价

在部署或升级服务器之前,您必须检索要使用的报价 ID。

$cloudSdk->listOffers();
[
    [
        'id'                 => 'aaaa-bbbb-cccc-dddd-eeee',
        'account_id'         => null, // Contains your account ID in the case of a custom offering.
        'billing_period'     => 12, // Billing period in months
        'price'              => 9990, // Price in cents.
        'type'               => 'usage',
        'sku'                => 'compute_standard_2gb',
        'name'               => 'server',
        'description'        => 'server',
        'show_in_store'      => 0,
        'specifications'     => [
            // Filled with specifications depending on the offering.
        ],
    
        'created_at'         => '2020-04-02T09:50:13+00:00',
        'updated_at'         => '2020-04-02T09:50:13+00:00',
    ]
];

获取账户数据中心

对于部署,您还需要知道要部署的数据中心。

$cloudSdk->listDatacenters();

响应还包含标准和高可用 VM 的可用性。这些是两种不同的存储类型,两种 VM 之间的差异也可以根据报价数据识别。

[
    [
        'id'            => 'aaaa-bbbb-cccc-dddd-eeee',
        'name'          => 'ams01',
        'description'   => 'Amsterdam',
        'city'          => 'ams',
        'country'       => 'nl',
        'timezone'      => 'Europe/Amsterdam',
        'standard_enabled'  => true,
        'ha_enabled'        => true
    ]
];

获取账户模板

用于 VM 部署的模板是预定义的,可以使用 listTemplates 查看。

$cloudSdk->listTemplates();
[
    [
        'id'            => 'aaaa-bbbb-cccc-dddd-eeee',
        'display_name'  => 'Fedora 30',
        'os'            => 'fedora',
        'version'       => 30,
        'created_at'    => '2020-04-02T09:50:13+00:00',
        'updated_at'    => '2020-04-02T09:50:13+00:00'
    ]
];

获取账户私有网络

用于 VM 部署的模板是预定义的,可以使用 listTemplates 查看。

$cloudSdk->listNetworks();
[
    [
        "id" => "d11c06f5-ff1c-41cd-8fc9-1ebcb3084501",
        "display_name" => "An example (No SG)",
        "datacenter_id" => "36616598-8e93-4118-a03c-94f99e5e1169",
        "manager" => "man.zone03.ams02.cldin.net",
        "cidr_ipv4" => "185.109.217.64/26",
        "cidr_ipv6" => "2a05:1500:600:4::/64",
        "created_at" => "2019-09-09T08:58:05+00:00",
        "updated_at" => "2019-09-09T08:58:05+00:00"
    ]
];

获取当前服务器的控制台 URL

如果您想打开基于浏览器的控制台,可以使用此方法生成 URL。

$cloudSdk->getConsoleUrl();
[
    'url' => 'www.test.nl'
];