簡體   English   中英

無法使用 boto3 創建 ResourceGroup:查詢格式無效

[英]Cannot create ResourceGroup using boto3: Query format not valid

我正在嘗試使用以下 boto3 片段創建資源組:

kwargs = {
    'Name': 'cluster.foo.io',
    'Description': 'AWS resources assigned to the foo cluster.',
    'ResourceQuery': {
        'Type': 'TAG_FILTERS_1_0',
        'Query': '[{"Key": "foo.io/cluster", "Values": ["cluster.foo.io"]}]',
    },
    'Tags': {
        'foo.io/cluster': 'cluster.foo.io'
    }
}

client = boto3.client("resource-groups")
resp = client.create_group(**kwargs)

但我收到以下錯誤:

File "/Users/benjamin/.pyenv/versions/3.7.3/Python.framework/Versions/3.7/lib/python3.7/site-packages/botocore/client.py", line 357, in _api_call
    return self._make_api_call(operation_name, kwargs)
File "/Users/benjamin/.pyenv/versions/3.7.3/Python.framework/Versions/3.7/lib/python3.7/site-packages/botocore/client.py", line 661, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.BadRequestException: An error occurred (BadRequestException) 
    when calling the CreateGroup operation: Query not valid: 
    Query format not valid: check JSON syntax

我一直將查詢與文檔中的示例進行比較,但要么我沒有看到差異,要么我在左側字段中很差。 我什至使用了json模塊,如下所示:

resp = self.resource_client.create_group(
    Name='cluster.foo.io',
    Description="AWS resources assigned to the foo cluster",
    ResourceQuery={
        "Type": "TAG_FILTERS_1_0",
        "Query": json.dumps([{"Key": "foo.io/cluster", "Values": ["cluster.foo.io"]}]),
    },
    Tags={
        "foo.io/cluster": "cluster.foo.io",
    },
)

任何幫助,將不勝感激!

查詢參數缺少 ResourceTypeFilters 和 TagFilters。 因此,ResourceQuery 應該如下所示:

'ResourceQuery': {
    'Type': 'TAG_FILTERS_1_0',
    'Query': "{\"ResourceTypeFilters\": [\"AWS::AllSupported\"], \"TagFilters\": [{\"Key\": \"foo.io/cluster\", \"Values\": [\"cluster.foo.io\"]}]}"
}

因此,您的代碼應替換如下(要替換的主要部分是 ResourceQuery:

query = {
    "ResourceTypeFilters": ["AWS::AllSupported"],
    "TagFilters": [{
        "Key": "foo.io/cluster",
        "Values": ["cluster.foo.io"]
    }]
}
resource_query = {
    'Type': 'TAG_FILTERS_1_0',
    'Query': json.dumps(query)
}
kwargs = {
    'Name': 'cluster.foo.io',
    'Description': 'AWS resources assigned to the foo cluster.',
    'ResourceQuery': resource_query,
    'Tags': {
        'foo.io/cluster': 'cluster.foo.io'
    }
}
client = boto3.client("resource-groups")
resp = client.create_group(**kwargs)

我參考了此處顯示的示例 CLI。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM