Esempio n. 1
0
    def testWithNoBackend(self):
        """ A backend service has no backend

           Expectation: the backend service itself will be migrated

        """
        ### create test resources
        backend_service_name = 'end-to-end-test-backend-service'
        backend_service_selfLink = \
            self.test_resource_creator.create_global_backend_service(
                'sample_internal_self_managed_backend_service.json',
                backend_service_name, [])['targetLink']
        original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            backend_service_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()

        ### check migration result
        # check backend service config
        # an internal backend service doesn't have a network config
        new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_backend_service_configs, new_backend_service_configs))
Esempio n. 2
0
    def testWithNoBackends(self):
        """ A backend service has no backend

        Expectation: the backend service itself will be migrated

        """
        ### create test resources
        backend_service_name = 'end-to-end-test-backend-service'
        operation = self.test_resource_creator.create_regional_backend_service(
            'sample_internal_backend_service.json', backend_service_name, [])
        original_backend_service_configs = self.google_api_interface.get_regional_backend_service_configs(
            backend_service_name)
        backend_service_selfLink = operation['targetLink']
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            backend_service_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        new_backend_service_configs = self.google_api_interface.get_regional_backend_service_configs(
            backend_service_name)

        # check backend service config
        self.assertTrue(
            check_selfLink_equal(new_backend_service_configs['network'],
                                 self.test_resource_creator.network_selfLink))
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_backend_service_configs, new_backend_service_configs))
        print('Pass the current test')
Esempio n. 3
0
    def get_backends_selfLinks(self) -> list:
        """ Get all backends of this forwarding rule. The backends list can
        contain backend service, or a target pool, or a target instance

        Returns: a list of GCE resource selfLinks

        """
        backends_selfLinks = []
        if self.forwarding_rule_configs != None and 'target' in self.forwarding_rule_configs:
            target_selfLink = self.forwarding_rule_configs['target']
            print(target_selfLink)
            self_link_executor = SelfLinkExecutor(self.compute,
                                                  target_selfLink,
                                                  self.network,
                                                  self.subnetwork)
            # it can be a target instance, target pool or a backend service
            if self_link_executor.is_a_supported_resource():
                backends_selfLinks.append(target_selfLink)
            # it is a target proxy
            else:
                target_proxy_type = self.get_target_proxy_type()
                target_proxy_name = self.get_target_proxy_name()
                target_proxy_configs = self.get_target_proxy_configs(
                    target_proxy_type, target_proxy_name,
                    self_link_executor.region)
                backends_selfLinks.extend(
                    self.
                    get_backend_service_selfLinks_from_target_proxy_configs(
                        target_proxy_configs))
        if 'backendService' in self.forwarding_rule_configs:
            backends_selfLinks.append(
                self.forwarding_rule_configs['backendService'])
        return list(set(backends_selfLinks))
Esempio n. 4
0
    def testWithoutAutoscalerAttached(self):
        ### create test resources
        group_name = 'end-to-end-test-managed-instance-group-1'
        operation = self.test_resource_creator.create_regional_managed_instance_group(
            self.test_resource_creator.legacy_instance_template_selfLink,
            group_name, 'sample_multi_zone_managed_instance_group.json')
        instance_group_selfLink = operation['targetLink']
        original_instance_template_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name)

        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            instance_group_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        new_instance_template_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name)
        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_configs,
                new_instance_template_configs))
        # network changed
        self.assertTrue(
            check_instance_template_network(
                new_instance_template_configs,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))

        print('Pass the current test')
Esempio n. 5
0
    def migrate_backends(self):
        """ Migrate the backends of the backend service one by one
        without deleting or recreating the backend service

        Args:
            backend_service_configs: the configs of the backend service

        """

        if 'backends' not in self.backend_service.backend_service_configs:
            return None
        backends = self.backend_service.backend_service_configs['backends']
        for i in range(len(backends)):
            backend = backends[i]
            migration_helper = SelfLinkExecutor(
                self.compute, backend['group'], self.network, self.subnetwork,
                self.preserve_instance_external_ip)
            backend_migration_handler = migration_helper.build_instance_group_migration_handler(
            )
            # The backend type is not an instance group, then just ignore
            if backend_migration_handler == None:
                continue
            self.backend_migration_handlers.append(backend_migration_handler)
            print('Detaching: %s' % (backend['group']))
            self.backend_service.detach_a_backend(backend['group'])
            print('Migrating: %s' % (backend['group']))
            backend_migration_handler.network_migration()
            print('Reattaching: %s' % (backend['group']))
            self.backend_service.reattach_all_backends()
            # wait for the first backend becoming healthy,
            # then continue migrate other backends
            if i == 0 and len(backends) > 1:
                self.backend_service.wait_for_backend_become_healthy(
                    backend['group'])
