Skip to content

Commit

Permalink
Merge pull request Netflix#64 from Netflix/modelsUseEnums
Browse files Browse the repository at this point in the history
Models use enums
  • Loading branch information
amitsharmaak committed May 29, 2014
2 parents de411c0 + 8676daa commit fa9b29c
Show file tree
Hide file tree
Showing 30 changed files with 1,340 additions and 683 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
/*
*
* Copyright 2013 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.genie.client;

import com.google.common.collect.Multimap;
import com.netflix.client.http.HttpRequest.Verb;
import com.netflix.genie.common.exceptions.CloudServiceException;
import com.netflix.genie.common.messages.ApplicationConfigRequest;
import com.netflix.genie.common.messages.ApplicationConfigResponse;
import com.netflix.genie.common.model.ApplicationConfig;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Singleton class, which acts as the client library for the Application
* Configuration Service.
*
* @author tgianos
*/
public final class ApplicationConfigServiceClient extends BaseGenieClient {

private static final Logger LOG = LoggerFactory
.getLogger(ApplicationConfigServiceClient.class);

private static final String BASE_CONFIG_APPLICATION_REST_URI
= BASE_REST_URI + "config/application";

// reference to the instance object
private static ApplicationConfigServiceClient instance;

/**
* Private constructor for singleton class.
*
* @throws IOException if there is any error during initialization
*/
private ApplicationConfigServiceClient() throws IOException {
super();
}

/**
* Returns the singleton instance that can be used by clients.
*
* @return ApplicationConfigServiceClient instance
* @throws IOException if there is an error instantiating client
*/
public static synchronized ApplicationConfigServiceClient getInstance() throws IOException {
if (instance == null) {
instance = new ApplicationConfigServiceClient();
}

return instance;
}

/**
* Create a new application configuration.
*
* @param config the object encapsulating the new application config to
* create
*
* @return extracted application config response
* @throws CloudServiceException
*/
public ApplicationConfig createApplicationConfig(final ApplicationConfig config)
throws CloudServiceException {
checkErrorConditions(config);

final ApplicationConfigRequest request = new ApplicationConfigRequest();
request.setApplicationConfig(config);

final ApplicationConfigResponse ccr = executeRequest(
Verb.POST,
BASE_CONFIG_APPLICATION_REST_URI,
null,
null,
request,
ApplicationConfigResponse.class);

if (ccr.getApplicationConfigs() == null || ccr.getApplicationConfigs().length == 0) {
String msg = "Unable to parse application config from response";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_INTERNAL_ERROR, msg);
}

// return the first (only) application config
return ccr.getApplicationConfigs()[0];
}

/**
* Create or update an application configuration.
*
* @param id the id for the application configuration to create or update
* @param config the object encapsulating the new application configuration
* to create
*
* @return extracted application configuration response
* @throws CloudServiceException
*/
public ApplicationConfig updateApplicationConfig(final String id, final ApplicationConfig config)
throws CloudServiceException {
if (StringUtils.isEmpty(id)) {
final String msg = "Required parameter id is missing. Unable to update.";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_BAD_REQUEST, msg);
}
//Check to make sure we have all the parameters we need for a valid
//application config
checkErrorConditions(config);

final ApplicationConfigRequest request = new ApplicationConfigRequest();
request.setApplicationConfig(config);

ApplicationConfigResponse ccr = executeRequest(
Verb.PUT,
BASE_CONFIG_APPLICATION_REST_URI,
id,
null,
request,
ApplicationConfigResponse.class);

if (ccr.getApplicationConfigs() == null || ccr.getApplicationConfigs().length == 0) {
String msg = "Unable to parse application config from response";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_INTERNAL_ERROR, msg);
}

// return the first (only) application config
return ccr.getApplicationConfigs()[0];
}

/**
* Gets information for a given id.
*
* @param id the application configuration id to get (can't be null or
* empty)
* @return the application configuration for this id
* @throws CloudServiceException
*/
public ApplicationConfig getApplicationConfig(final String id) throws CloudServiceException {
if (StringUtils.isEmpty(id)) {
final String msg = "Required parameter id is missing. Unable to get.";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_BAD_REQUEST, msg);
}

ApplicationConfigResponse ccr = executeRequest(
Verb.GET,
BASE_CONFIG_APPLICATION_REST_URI,
id,
null,
null,
ApplicationConfigResponse.class);

if (ccr.getApplicationConfigs() == null || ccr.getApplicationConfigs().length == 0) {
final String msg = "Unable to parse application config from response";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_INTERNAL_ERROR, msg);
}

