Skip to content

Commit 09c864e

Browse files
Added Agents Api V4
* Added build details to be represented.
1 parent 0af23a0 commit 09c864e

File tree

22 files changed

+1652
-2
lines changed

22 files changed

+1652
-2
lines changed

common/src/com/thoughtworks/go/server/service/AgentBuildingInfo.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,35 @@ public int hashCode() {
7171
return result;
7272
}
7373

74+
public String getPipelineName() {
75+
if(isBuilding()) {
76+
return buildLocator.split("/")[0];
77+
}
78+
return null;
79+
}
80+
81+
public String getJobName() {
82+
if (isBuilding()) {
83+
try {
84+
return buildLocator.split("/")[4];
85+
} catch (ArrayIndexOutOfBoundsException e) {
86+
return null;
87+
}
88+
}
89+
return null;
90+
}
91+
92+
public String getStageName() {
93+
if(isBuilding()) {
94+
try {
95+
return buildLocator.split("/")[2];
96+
} catch (ArrayIndexOutOfBoundsException e) {
97+
return null;
98+
}
99+
}
100+
return null;
101+
}
102+
74103
public boolean isBuilding() {
75104
return !buildingInfo.equals("");
76105
}

common/test/unit/com/thoughtworks/go/helper/AgentInstanceMother.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ public static AgentInstance lostContact(String buildLocator) {
224224
AgentRuntimeInfo newRuntimeInfo = AgentRuntimeInfo.initialState(agentConfig);
225225
newRuntimeInfo.setStatus(AgentStatus.LostContact);
226226
newRuntimeInfo.setUsableSpace(1000L);
227-
newRuntimeInfo.setBuildingInfo(new AgentBuildingInfo("", buildLocator));
227+
newRuntimeInfo.setBuildingInfo(new AgentBuildingInfo("buildInfo", buildLocator));
228228
instance.update(newRuntimeInfo);
229229
return instance;
230230
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2016 ThoughtWorks, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.thoughtworks.go.server.service;
18+
19+
import org.junit.Test;
20+
21+
import static org.hamcrest.core.Is.is;
22+
import static org.junit.Assert.*;
23+
24+
public class AgentBuildingInfoTest {
25+
@Test
26+
public void shouldReturnThePipelineName() {
27+
AgentBuildingInfo agentBuildingInfo = new AgentBuildingInfo("buildInfo", "foo");
28+
assertThat(agentBuildingInfo.getPipelineName(), is("foo"));
29+
}
30+
31+
@Test
32+
public void shouldReturnTheStageName() {
33+
AgentBuildingInfo agentBuildingInfo = new AgentBuildingInfo("buildInfo", "foo/1/bar");
34+
assertThat(agentBuildingInfo.getStageName(), is("bar"));
35+
}
36+
37+
@Test
38+
public void shouldReturnTheJobName() {
39+
AgentBuildingInfo agentBuildingInfo = new AgentBuildingInfo("buildInfo", "foo/1/bar/3/job");
40+
assertThat(agentBuildingInfo.getJobName(), is("job"));
41+
}
42+
43+
@Test
44+
public void shouldReturnNullTheJobName() {
45+
AgentBuildingInfo agentBuildingInfo = new AgentBuildingInfo("buildInfo", "foo");
46+
assertNull(agentBuildingInfo.getJobName());
47+
}
48+
49+
@Test
50+
public void shouldReturnNullForStageName() {
51+
AgentBuildingInfo agentBuildingInfo = new AgentBuildingInfo("buildInfo", "foo");
52+
assertNull(agentBuildingInfo.getStageName());
53+
}
54+
55+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
##########################################################################
2+
# Copyright 2016 ThoughtWorks, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
##########################################################################
16+
17+
module ApiV4
18+
class AgentsController < ApiV4::BaseController
19+
20+
before_action :set_cache_control
21+
before_action :check_user_and_404
22+
before_action :check_admin_user_and_401, except: [:index, :show]
23+
before_action :set_default_values_if_not_present, only: [:bulk_update]
24+
before_action :load_agent, only: [:show, :edit, :update, :destroy, :enable, :disable]
25+
26+
def index
27+
presenters = ApiV4::AgentsRepresenter.new(agent_service.agentEnvironmentMap, security_service, current_user)
28+
response_hash = presenters.to_hash(url_builder: self)
29+
30+
if stale?(etag: Digest::MD5.hexdigest(JSON.generate(response_hash)))
31+
render DEFAULT_FORMAT => response_hash
32+
end
33+
end
34+
35+
def show
36+
render DEFAULT_FORMAT => agent_presenter.to_hash(url_builder: self)
37+
end
38+
39+
def update
40+
result = HttpOperationResult.new
41+
42+
@agent_instance = agent_service.updateAgentAttributes(current_user, result, params[:uuid], params[:hostname], maybe_join(params[:resources]), maybe_join(params[:environments]), to_enabled_tristate)
43+
44+
if result.isSuccess
45+
load_agent
46+
render DEFAULT_FORMAT => agent_presenter.to_hash(url_builder: self)
47+
else
48+
json = agent_presenter.to_hash(url_builder: self)
49+
render_http_operation_result(result, { data: json })
50+
end
51+
end
52+
53+
def destroy
54+
result = HttpOperationResult.new
55+
agent_service.deleteAgents(current_user, result, [params[:uuid]])
56+
render_http_operation_result(result)
57+
end
58+
59+
def bulk_update
60+
result = HttpLocalizedOperationResult.new
61+
uuids = params[:uuids]
62+
resources_to_add = params[:operations][:resources][:add]
63+
resources_to_remove = params[:operations][:resources][:remove]
64+
environments_to_add = params[:operations][:environments][:add]
65+
environment_to_remove = params[:operations][:environments][:remove]
66+
agent_service.bulkUpdateAgentAttributes(current_user, result, uuids, resources_to_add, resources_to_remove, environments_to_add, environment_to_remove, to_enabled_tristate)
67+
render_http_operation_result(result)
68+
end
69+
70+
def bulk_destroy
71+
result = HttpOperationResult.new
72+
agent_service.deleteAgents(current_user, result, Array.wrap(params[:uuids]))
73+
render_http_operation_result(result)
74+
end
75+
76+
private
77+
78+
def set_default_values_if_not_present
79+
params[:uuids] = params[:uuids] || []
80+
params[:operations] = params[:operations] || {}
81+
params[:operations][:resources] = params[:operations][:resources] || {}
82+
params[:operations][:environments] = params[:operations][:environments] || {}
83+
params[:operations][:resources][:add] = params[:operations][:resources][:add] || []
84+
params[:operations][:resources][:remove] = params[:operations][:resources][:remove] || []
85+
params[:operations][:environments][:add] = params[:operations][:environments][:add] || []
86+
params[:operations][:environments][:remove] = params[:operations][:environments][:remove] || []
87+
end
88+
89+
def maybe_join(obj)
90+
if obj.is_a?(Array)
91+
obj.join(',')
92+
else
93+
obj
94+
end
95+
end
96+
97+
def set_cache_control
98+
response.headers['Cache-Control'] = 'private, must-revalidate'
99+
end
100+
101+
private
102+
103+
attr_reader :agent_instance
104+
105+
def to_enabled_tristate
106+
enabled = params[:agent_config_state]
107+
if enabled.blank?
108+
TriState.UNSET
109+
elsif enabled =~ /\Aenabled\Z/i
110+
TriState.TRUE
111+
elsif enabled =~ /\Adisabled\Z/i
112+
TriState.FALSE
113+
else
114+
raise BadRequest.new('The value of `agent_config_state` can be one of `Enabled`, `Disabled` or null.')
115+
end
116+
end
117+
118+
def load_agent
119+
@agent_instance = agent_service.findAgent(params[:uuid])
120+
raise RecordNotFound if @agent_instance.nil? || @agent_instance.isNullAgent()
121+
end
122+
123+
def agent_presenter
124+
ApiV4::AgentRepresenter.new({agent: @agent_instance, environments: environment_config_service.environmentsFor(@agent_instance.getUuid()), security_service: security_service, current_user: current_user})
125+
end
126+
127+
end
128+
129+
end
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
##########################GO-LICENSE-START################################
2+
# Copyright 2015 ThoughtWorks, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
##########################GO-LICENSE-END##################################
16+
17+
module ApiV4
18+
class BaseController < ::ApplicationController
19+
20+
class BadRequest < StandardError
21+
end
22+
23+
include AuthenticationHelper
24+
25+
FORMATS = [:json_hal_v4]
26+
DEFAULT_FORMAT = FORMATS.last
27+
DEFAULT_ACCEPTS_HEADER = Mime[DEFAULT_FORMAT].to_s
28+
29+
skip_before_filter :verify_authenticity_token
30+
before_filter :verify_content_type_on_post
31+
before_filter :set_default_response_format
32+
33+
def set_default_response_format
34+
request.format = DEFAULT_FORMAT unless params[:format]
35+
end
36+
37+
def redirect_json_hal(url, status=302)
38+
json = {
39+
_links: {
40+
redirect: {
41+
href: url
42+
}
43+
}
44+
}
45+
46+
response.status = status
47+
render DEFAULT_FORMAT => json, location: url
48+
end
49+
50+
rescue_from RecordNotFound, with: :render_not_found_error
51+
rescue_from BadRequest, with: :render_bad_request
52+
rescue_from UnprocessableEntity, with: :render_unprocessable_entity_error
53+
54+
class << self
55+
def default_accepts_header
56+
DEFAULT_ACCEPTS_HEADER
57+
end
58+
end
59+
60+
protected
61+
62+
def to_tristate(var)
63+
TriState.from(var.to_s)
64+
rescue => e
65+
raise BadRequest.new(e.message)
66+
end
67+
68+
def render_http_operation_result(result, data = {})
69+
status = result.httpCode()
70+
if result.instance_of?(HttpOperationResult)
71+
render_message(result.detailedMessage(), status, data)
72+
else
73+
render_message(result.message(Spring.bean('localizer')), status, data)
74+
end
75+
end
76+
77+
def render_message(message, status = :ok, data = {})
78+
render DEFAULT_FORMAT => { message: message.strip }.merge(data), status: status
79+
end
80+
81+
def render_unprocessable_entity_error(exception)
82+
render_message("Your request could not be processed. #{exception.message}", :unprocessable_entity)
83+
end
84+
end
85+
end
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
##########################################################################
2+
# Copyright 2016 ThoughtWorks, Inc.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
##########################################################################
16+
17+
module ApiV4
18+
class ErrorsController < BaseController
19+
20+
def not_found
21+
render DEFAULT_FORMAT => { :message => 'The resource you requested was not found!' }, :status => 404
22+
end
23+
24+
end
25+
end

0 commit comments

Comments
 (0)