Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add log trigger support #24

Merged
merged 1 commit into from
Sep 28, 2017
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
55 changes: 55 additions & 0 deletions src/main/java/com/aliyuncs/fc/model/trigger/log/JobConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.aliyuncs.fc.model.trigger.log;

import com.google.gson.annotations.SerializedName;

/**
* TODO: add javadoc
*/
public class JobConfig {

@SerializedName("maxRetryTime")
private Integer maxRetryTime;

@SerializedName("triggerInterval")
private Integer triggerInterval;

public JobConfig() {
}

public Integer getMaxRetryTime() {
return maxRetryTime;
}

public JobConfig setMaxRetryTime(Integer maxRetryTime) {
this.maxRetryTime = maxRetryTime;
return this;
}

public Integer getTriggerInterval() {
return triggerInterval;
}

public JobConfig setTriggerInterval(Integer triggerInterval) {
this.triggerInterval = triggerInterval;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

JobConfig jobConfig = (JobConfig) o;

if (maxRetryTime != null ? !maxRetryTime.equals(jobConfig.maxRetryTime) : jobConfig.maxRetryTime != null)
return false;
return triggerInterval != null ? triggerInterval.equals(jobConfig.triggerInterval) : jobConfig.triggerInterval == null;
}

@Override
public int hashCode() {
int result = maxRetryTime != null ? maxRetryTime.hashCode() : 0;
result = 31 * result + (triggerInterval != null ? triggerInterval.hashCode() : 0);
return result;
}
}
46 changes: 46 additions & 0 deletions src/main/java/com/aliyuncs/fc/model/trigger/log/LogConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.aliyuncs.fc.model.trigger.log;

import com.google.gson.annotations.SerializedName;

/**
* TODO: add javadoc
*/
public class LogConfig {

@SerializedName("project")
private String project;

@SerializedName("logstore")
private String logstore;

public LogConfig(String project, String logstore) {
this.project = project;
this.logstore = logstore;
}

public String getProject() {
return project;
}

public String getLogstore() {
return logstore;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

LogConfig logConfig = (LogConfig) o;

if (project != null ? !project.equals(logConfig.project) : logConfig.project != null) return false;
return logstore != null ? logstore.equals(logConfig.logstore) : logConfig.logstore == null;
}

@Override
public int hashCode() {
int result = project != null ? project.hashCode() : 0;
result = 31 * result + (logstore != null ? logstore.hashCode() : 0);
return result;
}
}
35 changes: 35 additions & 0 deletions src/main/java/com/aliyuncs/fc/model/trigger/log/SourceConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.aliyuncs.fc.model.trigger.log;

import com.google.gson.annotations.SerializedName;

/**
* TODO: add javadoc
*/
public class SourceConfig {

@SerializedName("logstore")
private String logstore;

public SourceConfig(String logstore) {
this.logstore = logstore;
}

public String getLogstore() {
return logstore;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

SourceConfig that = (SourceConfig) o;

return logstore != null ? logstore.equals(that.logstore) : that.logstore == null;
}

@Override
public int hashCode() {
return logstore != null ? logstore.hashCode() : 0;
}
}
98 changes: 98 additions & 0 deletions src/main/java/com/aliyuncs/fc/model/trigger/log/TriggerConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.aliyuncs.fc.model.trigger.log;

import com.google.gson.annotations.SerializedName;

import java.util.Map;

