def _clear_interface_refs(deployment):
    # for each resource group
    for resource in [r for r in deployment.resources if r.type == 'AWS::CloudFormation::Stack']:
        stack_id = resource.physical_id
        resource_group = stack_info.ResourceGroupInfo(
            deployment.stack_manager,
            stack_id,
            resource_group_name=resource.logical_id,
            session=deployment.session,
            deployment_info=deployment)

        for rg_lambda in [l for l in resource_group.resources if l.type == 'AWS::Lambda::Function']:
            try:
                function_config = lambda_client.get_function_configuration(FunctionName=rg_lambda.physical_id)
                existing_env = function_config.get("Environment", {})
                env_vars = existing_env.get("Variables", {})

                to_remove = [var for var in env_vars if SERVICE_DIRECTORY_PREFIX in var]

                for rem in to_remove:
                    del env_vars[rem]
                existing_env["Variables"] = env_vars
                response = lambda_client.update_function_configuration(FunctionName=rg_lambda.physical_id, Environment=existing_env)
                # Passing an empty list removes the policy.
                _add_service_access_policy_to_role(function_config["Role"], [])
            except ClientError as e:
                if e.response["Error"]["Code"] not in ["NoSuchEntity", "AccessDenied", "ResourceNotFoundException"]:
                    raise e
 def test_constructor(self):
     target = stack_info.ResourceGroupInfo(
         MOCK_STACK_ARN,
         client=MOCK_CLIENT,
         stack_description=MOCK_STACK_DESCRIPTION)
     self.assertEquals(target.stack_type,
                       stack_info.StackInfo.STACK_TYPE_RESOURCE_GROUP)
     self.assertIs(target.client, MOCK_CLIENT)
     self.assertIs(target.stack_description, MOCK_STACK_DESCRIPTION)
 def test_resource_group_name_discovered(self):
     mock_resource_group_name = 'test-name'
     mock_parameters = {'ResourceGroupName': mock_resource_group_name}
     with mock.patch(
             'resource_manager_common.stack_info.ResourceGroupInfo.parameters',
             new=mock.PropertyMock(return_value=mock_parameters)):
         target = stack_info.ResourceGroupInfo(MOCK_STACK_ARN)
         actual_resource_group_name = target.resource_group_name
         self.assertEquals(actual_resource_group_name,
                           mock_resource_group_name)
def _get_resource_group(resource_group_name, deployment):
    for resource in deployment.resources:
        if resource.type == 'AWS::CloudFormation::Stack':
            stack_id = resource.physical_id
            if stack_id is not None and resource_group_name == resource.logical_id:
                return stack_info.ResourceGroupInfo(
                    deployment.stack_manager,
                    stack_id, 
                    resource_group_name=resource.logical_id,
                    session=deployment.session, 
                    deployment_info=deployment)
    return None
 def test_deployment_discovered(self):
     mock_deployment_stack_id = 'test-deployment-stack-id'
     mock_parameters = {'DeploymentStackArn': mock_deployment_stack_id}
     mock_deployment = 'test-deployment'
     with mock.patch('resource_manager_common.stack_info.DeploymentInfo',
                     return_value=mock_deployment) as mock_DeploymentInfo:
         with mock.patch(
                 'resource_manager_common.stack_info.ResourceGroupInfo.parameters',
                 new=mock.PropertyMock(return_value=mock_parameters)):
             target = stack_info.ResourceGroupInfo(MOCK_STACK_ARN)
             actual_deployment = target.deployment
             actual_deployment_2 = target.deployment
             self.assertEquals(actual_deployment, mock_deployment)
             self.assertEquals(actual_deployment_2, mock_deployment)
             mock_DeploymentInfo.assert_called_once_with(
                 mock_deployment_stack_id, client=target.client)
 def test_deployment_provided(self):
     mock_deployment = 'test_deployment'
     target = stack_info.ResourceGroupInfo(MOCK_STACK_ARN,
                                           deployment_info=mock_deployment)
     self.assertEquals(target.deployment, mock_deployment)
 def test_resource_group_name_provided(self):
     mock_resource_group_name = 'test_resource_group_name'
     target = stack_info.ResourceGroupInfo(
         MOCK_STACK_ARN, resource_group_name=mock_resource_group_name)
     self.assertEquals(target.resource_group_name, mock_resource_group_name)