DataWorks provides built-in checks, such as code reviews before task deployment and validations from the Data Governance Center. You can also integrate custom validation logic to enforce process controls in DataWorks. This topic demonstrates how to use an extension to prohibit specific functions in a workspace by using an example that checks for the MAX_PT function during a commit or deployment.
Scenario
This best practice provides an example of how to check for the MAX_PT function in code that is committed and deployed.
|
Step |
Core process |
Key feature |
|
1 |
OpenEvent sends file commit and deployment messages from a workspace to EventBridge. EventBridge then filters the event messages and sends them to your service. |
When you enable message subscription, you use an event rule to subscribe to specific event types, such as file commit (dataworks:FileChange:CommitFile) and file deployment (dataworks:FileChange:DeployFile). For configuration details, see Best practices: (Advanced feature) Prohibit the use of the MAX_PT function. |
|
2 |
A local or online service receives the messages. The extension is configured to handle events as follows:
|
For configuration details, see Best practices: (Advanced feature) Prohibit the use of the MAX_PT function. |
Prerequisites
-
You have activated EventBridge. For more information, see Billing.
-
You have activated DataWorks. For more information, see Purchase guide.
-
You have created a workspace in DataWorks. For more information, see Create a workspace.
Procedure
Step 1: Configure a custom event bus
-
Log on to the EventBridge console. In the left-side navigation pane, click Event Bus.
-
Click the
icon to Create Custom Event Bus.-
In the Event Bus section, enter a Name for the custom event bus, and then click Next step to configure the event source.
-
Click Skip to bypass the Event Source, Rule, and Target configurations. You must also enter a Description.
-
-
In the left-side navigation pane, click Event Bus. Find the event bus you created and click its name to go to the overview page.
-
In the left-side navigation pane, click Event Rules, and then click Create Rule.
-
In this best practice, the custom EventBridge event bus is defined to receive file commit and file deployment event messages from DataWorks. The following sample shows the core parameter settings.
-
Configure basic info: Specify a custom name for the rule.
-
Configure event pattern.
-
Event Source Type: Select Custom Event Source.
-
Event Source: Leave this unconfigured.
-
Pattern content: Enter the following content in JSON format.
{ "source": [ "acs.dataworks" ], "type": [ "dataworks:FileChange:CommitFile", "dataworks:FileChange:DeployFile" ] }-
source: Defines the product name identifier of the event, which is acs.dataworks.
-
type: Defines the event type identifier for the product. Set this parameter to dataworks:FileChange:CommitFile and dataworks:FileChange:DeployFile.
-
-
Event pattern debugging: Modify the values for source and type, and then test the event. If the test is successful, click Next Step. Click Test to verify whether a custom event matches the pattern. If the test passes, the page displays a message indicating that the match succeeded and the event can be triggered.
-
-
Configure targets.
-
Service Type: Select HTTPS or HTTP. For more information about service types, see Manage event rules.
-
URL: Enter the URL to receive messages from the custom event bus. For example, enter
https://server_address:port_number/extensions/consumer. -
Body: Select Complete Event.
-
Network Type: Select Public Network.
-
-
-
Step 2: Configure an event distribution channel
Log on to the DataWorks console. In the target region, click in the left-side navigation pane. Click Go to Open Platform to open the Developer Backend page.
-
On the Developer Backend page, click OpenEvent in the left-side navigation pane. On the page that appears, click Add Event Distribution Channel and configure the parameters in the dialog box.
-
Workspace for Distribution of Event Messages: Select the workspace that you created.
-
Specify Custom Event Bus in EventBridge for Distribution of Event Messages: Select the event bus that you created in Step 1.
-
-
After you save the event distribution channel, find the channel and click Enable in the Operation column to enable it.
Step 3: Register and configure an extension
Log on to the DataWorks console. In the target region, click in the left-side navigation pane. Click Go to Open Platform to open the Developer Backend page.
-
On the Developer Backend page, click Extension in the left-side navigation pane. Then, click Register Extension and configure the parameters in the dialog box.
-
Deployment Method: Select Deploy based on self-managed service.
-
Register extension
-
Extension Name: Enter a custom name.
-
Extension Point Event: Select Pre-event for Node Commit and Pre-event for Node Deployment.
-
Workspace for Testing: After you configure this parameter, you can test the extension in the specified workspace after it is created but before it is submitted.
-
Parameter Configurations for Extension
Use this parameter to control the scope of the extension. In the specified workspace:
-
When a node is committed (Pre-event for Node Commit), the extension check is not triggered, and the commit process is not blocked.
-
When a node is deployed (Pre-event for Node Deployment), the extension check is triggered. If the validation fails, the deployment process is blocked.
Set the parameter to
extension.project.commit-file.disabled=YourProjectId. Replace YourProjectId with the ID of the workspace where the extension is disabled for the specified extension point event. -
-
Options for Extension: Use this to define the response options for failed validations. In the following example, the available options are Alert and Disable.
{ "type":"object", "properties":{ "checkStatus":{ "type":"number", "title":"Check method for MAX_PT function", "x-decorator":"FormItem", "x-component":"Radio.Group", "x-decorator-props":{ "tooltip":"Description file" }, "x-component-props":{ "dataSource":[ { "value":"WARN", "label":"Alert" }, { "value":"FAIL", "label":"Disable" } ], "mode":"multiple" } } } }
-
-
-
After you complete the configurations, click Determine to save the registered extension.
-
In the Operation column for the extension you created, click Submit to start the review process.
Note-
Extensions are reviewed by the DataWorks platform. The review is typically completed within three business days.
-
To test the extension you created, you must configure a Workspace for Testing.
-
-
After the extension is approved, click Publish in the Actions column.
-
Click the Manage Extensions button to open the page. Select the extension that you created, enable it, and click Settings to configure the control level for MAX_PT function usage in the workspace.
Develop and configure the extension
EventBridge receives JSON-formatted events from DataWorks through an HTTP request, parses the messages, and pushes them to the target service program. After processing the events, the program returns the results to DataWorks.
Sample code
This sample code calls the UpdateIDEEventResult API to return the processing result. It uses the messageId parameter in the API to retrieve detailed event information, checks whether the restricted function is included, and then returns the result to DataWorks by using UpdateIDEEventResult. For more information, see Develop an extension.
Build environment: Java 8 and the Maven build tool.
package com.aliyun.dataworks.demo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dataworks.config.Constants;
import com.aliyun.dataworks.config.EventCheckEnum;
import com.aliyun.dataworks.config.ExtensionParamProperties;
import com.aliyun.dataworks.services.DataWorksOpenApiClient;
import com.aliyun.dataworks_public20200518.Client;
import com.aliyun.dataworks_public20200518.models.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author dataworks demo
*/
@RestController
@RequestMapping("/extensions")
public class ExtensionsController {
@Autowired(required = false)
private DataWorksOpenApiClient dataWorksOpenApiClient;
@Autowired
private ExtensionParamProperties extensionParamProperties;
/**
* Receives messages pushed from EventBridge.
*
* @param jsonParam
*/
@PostMapping("/consumer")
public void consumerEventBridge(@RequestBody String jsonParam) {
JSONObject jsonObj = JSON.parseObject(jsonParam);
String eventCode = jsonObj.getString(Constants.EVENT_CODE_FILED);
if (Constants.COMMIT_FILE_EVENT_CODE.equals(eventCode) || Constants.DEPLOY_FILE_EVENT_CODE.equals(eventCode)) {
// Initialize the client.
Client client = dataWorksOpenApiClient.createClient();
try {
// Get information about the current event parameters.
String messageId = jsonObj.getString("id");
JSONObject data = jsonObj.getObject("data", JSONObject.class);
Long projectId = data.getLong("projectId");
// Initialize the event callback.
UpdateIDEEventResultRequest updateIDEEventResultRequest = new UpdateIDEEventResultRequest();
updateIDEEventResultRequest.setMessageId(messageId);
updateIDEEventResultRequest.setExtensionCode(extensionParamProperties.getExtensionCode());
// Query the data snapshot of the extension point when the extension point event is triggered.
GetIDEEventDetailRequest getIDEEventDetailRequest = new GetIDEEventDetailRequest();
getIDEEventDetailRequest.setMessageId(messageId);
getIDEEventDetailRequest.setProjectId(projectId);
GetIDEEventDetailResponse getIDEEventDetailResponse = client.getIDEEventDetail(getIDEEventDetailRequest);
String content = getIDEEventDetailResponse.getBody().getEventDetail().getCommittedFile().getContent();
// Check whether the code contains the restricted function.
if (content.contains(Constants.CHECK_CODE)) {
// Obtain the configuration of the extension options in the workspace.
GetOptionValueForProjectRequest getOptionValueForProjectRequest = new GetOptionValueForProjectRequest();
getOptionValueForProjectRequest.setProjectId(String.valueOf(projectId));
getOptionValueForProjectRequest.setExtensionCode(extensionParamProperties.getExtensionCode());
GetOptionValueForProjectResponse getOptionValueForProjectResponse = client.getOptionValueForProject(getOptionValueForProjectRequest);
JSONObject jsonObject = JSON.parseObject(getOptionValueForProjectResponse.getBody().getOptionValue());
// Note: You must fill in the format based on the actual settings in DataWorks.
String checkStatus = jsonObject.getString("checkStatus");
updateIDEEventResultRequest.setCheckResult(checkStatus);
updateIDEEventResultRequest.setCheckResultTip("The code contains a restricted function.");
} else {// Successful callback.
updateIDEEventResultRequest.setCheckResult(EventCheckEnum.OK.getCode());
updateIDEEventResultRequest.setCheckResultTip(EventCheckEnum.OK.getName());
}
// Send the callback to DataWorks.
UpdateIDEEventResultResponse response = client.updateIDEEventResult(updateIDEEventResultRequest);
// The unique ID of the request, used for subsequent troubleshooting.
System.out.println("response:" + response.getBody().getRequestId());
} catch (Exception e) {
// The error description.
System.out.println("ErrMsg:" + e.getMessage());
}
} else {
System.out.println("Failed to filter other events. Check the configuration steps.");
}
}
}
Deploy the sample project
-
Prepare the environment and project
-
Dependencies: Java 8 or later and the Maven build tool.
-
Project download link: extensions-demo-maxpt.zip (219 KB).
-
-
Deployment method
-
Local deployment: Package the project into a JAR file. On a local server with Java 8 and Maven, run the
java -jar yourapp.jarcommand to start the service. -
Cloud platform deployment: Package the project into a JAR file and upload it to a runtime environment, such as a Docker container or an ECS instance, for deployment.
NoteMake sure EventBridge can access the deployed service over the Internet.
-
-
In the project's root directory, run the packaging command to package the project into a JAR file.
mvn clean package -Dmaven.test.skip=true spring-boot:repackage -
Run the JAR file:
java -jar target/extensions-demo-maxpt-1.0.jarThe project starts successfully.
/\ / /'---'. __ __ ____ /'\'\\ ( ( )\'---'| '--'| '--'\/'--'| '\ \\\\ \/ /__) | |__| | | |__(___| | ) ) ) ) ' '__| | |__| |'| |___| | / / / / =========|_|===============|__/=/=/_/ :: Spring Boot :: (v2.7.2) 2022-08-22 11:17:35.261 INFO 28648 --- [ main] com.aliyun.dataworks.DemoStarter : Starting DemoStarter v1.0 using Java 1.8.0_151 on xxx.local with PID 28648 (/Users/xxx/ts/extensions-demo-maxpt) ns-demo-maxpt/target/extensions-demo-maxpt-1.0.jar started by guangzhen.zk in /Users/xxx/ts/extensions-demo-maxpt) 2022-08-22 11:17:35.264 INFO 28648 --- [ main] com.aliyun.dataworks.DemoStarter : No active profile set, falling back to 1 default profile: "default" 2022-08-22 11:17:35.276 INFO 28648 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2022-08-22 11:17:36.291 INFO 28648 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2022-08-22 11:17:36.291 INFO 28648 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.65] 2022-08-22 11:17:36.419 INFO 28648 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2022-08-22 11:17:36.419 INFO 28648 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1099 ms 2022-08-22 11:17:36.851 INFO 28648 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2022-08-22 11:17:36.860 INFO 28648 --- [ main] com.aliyun.dataworks.DemoStarter : Started DemoStarter in 1.98 seconds (JVM running for 2.449)Enter
http://localhost:8080/indexin a browser. If"hello world!"is returned, the application is deployed successfully. After the network is connected, you can subscribe to messages from EventBridge.
Result verification
After deploying the code and confirming your service is accessible from EventBridge, verify the result in the workspace where the extension is enabled.
Verification steps
-
On the Data Development page, create a node. In the node, edit the code to include the prohibited
MAX_PTfunction, then save and commit the node. -
Click the Publish button. On the Create Deployment Package page, deploy the node directly. This triggers the extension check.
NoteAccording to the extension configuration, the check does not apply to file commit events in the specified workspace. Therefore, the extension check is not triggered when you commit a node that contains the
MAX_PTfunction. The check is triggered only when you deploy the node that contains theMAX_PTfunction.