Esempio n. 6
0
    def testWithNoInstanceInTheGroup(self):
        ### create test resources
        unmanaged_instance_group_name = 'end-to-end-test-unmanaged-instance-group-1'
        self.test_resource_creator.create_unmanaged_instance_group(
            unmanaged_instance_group_name, [])
        original_config = self.google_api_interface.get_unmanaged_instance_group_configs(
            unmanaged_instance_group_name)

        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute, original_config['selfLink'],
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name, False)
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        new_config = self.google_api_interface.get_unmanaged_instance_group_configs(
            unmanaged_instance_group_name)

        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_config, new_config))

        # network changed
        self.assertTrue(
            check_selfLink_equal(new_config['network'],
                                 self.test_resource_creator.network_selfLink))
        print('Pass the current test')
    def testAttachingAnInternalForwardingRule(self):
        """ A targetInstance is in use by the forwarding rule

        Expectation: both the targetInstance and the forwarding rule will be migrated

        """
        ### create test resources
        forwarding_rule_name = 'end-to-end-test-forwarding-rule'
        target_instance_name = 'end-to-end-test-instance'

        operation = self.test_resource_creator.create_instance_using_template(
            target_instance_name,
            self.test_resource_creator.legacy_instance_template_selfLink)
        instance_selfLink = operation['targetLink']

        target_instance_selfLink = \
            self.test_resource_creator.create_a_target_instance(
                target_instance_name, instance_selfLink)['targetLink']
        original_instance_config = self.google_api_interface.get_instance_configs(
            target_instance_name)
        forwarding_rule_selfLink = \
            self.test_resource_creator.create_regional_forwarding_rule_with_target(
                'sample_tcp_regional_forwarding_rule_internal.json',
                forwarding_rule_name,
                target_instance_selfLink)['targetLink']
        original_forwarding_rule_config = self.google_api_interface.get_regional_forwarding_rule_config(
            forwarding_rule_name)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            target_instance_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )

        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()

        ### check migration result

        new_forwarding_rule_config = self.google_api_interface.get_regional_forwarding_rule_config(
            forwarding_rule_name)
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                original_forwarding_rule_config, new_forwarding_rule_config))
        # check instance network
        new_instance_config = self.google_api_interface.get_instance_configs(
            target_instance_name)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                new_instance_config, original_instance_config))
        # network changed
        self.assertTrue(
            check_instance_network(
                new_instance_config,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))

        print('Pass the current test')
    def testWithInstancesFromAnUnmanagedInstanceGroup(self):
        """ The target pool is served by an instance which is a member of an unmanaged instance group

        Expectation: the migration will not start.

        """
        ### create resources
        instance_name_1 = 'end-to-end-test-instance-1'
        operation = self.test_resource_creator.create_instance_using_template(
            instance_name_1,
            self.test_resource_creator.legacy_instance_template_selfLink)
        instance_selfLink_1 = operation['targetLink']

        instance_name_2 = 'end-to-end-test-instance-2'
        operation = self.test_resource_creator.create_instance_using_template(
            instance_name_2,
            self.test_resource_creator.legacy_instance_template_selfLink)
        instance_selfLink_2 = operation['targetLink']

        unmanaged_instance_group_name = 'end-to-end-test-unmanaged-instance-group-1'
        original_instances_in_group = [instance_name_1, instance_name_2]
        self.test_resource_creator.create_unmanaged_instance_group(
            unmanaged_instance_group_name, original_instances_in_group)
        original_group_config = self.google_api_interface.get_unmanaged_instance_group_configs(
            unmanaged_instance_group_name)

        target_pool_name = 'end-to-end-test-target-pool'
        operation = self.test_resource_creator.create_target_pool_with_health_check(
            'sample_target_pool_with_no_instance.json',
            target_pool_name, [], [instance_selfLink_1, instance_selfLink_2],
            health_check_selfLink=None)

        target_pool_selfLink = operation['targetLink']
        original_target_pool_config = self.google_api_interface.get_target_pool_config(
            target_pool_name)

        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            target_pool_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        # the migration will not start, and raise an error
        with self.assertRaises(AmbiguousTargetResource):
            migration_handler = selfLink_executor.build_migration_handler()
            migration_handler.network_migration()

        ### check migration result
        # unmanaged instance group doesn't change
        new_group_config = self.google_api_interface.get_unmanaged_instance_group_configs(
            unmanaged_instance_group_name)
        self.assertEqual(original_group_config, new_group_config)
        # target pool is unchanged
        new_target_pool_config = self.google_api_interface.get_target_pool_config(
            target_pool_name)
        self.assertEqual(original_target_pool_config, new_target_pool_config)

        print('Pass the current test')
Esempio n. 9
0
    def testAsBackendOfTargetPool(self):
        """ The unmanaged instance group serves a target pool, which means the
        instances in this instance group serve the target pool.

        Expectation: The migration can still succeed.
        But the instances might be detached from the target pool after the migration.

        """
        ### create test resources
        instance_name_1 = 'end-to-end-test-instance-1'
        operation = self.test_resource_creator.create_instance_using_template(
            instance_name_1,
            self.test_resource_creator.legacy_instance_template_selfLink)
        instance_selfLink_1 = operation['targetLink']
        original_instance_config = self.google_api_interface.get_instance_configs(
            instance_name_1)
        unmanaged_instance_group_name = 'end-to-end-test-unmanaged-instance-group-1'
        original_instances_in_group = [instance_name_1]
        unmanaged_instance_group_selfLink = \
            self.test_resource_creator.create_unmanaged_instance_group(
                unmanaged_instance_group_name,
                original_instances_in_group)['targetLink']
        original_instance_group_config = self.google_api_interface.get_unmanaged_instance_group_configs(
            unmanaged_instance_group_name)
        target_pool_name = 'end-to-end-test-target-pool'
        self.test_resource_creator.create_target_pool_with_health_check(
            'sample_target_pool_with_no_instance.json',
            target_pool_name, [], [instance_selfLink_1],
            health_check_selfLink=None)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            unmanaged_instance_group_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        # the migration is successful
        new_instance_config = self.google_api_interface.get_instance_configs(
            instance_name_1)
        new_instance_group_config = self.google_api_interface.get_unmanaged_instance_group_configs(
            unmanaged_instance_group_name)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_instance_config, new_instance_config))
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_instance_group_config, new_instance_group_config))

        # network changed
        self.assertTrue(
            check_instance_network(
                new_instance_config,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))
        print('Pass the current test')
    def testOnlyInstancesAsBackend(self):
        """ The target pool has single instances as its backends, no instance groups
        """
        ### create test resources
        instance_name_1 = 'end-to-end-test-instance-1'
        operation = self.test_resource_creator.create_instance_using_template(
            instance_name_1,
            self.test_resource_creator.legacy_instance_template_selfLink)
        instance_selfLink_1 = operation['targetLink']
        instance_name_2 = 'end-to-end-test-instance-2'
        operation = self.test_resource_creator.create_instance_using_template(
            instance_name_2,
            self.test_resource_creator.legacy_instance_template_selfLink)
        instance_selfLink_2 = operation['targetLink']

        target_pool_name = 'end-to-end-test-target-pool'
        operation = self.test_resource_creator.create_target_pool_with_health_check(
            'sample_target_pool_with_no_instance.json',
            target_pool_name, [], [instance_selfLink_1, instance_selfLink_2],
            health_check_selfLink=None)
        target_pool_selfLink = operation['targetLink']
        original_target_pool_instance_list = \
            self.google_api_interface.get_target_pool_config(target_pool_name)[
                'instances']

        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            target_pool_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        new_target_pool_instance_list = \
            self.google_api_interface.get_target_pool_config(target_pool_name)[
                'instances']
        # target pool's instances unchanged
        self.assertTrue(
            compare_two_list(new_target_pool_instance_list,
                             original_target_pool_instance_list))
        # instances' network changed
        new_instance_1_config = self.google_api_interface.get_instance_configs(
            instance_name_1)
        self.assertTrue(
            check_instance_network(
                new_instance_1_config,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))
        new_instance_2_config = self.google_api_interface.get_instance_configs(
            instance_name_2)
        self.assertTrue(
            check_instance_network(
                new_instance_2_config,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))
        print('Pass the current test')
