Repository: cloud-custodian/cloud-custodian

Issue:
AWS API Gateway rest-api metric filters do not work properly
**Describe the bug**
It looks like there is a bug related to the metric filters for `aws.rest-api` resources, specifically around the dimensions being incorrect.

**To Reproduce**
```bash
$ c7n-org run --dryrun -c accounts.yml -s output -u policies/api-gateway/policies.yml
Exception running policy:unused-api-gateways account:test-account region:us-east-1 error:'apigateway'
```
Given the follow policy:
```yaml
policies:
  - name: unused-api-gateways
    resource: rest-api
    filters:
      - type: metrics
        name: Count
        statistics: Sum
        days: 7
        period: 86400
        value: 5
        op: less-than
        missing-value: 0
```

**Expected behavior**
```bash
$ c7n-org run --dryrun -c accounts.yml -s output -u policies/api-gateway/policies.yml --debug
2021-06-01 17:07:47,460: c7n_org:INFO Ran account:test-account region:us-east-1 policy:unused-api-gateways matched:2 time:1.14
```


**Background (please complete the following information):**
 - OS: OS X
 - Python Version: Python 3.9
 - Custodian Version: 0.9.10.0
 - Cloud Provider: aws
 - Policy: [please exclude any account/sensitive information]
```yaml
policies:
  - name: unused-api-gateways
    resource: rest-api
    filters:
      - type: metrics
        name: Count
        statistics: Sum
        days: 7
        period: 86400
        value: 5
        op: less-than
        missing-value: 0
```
 - Traceback: `custodian.filters:WARNING CW Retrieval error: 'apigateway'`


**Additional context**
I already patched this and got it working on my local install, I will submit a follow up PR for review.



Discussion:
Thanks for reporting this, especially for including context about an upcoming PR!

Resolving patch:
diff --git a/c7n/filters/metrics.py b/c7n/filters/metrics.py
--- a/c7n/filters/metrics.py
+++ b/c7n/filters/metrics.py
@@ -97,6 +97,7 @@ class MetricsFilter(Filter):
 
     # ditto for spot fleet
     DEFAULT_NAMESPACE = {
+        'apigateway': 'AWS/ApiGateway',
         'cloudfront': 'AWS/CloudFront',
         'cloudsearch': 'AWS/CloudSearch',
         'dynamodb': 'AWS/DynamoDB',
diff --git a/c7n/resources/apigw.py b/c7n/resources/apigw.py
--- a/c7n/resources/apigw.py
+++ b/c7n/resources/apigw.py
@@ -8,7 +8,7 @@
 from contextlib import suppress
 
 from c7n.actions import ActionRegistry, BaseAction
-from c7n.filters import FilterRegistry, ValueFilter
+from c7n.filters import FilterRegistry, ValueFilter, MetricsFilter
 from c7n.filters.iamaccess import CrossAccountAccessFilter
 from c7n.filters.related import RelatedResourceFilter
 from c7n.manager import resources, ResourceManager
@@ -160,6 +160,14 @@ def generate_arn(self):
         return self._generate_arn
 
 
+@RestApi.filter_registry.register('metrics')
+class Metrics(MetricsFilter):
+
+    def get_dimensions(self, resource):
+        return [{'Name': 'ApiName',
+                 'Value': resource['name']}]
+
+
 @RestApi.filter_registry.register('cross-account')
 class RestApiCrossAccount(CrossAccountAccessFilter):
 


Test patch:
diff --git a/tests/test_apigw.py b/tests/test_apigw.py
--- a/tests/test_apigw.py
+++ b/tests/test_apigw.py
@@ -121,6 +121,38 @@ def test_rest_api_delete(self):
             client.delete_rest_api(restApiId='am0c2fyskg')
         self.assertEqual(e.exception.response['Error']['Code'], 'NotFoundException')
 
+    def test_rest_api_metrics(self):
+        factory = self.replay_flight_data("test_rest_api_delete")
+        p = self.load_policy(
+            {
+                "name": "unused-rest-api",
+                "resource": "rest-api",
+                "filters": [
+                    {
+                        "type": "metrics",
+                        "name": "Count",
+                        "days": 4,
+                        "period": 86400,
+                        "value": 1000,
+                        "op": "less-than",
+                    }
+                ],
+            },
+            session_factory=factory,
+        )
+        test_filter = p.resource_manager.filters[0]
+        resource_payload = {
+            "id": "am0c2fyskg",
+            "name": "c7n-test-2"
+        }
+        test_filter.process(resource_payload)
+        self.assertEqual(
+            test_filter.get_dimensions(resource_payload),
+            [
+                {"Name": "ApiName", "Value": "c7n-test-2"}
+            ],
+        )
+
 
 class TestRestResource(BaseTest):