Skip to content

Commit 200ec6e

Browse files
Martin Caslavskymartiner
authored andcommitted
add connectors
1 parent a8c475c commit 200ec6e

25 files changed

Lines changed: 816 additions & 0 deletions

src/main/java/com/gooddata/GoodData.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package com.gooddata;
55

66
import com.gooddata.account.AccountService;
7+
import com.gooddata.connectors.ConnectorService;
78
import com.gooddata.dataload.processes.ProcessService;
89
import com.gooddata.warehouse.WarehouseService;
910
import com.gooddata.dataset.DatasetService;
@@ -61,6 +62,7 @@ public class GoodData {
6162
private final DataStoreService dataStoreService;
6263
private final DatasetService datasetService;
6364
private final ReportService reportService;
65+
private final ConnectorService connectorService;
6466
private final ProcessService processService;
6567
private final WarehouseService warehouseService;
6668

@@ -129,6 +131,7 @@ public GoodData(String hostname, String login, String password, int port) {
129131
reportService = new ReportService(restTemplate);
130132
processService = new ProcessService(restTemplate, accountService);
131133
warehouseService = new WarehouseService(restTemplate, hostname, port);
134+
connectorService = new ConnectorService(restTemplate, projectService);
132135
}
133136

134137
private RestTemplate createRestTemplate(String login, String password, String hostname, HttpClientBuilder builder,
@@ -256,4 +259,7 @@ public ProcessService getProcessService() {
256259
public WarehouseService getWarehouseService() {
257260
return warehouseService;
258261
}
262+
public ConnectorService getConnectorService() {
263+
return connectorService;
264+
}
259265
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3+
*/
4+
package com.gooddata.connectors;
5+
6+
/**
7+
*
8+
*/
9+
public enum Connector {
10+
11+
ZENDESK4;
12+
13+
public String getName() {
14+
return name().toLowerCase();
15+
}
16+
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.gooddata.connectors;
2+
3+
import com.gooddata.GoodDataException;
4+
5+
/**
6+
* Connector exception
7+
*/
8+
public class ConnectorException extends GoodDataException {
9+
10+
public ConnectorException(final String message) {
11+
super(message);
12+
}
13+
14+
public ConnectorException(final String message, final Throwable cause) {
15+
super(message, cause);
16+
}
17+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3+
*/
4+
package com.gooddata.connectors;
5+
6+
import org.codehaus.jackson.annotate.JsonCreator;
7+
import org.codehaus.jackson.annotate.JsonIgnore;
8+
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
9+
import org.codehaus.jackson.annotate.JsonProperty;
10+
import org.codehaus.jackson.map.annotate.JsonSerialize;
11+
12+
/**
13+
* Connector process
14+
*/
15+
@JsonIgnoreProperties(ignoreUnknown = true)
16+
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
17+
public class ConnectorProcess {
18+
19+
private final Status status;
20+
private final String started; // todo date
21+
private final String finished; // todo date
22+
23+
@JsonCreator
24+
protected ConnectorProcess(@JsonProperty("status") Status status, @JsonProperty("started") String started,
25+
@JsonProperty("finished") String finished) {
26+
this.status = status;
27+
this.started = started;
28+
this.finished = finished;
29+
}
30+
31+
public Status getStatus() {
32+
return status;
33+
}
34+
35+
public String getStarted() {
36+
return started;
37+
}
38+
39+
public String getFinished() {
40+
return finished;
41+
}
42+
43+
@JsonIgnore
44+
public boolean isFinished() {
45+
return status != null && status.isFinished();
46+
}
47+
48+
@JsonIgnore
49+
public boolean isFailed() {
50+
return status != null && status.isFailed();
51+
}
52+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3+
*/
4+
package com.gooddata.connectors;
5+
6+
import com.gooddata.AbstractService;
7+
import com.gooddata.FutureResult;
8+
import com.gooddata.GoodDataException;
9+
import com.gooddata.GoodDataRestException;
10+
import com.gooddata.SimplePollHandler;
11+
import com.gooddata.gdc.UriResponse;
12+
import com.gooddata.project.Project;
13+
import com.gooddata.project.ProjectService;
14+
import com.gooddata.project.ProjectTemplate;
15+
import org.springframework.http.client.ClientHttpResponse;
16+
import org.springframework.web.client.RestClientException;
17+
import org.springframework.web.client.RestTemplate;
18+
19+
import java.io.IOException;
20+
import java.util.Collection;
21+
22+
import static com.gooddata.Validate.notNull;
23+
24+
/**
25+
* Connector Service
26+
*/
27+
public class ConnectorService extends AbstractService {
28+
29+
private final ProjectService projectService;
30+
31+
public ConnectorService(final RestTemplate restTemplate, final ProjectService projectService) {
32+
super(restTemplate);
33+
this.projectService = notNull(projectService, "projectService");
34+
}
35+
36+
/**
37+
* Create connector integration with given settings
38+
* @param project project
39+
* @param settings integration settings
40+
* @return created integration
41+
* @throws ConnectorException if integration can't be created
42+
*/
43+
public Integration createIntegration(final Project project, final Settings settings) {
44+
notNull(project, "project");
45+
notNull(settings, "settings");
46+
final Collection<ProjectTemplate> projectTemplates = projectService.getProjectTemplates(project);
47+
if (projectTemplates == null || projectTemplates.isEmpty()) {
48+
throw new GoodDataException("Project " + project.getId() + " doesn't contain a template reference");
49+
}
50+
final ProjectTemplate template = notNull(projectTemplates.iterator().next(), "project template");
51+
final Integration integration = createIntegration(project, settings.getConnector(), new Integration(template.getUrl()));
52+
updateSettings(project, settings);
53+
return integration;
54+
}
55+
56+
/**
57+
* Create connector integration
58+
* @param project project
59+
* @param connector connector
60+
* @param integration integration
61+
* @return created integration
62+
* @throws ConnectorException if integration can't be created
63+
*/
64+
public Integration createIntegration(final Project project, final Connector connector, final Integration integration) {
65+
notNull(project, "project");
66+
notNull(connector, "connector");
67+
notNull(integration, "integration");
68+
try {
69+
return restTemplate.postForObject(Integration.URL, integration, Integration.class, project.getId(), connector.getName());
70+
} catch (GoodDataRestException | RestClientException e) {
71+
throw new ConnectorException("Unable to create " + connector + " integration", e);
72+
}
73+
}
74+
75+
/**
76+
* Update integration settings
77+
* @param project project
78+
* @param settings integration settings
79+
* @throws ConnectorException if settings can't be updated
80+
*/
81+
public <T extends Settings> void updateSettings(final Project project, final T settings) {
82+
notNull(settings, "settings");
83+
notNull(project, "project");
84+
try {
85+
restTemplate.put(Settings.URL, settings, project.getId(), settings.getConnector().getName());
86+
} catch (GoodDataRestException | RestClientException e) {
87+
throw new ConnectorException("Unable to set " + settings.getConnector() + " settings", e);
88+
}
89+
}
90+
91+
/**
92+
* Execute connector process
93+
* @param project project
94+
* @param execution process execution
95+
* @return executed process
96+
* @throws ConnectorException if process execution fails
97+
*/
98+
public FutureResult<Process> executeProcess(final Project project, final ProcessExecution execution) {
99+
notNull(project, "project");
100+
notNull(execution, "execution");
101+
final String connectorName = execution.getConnector().getName();
102+
try {
103+
final UriResponse response = restTemplate.postForObject(Process.URL, execution, UriResponse.class, project.getId(), connectorName);
104+
return new FutureResult<>(this, new SimplePollHandler<Process>(response.getUri(), Process.class) {
105+
@Override
106+
public boolean isFinished(final ClientHttpResponse response) throws IOException {
107+
final Process process = extractData(response, Process.class);
108+
return process.isFinished();
109+
}
110+
111+
@Override
112+
public void handlePollResult(final Process pollResult) {
113+
super.handlePollResult(pollResult);
114+
if (pollResult.isFailed()) {
115+
throw new ConnectorException("Unable to execute " + connectorName + " process: " +
116+
pollResult.getStatus().getDescription());
117+
}
118+
}
119+
});
120+
} catch (GoodDataRestException | RestClientException e) {
121+
throw new ConnectorException("Unable to execute " + connectorName + " process", e);
122+
}
123+
}
124+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3+
*/
4+
package com.gooddata.connectors;
5+
6+
import org.codehaus.jackson.annotate.JsonCreator;
7+
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
8+
import org.codehaus.jackson.annotate.JsonProperty;
9+
import org.codehaus.jackson.annotate.JsonTypeInfo;
10+
import org.codehaus.jackson.annotate.JsonTypeName;
11+
import org.codehaus.jackson.map.annotate.JsonSerialize;
12+
13+
import static com.gooddata.Validate.notEmpty;
14+
15+
/**
16+
* Connectors integration
17+
*/
18+
@JsonTypeName("integration")
19+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
20+
@JsonIgnoreProperties(ignoreUnknown = true)
21+
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
22+
public class Integration {
23+
24+
public static final String URL = "/gdc/projects/{project}/connectors/{connector}/integration";
25+
26+
private String projectTemplate;
27+
private boolean active;
28+
private final ConnectorProcess lastFinishedProcess;
29+
private final ConnectorProcess lastSuccessfulProcess;
30+
private final ConnectorProcess runningProcess;
31+
32+
public Integration(final String projectTemplate) {
33+
this(projectTemplate, true, null, null, null);
34+
}
35+
36+
@JsonCreator
37+
Integration(@JsonProperty("projectTemplate") String projectTemplate, @JsonProperty("active") boolean active,
38+
@JsonProperty("lastFinishedProcess") ConnectorProcess lastFinishedProcess,
39+
@JsonProperty("lastSuccessfulProcess") ConnectorProcess lastSuccessfulProcess,
40+
@JsonProperty("runningProcess") ConnectorProcess runningProcess) {
41+
this.projectTemplate = notEmpty(projectTemplate, "projectTemplate");
42+
this.active = active;
43+
this.lastFinishedProcess = lastFinishedProcess;
44+
this.lastSuccessfulProcess = lastSuccessfulProcess;
45+
this.runningProcess = runningProcess;
46+
}
47+
48+
public String getProjectTemplate() {
49+
return projectTemplate;
50+
}
51+
52+
public void setProjectTemplate(final String projectTemplate) {
53+
this.projectTemplate = notEmpty(projectTemplate, "projectTemplate");
54+
}
55+
56+
public boolean isActive() {
57+
return active;
58+
}
59+
60+
public void setActive(final boolean active) {
61+
this.active = active;
62+
}
63+
64+
public ConnectorProcess getLastFinishedProcess() {
65+
return lastFinishedProcess;
66+
}
67+
68+
public ConnectorProcess getLastSuccessfulProcess() {
69+
return lastSuccessfulProcess;
70+
}
71+
72+
public ConnectorProcess getRunningProcess() {
73+
return runningProcess;
74+
}
75+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3+
*/
4+
package com.gooddata.connectors;
5+
6+
import org.codehaus.jackson.annotate.JsonCreator;
7+
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
8+
import org.codehaus.jackson.annotate.JsonProperty;
9+
import org.codehaus.jackson.annotate.JsonTypeInfo;
10+
import org.codehaus.jackson.annotate.JsonTypeName;
11+
import org.codehaus.jackson.map.annotate.JsonSerialize;
12+
13+
/**
14+
* Connector process
15+
*/
16+
@JsonTypeName("process")
17+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
18+
@JsonIgnoreProperties(ignoreUnknown = true)
19+
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
20+
public class Process extends ConnectorProcess {
21+
22+
public static final String URL = "/gdc/projects/{project}/connectors/{connector}/integration/processes";
23+
24+
@JsonCreator
25+
Process(@JsonProperty("status") Status status, @JsonProperty("started") String started,
26+
@JsonProperty("finished") String finished) {
27+
super(status, started, finished);
28+
}
29+
30+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3+
*/
4+
package com.gooddata.connectors;
5+
6+
import org.codehaus.jackson.annotate.JsonIgnore;
7+
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
8+
import org.codehaus.jackson.annotate.JsonTypeInfo;
9+
import org.codehaus.jackson.annotate.JsonTypeName;
10+
import org.codehaus.jackson.map.annotate.JsonSerialize;
11+
12+
/**
13+
* Connector process execution
14+
*/
15+
@JsonTypeName("process")
16+
@JsonTypeInfo(include = JsonTypeInfo.As.WRAPPER_OBJECT, use = JsonTypeInfo.Id.NAME)
17+
@JsonIgnoreProperties(ignoreUnknown = true)
18+
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
19+
public interface ProcessExecution {
20+
21+
@JsonIgnore
22+
Connector getConnector();
23+
24+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (C) 2007-2014, GoodData(R) Corporation. All rights reserved.
3+
*/
4+
package com.gooddata.connectors;
5+
6+
import org.codehaus.jackson.annotate.JsonIgnore;
7+
8+
/**
9+
* Integration settings
10+
*/
11+
public interface Settings {
12+
13+
final String URL = "/gdc/projects/{project}/connectors/{connector}/integration/settings";
14+
15+
@JsonIgnore
16+
Connector getConnector();
17+
18+
}

0 commit comments

Comments
 (0)