Esempio n. 11
0
    def testWithBackends(self):
        """ A backend service has backends

        Expectation: all the backends will be migrated

        """
        ### create test resources
        backend_service_name = 'end-to-end-test-backend-service'

        group_name_1 = 'end-to-end-test-managed-instance-group-1'
        operation = self.test_resource_creator.create_regional_managed_instance_group(
            self.test_resource_creator.legacy_instance_template_selfLink,
            group_name_1,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_1_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')

        original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)
        original_backend_selfLinks = [instance_group_1_selfLink]
        backend_service_selfLink = \
            self.test_resource_creator.create_global_backend_service(
                'sample_internal_self_managed_backend_service.json',
                backend_service_name, original_backend_selfLinks)['targetLink']
        original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            backend_service_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()

        ### check migration result
        # check backend service config
        new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_backend_service_configs, new_backend_service_configs))
        # check its backends
        new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)
        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_1_configs,
                new_instance_template_1_configs))

        # network changed
        self.assertTrue(
            check_instance_template_network(
                new_instance_template_1_configs,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))
Esempio n. 12
0
    def network_migration(self):
        """ Network migration for an internal forwarding rule.
         The forwarding rule will be deleted first.
         Then, the tool will migrate the backend service.
         Finally, recreate the forwarding rule in the target subnet.

         Returns:

         """
        if self.forwarding_rule.compare_original_network_and_target_network():
            print('The backend service %s is already using target subnet.' %
                  (self.forwarding_rule_name))
            return

        backends_selfLinks = self.forwarding_rule.backends_selfLinks
        for backends_selfLink in backends_selfLinks:
            selfLink_executor = SelfLinkExecutor(
                self.compute, backends_selfLink, self.network_name,
                self.subnetwork_name, self.preserve_instance_external_ip)
            # the backends can be a target instance or an internal backend service
            try:
                backends_migration_handler = selfLink_executor.build_migration_handler(
                )
            except UnsupportedBackendService:
                warnings.warn(
                    'The load balancing scheme of (%s) is not supported. '
                    'Continue migrating other backends.' % (backends_selfLink))
                continue
            if backends_migration_handler != None:
                self.backends_migration_handlers.append(
                    backends_migration_handler)
                if isinstance(backends_migration_handler,
                              BackendServiceMigration):
                    backend_service = backends_migration_handler.backend_service
                    if backend_service != None and backend_service.count_forwarding_rules(
                    ) > 1:
                        print(
                            'The backend service is associated with two or more forwarding rules, \n'
                            'so it can not be migrated. \n'
                            'Terminating. ')
                        return
        self.migration_status = MigrationStatus(1)
        print('Deleting: %s.' % (self.forwarding_rule_name))
        self.forwarding_rule.delete_forwarding_rule()
        self.migration_status = MigrationStatus(2)

        print('Migrating the backends of %s.' % (self.forwarding_rule_name))
        for backends_migration_handler in self.backends_migration_handlers:
            backends_migration_handler.network_migration()
        self.migration_status = MigrationStatus(3)

        print('Recreating the forwarding rule (%s) in the target subnet.' %
              (self.forwarding_rule_name))
        self.forwarding_rule.insert_forwarding_rule(
            self.forwarding_rule.new_forwarding_rule_configs)
        self.migration_status = MigrationStatus(4)
