Use the 1&1 PHP Cloud Server SDK

Table of Contents

Overview

This guide will show you how to programmatically use the 1&1 library to perform common management tasks also available through the 1&1 Control Panel.

The PHP Client Library wraps the latest version of the 1&1 REST API. All API operations are performed over SSL and authenticated using your 1&1 API Token. The API can be accessed within an instance running in 1&1 or directly over the Internet from any application that can send an HTTPS request and receive an HTTPS response.

Requirements

  • A platform (such as a server) which can send an HTTPS request and receive an HTTPS response.
  • The 1&1 PHP SDK installed and configured.

Operations

Using the Module

Official 1&1 REST API Documentation: https://cloudpanel-api.1and1.com/documentation/1and1/v1/en/documentation.html

The following examples are meant to give you a general overview of some of the things you can do with the 1&1 PHP SDK. For a detailed list of all methods and functionality, please see the /docs/reference.md file.

Creating a Server

<?php

require(__DIR__.'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// Instantiate Server Object
$server = $client->server();

// Create HDD's
$hdd1 = [
    'size' => 120,
    'is_main' => True
];

$hdds = [$hdd1];

// Create Server
$my_server = [
    'name' => 'Example Server',
    'description' => 'Example Desc',
    'hardware' => [
        'vcore' => 1,
        'cores_per_processor' => 1,
        'ram' => 1,
        'hdds' => $hdds
    ],
    'appliance_id' => '<IMAGE-ID>'
];

// Perform Request
$res = $server->create($my_server);
echo json_encode($res, JSON_PRETTY_PRINT);

Creating a Server with SSH Key Access

<?php

require(__DIR__.'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// Instantiate Server Object
$server = $client->server();

// Create HDD's
$hdd1 = [
    'size' => 120,
    'is_main' => True
];

$hdds = [$hdd1];

// Assign your public key to a variable
$pub_key = '<PUB-KEY>';

// Create Server
$my_server = [
    'name' => 'Example Server',
    'description' => 'Example Desc',
    'hardware' => [
        'vcore' => 1,
        'cores_per_processor' => 1,
        'ram' => 1,
        'hdds' => $hdds
    ],
    'appliance_id' => '<IMAGE-ID>',
    'rsa_key' => $pub_key
];

// Perform Request
$res = $server->create($my_server);
echo json_encode($res, JSON_PRETTY_PRINT);

Note: You may then SSH into your server by executing the following command in terminal

ssh –i <path_to_private_key_file> root@SERVER_IP

Creating a Firewall Policy

<?php

require(__DIR__.'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// Instantiate Firewall Object
$firewall_policy = $client->firewallPolicy();

// Create Rules
$rule1 = [
    'protocol' => 'TCP',
    'port_from' => 80,
    'port_to' => 80,
    'source' => '0.0.0.0'
];

$rules = [$rule1];

// Create Firewall
$args = [
    'name' => 'Example Firewall',
    'description' => 'Example Desc',
    'rules' => $rules
];

// Perform Request
$res = $firewall_policy->create($args);
echo json_encode($res, JSON_PRETTY_PRINT);

Creating a Load Balancer

<?php

require(__DIR__.'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// Instantiate Load Balancer Object
$load_balancer = $client->loadBalancer();

// Create Rules
$rule1 = [
    'protocol' => 'TCP',
    'port_balancer' => 80,
    'port_server' => 80,
    'source' => '0.0.0.0'
];

$rules = [$rule1];

// Create Load Balancer
$args = [
    'name' => 'Example LB',
    'health_check_test' => 'TCP',
    'health_check_interval' => 40,
    'persistence' => True,
    'persistence_time' => 1200,
    'method' => 'ROUND_ROBIN',
    'rules' => $rules
];

// Perform Request
$res = $load_balancer->create($args);
echo json_encode($res, JSON_PRETTY_PRINT);

Creating a Monitoring Policy

<?php

require(__DIR__.'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// Instantiate Monitoring Policy Object
$monitoring_policy = $client->monitoringPolicy();

// Create Threshold Values
$thresholds = [
    'cpu' => [
        'warning' => [
            'value' => 90,
            'alert' => False
        ],
        'critical' => [
            'value' => 95,
            'alert' => True
        ]
    ],
    'ram' => [
        'warning' => [
            'value' => 90,
            'alert' => False
        ],
        'critical' => [
            'value' => 95,
            'alert' => True
        ]
    ],
    'disk' => [
        'warning' => [
            'value' => 80,
            'alert' => False
        ],
        'critical' => [
            'value' => 90,
            'alert' => True
        ]
    ],
    'transfer' => [
        'warning' => [
            'value' => 1000,
            'alert' => False
        ],
        'critical' => [
            'value' => 2000,
            'alert' => True
        ]
    ],
    'internal_ping' => [
        'warning' => [
            'value' => 50,
            'alert' => False
        ],
        'critical' => [
            'value' => 100,
            'alert' => True
        ]
    ]
];

// Create Processes
$process1 = [
    'process' => 'test',
    'alert_if' => 'NOT_RUNNING',
    'email_notification' => True
];

$processes = [$process1];

// Create Ports
$port1 = [
    'protocol' => 'TCP',
    'port' => 22,
    'alert_if' => 'NOT_RESPONDING',
    'email_notification' => True
];

$ports = [$port1];

// Create Monitoring Policy
$args = [
    'name' => 'Example MP',
    'email' => 'test@example.com',
    'agent' => True,
    'thresholds' => $thresholds,
    'ports' => $ports,
    'processes' => $processes
];

// Perform Request
$res = $monitoring_policy->create($args);
echo json_encode($res, JSON_PRETTY_PRINT);

Then, add a server or two:

$server1 = '<SERVER-ID>';

$servers = [$server1];

$res = $monitoring_policy->addServers($servers);
echo json_encode($res, JSON_PRETTY_PRINT);

Updating Server Cores, Memory, and Disk

1&1 allows users to dynamically update cores, memory, and disk independently of each other. This removes the restriction of needing to upgrade to the next size up to receive an increase in memory. You can now simply increase the instances memory keeping your costs in-line with your resource needs.

The following code illustrates how you can update cores and memory:

<?php

require(__DIR__.'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// Instantiate Server Object
$server = $client->server();

$specs = [
    'vcore' => 2,
    'ram' => 6
];

$res = $server->modifyHardware($specs, '<SERVER-ID>');
echo json_encode($res, JSON_PRETTY_PRINT);

This is how you would update a server disk's size:

<?php

require(__DIR__.'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// Instantiate Server Object
$server = $client->server();

// Create HDD Resize
$resize = [
    'hdd_id' => '<HDD-ID>',
    'size' => 140
];

$res = $server->modifyHdd($resize, '<SERVER-ID>');
echo json_encode($res, JSON_PRETTY_PRINT);

Listing Servers, Images, Shared Storages, and More

Generating a list of resources is fairly straight forward. Every class in the library comes equipped with an all method. You may pass optional query parameters to help filter your results. By default, these parameters are all set to null.

Here are the parameters available to you:

      -page (integer): Allows to the use of pagination. Indicate which page to start on.

      -per_page (integer): Number of items per page.

      -sort (string): sort => 'name' retrieves a list of elements sorted alphabetically. sort => 'creation_date' retrieves a list of elements sorted by their creation date in descending order.

      -q (string): q is for query. Use this parameter to return only the items that match your search query.

      -fields (string): Returns only the parameters requested. (i.e. fields => 'id, name, description, hardware.ram')

Here are a few examples of how you would list resources:

<?php

require(__DIR__.'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// List All Servers On Your Account
$server = $client->server();

$res = $server->all();
echo json_encode($res, JSON_PRETTY_PRINT);

// List All Servers Whose Name Contains "My"
$server = $client->server();

$params = [
    'q' => 'My'
];

$res = $server->all($params);
echo json_encode($res, JSON_PRETTY_PRINT);

# List all images on your account
$image = $client->image();

$res = $image->all();
echo json_encode($res, JSON_PRETTY_PRINT);

Example App

This simple app creates a load balancer, firewall policy, and server. It then adds the load balancer and firewall policy to the server's initial IP address. You can access a server's initial IP by using the first_ip attribute on the Server class object, as seen in the example below.

The source code for the Example App can be found here.

<?php

require(dirname(__DIR__).'/vendor/autoload.php');

use src\oneandone\OneAndOne;

// Instantiate library with your API Token
$client = new OneAndOne('<API-TOKEN>');

// Create Load Balancer
$load_balancer = $client->loadBalancer();

$rule1 = [
    'protocol' => 'TCP',
    'port_balancer' => 80,
    'port_server' => 80,
    'source' => '0.0.0.0'
];

$rules = [$rule1];

$args = [
    'name' => 'Example LB',
    'health_check_test' => 'TCP',
    'health_check_interval' => 40,
    'persistence' => True,
    'persistence_time' => 1200,
    'method' => 'ROUND_ROBIN',
    'rules' => $rules
];

echo "Creating load balancer...\n";
$res = $load_balancer->create($args);
// Wait for Load Balancer to Deploy
echo $load_balancer->waitFor();

// Create Firewall Policy
$firewall_policy = $client->firewallPolicy();

$rule1 = [
    'protocol' => 'TCP',
    'port_from' => 80,
    'port_to' => 80,
    'source' => '0.0.0.0'
];

$rules = [$rule1];

$args = [
    'name' => 'Example Firewall',
    'description' => 'Example Desc',
    'rules' => $rules
];

echo "\nCreating firewall policy...\n";
$res = $firewall_policy->create($args);
// Wait for Firewall to Deploy
echo $firewall_policy->waitFor();

// Create Server
$server = $client->server();

$my_server = [
    'name' => 'Example App Server',
    'hardware' => [
        'fixed_instance_size_id' => '65929629F35BBFBA63022008F773F3EB'
    ],
    'appliance_id' => '6C902E5899CC6F7ED18595EBEB542EE1',
    'datacenter_id' => '5091F6D8CBFEF9C26ACE957C652D5D49'
];

echo "\nCreating server...\n";
$res = $server->create($my_server);
// Wait for Server to Deploy
echo $server->waitFor();

// Add the Load Balancer to the New IP
$add_lb = [
    'ip_id' => $server->first_ip['id'],
    'load_balancer_id' => $load_balancer->id
];

echo "\nAdding load balancer to the IP...\n";
$res = $server->addLoadBalancer($add_lb);
// Wait for load balancer to be added
echo $server->waitFor();

// Add the Firewall Policy to the New IP
$add_firewall = [
    'ip_id' => $server->first_ip['id'],
    'firewall_id' => $firewall_policy->id
];

echo "\nAdding firewall policy to the IP...\n";
$res = $server->addFirewall($add_firewall);
// Wait for firewall policy to be added
echo $server->waitFor();

// Cleanup
echo "\nEverything looks good!\n";
echo "\nLet's clean up the mess we just made.\n";

echo "\nDeleting server...\n";
$server->delete();
echo "Success!\n";

echo "\nDeleting load balancer...\n";
$load_balancer->delete();
echo "Success!\n";

echo "\nDeleting firewall policy...\n";
$firewall_policy->delete();
echo "Success!\n";

Comments

Tags: PHP