Search in sources :

Example 1 with UnhandledRequestTypeException

use of com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException in project go-plugins by gocd.

the class Plugin1 method handle.

@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {
    if ("configuration".equals(goPluginApiRequest.requestName())) {
        HashMap<String, Object> config = new HashMap<>();
        HashMap<String, Object> url = new HashMap<>();
        url.put("display-order", "0");
        url.put("display-name", "Url");
        url.put("required", true);
        config.put("Url", url);
        return DefaultGoPluginApiResponse.success(new GsonBuilder().create().toJson(config));
    } else if ("view".equals(goPluginApiRequest.requestName())) {
        return getViewRequest();
    }
    throw new UnhandledRequestTypeException(goPluginApiRequest.requestName());
}
Also used : UnhandledRequestTypeException(com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder)

Example 2 with UnhandledRequestTypeException

use of com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException in project go-plugins by gocd.

the class Plugin2 method handle.

@Override
public GoPluginApiResponse handle(GoPluginApiRequest goPluginApiRequest) throws UnhandledRequestTypeException {
    if ("configuration".equals(goPluginApiRequest.requestName())) {
        HashMap<String, Object> config = new HashMap<>();
        HashMap<String, Object> url = new HashMap<>();
        url.put("display-order", "0");
        url.put("display-name", "Url");
        url.put("required", true);
        config.put("Url", url);
        return DefaultGoPluginApiResponse.success(new GsonBuilder().create().toJson(config));
    } else if ("view".equals(goPluginApiRequest.requestName())) {
        return getViewRequest();
    }
    throw new UnhandledRequestTypeException(goPluginApiRequest.requestName());
}
Also used : UnhandledRequestTypeException(com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException) HashMap(java.util.HashMap) GsonBuilder(com.google.gson.GsonBuilder)

Example 3 with UnhandledRequestTypeException

use of com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException in project go-plugins by gocd.

the class DummyArtifactPlugin method handle.

@Override
public GoPluginApiResponse handle(GoPluginApiRequest request) throws UnhandledRequestTypeException {
    final RequestFromServer requestFromServer = RequestFromServer.from(request.requestName());
    try {
        switch(requestFromServer) {
            case REQUEST_GET_CAPABILITIES:
                return DefaultGoPluginApiResponse.success("{}");
            case REQUEST_STORE_CONFIG_METADATA:
                return DefaultGoPluginApiResponse.success(artifactStoreMetadata());
            case REQUEST_STORE_CONFIG_VIEW:
                return DefaultGoPluginApiResponse.success(new View("/artifact-store.template.html").toJSON());
            case REQUEST_STORE_CONFIG_VALIDATE:
                return DefaultGoPluginApiResponse.success(ArtifactStoreConfig.from(request.requestBody()).validate().toJSON());
            case REQUEST_PUBLISH_ARTIFACT_METADATA:
                return DefaultGoPluginApiResponse.success(ArtifactConfig.artifactConfigMetadata());
            case REQUEST_PUBLISH_ARTIFACT_VIEW:
                return DefaultGoPluginApiResponse.success(new View("/publish-artifact.template.html").toJSON());
            case REQUEST_PUBLISH_ARTIFACT_VALIDATE:
                return DefaultGoPluginApiResponse.success(ArtifactConfig.from(request.requestBody()).validate().toJSON());
            case REQUEST_FETCH_ARTIFACT_METADATA:
                return DefaultGoPluginApiResponse.success(FetchArtifact.metadata());
            case REQUEST_FETCH_ARTIFACT_VIEW:
                return DefaultGoPluginApiResponse.success(new View("/fetch-artifact.template.html").toJSON());
            case REQUEST_FETCH_ARTIFACT_VALIDATE:
                return DefaultGoPluginApiResponse.success(FetchArtifact.from(request.requestBody()).validate().toJSON());
            case REQUEST_PUBLISH_ARTIFACT:
                return publishArtifact(PublishArtifactRequest.fromJSON(request.requestBody()));
            case REQUEST_FETCH_ARTIFACT:
                return fetchArtifact(FetchArtifactRequest.fromJSON(request.requestBody()));
            case REQUEST_GET_PLUGIN_ICON:
                JsonObject jsonObject = new JsonObject();
                jsonObject.addProperty("content_type", "image/jpg");
                jsonObject.addProperty("data", Base64.getEncoder().encodeToString(ResourceReader.readBytes("/icon.jpg")));
                return DefaultGoPluginApiResponse.success(jsonObject.toString());
            default:
                throw new RuntimeException("Error while executing request" + request.requestName());
        }
    } catch (Exception e) {
        LOG.error("Error while executing request " + request.requestName(), e);
        throw new RuntimeException(e);
    }
}
Also used : JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) UnhandledRequestTypeException(com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException)

Example 4 with UnhandledRequestTypeException

use of com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException in project gocd-ecs-elastic-agent by gocd.

the class ECSElasticPlugin method handle.

