Repository: cloud-custodian/cloud-custodian

Issue:
Azure - API Manager - Action to resize/change sku



Discussion:

Resolving patch:
diff --git a/tools/c7n_azure/c7n_azure/resources/apimanagement.py b/tools/c7n_azure/c7n_azure/resources/apimanagement.py
--- a/tools/c7n_azure/c7n_azure/resources/apimanagement.py
+++ b/tools/c7n_azure/c7n_azure/resources/apimanagement.py
@@ -11,9 +11,12 @@
 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 # See the License for the specific language governing permissions and
 # limitations under the License.
-
+from c7n_azure.actions.base import AzureBaseAction
 from c7n_azure.provider import resources
 from c7n_azure.resources.arm import ArmResourceManager
+from azure.mgmt.resource.resources.models import GenericResource
+
+from c7n.utils import type_schema
 
 
 @resources.register('api-management')
@@ -45,3 +48,59 @@ class resource_type(ArmResourceManager.resource_type):
             'resourceGroup'
         )
         resource_type = 'Microsoft.ApiManagement/service'
+
+
+@ApiManagement.action_registry.register('resize')
+class Resize(AzureBaseAction):
+    """
+    Action to scale api management resource.
+    Required arguments: capacity in units and tier (Developer, Basic, Standard or Premium).
+
+    :example:
+    This policy will resize api management to Premium tier with 8 units.
+
+    .. code-block:: yaml
+
+        policies:
+          - name: resize-api
+            resource: azure.api-management
+            filters:
+              - type: value
+                key: name
+                value: test-api
+            actions:
+              - type: resize
+                tier: Premium
+                capacity: 8
+
+    """
+
+    schema = type_schema(
+        'resize',
+        required=['capacity', 'tier'],
+        **{
+            'capacity': {'type': 'number'},
+            'tier': {'enum': ['Developer', 'Basic', 'Standard', 'Premium']}
+        })
+
+    def __init__(self, data, manager=None):
+        super(Resize, self).__init__(data, manager)
+        self.capacity = self.data['capacity']
+        self.tier = self.data['tier']
+
+    def _prepare_processing(self):
+        self.client = self.session.client('azure.mgmt.resource.ResourceManagementClient')
+
+    def _process_resource(self, resource):
+        resource['sku']['capacity'] = self.capacity
+        resource['sku']['tier'] = self.tier
+        resource['sku']['name'] = self.tier
+
+        az_resource = GenericResource.deserialize(resource)
+
+        api_version = self.session.resource_api_version(resource['id'])
+
+        # create a GenericResource object with the required parameters
+        generic_resource = GenericResource(sku=az_resource.sku)
+
+        self.client.resources.update_by_id(resource['id'], api_version, generic_resource)


Test patch:
diff --git a/tools/c7n_azure/tests/test_apimanagement.py b/tools/c7n_azure/tests/test_apimanagement.py
--- a/tools/c7n_azure/tests/test_apimanagement.py
+++ b/tools/c7n_azure/tests/test_apimanagement.py
@@ -12,9 +12,14 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 from __future__ import absolute_import, division, print_function, unicode_literals
+from c7n_azure.resources.apimanagement import Resize
+from mock import MagicMock
 
 from azure_common import BaseTest, arm_template
 
+from c7n.utils import local_session
+from c7n_azure.session import Session
+
 
 class ApiManagementTest(BaseTest):
     def setUp(self):
@@ -42,3 +47,27 @@ def test_find_apimanagement_by_name(self):
         })
         resources = p.run()
         self.assertEqual(len(resources), 1)
+
+    def test_resize_action(self):
+        action = Resize(data={'capacity': 8, 'tier': 'Premium'})
+        action.client = MagicMock()
+        action.manager = MagicMock()
+        action.session = local_session(Session)
+
+        resource = {
+            'id': '/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/test-rg/'
+                  'providers/Microsoft.ApiManagement/service/test-apimanagement',
+            'name': 'test-apimanagement',
+            'type': 'Microsoft.ApiManagement/service',
+            'sku': {'name': 'Developer', 'capacity': 1, 'tier': 'Developer'},
+            'resourceGroup': 'test-rg'
+        }
+
+        action.process([resource])
+
+        update_by_id = action.client.resources.update_by_id
+
+        self.assertEqual(len(update_by_id.call_args_list), 1)
+        self.assertEqual(len(update_by_id.call_args_list[0][0]), 3)
+        self.assertEqual(update_by_id.call_args_list[0][0][2].serialize()['sku']['capacity'], 8)
+        self.assertEqual(update_by_id.call_args_list[0][0][2].serialize()['sku']['tier'], 'Premium')