Esempio n. 13
0
    def testSharingBackendWithAnotherBackendService(self):
        """ The internal backend service A shares the same backend with another backend service B

        Expectation: rollback

        """
        ### create test resources
        backend_service_1_name = 'end-to-end-test-backend-service-1'
        backend_service_2_name = 'end-to-end-test-backend-service-2'
        group_name_1 = 'end-to-end-test-managed-instance-group-1'
        operation = self.test_resource_creator.create_regional_managed_instance_group(
            self.test_resource_creator.legacy_instance_template_selfLink,
            group_name_1,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_1_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')
        original_instance_group_config = self.google_api_interface.get_multi_zone_managed_instance_group_configs(
            group_name_1)

        backend_service_1_selfLink = \
            self.test_resource_creator.create_regional_backend_service(
                'sample_internal_backend_service.json',
                backend_service_1_name, [instance_group_1_selfLink])[
                'targetLink']
        original_backend_service_1_configs = self.google_api_interface.get_regional_backend_service_configs(
            backend_service_1_name)
        self.test_resource_creator.create_regional_backend_service(
            'sample_internal_backend_service.json', backend_service_2_name,
            [instance_group_1_selfLink])
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            backend_service_1_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        with self.assertRaises(MigrationFailed):
            migration_handler = selfLink_executor.build_migration_handler()
            migration_handler.network_migration()
        ### check migration result
        new_backend_service_1_configs = self.google_api_interface.get_regional_backend_service_configs(
            backend_service_1_name)

        # check backend service config
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                original_backend_service_1_configs,
                new_backend_service_1_configs))
        # check its backends
        new_instance_group_config = self.google_api_interface.get_multi_zone_managed_instance_group_configs(
            group_name_1)
        self.assertTrue(
            instance_template_config_is_unchanged(
                original_instance_group_config, new_instance_group_config))
        print('Pass the current test')
Esempio n. 14
0
    def testAsBackendOfBackendService(self):
        """ The instance group is serving a backend service

        Expectation: The migration will fail and rollback to the legacy network.

        """
        ### create test resources
        instance_name_1 = 'end-to-end-test-instance-1'
        operation = self.test_resource_creator.create_instance_using_template(
            instance_name_1,
            self.test_resource_creator.legacy_instance_template_selfLink)

        original_instance_config = self.google_api_interface.get_instance_configs(
            instance_name_1)
        unmanaged_instance_group_name = 'end-to-end-test-unmanaged-instance-group-1'
        original_instances_in_group = [instance_name_1]
        unmanaged_instance_group_selfLink = \
            self.test_resource_creator.create_unmanaged_instance_group(
                unmanaged_instance_group_name,
                original_instances_in_group)['targetLink']
        original_instance_group_config = self.google_api_interface.get_unmanaged_instance_group_configs(
            unmanaged_instance_group_name)
        backend_service_name = 'end-to-end-test-backend-service'
        original_backend_selfLinks = [unmanaged_instance_group_selfLink]
        self.test_resource_creator.create_regional_backend_service(
            'sample_internal_backend_service.json', backend_service_name,
            original_backend_selfLinks)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            unmanaged_instance_group_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        with self.assertRaises(MigrationFailed):
            migration_handler = selfLink_executor.build_migration_handler()
            migration_handler.network_migration()

        ### check migration result
        # the migration is failed and the resources are rolled back
        new_instance_config = self.google_api_interface.get_instance_configs(
            instance_name_1)
        new_instance_group_config = self.google_api_interface.get_unmanaged_instance_group_configs(
            unmanaged_instance_group_name)
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                original_instance_config, new_instance_config))
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                original_instance_group_config, new_instance_group_config))

        print('Pass the current test')
Esempio n. 15
0
    def get_backend_service_selfLinks_from_target_proxy_configs(
            self, target_proxy_configs) -> list:
        """ Get all the backend services which are serving the forwarding rule

        Returns: a list pf backend service selfLink

        """
        if 'service' in target_proxy_configs:
            return [target_proxy_configs['service']]
        elif 'urlMap' in target_proxy_configs:
            backend_services_selfLinks = set()
            url_selfLink = target_proxy_configs['urlMap']
            self_link_executor = SelfLinkExecutor(self.compute, url_selfLink,
                                                  self.network,
                                                  self.subnetwork)
            urlMap_name = target_proxy_configs['urlMap'].split('/')[-1]
            if self_link_executor.region == None:
                urlMap_configs = self.compute.urlMaps().get(
                    project=self.project, urlMap=urlMap_name).execute()
            else:
                urlMap_configs = self.compute.regionUrlMaps().get(
                    project=self.project,
                    urlMap=urlMap_name,
                    region=self_link_executor.region).execute()
            find_all_matching_strings_from_a_dict(urlMap_configs,
                                                  "compute/v1/projects/",
                                                  backend_services_selfLinks)
            return list(backend_services_selfLinks)
        return []
Esempio n. 16
0
    def create_instance_migration_handler(self, instance_selfLink=None):
        """ Create an instance migration handler

        Args:
            instance_selfLink: url selfLink of the instance

        Returns:

        """
        if instance_selfLink == None:
            instance_selfLink = self.get_instance_selfLink()
        selfLink_executor = SelfLinkExecutor(
            self.compute, instance_selfLink, self.network, self.subnetwork,
            self.preserve_instance_external_ip)
        try:
            return selfLink_executor.build_instance_migration_handler()
        except:
            return None
Esempio n. 17
0
    def network_migration(self):
        """ Network migration for a external forwarding rule.
        The tool will migrate its backend services one by one.
        The forwarding rule itself will not be deleted or recreated.

        """
        if self.forwarding_rule.compare_original_network_and_target_network():
            print('The backend service %s is already using target subnet.' % (
                self.forwarding_rule_name))
            return

        backends_selfLinks = self.forwarding_rule.backends_selfLinks
        if backends_selfLinks == []:
            print(
                'No backend service needs to be migrated. Terminating the migration.')
            return

        for backends_selfLink in backends_selfLinks:
            selfLink_executor = SelfLinkExecutor(self.compute,
                                                 backends_selfLink,
                                                 self.network_name,
                                                 self.subnetwork_name,
                                                 self.preserve_instance_external_ip)
            try:
                backends_migration_handler = selfLink_executor.build_migration_handler()
            except UnsupportedBackendService:
                warnings.warn(
                    'The load balancing scheme of (%s) is not supported. '
                    'Continue migrating other backends.' % (backends_selfLink))
                continue            # Save handlers for rollback purpose
            if backends_migration_handler != None:
                if isinstance(backends_migration_handler,
                              BackendServiceMigration):
                    backend_service = backends_migration_handler.backend_service
                    if backend_service != None and backend_service.count_forwarding_rules() > 1:
                        print(
                            'The backend service is associated with two or more forwarding rules, \n'
                            'so it can not be migrated. \n'
                            'Terminating. ')
                        # this backend service will be ignored and will continue migrate other backend services
                        continue
                self.backends_migration_handlers.append(
                    backends_migration_handler)
                backends_migration_handler.network_migration()
