From 525789dcdcb1e89f8b3dd2438b2fc3b2277f5cfe Mon Sep 17 00:00:00 2001
From: sunhui <wb-sh373163@alibaba-inc.com>
Date: Sun, 23 Apr 2023 14:58:36 +0800
Subject: [PATCH 07/46] fence_aliyun: support filter for list-action

---
 agents/aliyun/fence_aliyun.py        | 20 ++++++++++++++++++--
 tests/data/metadata/fence_aliyun.xml |  5 +++++
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/agents/aliyun/fence_aliyun.py b/agents/aliyun/fence_aliyun.py
index 1e9c790c..c7785a2b 100644
--- a/agents/aliyun/fence_aliyun.py
+++ b/agents/aliyun/fence_aliyun.py
@@ -76,6 +76,14 @@ def get_nodes_list(conn, options):
 	result = {}
 	request = DescribeInstancesRequest()
 	request.set_PageSize(100)
+
+	if "--filter" in options:
+		filter_key = options["--filter"].split("=")[0].strip()
+		filter_value = options["--filter"].split("=")[1].strip()
+		params = request.get_query_params()
+		params[filter_key] = filter_value
+		request.set_query_params(params)
+
 	response = _send_request(conn, request)
 	if response is not None:
 		instance_list = response.get('Instances').get('Instance')
@@ -137,17 +145,25 @@ def define_new_opts():
 	all_opt["ram_role"] = {
 		"getopt": ":",
 		"longopt": "ram-role",
-		"help": "--ram-role=[name]        Ram Role",
+		"help": "--ram-role=[name]              Ram Role",
 		"shortdesc": "Ram Role.",
 		"required": "0",
 		"order": 5
 	}
+	all_opt["filter"] = {
+		"getopt": ":",
+		"longopt": "filter",
+		"help": "--filter=[key=value]           Filter (e.g. InstanceIds=[\"i-XXYYZZAA1\",\"i-XXYYZZAA2\"]",
+		"shortdesc": "Filter for list-action.",
+		"required": "0",
+		"order": 6
+	}
 
 # Main agent method
 def main():
 	conn = None
 
-	device_opt = ["port", "no_password", "region", "access_key", "secret_key", "ram_role"]
+	device_opt = ["port", "no_password", "region", "access_key", "secret_key", "ram_role", "filter"]
 
 	atexit.register(atexit_handler)
 
diff --git a/tests/data/metadata/fence_aliyun.xml b/tests/data/metadata/fence_aliyun.xml
index 56d79204..b0671889 100644
--- a/tests/data/metadata/fence_aliyun.xml
+++ b/tests/data/metadata/fence_aliyun.xml
@@ -38,6 +38,11 @@
 		<content type="string"  />
 		<shortdesc lang="en">Ram Role.</shortdesc>
 	</parameter>
+	<parameter name="filter" unique="0" required="0">
+		<getopt mixed="--filter=[key=value]" />
+		<content type="string"  />
+		<shortdesc lang="en">Filter for list-action.</shortdesc>
+	</parameter>
 	<parameter name="quiet" unique="0" required="0">
 		<getopt mixed="-q, --quiet" />
 		<content type="boolean"  />
-- 
2.25.1

