Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/main/java/com/openshift/internal/restclient/DefaultClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.slf4j.LoggerFactory;

import com.openshift.internal.restclient.http.HttpClientException;
import com.openshift.internal.restclient.http.UnauthorizedException;
import com.openshift.internal.restclient.http.UrlConnectionHttpClientBuilder;
import com.openshift.internal.restclient.model.Status;
import com.openshift.internal.restclient.model.properties.ResourcePropertiesRegistry;
Expand All @@ -38,13 +39,14 @@
import com.openshift.restclient.capability.CapabilityVisitor;
import com.openshift.restclient.capability.ICapability;
import com.openshift.restclient.http.IHttpClient;
import com.openshift.restclient.http.IHttpStatusCodes;
import com.openshift.restclient.model.IList;
import com.openshift.restclient.model.IResource;

/**
* @author Jeff Cantrill
*/
public class DefaultClient implements IClient{
public class DefaultClient implements IClient, IHttpStatusCodes{

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultClient.class);
private URL baseUrl;
Expand Down Expand Up @@ -343,6 +345,7 @@ public URL getBaseURL() {
return this.baseUrl;
}

@SuppressWarnings("deprecation")
@Override
public void setAuthorizationStrategy(IAuthorizationStrategy strategy) {
this.client.setAuthorizationStrategy(strategy);
Expand All @@ -351,9 +354,14 @@ public void setAuthorizationStrategy(IAuthorizationStrategy strategy) {
private OpenShiftException handleHttpClientException(String message, HttpClientException e) {
LOGGER.debug(message, e);
if (e.getMessage().startsWith("{")) {
return new OpenShiftException(message, e, factory.<Status>create(e.getMessage()));
Status status = factory.create(e.getMessage());
if(status.getCode() == STATUS_FORBIDDEN) {
//TODO replace with exception not based on HttpClient or refactor
throw new UnauthorizedException(status.getMessage(), e);
}
return new OpenShiftException(e, status, message);
} else {
return new OpenShiftException(message, e, null);
return new OpenShiftException(e, message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
* @author Jeff Cantrill
*/
public enum KubernetesAPIVersion implements APIModelVersion{
v1beta1(1);
v1beta1(1),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty convinced that using an enum for api version is not the right tool at hand. Enums typically are the right tool for closed exhaustive choice whereas in API versions we clearly have an open ended choice. I would clearly switch to a class and move away from enum.

v1beta3(2);

private int order;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public String getMessage(){
return asString(STATUS_MESSAGE);
}

@Override
public int getCode() {
return asInt(STATUS_CODE);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ public final class ResourcePropertiesRegistry implements ResourcePropertyKeys {
put(SERVICE_SELECTOR, new String [] {"selector"});
put(SERVICE_PORTALIP, new String [] {"portalIP"});
put(STATUS_MESSAGE, new String [] {"message"});
put(STATUS_CODE, new String [] {"code"});
}};

public static final Map<String, String []> V1BETA3_KUBERNETES_MAP = new HashMap<String, String []>(){{
put(ANNOTATIONS, new String [] {"metadata", "annotations"});
put(STATUS_MESSAGE, new String [] {"message"});
put(STATUS_CODE, new String [] {"code"});
}};

public static final Map<String, String []> V1BETA1_OPENSHIFT_MAP = new HashMap<String, String []>(){{
Expand Down Expand Up @@ -117,6 +124,9 @@ private ResourcePropertiesRegistry(){
versionPropertyMap.put(new VersionKey(OpenShiftAPIVersion.v1beta1, ResourceKind.Project), V1BETA1_OPENSHIFT_MAP);
versionPropertyMap.put(new VersionKey(OpenShiftAPIVersion.v1beta1, ResourceKind.Route), V1BETA1_OPENSHIFT_MAP);
versionPropertyMap.put(new VersionKey(OpenShiftAPIVersion.v1beta1, ResourceKind.Template), V1BETA1_OPENSHIFT_MAP);

//v1beta3
versionPropertyMap.put(new VersionKey(KubernetesAPIVersion.v1beta3, ResourceKind.Status), V1BETA3_KUBERNETES_MAP);
}

public static final ResourcePropertiesRegistry getInstance(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface ResourcePropertyKeys extends BuildConfigPropertyKeys{
static final String SERVICE_PORTALIP = "service.portalIP";

static final String STATUS_MESSAGE = "status.message";
static final String STATUS_CODE = "status.code";

static final String BUILD_STATUS = "build.status";
static final String BUILD_MESSAGE = "build.message";
Expand Down
8 changes: 1 addition & 7 deletions src/main/java/com/openshift/restclient/http/IHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* @deprecated This interface and its supporting classes to be deprecated in the near future.
*/
@Deprecated
public interface IHttpClient {
public interface IHttpClient extends IHttpStatusCodes{

public static final String HTTP = "http";

Expand All @@ -45,12 +45,6 @@ public interface IHttpClient {
public static final String AUTHORIZATION_BASIC = "Basic";
public static final String AUTHORIZATION_BEARER = "Bearer";

public static final int STATUS_OK = 200;
public static final int STATUS_INTERNAL_SERVER_ERROR = 500;
public static final int STATUS_BAD_REQUEST = 400;
public static final int STATUS_UNAUTHORIZED = 401;
public static final int STATUS_NOT_FOUND = 404;

public static final char SPACE = ' ';
public static final char COLON = ':';
public static final char COMMA = ',';
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/com/openshift/restclient/http/IHttpStatusCodes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright (c) 2015 Red Hat, Inc. Distributed under license by Red Hat, Inc.
* All rights reserved. This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Red Hat, Inc. - initial API and implementation
******************************************************************************/
package com.openshift.restclient.http;

/**
* HTTP Status codes
* @author jeff.cantrill
*
*/
public interface IHttpStatusCodes {

public static final int STATUS_OK = 200;
public static final int STATUS_INTERNAL_SERVER_ERROR = 500;
public static final int STATUS_BAD_REQUEST = 400;
public static final int STATUS_UNAUTHORIZED = 401;
public static final int STATUS_FORBIDDEN = 403;
public static final int STATUS_NOT_FOUND = 404;
}
6 changes: 6 additions & 0 deletions src/main/java/com/openshift/restclient/model/IStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ public interface IStatus extends IResource {
* @return
*/
String getMessage();

/**
* The HTTP status code
* @return an int
*/
int getCode();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*******************************************************************************
* Copyright (c) 2015 Red Hat, Inc. Distributed under license by Red Hat, Inc.
* All rights reserved. This program is made available under the terms of the
* Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Red Hat, Inc.
******************************************************************************/
package com.openshift.internal.restclient.model;

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import org.jboss.dmr.ModelNode;
import org.junit.BeforeClass;
import org.junit.Test;

import com.openshift.internal.restclient.model.properties.ResourcePropertiesRegistry;
import com.openshift.restclient.IClient;
import com.openshift.restclient.ResourceKind;
import com.openshift.restclient.model.IStatus;
import com.openshift.restclient.utils.Samples;

/**
* Test to validate the lookup paths are correct for the version
* @author Jeff Cantrill
*/
public class V1Beta3StatusTest{

private static IStatus status;

@BeforeClass
public static void setUp(){
IClient client = mock(IClient.class);
ModelNode node = ModelNode.fromJSONString(Samples.V1BETA3_Status.getContentAsString());
status = new Status(node, client, ResourcePropertiesRegistry.getInstance().get("v1beta3", ResourceKind.Status));
}

@Test
public void testGetMessage() {
assertEquals("\"/api/v1beta1/services/ruby-helloworld-sample\" is forbidden because foo cannot get on services with name \"ruby-helloworld-sample\" in default", status.getMessage());
}

@Test
public void testGetCode() {
assertEquals(403, status.getCode());
}
}
4 changes: 3 additions & 1 deletion src/test/java/com/openshift/restclient/utils/Samples.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ public enum Samples {
V1BETA1_POD("openshift3/v1beta1_pod.json"),
V1BETA1_REPLICATION_CONTROLLER("openshift3/v1beta1_replication_controller.json"),
V1BETA1_SERVICE("openshift3/v1beta1_service.json"),
V1BETA1_TEMPLATE("openshift3/v1beta1_template.json");
V1BETA1_TEMPLATE("openshift3/v1beta1_template.json"),

V1BETA3_Status("openshift3/v1beta3_status.json");

private static final String SAMPLES_FOLDER = "/samples/";

Expand Down
10 changes: 10 additions & 0 deletions src/test/resources/samples/openshift3/v1beta3_status.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"kind": "Status",
"apiVersion": "v1beta3",
"metadata": {},
"status": "Failure",
"message": "\"/api/v1beta1/services/ruby-helloworld-sample\" is forbidden because foo cannot get on services with name \"ruby-helloworld-sample\" in default",
"reason": "Forbidden",
"details": {},
"code": 403
}