/
Openstack.php
202 lines (165 loc) · 5.08 KB
/
Openstack.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php
/**
* Created by PhpStorm.
* User: wiz
* Date: 2015/12/16
* Time: 15:49
*/
require_once("./Http/Client.php");
require_once("./Common/Catalog.php");
require_once("./Common/CatalogItem.php");
require_once("./Compute/ComputeService.php");
require_once("./Volume/VolumeService.php");
require_once("./Identity/IdentityService.php");
class Openstack extends Client {
/**
* @var array = {"username"=>"admin", "password"=>"jcb410"}
*/
private $secret = array();
//@var string : e4ca38ebf28c4cf6a0f6f8ae32233726
private $token = null;
//@var string : tenant_id as e4ca38ebf28c4cf6a0f6f8ae32233726
private $tenant = null;
//@var object: \Service\Catalog
private $catalog = null;
//@var string (e.g. http://172.16.4.31)
private $authUrl = null;
//@var string (e.g. http://172/16/4/31/v2/16466sdfasdf68484/)
//private $baseUrl = null;
//@var string (e.g. 5000)
//private $tokenPort = null;
//TODO:
//private $user;
/**
* param: $secret must have key: "username" and "password"
*/
public function __construct($url, array $secret) {
if (!array_key_exists("username",$secret) || !array_key_exists("password", $secret)) {
echo "Openstack: __construct has err param: $secret";
return null;
}
$this->setSecret($secret);
$this->setAuthUrl($url);//ÕâÀïÖ»Óд¿ip
parent::__construct();
echo "<br/>Openstack __construct() ok <br/>";
}
/**
* Set the credentials for the client
*
* @param array $secret
* @return $this
*/
public function setSecret(array $secret = array()) {
$this->secret = $secret;
return $this;
}
/**
* Get the secret.
*
* @return array
*/
public function getSecret() {
return $this->secret;
}
public function setToken($token) {
$this->token = $token;
return $this;
}
public function getToken() {
return $this->token;
}
public function setTenant($tenant) {
$this->tenant = $tenant;
return $this;
}
public function getTenant() {
return $this->tenant;
}
/**
* @param: response(json) by http
* @return $this
*/
public function setCatalog($catalog) {
$this->catalog = Catalog::factory($catalog);
return $this;
}
public function getCatalog() {
return $this->catalog;
}
//return $this
public function setAuthUrl($url) {
$this->authUrl = $url;
return $this;
}
public function getAuthUrl() {
return $this->authUrl;
}
/*
//return $this
public function setTokenPort($port = "5000") {
$this->tokenPort = $port;
return $this;
}
public function getTokenPort() {
return $this->TokenPort;
}
*/
/**
* Formats the credentials array (as a string) for authentication
*
* @return string
*/
public function getCredentials() {
if (!empty($this->secret['username']) && !empty($this->secret['password'])) {
$credentials = array('auth' => array(
'passwordCredentials' => array(
'username' => $this->secret['username'],
'password' => $this->secret['password']
)
));
if (!empty($this->secret['tenantName'])) {
$credentials['auth']['tenantName'] = $this->secret['tenantName'];
} elseif (!empty($this->secret['tenantId'])) {
$credentials['auth']['tenantId'] = $this->secret['tenantId'];
}
return json_encode($credentials);
}else {
echo "Openstack : getCredential: err secret";
return null;
}
}
/**
* Authenticate the tenant using the supplied credentials
* and we will update X-Auth-Token in here.
* @return null
*/
public function authenticate() {
//TODO
/*$client = new Client();
$client->setURL("http://172.16.4.31:5000/v2.0/tokens");
$response = $client->post_json($this->getCredentials());
*/
$identity = IdentityService::factory($this);
$response = $identity->generateToken($this->getCredentials());
$body = json_decode($response);
//echo "access->serviceCatalog:<br/>";
//print_r($body);
//echo "<br/>";
$this->setCatalog($body->access->serviceCatalog);
$this->setToken($body->access->token->id);
parent::setHeader("X-Auth-Token", $this->getToken());
if (isset($body->access->token->tenant)) {
$this->setTenant($body->access->token->tenant->id);
}
}
//factory
public function computerService($name = null, $region = null, $urlType = null) {
$type = null;
return new ComputeService($this ,$type, $name, $region, $urlType);
}
//factory
public function volumeService($name = null, $region = null, $urlType = null) {
$type = null;
return new VolumeService($this, $type, $name, $region, $urlType);
}
};