@Override
public GoPluginApiResponse handle(GoPluginApiRequest request) {
    ClusterProfileProperties clusterProfileProperties;
    try {
        fetchServerIdFromServer();
        LOG.debug("Request from server: " + request.requestName());
        switch(Request.fromString(request.requestName())) {
            case PLUGIN_SETTINGS_GET_ICON:
                return new GetPluginSettingsIconExecutor().execute();
            case REQUEST_GET_ELASTIC_AGENT_PROFILE_METADATA:
                return new GetProfileMetadataExecutor().execute();
            case REQUEST_GET_ELASTIC_AGENT_PROFILE_VIEW:
                return new GetProfileViewExecutor().execute();
            case REQUEST_VALIDATE_ELASTIC_AGENT_PROFILE:
                return ProfileValidateRequest.fromJSON(request.requestBody()).executor().execute();
            case REQUEST_GET_CLUSTER_PROFILE_METADATA:
                return new GetClusterProfileMetadataExecutor().execute();
            case REQUEST_GET_CLUSTER_PROFILE_VIEW:
                return new GetClusterProfileViewRequestExecutor().execute();
            case REQUEST_VALIDATE_CLUSTER_PROFILE_CONFIGURATION:
                return ValidateClusterProfileRequest.fromJSON(request.requestBody()).executor().execute();
            case REQUEST_CREATE_AGENT:
                CreateAgentRequest createAgentRequest = CreateAgentRequest.fromJSON(request.requestBody());
                clusterProfileProperties = createAgentRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return createAgentRequest.executor(getAgentInstancesFor(clusterProfileProperties), pluginRequest, getEventStreamFor(clusterProfileProperties)).execute();
            case REQUEST_SHOULD_ASSIGN_WORK:
                ShouldAssignWorkRequest shouldAssignWorkRequest = ShouldAssignWorkRequest.fromJSON(request.requestBody());
                clusterProfileProperties = shouldAssignWorkRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return shouldAssignWorkRequest.executor(getAgentInstancesFor(clusterProfileProperties)).execute();
            case REQUEST_JOB_COMPLETION:
                JobCompletionRequest jobCompletionRequest = JobCompletionRequest.fromJSON(request.requestBody());
                clusterProfileProperties = jobCompletionRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return jobCompletionRequest.executor(getAgentInstancesFor(clusterProfileProperties), pluginRequest).execute();
            case REQUEST_SERVER_PING:
                ServerPingRequest serverPingRequest = ServerPingRequest.fromJSON(request.requestBody());
                List<ClusterProfileProperties> listOfClusterProfileProperties = serverPingRequest.allClusterProfileProperties();
                refreshInstancesForAllClusters(listOfClusterProfileProperties);
                return serverPingRequest.executor(clusterSpecificAgentInstances, pluginRequest).execute();
            case REQUEST_CLUSTER_STATUS_REPORT:
                ClusterStatusReportRequest clusterStatusReportRequest = ClusterStatusReportRequest.fromJSON(request.requestBody());
                clusterProfileProperties = clusterStatusReportRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return clusterStatusReportRequest.executor(getAgentInstancesFor(clusterProfileProperties), getEventStreamFor(clusterProfileProperties)).execute();
            case REQUEST_AGENT_STATUS_REPORT:
                AgentStatusReportRequest statusReportRequest = AgentStatusReportRequest.fromJSON(request.requestBody());
                clusterProfileProperties = statusReportRequest.clusterProfileProperties();
                refreshInstancesForCluster(clusterProfileProperties);
                return statusReportRequest.executor(getAgentInstancesFor(clusterProfileProperties)).execute();
            case REQUEST_GET_CAPABILITIES:
                return new GetCapabilitiesExecutor().execute();
            case REQUEST_CLUSTER_PROFILE_CHANGED:
                return new DefaultGoPluginApiResponse(200);
            case REQUEST_MIGRATE_CONFIGURATION:
                return MigrateConfigurationRequest.fromJSON(request.requestBody()).executor().execute();
            default:
                throw new UnhandledRequestTypeException(request.requestName());
        }
    } catch (AmazonEC2Exception e) {
        LOG.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
        return DefaultGoPluginApiResponse.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
    } catch (Exception e) {
        LOG.error("Failed to handle request " + request.requestName() + " due to:", e);
        return DefaultGoPluginApiResponse.error("Failed to handle request " + request.requestName() + " due to:" + e.getMessage());
    }
}
Also used : ClusterProfileProperties(com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties) DefaultGoPluginApiResponse(com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception) UnhandledRequestTypeException(com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException) UnhandledRequestTypeException(com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException) AmazonEC2Exception(com.amazonaws.services.ec2.model.AmazonEC2Exception)

Aggregations

UnhandledRequestTypeException (com.thoughtworks.go.plugin.api.exceptions.UnhandledRequestTypeException)4 GsonBuilder (com.google.gson.GsonBuilder)2 HashMap (java.util.HashMap)2 AmazonEC2Exception (com.amazonaws.services.ec2.model.AmazonEC2Exception)1 JsonObject (com.google.gson.JsonObject)1 DefaultGoPluginApiResponse (com.thoughtworks.go.plugin.api.response.DefaultGoPluginApiResponse)1 ClusterProfileProperties (com.thoughtworks.gocd.elasticagent.ecs.domain.ClusterProfileProperties)1 IOException (java.io.IOException)1