|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class DeploynautAPI |
|
5
|
|
|
* |
|
6
|
|
|
* Entry point for the deploynaut API |
|
7
|
|
|
* |
|
8
|
|
|
* /naut/api/projectname/ |
|
9
|
|
|
* /naut/api/projectname/environmentname |
|
10
|
|
|
* |
|
11
|
|
|
* @todo It might be good to break this out to a controller chain so that |
|
12
|
|
|
* "/naut/api/projectname/environmentname" controller chain becomes |
|
13
|
|
|
* DeploynautAPI > APIProject > APIEnvironment |
|
14
|
|
|
* |
|
15
|
|
|
*/ |
|
16
|
|
|
class DeploynautAPI extends APINoun |
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Default URL handlers - (Action)/(ID)//(OtherID) |
|
21
|
|
|
* |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $url_handlers = array( |
|
25
|
|
|
'' => 'listProjects', |
|
26
|
|
|
'$Project//fetch' => 'project', |
|
27
|
|
|
'$Project/$Environment!' => 'environment', |
|
28
|
|
|
'$Project/' => 'project', |
|
29
|
|
|
); |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
public static $allowed_actions = array( |
|
35
|
|
|
'project', |
|
36
|
|
|
'environment', |
|
37
|
|
|
'listProjects' |
|
38
|
|
|
); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $link = 'deploynaut/api'; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param SS_HTTPRequest $request |
|
47
|
|
|
* @return SS_HTTPResponse |
|
48
|
|
|
*/ |
|
49
|
|
|
public function listProjects(SS_HTTPRequest $request) |
|
50
|
|
|
{ |
|
51
|
|
|
$response = array( |
|
52
|
|
|
'href' => Director::absoluteURL($this->Link()), |
|
53
|
|
|
'projects' => array(), |
|
54
|
|
|
); |
|
55
|
|
|
|
|
56
|
|
|
if ($request->httpMethod() != 'GET') { |
|
57
|
|
|
return $this->message('API not found', 404); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
foreach (DNProject::get() as $item) { |
|
61
|
|
|
if ($item->canView($this->getMember())) { |
|
62
|
|
|
$response['projects'][] = array( |
|
63
|
|
|
"name" => $item->Name, |
|
64
|
|
|
"href" => Director::absoluteURL($item->APILink("")), |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return $this->getAPIResponse($response); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Controller Action |
|
74
|
|
|
* |
|
75
|
|
|
* @param SS_HTTPRequest $request |
|
76
|
|
|
* @return APIProject |
|
77
|
|
|
*/ |
|
78
|
|
|
public function project(SS_HTTPRequest $request) |
|
|
|
|
|
|
79
|
|
|
{ |
|
80
|
|
|
$project = $this->getProject(); |
|
81
|
|
|
if (!$project) { |
|
82
|
|
|
return $this->project404Response(); |
|
83
|
|
|
} |
|
84
|
|
|
return new APIProject($this, $project); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Controller Action |
|
89
|
|
|
* |
|
90
|
|
|
* @param SS_HTTPRequest $request |
|
91
|
|
|
* @return APIEnvironment |
|
92
|
|
|
*/ |
|
93
|
|
|
public function environment(SS_HTTPRequest $request) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
$project = $this->getProject(); |
|
96
|
|
|
if (!$project) { |
|
97
|
|
|
return $this->project404Response(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
$environment = $this->getEnvironment(); |
|
101
|
|
|
if (!$environment) { |
|
102
|
|
|
return $this->environment404Response(); |
|
103
|
|
|
} |
|
104
|
|
|
return new APIEnvironment($this, $environment); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return SS_HTTPResponse |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function project404Response() |
|
111
|
|
|
{ |
|
112
|
|
|
$projectName = Convert::raw2xml($this->getRequest()->latestParam('Project')); |
|
113
|
|
|
return new SS_HTTPResponse('Project "' . $projectName . '" not found.', 404); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return SS_HTTPResponse |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function environment404Response() |
|
120
|
|
|
{ |
|
121
|
|
|
$envName = Convert::raw2xml($this->getRequest()->latestParam('Environment')); |
|
122
|
|
|
return new SS_HTTPResponse('Environment "' . $envName . '" not found.', 404); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Get project from URL |
|
127
|
|
|
* |
|
128
|
|
|
* @return DNProject |
|
129
|
|
|
*/ |
|
130
|
|
|
protected function getProject() |
|
131
|
|
|
{ |
|
132
|
|
|
$projectName = $this->getRequest()->param('Project'); |
|
133
|
|
|
return DNProject::get()->filter('Name', $projectName)->first(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get environment from URL |
|
138
|
|
|
* |
|
139
|
|
|
* @return DNEnvironment |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function getEnvironment() |
|
142
|
|
|
{ |
|
143
|
|
|
$projectName = $this->getRequest()->param('Project'); |
|
144
|
|
|
$project = DNProject::get()->filter('Name', $projectName)->first(); |
|
145
|
|
|
$environmentName = $this->getRequest()->param('Environment'); |
|
146
|
|
|
return $project->Environments()->filter('Name', $environmentName)->first(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return string |
|
151
|
|
|
*/ |
|
152
|
|
|
public function Link() |
|
153
|
|
|
{ |
|
154
|
|
|
return 'naut/api'; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.