Skip to content

Commit

Permalink
Merge pull request #246 from ostumpf/ost-warehouse
Browse files Browse the repository at this point in the history
FEATURE: add JDBC connection URL to Warehouse instance

close #247
  • Loading branch information
martiner committed Nov 24, 2015
2 parents fa22043 + 0f4a1ac commit 071e53b
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/gooddata/GoodData.java
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ protected GoodData(String hostname, String login, String password, int port, Str
datasetService = new DatasetService(getRestTemplate(), dataStoreService);
reportService = new ReportService(getRestTemplate());
processService = new ProcessService(getRestTemplate(), accountService, dataStoreService);
warehouseService = new WarehouseService(getRestTemplate(), hostname, port);
warehouseService = new WarehouseService(getRestTemplate());
connectorService = new ConnectorService(getRestTemplate(), projectService);
}

Expand Down
42 changes: 19 additions & 23 deletions src/main/java/com/gooddata/warehouse/Warehouse.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public class Warehouse {
public static final String URI = Warehouses.URI + "/{" + ID_PARAM + "}";

public static final UriTemplate TEMPLATE = new UriTemplate(URI);
public static final UriTemplate JDBC_CONNECTION_TEMPLATE = new UriTemplate("jdbc:gdc:datawarehouse://{host}:{port}/gdc/datawarehouse/instances/{id}");

private static final String SELF_LINK = "self";
private static final String STATUS_ENABLED = "ENABLED";
Expand All @@ -48,9 +47,7 @@ public class Warehouse {
private String status;
private String environment;
private Map<String, String> links;

private String warehouseHost;
private int warehousePort = 443;
private String connectionUrl;

public Warehouse(String title, String authToken) {
this(title, authToken, null);
Expand All @@ -64,19 +61,21 @@ public Warehouse(String title, String authToken, String description) {

@JsonCreator
public Warehouse(@JsonProperty("title") String title, @JsonProperty("authorizationToken") String authToken,
@JsonProperty("description") String description,
@JsonProperty("created") @JsonDeserialize(using = ISODateTimeDeserializer.class) DateTime created,
@JsonProperty("updated") @JsonDeserialize(using = ISODateTimeDeserializer.class) DateTime updated,
@JsonProperty("createdBy") String createdBy, @JsonProperty("updatedBy") String updatedBy,
@JsonProperty("status") String status, @JsonProperty("environment") String environment,
@JsonProperty("links") Map<String, String> links) {
@JsonProperty("description") String description,
@JsonProperty("created") @JsonDeserialize(using = ISODateTimeDeserializer.class) DateTime created,
@JsonProperty("updated") @JsonDeserialize(using = ISODateTimeDeserializer.class) DateTime updated,
@JsonProperty("createdBy") String createdBy, @JsonProperty("updatedBy") String updatedBy,
@JsonProperty("status") String status, @JsonProperty("environment") String environment,
@JsonProperty("connectionUrl") String connectionUrl,
@JsonProperty("links") Map<String, String> links) {
this(title, authToken, description);
this.created = created;
this.updated = updated;
this.createdBy = createdBy;
this.updatedBy = updatedBy;
this.status = status;
this.environment = environment;
this.connectionUrl = connectionUrl;
this.links = links;
}

Expand All @@ -92,6 +91,13 @@ public String getDescription() {
return description;
}

/**
* Gets the JDBC connection string.
*
* @return JDBC connection string
*/
public String getConnectionUrl() { return connectionUrl; }

public void setTitle(String title) {
this.title = title;
}
Expand Down Expand Up @@ -122,14 +128,6 @@ public String getStatus() {
return status;
}

void setWarehouseHost(String warehouseHost) {
this.warehouseHost = warehouseHost;
}

void setWarehousePort(int warehousePort) {
this.warehousePort = warehousePort;
}

public String getEnvironment() {
return environment;
}
Expand All @@ -150,15 +148,13 @@ public Map<String, String> getLinks() {

/**
* Get jdbc connection string. Works only on Warehouse loaded from API (using WarehouseService).
* @deprecated Use {@link #getConnectionUrl()} instead.
* @return jdbc connection string
*/
@JsonIgnore
@Deprecated
public String getJdbcConnectionString() {
if (warehouseHost == null) {
throw new IllegalStateException("Please set warehouseHost " +
"to be able to construct jdbc connection string");
}
return JDBC_CONNECTION_TEMPLATE.expand(warehouseHost, warehousePort, getId()).toString();
return getConnectionUrl();
}

@JsonIgnore
Expand Down
31 changes: 5 additions & 26 deletions src/main/java/com/gooddata/warehouse/WarehouseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.gooddata.collections.PageableList;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
Expand All @@ -25,21 +26,14 @@
*/
public class WarehouseService extends AbstractService {

private final String warehouseHost;
private final int warehousePort;

/**
* Sets RESTful HTTP Spring template. Should be called from constructor of concrete service extending
* this abstract one.
*
* @param restTemplate RESTful HTTP Spring template
* @param warehouseHost host to connect warehouses
* @param warehousePort port to connect warehouses
*/
public WarehouseService(RestTemplate restTemplate, String warehouseHost, int warehousePort) {
public WarehouseService(RestTemplate restTemplate) {
super(restTemplate);
this.warehouseHost = notNull(warehouseHost, "warehouseHost");
this.warehousePort = warehousePort;
}

/**
Expand Down Expand Up @@ -80,7 +74,7 @@ protected void onFinish() {
public void handlePollResult(WarehouseTask pollResult) {
try {
final Warehouse warehouse = restTemplate.getForObject(pollResult.getWarehouseLink(), Warehouse.class);
setResult(setWarehouseConnection(warehouse));
setResult(warehouse);
} catch (GoodDataException | RestClientException e) {
throw new GoodDataException("Warehouse creation finished, but can't get created warehouse, uri: "
+ pollResult.getWarehouseLink(), e);
Expand Down Expand Up @@ -116,7 +110,7 @@ public void removeWarehouse(Warehouse warehouse) {
public Warehouse getWarehouseByUri(String uri) {
notEmpty(uri, "uri");
try {
return setWarehouseConnection(restTemplate.getForObject(uri, Warehouse.class));
return restTemplate.getForObject(uri, Warehouse.class);
} catch (GoodDataRestException e) {
if (HttpStatus.NOT_FOUND.value() == e.getStatusCode()) {
throw new WarehouseNotFoundException(uri, e);
Expand Down Expand Up @@ -171,7 +165,7 @@ private PageableList<Warehouse> listWarehouses(final URI uri) {
if (result == null) {
return new PageableList<>();
}
return setWarehouseConnection(result);
return result;
} catch (GoodDataException | RestClientException e) {
throw new GoodDataException("Unable to list Warehouses", e);
}
Expand Down Expand Up @@ -257,19 +251,4 @@ public Warehouse updateWarehouse(Warehouse toUpdate) {

return getWarehouseByUri(toUpdate.getUri());
}

private Warehouse setWarehouseConnection(Warehouse warehouse) {
notNull(warehouse, "warehouse");
warehouse.setWarehouseHost(warehouseHost);
warehouse.setWarehousePort(warehousePort);
return warehouse;
}

private PageableList<Warehouse> setWarehouseConnection(PageableList<Warehouse> warehouses) {
notNull(warehouses, "warehouses");
for (Warehouse warehouse : warehouses) {
setWarehouseConnection(warehouse);
}
return warehouses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void createWarehouse() throws Exception {
final Warehouse wh = new Warehouse(title, warehouseToken);
wh.setEnvironment(Environment.TESTING);
warehouse = service.createWarehouse(wh).get();
String jdbc = warehouse.getJdbcConnectionString();
String jdbc = warehouse.getConnectionUrl();
}

@Test(groups = "warehouse", dependsOnMethods = "createWarehouse")
Expand Down
7 changes: 6 additions & 1 deletion src/test/java/com/gooddata/warehouse/WarehouseServiceIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class WarehouseServiceIT extends AbstractGoodDataIT {
private static final String WAREHOUSE_URI = Warehouse.TEMPLATE.expand(WAREHOUSE_ID).toString();
private static final String WAREHOUSE_USER_URI = WarehouseUsers.TEMPLATE.expand(WAREHOUSE_ID).toString();

private static final String CONNECTION_URL = "CONNECTION_URL";

private WarehouseTask pollingTask;
private WarehouseTask finishedTask;
private Warehouse warehouse;
Expand Down Expand Up @@ -71,7 +73,7 @@ public void shouldCreateWarehouse() throws Exception {
final Warehouse created = gd.getWarehouseService().createWarehouse(new Warehouse(TITLE, "{Token}", "Storage")).get();
assertThat(created, notNullValue());
assertThat(created.getTitle(), is(TITLE));
assertThat(created.getJdbcConnectionString(), is("jdbc:gdc:datawarehouse://localhost:" + port() + WAREHOUSE_URI));
assertThat(created.getConnectionUrl(), is(CONNECTION_URL));
}

@Test(expectedExceptions = GoodDataException.class)
Expand Down Expand Up @@ -103,6 +105,8 @@ public void shouldListWarehouses() throws Exception {
final PageableList<Warehouse> list = gd.getWarehouseService().listWarehouses();
assertThat(list, notNullValue());
assertThat(list, hasSize(2));
assertThat(list.get(0).getConnectionUrl(), notNullValue());
assertThat(list.get(1).getConnectionUrl(), notNullValue());
}

@Test
Expand All @@ -128,6 +132,7 @@ public void shouldGetWarehouse() throws Exception {
final Warehouse warehouse = gd.getWarehouseService().getWarehouseById(WAREHOUSE_ID);
assertThat(warehouse, notNullValue());
assertThat(warehouse.getTitle(), is(TITLE));
assertThat(warehouse.getConnectionUrl(), notNullValue());
}

@Test
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/com/gooddata/warehouse/WarehouseTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class WarehouseTest {
public static final String CREATED_BY = "/gdc/account/profile/createdBy";
public static final String UPDATED_BY = "/gdc/account/profile/updatedBy";
public static final String STATUS = "ENABLED";
public static final String CONNECTION_URL = "CONNECTION_URL";
public static final Map<String, String> LINKS = new LinkedHashMap<String, String>() {{
put("self", "/gdc/datawarehouse/instances/instanceId");
put("parent", "/gdc/datawarehouse/instances");
Expand All @@ -42,13 +43,13 @@ public void testSerializationForInstanceCreation() throws Exception {

@Test
public void testSerialization() throws Exception {
final Warehouse warehouse = new Warehouse(TITLE, TOKEN, DESCRIPTION, CREATED, UPDATED, CREATED_BY, UPDATED_BY, STATUS, ENVIRONMENT, LINKS);
final Warehouse warehouse = new Warehouse(TITLE, TOKEN, DESCRIPTION, CREATED, UPDATED, CREATED_BY, UPDATED_BY, STATUS, ENVIRONMENT, CONNECTION_URL, LINKS);
assertThat(warehouse, serializesToJson("/warehouse/warehouse.json"));
}

@Test
public void testSerializationWithNullToken() throws Exception {
final Warehouse warehouse = new Warehouse(TITLE, null, DESCRIPTION, CREATED, UPDATED, CREATED_BY, UPDATED_BY, STATUS, ENVIRONMENT, LINKS);
final Warehouse warehouse = new Warehouse(TITLE, null, DESCRIPTION, CREATED, UPDATED, CREATED_BY, UPDATED_BY, STATUS, ENVIRONMENT, CONNECTION_URL, LINKS);
assertThat(warehouse, serializesToJson("/warehouse/warehouse-null-token.json"));
}

Expand All @@ -67,6 +68,7 @@ public void testDeserialization() throws Exception {
assertThat(warehouse.getUpdated(), is(UPDATED));
assertThat(warehouse.getStatus(), is(STATUS));
assertThat(warehouse.getLinks(), is(LINKS));
assertThat(warehouse.getConnectionUrl(), is(CONNECTION_URL));
}

@Test
Expand All @@ -84,5 +86,6 @@ public void testDeserializationWithNullToken() throws Exception {
assertThat(warehouse.getUpdated(), is(UPDATED));
assertThat(warehouse.getStatus(), is(STATUS));
assertThat(warehouse.getLinks(), is(LINKS));
assertThat(warehouse.getConnectionUrl(), is(CONNECTION_URL));
}
}
1 change: 1 addition & 0 deletions src/test/resources/warehouse/warehouse-null-token.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"createdBy" : "/gdc/account/profile/createdBy",
"updatedBy" : "/gdc/account/profile/updatedBy",
"environment" : "TESTING",
"connectionUrl" : "CONNECTION_URL",
"links" : {
"self" : "/gdc/datawarehouse/instances/instanceId",
"parent" : "/gdc/datawarehouse/instances",
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/warehouse/warehouse.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"createdBy" : "/gdc/account/profile/createdBy",
"updatedBy" : "/gdc/account/profile/updatedBy",
"environment" : "TESTING",
"connectionUrl" : "CONNECTION_URL",
"links" : {
"self" : "/gdc/datawarehouse/instances/instanceId",
"parent" : "/gdc/datawarehouse/instances",
Expand Down
2 changes: 2 additions & 0 deletions src/test/resources/warehouse/warehouses.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"updated" : "2014-03-17T13:02:41.000Z",
"createdBy" : "/gdc/account/profile/{profile-id}",
"updatedBy" : "/gdc/account/profile/{profile-id}",
"connectionUrl" : "test",
"links" : {
"self" : "/gdc/datawarehouse/instances/{instance-id}",
"parent" : "/gdc/datawarehouse/instances",
Expand All @@ -27,6 +28,7 @@
"updated" : "2014-04-09T22:10:26.000Z",
"createdBy" : "/gdc/account/profile/{profile-id}",
"updatedBy" : "/gdc/account/profile/{profile-id}",
"connectionUrl" : "test",
"links" : {
"self" : "/gdc/datawarehouse/instances/{instance-id}",
"parent" : "/gdc/datawarehouse/instances",
Expand Down

0 comments on commit 071e53b

Please sign in to comment.