Cdn_RackSpace_Api_Tokens.php
3.65 KB
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
<?php
namespace W3TC;
class Cdn_RackSpace_Api_Tokens {
static public function authenticate( $user_name, $api_key ) {
$request_json = array( 'auth' =>
array( 'RAX-KSKEY:apiKeyCredentials' => array(
'username' => $user_name,
'apiKey' => $api_key
) ) );
$result = wp_remote_post(
'https://identity.api.rackspacecloud.com/v2.0/tokens',
array(
'headers' => array(
'Accept' => 'application/json',
'Content-Type' => 'application/json'
),
'sslcertificates' => dirname( __FILE__ ) .
'/Cdn_RackSpace_Api_CaCert.pem',
'body' => json_encode( $request_json )
)
);
$response = self::_decode_response( $result );
if ( !isset( $response['access'] ) )
throw new \Exception(
'Unexpected authentication response: access token not found' );
$r = $response['access'];
// fill service descriptors by region
if ( !isset( $r['serviceCatalog'] ) )
throw new \Exception(
'Unexpected authentication response: serviceCatalog token not found' );
$services = $r['serviceCatalog'];
return array(
'access_token' => $r['token']['id'],
'services' => $services
);
}
static public function cloudfiles_services_by_region( $services ) {
$by_region = array();
foreach ( $services as $s ) {
if ( $s['type'] == 'object-store' ) {
foreach ( $s['endpoints'] as $endpoint ) {
$region = $endpoint['region'];
if ( !isset( $by_region[$region] ) )
$by_region[$region] = array();
$by_region[$region]['object-store.publicURL'] =
$endpoint["publicURL"];
$by_region[$region]['object-store.internalURL'] =
$endpoint["internalURL"];
}
} elseif ( $s['type'] == 'rax:object-cdn' ) {
foreach ( $s['endpoints'] as $endpoint ) {
$region = $endpoint['region'];
if ( !isset( $by_region[$region] ) )
$by_region[$region] = array();
$by_region[$region]['object-cdn.publicURL'] =
$endpoint["publicURL"];
}
}
}
$by_region = self::_add_region_names( $by_region );
return $by_region;
}
static public function cdn_services_by_region( $services ) {
$by_region = array();
foreach ( $services as $s ) {
if ( $s['type'] == 'rax:cdn' ) {
foreach ( $s['endpoints'] as $endpoint ) {
$region = $endpoint['region'];
if ( !isset( $by_region[$region] ) )
$by_region[$region] = array();
$by_region[$region]['cdn.publicURL'] =
$endpoint["publicURL"];
}
}
}
$by_region = self::_add_region_names( $by_region );
return $by_region;
}
static private function _add_region_names( $by_region ) {
// try to decode region names
$region_names = array(
'ORD' => 'Chicago (ORD)',
'DFW' => 'Dallas/Ft. Worth (DFW)',
'HKG' => 'Hong Kong (HKG)',
'LON' => 'London (LON)',
'IAD' => 'Northern Virginia (IAD)',
'SYD' => 'Sydney (SYD)'
);
$keys = array_keys( $by_region );
foreach ( $keys as $region ) {
if ( isset( $region_names[$region] ) )
$by_region[$region]['name'] = $region_names[$region];
else
$by_region[$region]['name'] = $region;
}
return $by_region;
}
static private function _decode_response( $result ) {
if ( is_wp_error( $result ) )
throw new \Exception( 'Failed to reach API endpoint' );
$response_json = @json_decode( $result['body'], true );
if ( is_null( $response_json ) )
throw new \Exception(
'Failed to reach API endpoint, got unexpected response ' .
$result['body'] );
if ( isset( $response_json['unauthorized']['message'] ) )
throw new \Exception( $response_json['unauthorized']['message'] );
if ( $result['response']['code'] != '200' && $result['response']['code'] != '201' )
throw new \Exception( $result['body'] );
return $response_json;
}
}