Esempio n. 18
0
    def network_migration(self):
        """ Migrate the network of an unmanaged instance group.
          The instances belonging to this instance group will
          be migrated one by one.
          """
        self.migration_status = 0
        if self.instance_group.compare_original_network_and_target_network():
            print('The instance group %s is already using the target subnet.' %
                  (self.instance_group_name))
            return
        self.migration_status = 1
        for instance_selfLink in self.instance_group.instance_selfLinks:
            selfLink_executor = SelfLinkExecutor(self.compute,
                                                 instance_selfLink,
                                                 self.network_name,
                                                 self.subnetwork_name,
                                                 self.preserve_external_ip)
            instance_migration_handler = selfLink_executor.build_migration_handler(
            )

            if instance_migration_handler != None:
                self.instance_migration_handlers.append(
                    instance_migration_handler)
                # print('Detaching the instance %s.' %(instance_selfLink))
                # self.instance_group.remove_an_instance(instance_selfLink)
                instance_migration_handler.network_migration(force=True)
        self.migration_status = 2

        print('Deleting: %s.' % (self.instance_group_name))
        self.instance_group.delete_instance_group()
        self.migration_status = 3

        print(
            'Recreating the instance group using the same configuration in the new network.'
        )
        self.instance_group.create_instance_group(
            self.instance_group.new_instance_group_configs)
        self.migration_status = 4
        print('Adding the instances back to the instance group: %s.' %
              (self.instance_group_name))
        self.instance_group.add_all_instances()
        self.migration_status = 5
Esempio n. 19
0
    def build_instance_migration_handlers(self):
        """ Use instance's selfLinks to create a list of InstanceMigrationHandler

        Returns:

        """
        for selfLink in self.target_pool.attached_single_instances_selfLinks:
            executor = SelfLinkExecutor(self.compute, selfLink, self.network,
                                        self.subnetwork,
                                        self.preserve_instance_external_ip)
            try:
                instance_migration_handler = executor.build_instance_migration_handler(
                )
                self.instance_migration_handlers.append(
                    instance_migration_handler)
            except HttpError as e:
                if 'not found' in e._get_reason():
                    continue
                else:
                    raise e
Esempio n. 20
0
    def migrate_backends(self):
        """ Migrate the backends of the backend service one by one

        Args:
            backend_service_configs: the configs of the backend service

        """
        if 'backends' not in self.backend_service.backend_service_configs:
            return None
        backends = self.backend_service.backend_service_configs['backends']
        for backend in backends:
            selfLink_executor = SelfLinkExecutor(
                self.compute, backend['group'], self.network, self.subnetwork,
                self.preserve_instance_external_ip)
            backend_migration_handler = selfLink_executor.build_migration_handler(
            )
            if backend_migration_handler != None:
                backend_migration_handler.network_migration()
                self.backend_migration_handlers.append(
                    backend_migration_handler)
Esempio n. 21
0
    def testAsBackendOfTargetPool(self):
        """ The managed instance group serves a target pool.

        Expectation: The migration will not start.

        """
        ### create test resources
        group_name = 'end-to-end-test-managed-instance-group-1'
        instance_group_selfLink = \
            self.test_resource_creator.create_regional_managed_instance_group(
                self.test_resource_creator.legacy_instance_template_selfLink,
                group_name,
                'sample_multi_zone_managed_instance_group.json')[
                'targetLink']
        original_instance_template_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name)

        target_pool_name = 'end-to-end-test-target-pool'
        self.test_resource_creator.create_target_pool_with_health_check(
            'sample_target_pool_with_no_instance.json',
            target_pool_name, [group_name], [],
            health_check_selfLink=None)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            instance_group_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        with self.assertRaises(MigrationFailed):
            migration_handler = selfLink_executor.build_migration_handler()
            migration_handler.network_migration()
        ### check migration result
        # the migration didn't start
        new_instance_template_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name)
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                new_instance_template_configs, new_instance_template_configs))
        print('Pass the current test')
Esempio n. 22
0
    def testAsBackendOfBackendService(self):
        """ The managed instance group serves a backend service.

        Expectation: The migration will fail and rollback.

        """
        ### create test resources
        group_name = 'end-to-end-test-managed-instance-group-1'
        instance_group_selfLink = \
            self.test_resource_creator.create_regional_managed_instance_group(
                self.test_resource_creator.legacy_instance_template_selfLink,
                group_name,
                'sample_multi_zone_managed_instance_group.json')[
                'targetLink'].replace('/instanceGroupManagers/',
                                      '/instanceGroups/')
        original_instance_group_config = self.google_api_interface.get_multi_zone_managed_instance_group_configs(
            group_name)
        backend_service_name = 'end-to-end-test-backend-service'
        self.test_resource_creator.create_regional_backend_service(
            'sample_internal_backend_service.json', backend_service_name,
            [instance_group_selfLink])
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            instance_group_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        with self.assertRaises(MigrationFailed):
            migration_handler = selfLink_executor.build_migration_handler()
            migration_handler.network_migration()

        ### check migration result
        # the migration is failed and the resources are rolled back
        new_instance_group_config = self.google_api_interface.get_multi_zone_managed_instance_group_configs(
            group_name)
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                original_instance_group_config, new_instance_group_config))
        print('Pass the current test')
