forked from ssrini/scalr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environments.php
197 lines (158 loc) · 6.62 KB
/
Environments.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
<?php
class Scalr_UI_Controller_Environments extends Scalr_UI_Controller
{
const CALL_PARAM_NAME = 'envId';
private $checkVarError;
public static function getApiDefinitions()
{
return array('xListEnvironments', 'xGetInfo', 'xCreate', 'xSave', 'xRemove');
}
/**
* {@inheritdoc}
* @see Scalr_UI_Controller::hasAccess()
*/
public function hasAccess()
{
return parent::hasAccess() && ($this->user->canManageAcl() || $this->user->isTeamOwner());
}
public function xListEnvironmentsAction()
{
$this->request->defineParams(array(
'sort' => array('type' => 'json')
));
$selectStmt = "SELECT e.id, e.name, e.dt_added AS dtAdded, e.status";
if ($this->user->isAccountOwner()) {
$sql = "
$selectStmt
FROM client_environments e
WHERE e.client_id = ? AND :FILTER:
GROUP BY e.id
";
$params = array($this->user->getAccountId());
} else {
$sql = "
$selectStmt
FROM client_environments e
JOIN account_team_envs te ON e.id = te.env_id
JOIN account_team_users tu ON te.team_id = tu.team_id
WHERE e.client_id = ? AND tu.user_id = ? AND :FILTER:
GROUP BY e.id
";
$params = array($this->user->getAccountId(), $this->user->id);
}
$response = $this->buildResponseFromSql($sql, array('id', 'name', 'dtAdded', 'status'), array(), $params);
foreach ($response['data'] as &$row) {
$row['platforms'] = array();
foreach (Scalr_Environment::init()->loadById($row['id'])->getEnabledPlatforms() as $platform) {
$row['platforms'][] = SERVER_PLATFORMS::GetName($platform);
}
$row['platforms'] = implode(', ', $row['platforms']);
$row['dtAdded'] = Scalr_Util_DateTime::convertTz($row['dtAdded']);
}
$this->response->data($response);
}
public function xRemoveAction()
{
if (!$this->user->isAccountOwner())
throw new Scalr_Exception_InsufficientPermissions();
$env = Scalr_Environment::init()->loadById($this->getParam('envId'));
$this->user->getPermissions()->validate($env);
$env->delete();
if ($env->id == $this->getEnvironmentId())
Scalr_Session::getInstance()->setEnvironmentId(null); // reset
$this->response->success("Environment has been successfully removed.");
$this->response->data(array(
'env' => array(
'id' => $env->id
),
'flagReload' => ($env->id == $this->getEnvironmentId() ? true : false)
));
}
public function xCreateAction()
{
if (!$this->user->isAccountOwner())
throw new Scalr_Exception_InsufficientPermissions();
if (!$this->getParam('name'))
throw new Exception('Name cannot be blank.');
$this->user->getAccount()->validateLimit(Scalr_Limits::ACCOUNT_ENVIRONMENTS, 1);
$env = $this->user->getAccount()->createEnvironment($this->getParam('name'));
$this->response->success("Environment has been successfully created.");
$this->response->data(array(
'env' => array(
'id' => $env->id,
'name' => $env->name,
)
));
}
protected function getEnvironmentInfo()
{
$env = Scalr_Environment::init();
$env->loadById($this->getParam('envId'));
$this->user->getPermissions()->validate($env);
$params = array();
$params[ENVIRONMENT_SETTINGS::TIMEZONE] = $env->getPlatformConfigValue(ENVIRONMENT_SETTINGS::TIMEZONE);
return array(
'id' => $env->id,
'name' => $env->name,
'params' => $params,
'enabledPlatforms' => $env->getEnabledPlatforms()
);
}
public function xGetInfoAction()
{
$this->response->data(array('environment' => $this->getEnvironmentInfo()));
}
private function checkVar($name, $type, $env, $requiredError = '', $group = '')
{
$varName = str_replace('.', '_', ($group != '' ? $name . '.' . $group : $name));
switch ($type) {
case 'int':
if ($this->getParam($varName)) {
return intval($this->getParam($varName));
} else {
$value = $env->getPlatformConfigValue($name, true, $group);
if (!$value && $requiredError)
$this->checkVarError[$name] = $requiredError;
return $value;
}
break;
case 'string':
if ($this->getParam($varName)) {
return $this->getParam($varName);
} else {
$value = $env->getPlatformConfigValue($name, true, $group);
if ($value == '' && $requiredError)
$this->checkVarError[$name] = $requiredError;
return $value;
}
break;
case 'password':
if ($this->getParam($varName) && $this->getParam($varName) != '******') {
return $this->getParam($varName);
} else {
$value = $env->getPlatformConfigValue($name, true, $group);
if ($value == '' && $requiredError)
$this->checkVarError[$name] = $requiredError;
return $value;
}
break;
case 'bool':
return $this->getParam($varName) ? 1 : 0;
}
}
public function xSaveAction()
{
$this->request->defineParams(array('envId' => array('type' => 'int')));
$env = Scalr_Environment::init()->loadById($this->getParam('envId'));
$this->user->getPermissions()->validate($env);
if (!($this->user->isAccountOwner() || $this->user->isAccountAdmin()))
throw new Scalr_Exception_InsufficientPermissions();
$pars = array();
// check for settings
$pars[ENVIRONMENT_SETTINGS::TIMEZONE] = $this->checkVar(ENVIRONMENT_SETTINGS::TIMEZONE, 'string', $env, "Timezone required");
$env->setPlatformConfig($pars);
if (!$this->user->getAccount()->getSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED))
$this->user->getAccount()->setSetting(Scalr_Account::SETTING_DATE_ENV_CONFIGURED, time());
$this->response->success('Environment has been saved.');
}
}