Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.45% covered (success)
95.45%
21 / 22
85.71% covered (warning)
85.71%
6 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
ObjectStorageService
95.45% covered (success)
95.45%
21 / 22
85.71% covered (warning)
85.71%
6 / 7
10
0.00% covered (danger)
0.00%
0 / 1
 getClusters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getObjectStoreSubs
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getObjectStoreSub
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createObjectStoreSub
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 deleteObjectStoreSub
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 updateObjectStoreSub
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 regenObjectStoreKeys
92.31% covered (success)
92.31%
12 / 13
0.00% covered (danger)
0.00%
0 / 1
3.00
1<?php
2
3declare(strict_types=1);
4
5namespace Vultr\VultrPhp\Services\ObjectStorage;
6
7use Vultr\VultrPhp\Services\VultrService;
8use Vultr\VultrPhp\Services\VultrServiceException;
9use Vultr\VultrPhp\Util\ListOptions;
10use Vultr\VultrPhp\Util\VultrUtil;
11use Vultr\VultrPhp\VultrClientException;
12
13/**
14 * Object storage service handler, for all object-storage endpoints.
15 *
16 * @see https://www.vultr.com/api/#tag/s3
17 */
18class ObjectStorageService extends VultrService
19{
20    /**
21     * Get object storage cluster regions that are available to be deployed in.
22     *
23     * @see https://www.vultr.com/api/#operation/list-object-storage-clusters
24     * @param $options - ListOptions|null - Interact via reference.
25     * @throws ObjectStorageException
26     * @return array
27     */
28    public function getClusters(?ListOptions &$options = null) : array
29    {
30        return $this->getListObjects('object-storage/clusters', new ObjStoreCluster(), $options);
31    }
32
33    /**
34     * Get all object storage subscriptions on the account.
35     *
36     * @see https://www.vultr.com/api/#tag/s3/operation/list-object-storages
37     * @param $options - ListOptions|null - Interact via reference.
38     * @throws ObjectStorageException
39     * @return array
40     */
41    public function getObjectStoreSubs(?ListOptions &$options = null) : array
42    {
43        return $this->getListObjects('object-storage', new ObjectStorage(), $options);
44    }
45
46    /**
47     * Get a specific object storage subscription on the account.
48     *
49     * @see https://www.vultr.com/api/#tag/s3/operation/get-object-storage
50     * @param $object_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60
51     * @throws ObjectStorageException
52     * @return ObjectStorage
53     */
54    public function getObjectStoreSub(string $object_id) : ObjectStorage
55    {
56        return $this->getObject('object-storage/'.$object_id, new ObjectStorage());
57    }
58
59    /**
60     * Create a object storage subscription in a region based on the cluster_id.
61     *
62     * @see https://www.vultr.com/api/#operation/create-object-storage
63     * @param $cluster_id - integer - @see https://www.vultr.com/api/#operation/list-object-storage-clusters
64     * @param $label - string|null - Null means omitted from the request.
65     * @throws ObjectStorageException
66     * @return ObjectStorage
67     */
68    public function createObjectStoreSub(int $cluster_id, ?string $label = null) : ObjectStorage
69    {
70        $params = [
71            'cluster_id' => $cluster_id
72        ];
73
74        if ($label !== null)
75        {
76            $params['label'] = $label;
77        }
78
79        return $this->createObject('object-storage', new ObjectStorage(), $params);
80    }
81
82    /**
83     * Delete the object storage subscription and its data.
84     *
85     * @see https://www.vultr.com/api/#operation/delete-object-storage
86     * @param $object_id - string - Example: cb676a46-66fd-4dfb-b839-443f2e6c0b60
87     * @throws ObjectStorageException
88     * @return void
89     */
90    public function deleteObjectStoreSub(string $object_id) : void
91    {
92        $this->deleteObject('object-storage/'.$object_id, new ObjectStorage());
93    }
94
95    /**
96     * Update an object storage subscription on the account.
97     *
98     * @see https://www.vultr.com/api/#operation/update-object-storage
99     * @param $obj - ObjectStorage - Fully initialized object with your updated parameters.
100     * @throws ObjectStorageException
101     * @return void
102     */
103    public function updateObjectStoreSub(ObjectStorage $obj) : void
104    {
105        $this->patchObject('object-storage/'.$obj->getId(), $obj);
106    }
107
108    /**
109     * Regenerate the object storage access and secret keys for the subscription.
110     *
111     * @see https://www.vultr.com/api/#operation/regenerate-object-storage-keys
112     * @param $obj - ObjectStorage - Fully initialized object.
113     * @throws ObjectStorageException
114     * @throws VultrServiceException
115     * @throws VultrException
116     * @return ObjectStorage
117     */
118    public function regenObjectStoreKeys(ObjectStorage $obj) : ObjectStorage
119    {
120        $client = $this->getClientHandler();
121
122        try
123        {
124            $response = $client->post('object-storage/'.$obj->getId().'/regenerate-keys');
125        }
126        catch (VultrClientException $e)
127        {
128            throw new ObjectStorageException('Failed to regenerate object storage keys: '.$e->getMessage(), $e->getHTTPCode(), $e);
129        }
130
131        $return_obj = clone $obj;
132
133        $decode = VultrUtil::decodeJSON((string)$response->getBody(), true);
134
135        if (!isset($decode['s3_credentials']))
136        {
137            throw new VultrServiceException('Failed to deserialize response, invalid json. Missing `s3_credentials` during regeneration of s3 keys.');
138        }
139
140        $credentials = $decode['s3_credentials'];
141        $return_obj->setS3Hostname($credentials['s3_hostname']);
142        $return_obj->setS3AccessKey($credentials['s3_access_key']);
143        $return_obj->setS3SecretKey($credentials['s3_secret_key']);
144
145        return $return_obj;
146    }
147}