From 51582354f0338fc79f6bd3242246575cace5c4a5 Mon Sep 17 00:00:00 2001 From: Hongqi Wang Date: Thu, 28 Sep 2017 00:52:20 -0700 Subject: [PATCH] Add log trigger support --- .../fc/model/trigger/log/JobConfig.java | 55 +++++++++++ .../fc/model/trigger/log/LogConfig.java | 46 +++++++++ .../fc/model/trigger/log/SourceConfig.java | 35 +++++++ .../fc/model/trigger/log/TriggerConfig.java | 98 +++++++++++++++++++ .../fc/FunctionComputeClientTest.java | 88 ++++++++++++++++- 5 files changed, 319 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/aliyuncs/fc/model/trigger/log/JobConfig.java create mode 100644 src/main/java/com/aliyuncs/fc/model/trigger/log/LogConfig.java create mode 100644 src/main/java/com/aliyuncs/fc/model/trigger/log/SourceConfig.java create mode 100644 src/main/java/com/aliyuncs/fc/model/trigger/log/TriggerConfig.java diff --git a/src/main/java/com/aliyuncs/fc/model/trigger/log/JobConfig.java b/src/main/java/com/aliyuncs/fc/model/trigger/log/JobConfig.java new file mode 100644 index 0000000..0ee2c99 --- /dev/null +++ b/src/main/java/com/aliyuncs/fc/model/trigger/log/JobConfig.java @@ -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; + } +} diff --git a/src/main/java/com/aliyuncs/fc/model/trigger/log/LogConfig.java b/src/main/java/com/aliyuncs/fc/model/trigger/log/LogConfig.java new file mode 100644 index 0000000..031d26c --- /dev/null +++ b/src/main/java/com/aliyuncs/fc/model/trigger/log/LogConfig.java @@ -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; + } +} diff --git a/src/main/java/com/aliyuncs/fc/model/trigger/log/SourceConfig.java b/src/main/java/com/aliyuncs/fc/model/trigger/log/SourceConfig.java new file mode 100644 index 0000000..45eb621 --- /dev/null +++ b/src/main/java/com/aliyuncs/fc/model/trigger/log/SourceConfig.java @@ -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; + } +} diff --git a/src/main/java/com/aliyuncs/fc/model/trigger/log/TriggerConfig.java b/src/main/java/com/aliyuncs/fc/model/trigger/log/TriggerConfig.java new file mode 100644 index 0000000..f748582 --- /dev/null +++ b/src/main/java/com/aliyuncs/fc/model/trigger/log/TriggerConfig.java @@ -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 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 getFunctionParameter() { + return functionParameter; + } + + public TriggerConfig setFunctionParameter(Map 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; + } +} \ No newline at end of file diff --git a/src/test/java/com/aliyuncs/fc/FunctionComputeClientTest.java b/src/test/java/com/aliyuncs/fc/FunctionComputeClientTest.java index 876cb28..79dff80 100644 --- a/src/test/java/com/aliyuncs/fc/FunctionComputeClientTest.java +++ b/src/test/java/com/aliyuncs/fc/FunctionComputeClientTest.java @@ -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; @@ -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"; @@ -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; @@ -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); @@ -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())); } @@ -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); @@ -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; @@ -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()).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)