Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
ReservedIPService
100.00% covered (success)
100.00%
24 / 24
100.00% covered (success)
100.00%
7 / 7
12
100.00% covered (success)
100.00%
1 / 1
 getReservedIP
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReservedIPs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 deleteReservedIP
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createReservedIP
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 convertInstanceIP
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 attachReservedIP
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 detachReservedIP
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Vultr\VultrPhp\Services\ReservedIP;
6
7use Vultr\VultrPhp\Services\VultrService;
8use Vultr\VultrPhp\Util\ListOptions;
9use Vultr\VultrPhp\Util\VultrUtil;
10use Vultr\VultrPhp\VultrClientException;
11
12/**
13 * Reserved IP service handler, for all reserved-ips endpoints.
14 *
15 * @see https://www.vultr.com/api/#tag/reserved-ip
16 */
17class ReservedIPService extends VultrService
18{
19    /**
20     * @param $reserved_id - string - UUID of the reserved ip
21     * @throws ReservedIPException
22     * @throws VultrException
23     * @return ReservedIP
24     */
25    public function getReservedIP(string $reserved_id) : ReservedIP
26    {
27        return $this->getObject('reserved-ips/'.$reserved_id, new ReservedIP());
28    }
29
30    /**
31     * @param $options - ListOptions|null - Interact via reference.
32     * @throws ReservedIPException
33     * @return ReservedIP[]
34     */
35    public function getReservedIPs(?ListOptions &$options = null) : array
36    {
37        return $this->getListObjects('reserved-ips', new ReservedIP(), $options);
38    }
39
40    /**
41     * @param $reserved_id - string
42     * @throws ReservedIPException
43     * @return void
44     */
45    public function deleteReservedIP(string $reserved_id) : void
46    {
47        $this->deleteObject('reserved-ips/'.$reserved_id, new ReservedIP());
48    }
49
50    /**
51     * @param $region - string - Region identifaction site code.
52     * @param $ip_type - string - v4 or v6
53     * @param $label - string - What shall you name it?
54     * @throws ReservedIPException
55     * @return ReservedIP
56     */
57    public function createReservedIP(string $region, string $ip_type, string $label = '') : ReservedIP
58    {
59        $params = [
60            'region' => $region,
61            'ip_type' => $ip_type,
62        ];
63
64        if ($label != '')
65        {
66            $params['label'] = $label;
67        }
68
69        return $this->createObject('reserved-ips', new ReservedIP(), $params);
70    }
71
72    /**
73     * @see https://www.vultr.com/api/#tag/reserved-ip/operation/convert-reserved-ip
74     * @param $ip_address - string - Example: 192.168.0.1
75     * @param $label - string|null
76     * @throws ReservedIPException
77     * @throws VultrException
78     * @return ReservedIP
79     */
80    public function convertInstanceIP(string $ip_address, ?string $label = null) : ReservedIP
81    {
82        $client = $this->getClientHandler();
83
84        $params = [
85            'ip_address' => $ip_address
86        ];
87
88        if ($label !== null)
89        {
90            $params['label'] = $label;
91        }
92
93        try
94        {
95            $response = $client->post('reserved-ips/convert', $params);
96        }
97        catch (VultrClientException $e)
98        {
99            throw new ReservedIPException('Failed to convert instance ip: '.$e->getMessage(), $e->getHTTPCode(), $e);
100        }
101
102        $model = new ReservedIP();
103        return VultrUtil::convertJSONToObject((string)$response->getBody(), $model, $model->getResponseName());
104    }
105
106    /**
107     * @see https://www.vultr.com/api/#tag/reserved-ip/operation/attach-reserved-ip
108     * @param $reserved_ip - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60
109     * @param $instance_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60
110     * @throws ReservedIPException
111     * @return void
112     */
113    public function attachReservedIP(string $reserved_ip, string $instance_id) : void
114    {
115        $client = $this->getClientHandler();
116
117        try
118        {
119            $client->post('reserved-ips/'.$reserved_ip.'/attach', ['instance_id' => $instance_id]);
120        }
121        catch (VultrClientException $e)
122        {
123            throw new ReservedIPException('Failed to attach reserved ip: '.$e->getMessage(), $e->getHTTPCode(), $e);
124        }
125    }
126
127    /**
128     * @see https://www.vultr.com/api/#tag/reserved-ip/operation/detach-reserved-ip
129     * @param $reserved_ip - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60
130     * @throws ReservedIPException
131     * @return void
132     */
133    public function detachReservedIP(string $reserved_ip) : void
134    {
135        $client = $this->getClientHandler();
136
137        try
138        {
139            $client->post('reserved-ips/'.$reserved_ip.'/detach');
140        }
141        catch (VultrClientException $e)
142        {
143            throw new ReservedIPException('Failed to detach reserved ip: '.$e->getMessage(), $e->getHTTPCode(), $e);
144        }
145    }
146}