Skip to content

Commit

Permalink
CHE-2654: Changing Agent REST API (adding 'id' and 'description' fiel…
Browse files Browse the repository at this point in the history
…ds). Updating "Agents" section in the dashboard for using new API

Signed-off-by: Ilya Buziuk <[email protected]>
  • Loading branch information
ibuziuk committed Oct 25, 2016
1 parent 50af652 commit 8217320
Show file tree
Hide file tree
Showing 35 changed files with 200 additions and 405 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,42 +27,41 @@ export class ListAgentsController {

this.cheAgent.fetchAgents().then(() => {
this.buildAgentsList();
}, (error) => {
if (error.status === 304) {
this.buildAgentsList();
}
});
}

buildAgentsList() {
this.agentsList = [];
this.availableAgents = this.cheAgent.getAgents();
this.availableAgents.forEach(agent => {
let isEnabled = this.isEnabled(agent, this.agents);
this.agentsList.push({ "name": agent, "isEnabled": isEnabled });
this.allAgents = this.cheAgent.getAgents();

this.allAgents.forEach(agent => {
let agentItem = angular.copy(agent);
let isEnabled = this.isEnabled(agent.id, this.agents);
agentItem.isEnabled = isEnabled;
this.agentsList.push(agentItem);
});
}

updateAgent(agent) {
if (agent.isEnabled) {
this.agents.push(agent.name);
this.agents.push(agent.id);
} else {
this.agents.splice(this.agents.indexOf(agent.name), 1);
this.agents.splice(this.agents.indexOf(agent.id), 1);
}
return this.agentsOnChange().then(() => { this.buildAgentsList() });
this.agentsOnChange();
}

/**
* Switching of the "ws-agent" must happen only via "Dev" slider.
* "ws-agent" should be listed, but always disabled regardless of the state
* @param agentName {string}
* @param agentId {string}
*/
needToDisable(agentName) {
return (agentName === "org.eclipse.che.ws-agent");
needToDisable(agentId) {
return (agentId === "org.eclipse.che.ws-agent");
}

isEnabled(agentName, agents) {
return (-1 !== agents.indexOf(agentName));
isEnabled(agentId, agents) {
return (-1 !== agents.indexOf(agentId));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</che-list-header>

<che-list flex>
<che-list-item ng-repeat="agent in listAgentsController.agentsList">
<che-list-item ng-repeat="agent in listAgentsController.agentsList | orderBy: 'id'">
<div flex="100" layout="row" layout-align="start stretch" class="agent-item-row">
<div layout="row" layout-align="start center">
</div>
Expand All @@ -38,7 +38,7 @@
<div flex="20">
<div>
<md-switch ng-model="agent.isEnabled"
ng-disabled="listAgentsController.needToDisable(agent.name)"
ng-disabled="listAgentsController.needToDisable(agent.id)"
ng-change="listAgentsController.updateAgent(agent)"
aria-label="Agent">
<span ng-if="agent.isEnabled === true">ACTIVE</span>
Expand All @@ -47,7 +47,7 @@
</div>
</div>
<div flex="50" class="agent-description">
<span>No Description Available</span>
<span>{{agent.description}}</span>
</div>
</div>
</div>
Expand Down
25 changes: 18 additions & 7 deletions dashboard/src/components/api/che-agent.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,34 +21,45 @@ export class CheAgent {
* Default constructor that is using resource
* @ngInject for Dependency injection
*/
constructor ($resource) {
constructor($resource, $q) {

// keep resource
this.$resource = $resource;
this.$q = $q;

// agents
this.agents = [];

// remote call
this.remoteAgentAPI = this.$resource('/api/agent',{}, {
getAgents: {method: 'GET', url: '/api/agent', isArray: true
}});
this.remoteAgentAPI = this.$resource('/api/agent', {}, {
getAgents: { method: 'GET', url: '/api/agent', isArray: true }
});
}

/**
* Fetch the agents
*/
fetchAgents() {
var defer = this.$q.defer();
let promise = this.remoteAgentAPI.getAgents().$promise;
let updatedPromise = promise.then((agents) => {

promise.then((agents) => {
// reset global list
this.agents.length = 0;

agents.forEach((agent) => {
this.agents.push(agent);
});
defer.resolve();
}, (error) => {
if (error.status != 304) {
defer.reject(error);
} else {
defer.resolve();
}
});
return updatedPromise;

return defer.promise;
}

/**
Expand All @@ -58,5 +69,5 @@ export class CheAgent {
getAgents() {
return this.agents;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@
@DTO
public interface AgentDto extends Agent {

@Override
String getId();

void setId(String id);

AgentDto withId(String id);

@Override
String getName();

Expand All @@ -36,6 +43,13 @@ public interface AgentDto extends Agent {

AgentDto withVersion(String version);

@Override
String getDescription();

void setDescription(String description);

AgentDto withDescription(String description);

@Override
List<String> getDependencies();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
* @author Anatoliy Bazko
*/
public interface Agent {

/**
* Returns the id of the agent.
*/
String getId();

/**
* Returns the name of the agent.
Expand All @@ -30,6 +35,11 @@ public interface Agent {
*/
String getVersion();

/**
* Returns the description of the agent.
*/
String getDescription();

/**
* Returns the depending agents, that must be applied before.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@
import org.eclipse.che.commons.annotation.Nullable;

/**
* A pair of name and version of the agent.
* A pair of id and version of the agent.
* Version part is not mandatory.
*
* @author Anatolii Bazko
*/
public interface AgentKey {
/**
* @return the name of the agent
* @return the id of the agent
*/
String getName();
String getId();

/**
* @return the version of the agent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.eclipse.che.api.agent.shared.model.Agent;
import org.eclipse.che.api.agent.shared.model.AgentKey;

import java.util.Collection;
import java.util.List;

/**
Expand Down Expand Up @@ -43,23 +44,23 @@ public interface AgentRegistry {
/**
* Returns a list of the available versions of the specific agent.
*
* @param name
* the name of the agent
* @param id
* the id of the agent
* @return list of versions
* @throws AgentNotFoundException
* if agent not found in the registry
* @throws AgentException
* if unexpected error occurred
*/
List<String> getVersions(String name) throws AgentException;
List<String> getVersions(String id) throws AgentException;


/**
* Returns the list of available agents.
* Returns the collection of available agents.
*
* @return list of agents
* @return collection of agents
* @throws AgentException
* if unexpected error occurred
*/
List<String> getAgents() throws AgentException;
Collection<Agent> getAgents() throws AgentException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import java.util.Collection;
import java.util.List;

import static javax.ws.rs.core.MediaType.APPLICATION_JSON;
Expand All @@ -57,15 +59,15 @@ public AgentRegistryService(AgentRegistry agentRegistry) {
}

@GET
@Path("/name/{name}")
@Path("/id/{id}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Gets the latest version of the agent", response = AgentDto.class)
@ApiResponses({@ApiResponse(code = 200, message = "The response contains requested agent entity"),
@ApiResponse(code = 404, message = "Agent not found in the registry"),
@ApiResponse(code = 500, message = "Internal server error occurred")})
public Agent getByName(@ApiParam("The agent name") @PathParam("name") String name) throws ApiException {
public Agent getById(@ApiParam("The agent id") @PathParam("id") String id) throws ApiException {
try {
return asDto(agentRegistry.getAgent(new AgentKeyImpl(name)));
return asDto(agentRegistry.getAgent(new AgentKeyImpl(id)));
} catch (AgentNotFoundException e) {
throw new NotFoundException(e.getMessage());
} catch (AgentException e) {
Expand All @@ -74,16 +76,16 @@ public Agent getByName(@ApiParam("The agent name") @PathParam("name") String nam
}

@GET
@Path("/name/{name}/version/{version}")
@Path("/id/{id}/version/{version}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Gets the specific version of the agent", response = AgentDto.class)
@ApiResponses({@ApiResponse(code = 200, message = "The response contains requested agent entity"),
@ApiResponse(code = 404, message = "Agent not found in the registry"),
@ApiResponse(code = 500, message = "Internal server error occurred")})
public Agent getByName(@ApiParam("The agent name") @PathParam("name") String name,
public Agent getByName(@ApiParam("The agent id") @PathParam("id") String id,
@ApiParam("The agent version") @PathParam("version") String version) throws ApiException {
try {
return asDto(agentRegistry.getAgent(new AgentKeyImpl(name, version)));
return asDto(agentRegistry.getAgent(new AgentKeyImpl(id, version)));
} catch (AgentNotFoundException e) {
throw new NotFoundException(e.getMessage());
} catch (AgentException e) {
Expand All @@ -93,15 +95,15 @@ public Agent getByName(@ApiParam("The agent name") @PathParam("name") String nam
}

@GET
@Path("/versions/{name}")
@Path("/versions/{id}")
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get a list of available versions of the giving agent", response = List.class)
@ApiResponses({@ApiResponse(code = 200, message = "The response contains available versions of the giving agent"),
@ApiResponse(code = 404, message = "Agent not found"),
@ApiResponse(code = 500, message = "Internal server error occurred")})
public List<String> getVersions(@ApiParam("The agent name") @PathParam("name") String name) throws ApiException {
public List<String> getVersions(@ApiParam("The agent id") @PathParam("id") String id) throws ApiException {
try {
return agentRegistry.getVersions(name);
return agentRegistry.getVersions(id);
} catch (AgentNotFoundException e) {
throw new NotFoundException(e.getMessage());
} catch (AgentException e) {
Expand All @@ -111,10 +113,10 @@ public List<String> getVersions(@ApiParam("The agent name") @PathParam("name") S

@GET
@Produces(APPLICATION_JSON)
@ApiOperation(value = "Get a list of the available agents", response = List.class)
@ApiResponses({@ApiResponse(code = 200, message = "The response contains list of available agents"),
@ApiOperation(value = "Get a collection of the available agents", response = Collection.class)
@ApiResponses({@ApiResponse(code = 200, message = "The response contains collection of available agents"),
@ApiResponse(code = 500, message = "Internal server error occurred")})
public List<String> getAgents() throws ApiException {
public Collection<Agent> getAgents() throws ApiException {
try {
return agentRegistry.getAgents();
} catch (AgentNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ public interface AgentRegistryUrlProvider {
* Returns url to fetch available versions of the agent.
* @param name
* the agent name
* the agent id
* @return {@link URL}
* @throws AgentException
* if unexpected error occurred
*/
URL getAgentVersionsUrl(String name) throws AgentException;
URL getAgentVersionsUrl(String id) throws AgentException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
public class DtoConverter {

public static AgentDto asDto(Agent agent) {
return newDto(AgentDto.class).withName(agent.getName())
return newDto(AgentDto.class).withId(agent.getId())
.withName(agent.getName())
.withVersion(agent.getVersion())
.withDescription(agent.getDescription())
.withProperties(agent.getProperties())
.withScript(agent.getScript())
.withDependencies(agent.getDependencies());
Expand Down
Loading

0 comments on commit 8217320

Please sign in to comment.