You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat(cli): can ignore errors and return dummy value in CloudControl API context provider (#211)
CDK app with CC API Context Provider fails if the resource we want to
get doesn't exist.
```
[Error at /LookupStack] Encountered CC API error while getting resource MyLookupTestRole. Error: ResourceNotFoundException: AWS::IAM::Role Handler returned status FAILED: The role with name MyLookupTestRole cannot be found. (Service: Iam, Status Code: 404, Request ID: xxxx-xxx-xxxx-xxxx) (HandlerErrorCode: NotFound, RequestToken: xxxx-xxx-xxxx-xxxx)
```
Also CC API Context Provider (`CcApiContextProviderPlugin`) cannot
ignore errors even if `ignoreErrorOnMissingContext` is passed in
aws-cdk-lib because the current code in aws-cdk-**cli** doesn't handle
the property.
Sample cdk-lib code (based on my
[PR](aws/aws-cdk#33603) for IAM Role
fromLookup):
```ts
const response: {[key: string]: any}[] = ContextProvider.getValue(scope, {
provider: cxschema.ContextProvider.CC_API_PROVIDER,
props: {
typeName: 'AWS::IAM::Role',
exactIdentifier: options.roleName,
propertiesToReturn: [
'Arn',
],
} as cxschema.CcApiContextQuery,
dummyValue: [
{
// eslint-disable-next-line @cdklabs/no-literal-partition
Arn: 'arn:aws:iam::123456789012:role/DUMMY_ARN',
},
],
ignoreErrorOnMissingContext: options.returnDummyRoleOnMissing, // added
}).value;
```
However it would be good if the provider can ignore errors and return
any dummy value to cdk library. This allows all resources to be **used
if available, or created if not.**
Actually, SSM and KMS provider can ignore errors:
KMS:
https://github.com/aws/aws-cdk-cli/blob/main/packages/aws-cdk/lib/context-providers/keys.ts#L43-L48
SSM:
https://github.com/aws/aws-cdk-cli/blob/main/packages/aws-cdk/lib/context-providers/ssm-parameters.ts#L27-L30
For example, in cdk-lib, we can specify
[ignoreErrorOnMissingContext](https://github.com/aws/aws-cdk/blob/v2.182.0/packages/aws-cdk-lib/aws-kms/lib/key.ts#L741-L744)
in the `fromLookup`. The `dummyValue` and `ignoreErrorOnMissingContext`
properties can also be specified in
[GetContextValueOptions](https://github.com/go-to-k/aws-cdk/blob/45a276fd0fc9b7b08f69f7faf3d0091796ab1663/packages/aws-cdk-lib/core/lib/context-provider.ts#L31-L45).
## cli-integ
aws/aws-cdk-cli-testing#50
---
By submitting this pull request, I confirm that my contribution is made
under the terms of the Apache-2.0 license
---------
Co-authored-by: Rico Hermans <[email protected]>
Copy file name to clipboardExpand all lines: packages/@aws-cdk/cloud-assembly-schema/schema/cloud-assembly.schema.json
+11-3
Original file line number
Diff line number
Diff line change
@@ -1032,20 +1032,28 @@
1032
1032
"type": "string"
1033
1033
},
1034
1034
"exactIdentifier": {
1035
-
"description": "exactIdentifier of the resource.\nSpecifying exactIdentifier will return at most one result.\nEither exactIdentifier or propertyMatch should be specified. (Default - None)",
1035
+
"description": "Identifier of the resource to look up using `GetResource`.\n\nSpecifying exactIdentifier will return exactly one result, or throw an error. (Default - Either exactIdentifier or propertyMatch should be specified.)",
1036
1036
"type": "string"
1037
1037
},
1038
1038
"propertyMatch": {
1039
-
"description": "This indicates the property to search for.\nIf both exactIdentifier and propertyMatch are specified, then exactIdentifier is used.\nSpecifying propertyMatch will return 0 or more results.\nEither exactIdentifier or propertyMatch should be specified. (Default - None)",
1039
+
"description": "Returns any resources matching these properties, using `ListResources`.\n\nSpecifying propertyMatch will return 0 or more results.\n\n## Notes on property completeness\n\nCloudControl API's `ListResources` may return fewer properties than\n`GetResource` would, depending on the resource implementation.\n\nThe resources that `propertyMatch` matches against will *only ever* be the\nproperties returned by the `ListResources` call. (Default - Either exactIdentifier or propertyMatch should be specified.)",
1040
1040
"$ref": "#/definitions/Record<string,unknown>"
1041
1041
},
1042
1042
"propertiesToReturn": {
1043
-
"description": "This is a set of properties returned from CC API that we want to return from ContextQuery.",
1043
+
"description": "This is a set of properties returned from CC API that we want to return from ContextQuery.\n\nIf any properties listed here are absent from the target resource, an error will be thrown.\n\nThe returned object will always include the key `Identifier` with the CC-API returned\nfield `Identifier`.\n\n## Notes on property completeness\n\nCloudControl API's `ListResources` may return fewer properties than\n`GetResource` would, depending on the resource implementation.\n\nThe returned properties here are *currently* selected from the response\nobject that CloudControl API returns to the CDK CLI.\n\nHowever, if we find there is need to do so, we may decide to change this\nbehavior in the future: we might change it to perform an additional\n`GetResource` call for resources matched by `propertyMatch`.",
1044
1044
"type": "array",
1045
1045
"items": {
1046
1046
"type": "string"
1047
1047
}
1048
1048
},
1049
+
"dummyValue": {
1050
+
"description": "The value to return if the resource was not found and `ignoreErrorOnMissingContext` is true.\n\nIf supplied, `dummyValue` should be an array of objects.\n\n`dummyValue` does not have to have elements, and it may have objects with\ndifferent properties than the properties in `propertiesToReturn`, but it\nwill be easiest for downstream code if the `dummyValue` conforms to\nthe expected response shape. (Default - No dummy value available)"
1051
+
},
1052
+
"ignoreErrorOnMissingContext": {
1053
+
"description": "Ignore an error and return the `dummyValue` instead if the resource was not found.\n\n- In case of an `exactIdentifier` lookup, return the `dummyValue` if the resource with\n that identifier was not found.\n- In case of a `propertyMatch` lookup, this setting currently does not have any effect,\n as `propertyMatch` queries can legally return 0 resources.\n\nif `ignoreErrorOnMissingContext` is set, `dummyValue` should be set and be an array.",
thrownewContextProviderError(`Specify either exactIdentifier or propertyMatch, but not both. Failed to find resources using CC API for type ${args.typeName}.`);
23
+
thrownewContextProviderError(`Provider protocol error: specify either exactIdentifier or propertyMatch, but not both (got ${JSON.stringify(args)})`);
29
24
}
30
-
if(!args.exactIdentifier&&!args.propertyMatch){
31
-
thrownewContextProviderError(`Neither exactIdentifier nor propertyMatch is specified. Failed to find resources using CC API for type ${args.typeName}.`);
thrownewContextProviderError(`Provider protocol error: if ignoreErrorOnMissingContext is set, a dummyValue must be supplied (got ${JSON.stringify(args)})`);
thrownewContextProviderError(`Encountered CC API error while listing ${typeName} resources matching ${JSON.stringify(propertyMatch)}: ${err.message}`);
120
+
}
121
+
throwerr;
124
122
}
125
-
returnresultObjs;
126
123
}
127
124
}
125
+
126
+
/**
127
+
* Convert a CC API response object into a nicer object (parse the JSON)
0 commit comments