// return the first (only) application config
return ccr.getApplicationConfigs()[0];
}

/**
* Gets a set of application configurations for the given parameters.
*
* @param params key/value pairs in a map object.<br>
*
* More details on the parameters can be found on the Genie User Guide on
* GitHub.
* @return List of application configuration elements that match the filter
* @throws CloudServiceException
*/
public List<ApplicationConfig> getApplicationConfigs(final Multimap<String, String> params)
throws CloudServiceException {
final ApplicationConfigResponse ccr = executeRequest(
Verb.GET,
BASE_CONFIG_APPLICATION_REST_URI,
null,
params,
null,
ApplicationConfigResponse.class);

// this will only happen if 200 is returned, and parsing fails for some
// reason
if (ccr.getApplicationConfigs() == null || ccr.getApplicationConfigs().length == 0) {
String msg = "Unable to parse application config from response";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_INTERNAL_ERROR, msg);
}

// if we get here, there are non-zero application config elements - return all
return Arrays.asList(ccr.getApplicationConfigs());
}

/**
* Delete an application configuration using its id.
*
* @param id the id for the application configuration to delete
* @return the deleted application configuration
* @throws CloudServiceException
*/
public ApplicationConfig deleteApplicationConfig(final String id) throws CloudServiceException {
if (StringUtils.isEmpty(id)) {
String msg = "Missing required parameter: id";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_BAD_REQUEST, msg);
}

ApplicationConfigResponse ccr = executeRequest(
Verb.DELETE,
BASE_CONFIG_APPLICATION_REST_URI,
id,
null,
null,
ApplicationConfigResponse.class);

if (ccr.getApplicationConfigs() == null || ccr.getApplicationConfigs().length == 0) {
String msg = "Unable to parse application config from response";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_INTERNAL_ERROR, msg);
}

// return the first (only) application config
return ccr.getApplicationConfigs()[0];
}

/**
* Check to make sure that the required parameters exist.
*
* @param config The configuration to check
* @throws CloudServiceException
*/
private void checkErrorConditions(final ApplicationConfig config) throws CloudServiceException {
if (config == null) {
final String msg = "Required parameter config can't be NULL";
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_BAD_REQUEST, msg);
}

final List<String> messages = new ArrayList<String>();
if (StringUtils.isEmpty(config.getUser())) {
messages.add("User name is missing and is required. Unable to create.\n");
}
if (StringUtils.isEmpty(config.getName())) {
messages.add("Application name is missing and is required. Unable to create.\n");
}
if (config.getStatus() == null) {
messages.add("No application status entered. Required to create\n");
}

if (!messages.isEmpty()) {
final StringBuilder builder = new StringBuilder();
builder.append("Cluster configuration errors:\n");
for (final String message : messages) {
builder.append(message);
}
final String msg = builder.toString();
LOG.error(msg);
throw new CloudServiceException(HttpURLConnection.HTTP_BAD_REQUEST, msg);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map.Entry;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -47,7 +49,7 @@
* @author skrishnan
* @author tgianos
*/
public abstract class BaseGenieClient {
public class BaseGenieClient {

private static final String CLOUD = "cloud";

Expand Down Expand Up @@ -85,7 +87,7 @@ protected BaseGenieClient() throws IOException {
*
* @param env "prod" or "test" or "dev"
*/
public static synchronized void initEureka(String env) {
public static synchronized void initEureka(final String env) {
if (env != null) {
System.setProperty("eureka.environment", env);
}
Expand Down Expand Up @@ -133,7 +135,7 @@ protected <T extends BaseResponse> T executeRequest(
RestClient genieClient = (RestClient) ClientFactory
.getNamedClient(NIWS_CLIENT_NAME_GENIE);
HttpRequest.Builder builder = HttpRequest.newBuilder()
.verb(verb).header("Accept", "application/json")
.verb(verb).header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON)
.uri(new URI(requestUri)).entity(request);
if (params != null) {
for (Entry<String, String> param : params.entries()) {
Expand Down Expand Up @@ -235,7 +237,7 @@ private <T extends BaseResponse> T extractEntityFromClientResponse(
}

/**
* Given a urlPath such as genie/v0/jobs and a uuid, constructs the request
* Given a urlPath such as genie/v1/jobs and a uuid, constructs the request
* uri.
*
* @param baseRestUri e.g. genie/v1/jobs
Expand Down
Loading

0 comments on commit fa9b29c

Please sign in to comment.