Esempio n. 23
0
    def testWithForwardingRuleAttached(self):
        """ A forwaring rule is using the external backend service.
          Try to migrate the external backend service directly.

          Expectation: still can migrate

        """
        ### create test resources
        backend_service_name = 'end-to-end-test-backend-service'
        original_backend_selfLinks = []
        operation = self.test_resource_creator.create_regional_backend_service(
            'sample_internal_backend_service.json', backend_service_name,
            original_backend_selfLinks)
        original_backend_service_configs = self.google_api_interface.get_regional_backend_service_configs(
            backend_service_name)
        backend_service_selfLink = operation['targetLink']
        self.test_resource_creator.create_regional_forwarding_rule_with_backend_service(
            'sample_tcp_regional_forwarding_rule_internal.json',
            backend_service_name, backend_service_selfLink)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            backend_service_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        with self.assertRaises(MigrationFailed):
            migration_handler = selfLink_executor.build_migration_handler()
            migration_handler.network_migration()

        ### check migration result
        # migration didn't start
        new_backend_service_configs = self.google_api_interface.get_regional_backend_service_configs(
            backend_service_name)
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                original_backend_service_configs, new_backend_service_configs))
        print('Pass the current test')
    def testWithNoBackendsAttached(self):
        backend_service_name = 'end-to-end-test-backend-service'

        operation = self.test_resource_creator.create_global_backend_service(
            'sample_external_backend_service.json', backend_service_name, [])
        original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name)
        backend_service_selfLink = operation['targetLink']
        # start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            backend_service_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_backend_service_configs, new_backend_service_configs))
        print('Pass the current test')
