/
DeploynautAPI.php
147 lines (131 loc) · 3.23 KB
/
DeploynautAPI.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
<?php
/**
* Class DeploynautAPI
*
* Entry point for the deploynaut API
*
* /naut/api/projectname/
* /naut/api/projectname/environmentname
*
* @todo It might be good to break this out to a controller chain so that
* "/naut/api/projectname/environmentname" controller chain becomes
* DeploynautAPI > APIProject > APIEnvironment
*
*/
class DeploynautAPI extends APINoun {
/**
* Default URL handlers - (Action)/(ID)//(OtherID)
*
* @var array
*/
private static $url_handlers = array(
'' => 'listProjects',
'$Project//fetch' => 'project',
'$Project/$Environment!' => 'environment',
'$Project/' => 'project',
);
/**
* @var array
*/
public static $allowed_actions = array(
'project',
'environment',
'listProjects'
);
/**
* @var string
*/
protected $link = 'deploynaut/api';
/**
* @param \SS_HTTPRequest $request
* @return \SS_HTTPResponse
*/
public function listProjects(\SS_HTTPRequest $request) {
$response = array(
'href' => Director::absoluteURL($this->Link()),
'projects' => array(),
);
if($request->httpMethod() != 'GET') {
return $this->message('API not found', 404);
}
foreach(DNProject::get() as $item) {
if($item->canView($this->getMember())) {
$response['projects'][] = array(
"name" => $item->Name,
"href" => Director::absoluteURL($item->APILink("")),
);
}
}
return $this->getAPIResponse($response);
}
/**
* Controller Action
*
* @param \SS_HTTPRequest $request
* @return APIProject
*/
public function project(\SS_HTTPRequest $request) {
$project = $this->getProject();
if(!$project) {
return $this->project404Response();
}
return new APIProject($this, $project);
}
/**
* Controller Action
*
* @param \SS_HTTPRequest $request
* @return APIEnvironment
*/
public function environment(\SS_HTTPRequest $request) {
$project = $this->getProject();
if(!$project) {
return $this->project404Response();
}
$environment = $this->getEnvironment();
if(!$environment) {
return $this->environment404Response();
}
return new APIEnvironment($this, $environment);
}
/**
* @return SS_HTTPResponse
*/
protected function project404Response() {
$projectName = Convert::raw2xml($this->getRequest()->latestParam('Project'));
return new SS_HTTPResponse('Project "' . $projectName . '" not found.', 404);
}
/**
* @return SS_HTTPResponse
*/
protected function environment404Response() {
$envName = Convert::raw2xml($this->getRequest()->latestParam('Environment'));
return new SS_HTTPResponse('Environment "' . $envName . '" not found.', 404);
}
/**
* Get project from URL
*
* @return DNProject
*/
protected function getProject() {
$projectName = $this->getRequest()->param('Project');
return DNProject::get()->filter('Name', $projectName)->first();
}
/**
* Get environment from URL
*
* @return DNEnvironment
*/
protected function getEnvironment() {
$projectName = $this->getRequest()->param('Project');
$project = DNProject::get()->filter('Name', $projectName)->first();
$environmentName = $this->getRequest()->param('Environment');
return $project->Environments()->filter('Name', $environmentName)->first();
}
/**
* @return string
*/
public function Link() {
return 'naut/api';
}
}