# Copyright 2016-2017 Capital One Services, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 __future__ import absolute_import, division, print_function, unicode_literals

from c7n.filters import revisions
from c7n.resources.vpc import SecurityGroupDiff, SecurityGroupPatch
from .common import BaseTest


class SGDiffLibTest(BaseTest):

    def test_sg_diff_remove_ingress(self):
        factory = self.replay_flight_data('test_sg_config_ingres_diff')
        p = self.load_policy({
            'name': 'sg-differ',
            'resource': 'security-group',
            'filters': [
                {'GroupId': 'sg-65229a0c'},
                {'type': 'diff',
                 'selector': 'date',
                 'selector_value': '2017/01/27 00:40Z'}],
        }, session_factory=factory)

        resources = p.run()
        self.assertEqual(len(resources), 1)
        self.maxDiff = None
        self.assertEqual(
            resources[0]['c7n:diff'],
            {'ingress': {
                'removed': [{u'FromPort': 0,
                             u'IpProtocol': u'tcp',
                             u'IpRanges': [],
                             u'Ipv6Ranges': [],
                             u'PrefixListIds': [],
                             u'ToPort': 0,
                             u'UserIdGroupPairs': [
                                 {u'GroupId': u'sg-aa6c90c3',
                                  u'UserId': u'644160558196'}]}]}})

    def test_json_diff_pitr(self):
        factory = self.replay_flight_data('test_sg_config_diff')
        p = self.load_policy({
            'name': 'sg-differ',
            'resource': 'security-group',
            'filters': [
                {'GroupId': 'sg-a38ed1de'},
                {'type': 'json-diff',
                 'selector': 'date',
                 'selector_value': '2016/12/11 17:25Z'}],
        }, session_factory=factory)

        resources = p.run()
        self.maxDiff = None
        self.assertEqual(len(resources), 1)
        for change in [
            {u'op': u'add',
             u'path': u'/IpPermissionsEgress/0/UserIdGroupPairs/0',
             u'value': {u'GroupId': u'sg-a08ed1dd',
                        u'UserId': u'644160558196'}},
            {u'op': u'replace',
             u'path': u'/Tags/1/Key',
             u'value': u'Scope'},
            {u'op': u'replace',
             u'path': u'/Tags/1/Value',
             u'value': u'account'},
            {u'op': u'add',
             u'path': u'/Tags/2',
             u'value': {u'Key': u'NetworkLocation', u'Value': u'DMZ'}},
            {u'op': u'replace',
             u'path': u'/IpPermissions/1/FromPort',
             u'value': 22},
            {u'op': u'replace',
             u'path': u'/IpPermissions/1/IpRanges/0/CidrIp',
             u'value': u'10.0.0.0/24'},
            {u'op': u'replace',
             u'path': u'/IpPermissions/1/ToPort',
             u'value': 22},
            {u'op': u'add',
             u'path': u'/IpPermissions/2',
             u'value': {u'FromPort': 8485,
                        u'IpProtocol': u'tcp',
                        u'IpRanges': [],
                        u'Ipv6Ranges': [],
                        u'PrefixListIds': [],
                        u'ToPort': 8485,
                        u'UserIdGroupPairs': [{u'GroupId': u'sg-a38ed1de',
                                               u'UserId': u'644160558196'}]}},
            {u'op': u'add',
             u'path': u'/IpPermissions/3',
             u'value': {u'FromPort': 443,
                        u'IpProtocol': u'tcp',
                        u'IpRanges': [{u'CidrIp': u'10.42.1.0/24'}],
                        u'Ipv6Ranges': [],
                        u'PrefixListIds': [],
                        u'ToPort': 443,
                        u'UserIdGroupPairs': []}}]:
            self.assertTrue(change in resources[0]['c7n:diff'])

    def test_sg_diff_pitr(self):
        factory = self.replay_flight_data('test_sg_config_diff')
        p = self.load_policy({
            'name': 'sg-differ',
            'resource': 'security-group',
            'filters': [
                {'GroupId': 'sg-a38ed1de'},
                {'type': 'diff',
                 'selector': 'date',
                 'selector_value': '2016/12/11 17:25Z'}],
        }, session_factory=factory)

        resources = p.run()
        self.assertEqual(len(resources), 1)
        self.maxDiff = None
        self.assertEqual(resources[0]['c7n:diff'],
            {'egress': {
                'added': [{u'IpProtocol': u'-1',
                           u'IpRanges': [{u'CidrIp': u'0.0.0.0/0'}],
                           u'Ipv6Ranges': [],
                           u'PrefixListIds': [],
                           u'UserIdGroupPairs': [{u'GroupId': u'sg-a08ed1dd',
                                                  u'UserId': u'644160558196'}]}],
                'removed': [{
                             u'IpProtocol': u'-1',
                             u'IpRanges': [{u'CidrIp': u'0.0.0.0/0'}],
                             u'Ipv6Ranges': [],
                             u'PrefixListIds': [],
                             u'UserIdGroupPairs': []}]},
             'ingress': {
                 'added': [{u'FromPort': 22,
                            u'IpProtocol': u'tcp',
                            u'IpRanges': [{u'CidrIp': u'10.0.0.0/24'}],
                            u'Ipv6Ranges': [],
                            u'PrefixListIds': [],
                            u'ToPort': 22,
                            u'UserIdGroupPairs': []},
                           {u'FromPort': 8485,
                            u'IpProtocol': u'tcp',
                            u'IpRanges': [],
                            u'Ipv6Ranges': [],
                            u'PrefixListIds': [],
                            u'ToPort': 8485,
                            u'UserIdGroupPairs': [{u'GroupId': u'sg-a38ed1de',
                                                   u'UserId': u'644160558196'}]},
                        ]},
             'tags': {'added': {u'Scope': u'account'}}})

    def test_sg_patch_pitr(self):
        factory = self.replay_flight_data('test_sg_config_patch_pitr')
        p = self.load_policy({
            'name': 'sg-differ',
            'resource': 'security-group',
            'filters': [