Esempio n. 25
0
    def testWithBackendServiceAttached(self):
        """ A backend service is in use by this forwarding rule

        Expectation: both the forwarding rule and the backend service will be migrated.

        """
        ### create test resources
        forwarding_rule_name = 'end-to-end-test-forwarding-rule'
        backend_service_name = 'end-to-end-test-backend-service'
        group_name_1 = 'end-to-end-test-managed-instance-group-1'
        operation = self.test_resource_creator.create_regional_managed_instance_group(
            self.test_resource_creator.legacy_instance_template_selfLink,
            group_name_1,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_1_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')
        original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)

        backend_service_selfLink = \
            self.test_resource_creator.create_regional_backend_service(
                'sample_internal_backend_service.json',
                backend_service_name, [instance_group_1_selfLink])['targetLink']
        original_backend_service_configs = self.google_api_interface.get_regional_backend_service_configs(
            backend_service_name)
        forwarding_rule_selfLink = \
            self.test_resource_creator.create_regional_forwarding_rule_with_backend_service(
                'sample_tcp_regional_forwarding_rule_internal.json',
                forwarding_rule_name,
                backend_service_selfLink)['targetLink']
        original_forwarding_rule_config = self.google_api_interface.get_regional_forwarding_rule_config(
            forwarding_rule_name)
        ### start migration
        selfLink_executor = SelfLinkExecutor(self.compute,
                                             forwarding_rule_selfLink,
                                             self.test_resource_creator.network_name,
                                             self.test_resource_creator.subnetwork_name,
                                             )

        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()

        ### check migration result
        # check internal forwarding rule
        new_forwarding_rule_config = self.google_api_interface.get_regional_forwarding_rule_config(
            forwarding_rule_name)
        self.assertTrue(resource_config_is_unchanged_except_for_network(
            original_forwarding_rule_config,
            new_forwarding_rule_config))
        self.assertTrue(
            check_selfLink_equal(new_forwarding_rule_config['network'],
                                 self.test_resource_creator.network_selfLink))

        # check backend service config
        new_backend_service_configs = self.google_api_interface.get_regional_backend_service_configs(
            backend_service_name)
        self.assertTrue(resource_config_is_unchanged_except_for_network(
            original_backend_service_configs,
            new_backend_service_configs))
        self.assertEqual(new_backend_service_configs['network'],
                         self.test_resource_creator.network_selfLink)
        # check its backends
        new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs \
                (
                group_name_1)
        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_1_configs,
                new_instance_template_1_configs)
        )
        self.assertTrue(
            check_instance_template_network(new_instance_template_1_configs,
                                            self.test_resource_creator.network_selfLink,
                                            self.test_resource_creator.subnetwork_selfLink))

        print('Pass the current test')
    def testAutomodeSubnetworkExistsInBothRegions(self):
        """ A backend service has two instance groups from two regions as backends.
        The network is an auto-mode network, and it has subnet with the same name
        in all regions.

        Expectation: all the backends will be migrated

        """
        ### create test resources
        auto_subnetwork_name = 'end-to-end-test-auto-subnetwork'
        try:
            target_network_selfLink = \
                self.google_api_interface_region_1.get_network(
                    auto_subnetwork_name)['selfLink']
        except:
            target_network_selfLink = \
                self.google_api_interface_region_1.create_auto_subnetwork(
                    auto_subnetwork_name)[
                    'targetLink']

        group_name_1 = 'end-to-end-test-managed-instance-group-1'
        operation = self.test_resource_creator_region_1.create_regional_managed_instance_group(
            self.test_resource_creator_region_1.
            legacy_instance_template_selfLink,
            group_name_1,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_1_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')
        original_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs(
            group_name_1)
        group_name_2 = 'end-to-end-test-managed-instance-group-2'
        operation = self.test_resource_creator_region_2.create_regional_managed_instance_group(
            self.test_resource_creator_region_2.
            legacy_instance_template_selfLink,
            group_name_2,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_2_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')
        original_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs(
            group_name_2)

        backend_service_name = 'end-to-end-test-backend-service'
        operation = self.test_resource_creator_region_1.create_global_backend_service(
            'sample_external_backend_service.json', backend_service_name,
            [instance_group_2_selfLink, instance_group_1_selfLink])
        original_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs(
            backend_service_name)
        backend_service_selfLink = operation['targetLink']
        ### start migration
        selfLink_executor = SelfLinkExecutor(self.compute,
                                             backend_service_selfLink,
                                             auto_subnetwork_name,
                                             auto_subnetwork_name)
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        new_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs(
            backend_service_name)

        # check backend service config
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                original_backend_service_configs, new_backend_service_configs))
        # check its backends
        new_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs(
            group_name_1)
        new_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs(
            group_name_2)
        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_1_configs,
                new_instance_template_1_configs))
        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_2_configs,
                new_instance_template_2_configs))

        # network changed
        self.assertTrue(
            check_instance_template_network(new_instance_template_1_configs,
                                            target_network_selfLink))

        self.assertTrue(
            check_instance_template_network(new_instance_template_2_configs,
                                            target_network_selfLink))

        print('Pass the current test')
    def testCustomSubnetworkExistsInOnlyOneRegion(self):
        """ A backend service has two instance groups from two regions as backends.

         Expectation: rollback will happen

        """
        ### create test resources
        target_subnetwork_name = 'subnetwork-only-in-region-1'
        target_subnetwork_selfLink = \
            self.google_api_interface_region_1.create_subnetwork_using_random_ip_range(
                target_subnetwork_name,
                self.test_resource_creator_region_1.network_selfLink
            )['targetLink']
        group_name_1 = 'end-to-end-test-managed-instance-group-1'
        operation = self.test_resource_creator_region_1.create_regional_managed_instance_group(
            self.test_resource_creator_region_1.
            legacy_instance_template_selfLink,
            group_name_1,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_1_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')
        original_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs(
            group_name_1)
        group_name_2 = 'end-to-end-test-managed-instance-group-2'
        operation = self.test_resource_creator_region_2.create_regional_managed_instance_group(
            self.test_resource_creator_region_2.
            legacy_instance_template_selfLink,
            group_name_2,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_2_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')
        original_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs(
            group_name_2)

        backend_service_name = 'end-to-end-test-backend-service'
        operation = self.test_resource_creator_region_1.create_global_backend_service(
            'sample_external_backend_service.json', backend_service_name,
            [instance_group_2_selfLink, instance_group_1_selfLink])
        original_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs(
            backend_service_name)
        backend_service_selfLink = operation['targetLink']
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute, backend_service_selfLink,
            self.test_resource_creator_region_1.network_name,
            target_subnetwork_name)
        with self.assertRaises(MigrationFailed):
            migration_handler = selfLink_executor.build_migration_handler()
            migration_handler.network_migration()
        ### check migration result
        new_backend_service_configs = self.google_api_interface_region_1.get_global_backend_service_configs(
            backend_service_name)

        # check backend service config
        self.assertTrue(
            resource_config_is_unchanged_including_network(
                original_backend_service_configs, new_backend_service_configs))
        # check its backends
        new_instance_template_1_configs = self.google_api_interface_region_1.get_multi_zone_instance_template_configs(
            group_name_1)
        new_instance_template_2_configs = self.google_api_interface_region_2.get_multi_zone_instance_template_configs(
            group_name_2)
        # configs are unchanged including network
        self.assertTrue(
            instance_template_config_is_unchanged(
                original_instance_template_1_configs,
                new_instance_template_1_configs))
        self.assertTrue(
            instance_template_config_is_unchanged(
                original_instance_template_2_configs,
                new_instance_template_2_configs))

        print('Pass the current test')
Esempio n. 28
0
    def testWithTargetHttpProxy(self):
        ### create test resources
        forwarding_rule_name = 'end-to-end-test-forwarding-rule'
        group_name_1 = 'end-to-end-test-managed-instance-group-1'
        operation = self.test_resource_creator.create_regional_managed_instance_group(
            self.test_resource_creator.legacy_instance_template_selfLink,
            group_name_1,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_1_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')

        original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)
        backend_service_name = 'end-to-end-test-backend-service'
        original_backend_selfLinks = [instance_group_1_selfLink]
        operation = self.test_resource_creator.create_global_backend_service(
            'sample_http_backend_service.json', backend_service_name,
            original_backend_selfLinks)
        original_backend_service_configs = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name)

        backend_service_selfLink = operation['targetLink']
        urlmap_selfLink = \
            self.test_resource_creator.create_urlmapping(backend_service_name,
                                                         backend_service_selfLink)[
                'targetLink']
        proxy_selfLink = self.test_resource_creator.create_http_target_proxy(
            backend_service_name, urlmap_selfLink)['targetLink']
        forwarding_rule_selfLink = \
            self.test_resource_creator.create_global_forwarding_rule_with_target(
                'sample_global_forwarding_rule.json',
                forwarding_rule_name,
                proxy_selfLink)['targetLink']
        original_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config(
            forwarding_rule_name)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            forwarding_rule_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        # check forwarding rule config
        new_forwarding_rule_config = self.google_api_interface.get_global_forwarding_rule_config(
            forwarding_rule_name)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_forwarding_rule_config, new_forwarding_rule_config))
        # check backend service config
        new_backend_service_configs = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_backend_service_configs, new_backend_service_configs))
        # check its backends
        new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)
        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_1_configs,
                new_instance_template_1_configs))
        # network changed
        self.assertTrue(
            check_instance_template_network(
                new_instance_template_1_configs,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))

        print('Pass the current test')
