This repository was archived by the owner on Jun 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhostgroup.route.php
More file actions
168 lines (162 loc) · 7.06 KB
/
hostgroup.route.php
File metadata and controls
168 lines (162 loc) · 7.06 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
//
// Copyright (c) 2013, Zynga Inc.
// https://github.com/zynga/saigon
// Author: Matt West (https://github.com/mhwest13)
// License: BSD 2-Clause
//
/*
* Saigon API - Host Groups Routes
*/
$app->get('/sapi/hostgroups/:deployment(/:staged)', function ($deployment, $staged = false) use ($app) {
check_deployment_exists($app, $deployment);
$request = $app->request();
$commonMerge = $request->get('common');
if ($staged == 1) {
$revs = RevDeploy::getDeploymentRevs($deployment);
if ($revs['currrev'] == $revs['nextrev']) {
$apiResponse = new APIViewData(1, $deployment, "Unable to detect staged revision to reference");
$app->halt(404, $apiResponse->returnJson());
}
$deployRev = $revs['nextrev'];
}
else {
$deployRev = RevDeploy::getDeploymentRev($deployment);
}
$apiResponse = new APIViewData(0, $deployment, false);
if ($commonMerge == 1) {
$apiResponse->setExtraResponseData('host_groups',
RevDeploy::getCommonMergedDeploymentHostGroups($deployment, $deployRev)
);
}
else {
$apiResponse->setExtraResponseData('host_groups',
RevDeploy::getDeploymentHostGroupswInfo($deployment, $deployRev)
);
}
$apiResponse->printJson();
})->name('saigon-api-get-host-groups');
$app->get('/sapi/hostgroup/:deployment/:hostgroup(/:staged)', function ($deployment, $hostgroup, $staged = false) use ($app) {
check_deployment_exists($app, $deployment);
if ($staged == 1) {
$revs = RevDeploy::getDeploymentRevs($deployment);
if ($revs['currrev'] == $revs['nextrev']) {
$apiResponse = new APIViewData(1, $deployment, "Unable to detect staged revision to reference");
$app->halt(404, $apiResponse->returnJson());
}
$deployRev = $revs['nextrev'];
}
else {
$deployRev = RevDeploy::getDeploymentRev($deployment);
}
if (preg_match('/,/', $hostgroup)) {
$hostgroups = preg_split('/\s?,\s?/', $hostgroup);
$results = array();
foreach ($hostgroups as $hgtemp) {
if (RevDeploy::existsDeploymentHostGroup($deployment, $hgtemp, $deployRev) === true) {
$results[$hgtemp] = RevDeploy::getDeploymentHostGroup($deployment, $hgtemp, $deployRev);
}
}
if (empty($results)) {
$apiResponse = new APIViewData(1, $deployment, "Unable to detect host groups specified: $hostgroup");
$app->halt(404, $apiResponse->returnJson());
}
else {
$apiResponse = new APIViewData(0, $deployment, false);
$apiResponse->setExtraResponseData('host_groups', $results);
}
}
else {
if (RevDeploy::existsDeploymentHostGroup($deployment, $hostgroup, $deployRev) === false) {
$apiResponse = new APIViewData(1, $deployment, "Unable to detect host groups specified: $hostgroup");
$app->halt(404, $apiResponse->returnJson());
}
else {
$hostGroupsInfo = RevDeploy::getDeploymentHostGroup($deployment, $hostgroup, $deployRev);
$apiResponse = new APIViewData(0, $deployment, false);
$apiResponse->setExtraResponseData('host_group', $hostGroupsInfo);
}
}
$apiResponse->printJson();
})->name('saigon-api-get-host-groups');
$app->post('/sapi/hostgroup/:deployment', function ($deployment) use ($app) {
check_deployment_exists($app, $deployment);
check_auth($app, $deployment);
check_revision_status($deployment);
$request = $app->request();
$contentType = $request->headers('Content-Type');
if ($contentType == 'application/json') {
$hostGroupsInfo = $request->getBody();
$hostGroupsInfo = json_decode($hostGroupsInfo,true);
}
elseif (preg_match("/form-(data|urlencoded)/", $contentType)) {
$keys = array('name','alias');
$hostGroupsInfo = array();
foreach ($keys as $key) {
$value = $request->post($key);
if ($value !== null) {
$hostGroupsInfo[$key] = $value;
}
}
}
if (empty($hostGroupsInfo)) {
$apiResponse = new APIViewData(1, $deployment, "Unable to detect any host groups information to process");
$app->halt(404, $apiResponse->returnJson());
}
// A bit of param validation
if ((!isset($hostGroupsInfo['name'])) || (empty($hostGroupsInfo['name']))) {
$apiResponse = new APIViewData(1, $deployment,
"Unable to detect name parameter"
);
$app->halt(404, $apiResponse->returnJson());
}
elseif (preg_match_all('/[^\w.-]/s', $hostGroupsInfo['name'], $forbidden)) {
$apiResponse = new APIViewData(1, $deployment,
"Unable to use host groups name specified, detected forbidden characters " . implode('', array_unique($forbidden[0]))
);
$app->halt(404, $apiResponse->returnJson());
}
if ((!isset($hostGroupsInfo['alias'])) || (empty($hostGroupsInfo['alias']))) {
$apiResponse = new APIViewData(1, $deployment,
"Unable to detect alias parameter (longer human readable information about host, one simple line)"
);
$app->halt(404, $apiResponse->returnJson());
}
elseif (preg_match_all('/[^\w.-\s]/s', $hostGroupsInfo['alias'], $forbidden)) {
$apiResponse = new APIViewData(1, $deployment,
"Unable to use host groups alias specified, detected forbidden characters " . implode('', array_unique($forbidden[0]))
);
$app->halt(404, $apiResponse->returnJson());
}
$deployRev = RevDeploy::getDeploymentNextRev($deployment);
if (RevDeploy::existsDeploymentHostGroup($deployment, $hostGroupsInfo['name'], $deployRev) === true) {
RevDeploy::modifyDeploymentHostGroup($deployment, $hostGroupsInfo['name'], $hostGroupsInfo, $deployRev);
$apiResponse = new APIViewData(0, $deployment,
"Successfully Modified Host Group " . $hostGroupsInfo['name']
);
}
else {
RevDeploy::createDeploymentHostGroup($deployment, $hostGroupsInfo['name'], $hostGroupsInfo, $deployRev);
$apiResponse = new APIViewData(0, $deployment,
"Successfully Created Host Group " . $hostGroupsInfo['name']
);
}
$apiResponse->printJson();
})->name('saigon-api-create-host-groups');
$app->delete('/sapi/hostgroup/:deployment/:hostgroup', function($deployment, $hostgroup) use ($app) {
check_deployment_exists($app, $deployment);
check_auth($app, $deployment);
check_revision_status($deployment);
$deployRev = RevDeploy::getDeploymentNextRev($deployment);
if (preg_match('/,/', $hostgroup)) {
$hostgroups = preg_split('/\s?,\s?/', $hostgroup);
foreach ($hostgroups as $hgtemp) {
RevDeploy::deleteDeploymentHostGroup($deployment, $hgtemp, $deployRev);
}
}
else {
RevDeploy::deleteDeploymentHostGroup($deployment, $hostgroup, $deployRev);
}
$apiResponse = new APIViewData(0, $deployment, "Successfully Removed Host Groups: $hostgroup");
$apiResponse->printJson();
})->name('saigon-api-delete-host-groups');