/**
* TODO: add javadoc
*/
public class TriggerConfig {

@SerializedName("sourceConfig")
private SourceConfig sourceConfig;

@SerializedName("jobConfig")
private JobConfig jobConfig;

@SerializedName("logConfig")
private LogConfig logConfig;

@SerializedName("functionParameter")
private Map<String, Object> functionParameter;

@SerializedName("enable")
private Boolean enable;

public TriggerConfig() {
}

public SourceConfig getSourceConfig() {
return sourceConfig;
}

public TriggerConfig setSourceConfig(SourceConfig sourceConfig) {
this.sourceConfig = sourceConfig;
return this;
}

public JobConfig getJobConfig() {
return jobConfig;
}

public TriggerConfig setJobConfig(JobConfig jobConfig) {
this.jobConfig = jobConfig;
return this;
}

public LogConfig getLogConfig() {
return logConfig;
}

public TriggerConfig setLogConfig(LogConfig logConfig) {
this.logConfig = logConfig;
return this;
}

public Map<String, Object> getFunctionParameter() {
return functionParameter;
}

public TriggerConfig setFunctionParameter(Map<String, Object> functionParameter) {
this.functionParameter = functionParameter;
return this;
}

public Boolean isEnable() {
return enable;
}

public TriggerConfig setEnable(Boolean enable) {
this.enable = enable;
return this;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

TriggerConfig that = (TriggerConfig) o;

if (enable != that.enable) return false;
if (sourceConfig != null ? !sourceConfig.equals(that.sourceConfig) : that.sourceConfig != null) return false;
if (jobConfig != null ? !jobConfig.equals(that.jobConfig) : that.jobConfig != null) return false;
if (logConfig != null ? !logConfig.equals(that.logConfig) : that.logConfig != null) return false;
return functionParameter != null ? functionParameter.equals(that.functionParameter) : that.functionParameter == null;
}

@Override
public int hashCode() {
int result = sourceConfig != null ? sourceConfig.hashCode() : 0;
result = 31 * result + (jobConfig != null ? jobConfig.hashCode() : 0);
result = 31 * result + (logConfig != null ? logConfig.hashCode() : 0);
result = 31 * result + (functionParameter != null ? functionParameter.hashCode() : 0);
result = 31 * result + (enable ? 1 : 0);
return result;
}
}
88 changes: 85 additions & 3 deletions src/test/java/com/aliyuncs/fc/FunctionComputeClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
import com.aliyuncs.fc.model.FunctionMetadata;
import com.aliyuncs.fc.model.OSSTriggerConfig;
import com.aliyuncs.fc.model.TriggerMetadata;
import com.aliyuncs.fc.model.trigger.log.JobConfig;
import com.aliyuncs.fc.model.trigger.log.LogConfig;
import com.aliyuncs.fc.model.trigger.log.SourceConfig;
import com.aliyuncs.fc.model.trigger.log.TriggerConfig;
import com.aliyuncs.fc.request.CreateFunctionRequest;
import com.aliyuncs.fc.request.CreateServiceRequest;
import com.aliyuncs.fc.request.CreateTriggerRequest;
Expand Down Expand Up @@ -94,9 +98,13 @@ public class FunctionComputeClientTest {
private static final String CODE_BUCKET = System.getenv("CODE_BUCKET");
private static final String CODE_OBJECT = System.getenv("CODE_OBJECT");
private static final String INVOCATION_ROLE = System.getenv("INVOCATION_ROLE");
private static final String LOG_PROJECT = System.getenv("LOG_PROJECT");
private static final String LOG_STORE = System.getenv("LOG_STORE");

private static final String OSS_SOURCE_ARN =
String.format("acs:oss:%s:%s:%s", REGION, ACCOUNT_ID, CODE_BUCKET);
private static final String LOG_SOURCE_ARN =
String.format("acs:log:%s:%s:project/%s", REGION, ACCOUNT_ID, LOG_PROJECT);
private static final String SERVICE_NAME = "testServiceJavaSDK";
private static final String SERVICE_DESC_OLD = "service desc";
private static final String SERVICE_DESC_NEW = "service desc updated";
Expand All @@ -105,6 +113,7 @@ public class FunctionComputeClientTest {
private static final String FUNCTION_DESC_NEW = "function desc updated";
private static final String TRIGGER_NAME = "testTrigger";
private static final String TRIGGER_TYPE_OSS = "oss";
private static final String TRIGGER_TYPE_LOG = "log";
public static final String STS_API_VERSION = "2015-04-01";

private FunctionComputeClient client;
Expand Down Expand Up @@ -201,7 +210,7 @@ private CreateServiceResponse createService(String serviceName) {
return client.createService(createSReq);
}

private CreateTriggerResponse createTrigger(String triggerName, String prefix, String suffix) {
private CreateTriggerResponse createOssTrigger(String triggerName, String prefix, String suffix) {
CreateTriggerRequest createTReq = new CreateTriggerRequest(SERVICE_NAME, FUNCTION_NAME);
createTReq.setTriggerName(triggerName);
createTReq.setTriggerType(TRIGGER_TYPE_OSS);
Expand Down Expand Up @@ -396,7 +405,7 @@ public void ignoreTestListTriggers() {
for (int i = 0; i < numTriggers; i++) {
String prefix = "prefix";
String suffix = "suffix";
CreateTriggerResponse createTResp = createTrigger(TRIGGER_NAME + i,
CreateTriggerResponse createTResp = createOssTrigger(TRIGGER_NAME + i,
prefix + i, suffix + i);
assertFalse(Strings.isNullOrEmpty(createTResp.getRequestId()));
}
Expand Down Expand Up @@ -1100,7 +1109,7 @@ private void testCRUDHelper(boolean testTrigger) throws ParseException, Interrup
String tfPrefix = "prefix";
String tfSuffix = "suffix";

createTrigger(TRIGGER_NAME, tfPrefix, tfSuffix);
createOssTrigger(TRIGGER_NAME, tfPrefix, tfSuffix);

// List Triggers
ListTriggersRequest listTReq = new ListTriggersRequest(SERVICE_NAME, FUNCTION_NAME);
Expand Down Expand Up @@ -1162,6 +1171,9 @@ private void testCRUDHelper(boolean testTrigger) throws ParseException, Interrup
// Delete Trigger
deleteTrigger(SERVICE_NAME, FUNCTION_NAME, TRIGGER_NAME);
}

testLogTrigger();

// Delete Function
DeleteFunctionRequest deleteFReq = new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
int numFunctionsOld = listFResp.getFunctions().length;
Expand Down Expand Up @@ -1200,6 +1212,76 @@ private void testCRUDHelper(boolean testTrigger) throws ParseException, Interrup
}
}

private void testLogTrigger() throws ParseException {
String triggerName = TRIGGER_TYPE_LOG + "_" + TRIGGER_NAME;
TriggerConfig triggerConfig = new TriggerConfig().setSourceConfig(new SourceConfig(LOG_STORE)).
setJobConfig(new JobConfig().setMaxRetryTime(3).setTriggerInterval(60)).
setLogConfig(new LogConfig("", "")).
setFunctionParameter(new HashMap<String, Object>()).setEnable(true);
CreateTriggerRequest createTReq = new CreateTriggerRequest(SERVICE_NAME, FUNCTION_NAME);
createTReq.setTriggerName(triggerName);
createTReq.setTriggerType(TRIGGER_TYPE_LOG);
createTReq.setInvocationRole(INVOCATION_ROLE);
createTReq.setSourceArn(LOG_SOURCE_ARN);
createTReq.setTriggerConfig(triggerConfig);
client.createTrigger(createTReq);

// List Triggers
ListTriggersRequest listTReq = new ListTriggersRequest(SERVICE_NAME, FUNCTION_NAME);
ListTriggersResponse listTResp = client.listTriggers(listTReq);
assertFalse(Strings.isNullOrEmpty(listTResp.getRequestId()));
assertEquals(1, listTResp.getTriggers().length);
TriggerMetadata triggerOld = listTResp.getTriggers()[0];
assertEquals(triggerName, triggerOld.getTriggerName());


try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
UpdateTriggerRequest req = new UpdateTriggerRequest(SERVICE_NAME, FUNCTION_NAME, triggerName);
req.setInvocationRole(INVOCATION_ROLE);
req.setTriggerConfig(
new TriggerConfig().
setJobConfig(new JobConfig().setMaxRetryTime(5).setTriggerInterval(120)));
UpdateTriggerResponse updateTResp = client.updateTrigger(req);
assertEquals(triggerOld.getTriggerName(), updateTResp.getTriggerName());
assertEquals(triggerOld.getInvocationRole(), updateTResp.getInvocationRole());
assertEquals(triggerOld.getSourceArn(), updateTResp.getSourceArn());
Gson gson = new Gson();
TriggerConfig tcOld = gson
.fromJson(gson.toJson(triggerOld.getTriggerConfig()), TriggerConfig.class);
TriggerConfig tcNew = gson
.fromJson(gson.toJson(updateTResp.getTriggerConfig()), TriggerConfig.class);
assertEquals(triggerOld.getCreatedTime(), updateTResp.getCreatedTime());
assertEquals(triggerOld.getTriggerType(), updateTResp.getTriggerType());
assertEquals(triggerOld.getInvocationRole(), updateTResp.getInvocationRole());
assertEquals(tcOld.getSourceConfig(), tcNew.getSourceConfig());
assertEquals(tcOld.getLogConfig(), tcNew.getLogConfig());
assertEquals(tcOld.isEnable(), tcNew.isEnable());
assertNotEquals(tcOld.getJobConfig(), tcNew.getJobConfig());

Date dateOld = DATE_FORMAT.parse(triggerOld.getLastModifiedTime());
Date dateNew = DATE_FORMAT.parse(updateTResp.getLastModifiedTime());
assertTrue(dateOld.before(dateNew));

// Get Trigger
GetTriggerRequest getTReq = new GetTriggerRequest(SERVICE_NAME, FUNCTION_NAME,
triggerName);
GetTriggerResponse getTResp = client.getTrigger(getTReq);
TriggerConfig getTConfig = gson
.fromJson(gson.toJson(getTResp.getTriggerConfig()), TriggerConfig.class);
assertFalse(Strings.isNullOrEmpty(getTResp.getRequestId()));
assertEquals(triggerName, getTResp.getTriggerName());
assertEquals(LOG_SOURCE_ARN, getTResp.getSourceARN());
assertEquals(TRIGGER_TYPE_LOG, getTResp.getTriggerType());
assertEquals(5, getTConfig.getJobConfig().getMaxRetryTime().intValue());
assertEquals(120, getTConfig.getJobConfig().getTriggerInterval().intValue());

// Delete Trigger
deleteTrigger(SERVICE_NAME, FUNCTION_NAME, triggerName);
}

private void verifyUpdate(String nameOld, String nameNew, String idOld,
String idNew, String lastModifiedTimeOld, String lastModifiedTimeNew,
String createdTimeOld, String createdTimeNew, String descOld, String descNew)
Expand Down