Esempio n. 29
0
    def testWithTargetHttpProxyRelatedToMultipleBackendServices(self):
        """ The urlMapping is related to two backend service.
        All the backend services will be migrated.
        """
        ### create test resrouces
        forwarding_rule_name = 'end-to-end-test-forwarding-rule'

        group_name_1 = 'end-to-end-test-managed-instance-group-1'
        operation = self.test_resource_creator.create_regional_managed_instance_group(
            self.test_resource_creator.legacy_instance_template_selfLink,
            group_name_1,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_1_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')
        original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)
        group_name_2 = 'end-to-end-test-managed-instance-group-2'
        operation = self.test_resource_creator.create_regional_managed_instance_group(
            self.test_resource_creator.legacy_instance_template_selfLink,
            group_name_2,
            'sample_multi_zone_managed_instance_group.json',
        )
        instance_group_2_selfLink = operation['targetLink'].replace(
            '/instanceGroupManagers/', '/instanceGroups/')
        original_instance_template_2_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_2)
        backend_service_name_1 = 'end-to-end-test-backend-service-1'
        original_backend_selfLinks_1 = [instance_group_1_selfLink]
        backend_service_selfLink_1 = \
            self.test_resource_creator.create_global_backend_service(
                'sample_http_backend_service.json',
                backend_service_name_1, original_backend_selfLinks_1)[
                'targetLink']

        backend_service_name_2 = 'end-to-end-test-backend-service-2'
        original_backend_selfLinks_2 = [instance_group_2_selfLink]
        backend_service_selfLink_2 = \
            self.test_resource_creator.create_global_backend_service(
                'sample_http_backend_service.json',
                backend_service_name_2, original_backend_selfLinks_2)[
                'targetLink']

        backend_service_selfLinks = [
            backend_service_selfLink_1, backend_service_selfLink_2
        ]
        urlmap_name = 'end-to-ent-test-urlmapping'
        urlmap_selfLink = \
            self.test_resource_creator.create_urlmapping_using_two_backend_service(
                urlmap_name,
                backend_service_selfLinks)[
                'targetLink']
        proxy_selfLink = self.test_resource_creator.create_http_target_proxy(
            urlmap_name, urlmap_selfLink)['targetLink']

        forwarding_rule_selfLink = \
            self.test_resource_creator.create_global_forwarding_rule_with_target(
                'sample_global_forwarding_rule.json',
                forwarding_rule_name,
                proxy_selfLink)['targetLink']

        original_backend_service_configs_1 = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name_1)

        original_backend_service_configs_2 = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name_2)

        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            forwarding_rule_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        new_backend_service_configs_1 = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name_1)
        new_backend_service_configs_2 = self.google_api_interface.get_global_backend_service_configs(
            backend_service_name_2)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_backend_service_configs_1,
                new_backend_service_configs_1))
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_backend_service_configs_2,
                new_backend_service_configs_2))

        # check its backends
        new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)
        new_instance_template_2_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_2)

        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_1_configs,
                new_instance_template_1_configs))
        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_2_configs,
                new_instance_template_2_configs))

        # network changed
        self.assertTrue(
            check_instance_template_network(
                new_instance_template_1_configs,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))
        self.assertTrue(
            check_instance_template_network(
                new_instance_template_2_configs,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))

        print('Pass the current test')
    def testInstancesAndManagedInstanceGroupsMixedBackends(self):
        """ The target pool served by both instances and managed instance groups
        """
        ### create test resources
        group_name_1 = 'end-to-end-test-managed-instance-group-1'
        self.test_resource_creator.create_regional_managed_instance_group(
            self.test_resource_creator.legacy_instance_template_selfLink,
            group_name_1,
            'sample_multi_zone_managed_instance_group.json',
        )
        original_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)
        instance_name_1 = 'end-to-end-test-instance-1'
        operation = self.test_resource_creator.create_instance_using_template(
            instance_name_1,
            self.test_resource_creator.legacy_instance_template_selfLink)
        instance_1_selfLink = operation['targetLink']
        original_instance_1_config = self.google_api_interface.get_instance_configs(
            instance_name_1)
        target_pool_name = 'end-to-end-test-target-pool'
        operation = self.test_resource_creator.create_target_pool_with_health_check(
            'sample_target_pool_with_no_instance.json',
            target_pool_name, [group_name_1], [instance_1_selfLink],
            health_check_selfLink=None)
        target_pool_selfLink = operation['targetLink']
        # time allowance to let the instance groups create instances
        time.sleep(10)
        ### start migration
        selfLink_executor = SelfLinkExecutor(
            self.compute,
            target_pool_selfLink,
            self.test_resource_creator.network_name,
            self.test_resource_creator.subnetwork_name,
        )
        migration_handler = selfLink_executor.build_migration_handler()
        migration_handler.network_migration()
        ### check migration result
        new_instance_template_1_configs = self.google_api_interface.get_multi_zone_instance_template_configs(
            group_name_1)
        self.assertTrue(
            instance_template_config_is_unchanged_except_for_network_and_name(
                original_instance_template_1_configs,
                new_instance_template_1_configs))

        new_instance_1_config = self.google_api_interface.get_instance_configs(
            instance_name_1)
        self.assertTrue(
            resource_config_is_unchanged_except_for_network(
                original_instance_1_config, new_instance_1_config))
        # network changed
        self.assertTrue(
            check_instance_template_network(
                new_instance_template_1_configs,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))
        self.assertTrue(
            check_instance_network(
                new_instance_1_config,
                self.test_resource_creator.network_selfLink,
                self.test_resource_creator.subnetwork_selfLink))
        print('Pass the current test')