HPCloud-PHP  1.2.0
PHP bindings for HPCloud and OpenStack services.
 All Classes Namespaces Files Functions Variables Pages
Snapshot.php
Go to the documentation of this file.
1 <?php
2 /* ============================================================================
3 (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of
8 the Software, and to permit persons to whom the Software is furnished to do so,
9 subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 ============================================================================ */
22 /**
23  * @file
24  *
25  * This file contains the DBaaS Snapshot class.
26  */
27 
28 namespace HPCloud\Services\DBaaS;
29 
30 use \HPCloud\Services\DBaaS\SnapshotDetails;
31 
32 /**
33  * Manage snapshots.
34  *
35  * A snapshot is an image (a backup) of a particular database taken at a
36  * particular point in time. They can be used by HP Cloud Services to restore
37  * a database instance to a particular point in time.
38  *
39  * Snapshotscan be created and deleted. Information about the snapshots can
40  * be retrieved either as lists of snapshots or as individual snapshot details.
41  *
42  * Generally, Snapshot objects should be created through the
43  * HPCloud::Services::DBaaS::snapshot() factory, and not created directly.
44  *
45  * Any operation that goes to the remote server may throw one of the
46  * HPCloud::Exception exceptions.
47  */
48 class Snapshot extends Operations {
49 
50  protected $token;
51  protected $projectId;
52  protected $url;
53  protected $client;
54 
55  public function __construct($token, $projectId, $endpoint) {
56  $this->token = $token;
57  $this->projectId = $projectId;
58  $this->url = $endpoint;
59  $this->client = \HPCloud\Transport::instance();
60  }
61 
62  /**
63  * Get a list of snapshot details.
64  *
65  * @param string $instanceId
66  * An optional database instance ID. If set, only snapshots for
67  * the given instance will be returned.
68  * @retval array
69  * @return array
70  * An array of HPCloud::Services::DBaaS::SnapshotDetails
71  * instances.
72  */
73  public function listSnapshots($instanceId = NULL) {
74  $url = $this->url . '/snapshots';
75  if (!empty($instanceId)) {
76  $url .= '?instanceId=' . rawurlencode($instanceId);
77  }
78  $headers = $this->headers();
79  $retval = $resp = $this->client->doRequest($url, 'GET', $headers);
80 
81  $json = json_decode($retval, TRUE);
82  $list = array();
83  foreach ($json['snapshots'] as $item) {
84  $list[] = SnapshotDetails::newFromJSON($item);
85  }
86 
87  return $list;
88  }
89 
90  /**
91  * Create a new snapshot of a given instance.
92  *
93  * Given the ID of a database instance and a
94  * mnemonic name for the snapshot, take a snapshot of
95  * the given database.
96  *
97  * Note that subsequent references to this snapshot must
98  * be made by snapshot ID, not by `$name`.
99  *
100  * @param string $instanceId
101  * The instance ID for the database to snapshot.
102  * @param string $name
103  * A human-readable name for the snapshot. Internally,
104  * a snapshot ID will be used to reference this
105  * snapshot.
106  * @retval HPCloud::Services::DBaaS::SnapshotDetails
107  * @return \HPCloud\Services\DBaaS\SnapshotDetails
108  * A snapshot details object containing information about
109  * the snapshot.
110  */
111  public function create($instanceId, $name) {
112  $url = $this->url . '/snapshots';
113  $create = array(
114  'snapshot' => array(
115  'instanceId' => $instanceId,
116  'name' => $name,
117  )
118  );
119 
120  $json = json_encode($create);
121  $resp = $this->client->doRequest($url, 'POST', $this->headers(), $json);
122 
123  $data = json_decode($resp, TRUE);
124 
125  return SnapshotDetails::newFromJSON($data['snapshot']);
126  }
127 
128  /**
129  * Given a snapshot ID, delete the snapshot.
130  *
131  * @param string $snapshotId
132  * The snapshot ID for the snapshot that should
133  * be deleted.
134  * @retval boolean
135  * @return boolean
136  * Returns boolean TRUE on success. Throws one of the
137  * HPCloud::Exception instances on failure.
138  * @throws HPCloud::Exception
139  * One of the Transport class of exceptions.
140  */
141  public function delete($snapshotId) {
142  $url = sprintf('%s/snapshots/%s', $this->url, $snapshotId);
143  $this->client->doRequest($url, 'DELETE', $this->headers());
144 
145  return TRUE;
146  }
147 
148  /**
149  * Get the details for a particular snapshot.
150  *
151  * @param string $snapshotId
152  * The snapshot ID.
153  *
154  * @retval HPCloud::Services::DBaaS::SnapshotDetails
155  * @return \HPCloud\Services\DBaaS\SnapshotDetails
156  * The details object.
157  */
158  public function describe($snapshotId) {
159  $url = sprintf('%s/snapshots/%s', $this->url, $snapshotId);
160  $res = $this->client->doRequest($url, 'GET', $this->headers());
161 
162  $json = json_decode($res->content(), TRUE);
163 
164  return SnapshotDetails::newFromJSON($json['snapshot']);
165  }
166 
167 
168 }