Exemplo n.º 1
0
    def test_retrieve_image_raise_exception_when_format_name_not_found(
            self, mock_remove_special_chars):
        # given
        api = Api("url",
                  username="******",
                  password="******",
                  headers=None,
                  disable_ssl_certificate_validation=False,
                  timeout=constants.HTTP_TIMEOUT)
        target_format = uforge.TargetFormat()
        image_format = uforge.ImageFormat()
        image_format.name = "vcenter"
        target_format.format = image_format

        builder = {"hardwareSettings": {"memory": 512, "hwType": 4}}

        mock_remove_special_chars.return_value = "vcenternotfound"

        # when
        with self.assertRaises(Exception) as e:
            migration_utils.retrieve_image(builder, target_format, api,
                                           "login")

        # then
        self.assertTrue(
            "TargetFormat type is unsupported: vcenter" in e.exception)
Exemplo n.º 2
0
    def test_retrieve_image_raise_exception_when_format_name_not_found(self, mock_remove_special_chars):
        # given
        api = Api("url", username="******", password="******", headers=None,
                  disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        target_format = uforge.TargetFormat()
        image_format = uforge.ImageFormat()
        image_format.name = "vcenter"
        target_format.format = image_format

        builder = {
            "hardwareSettings": {
                "memory": 512,
                "hwType": 4
            }
        }

        mock_remove_special_chars.return_value = "vcenternotfound"

        # when
        with self.assertRaises(Exception) as e:
            migration_utils.retrieve_image(builder, target_format, api, "login")

        # then
        self.assertTrue("TargetFormat type is unsupported: vcenter" in e.exception)
Exemplo n.º 3
0
    def do_launch(self, args):
        try:
            # add arguments
            do_parser = self.arg_launch()
            do_args = do_parser.parse_args(shlex.split(args))

            # if the help command is called, parse_args returns None object
            if not do_args:
                return 2

            try:
                uforge_password = self.password
            except AttributeError:
                printer.out("Using API keys with command 'hammr migration launch' is not yet supported. Please use password.", printer.ERROR)
                return 2

            migration_config = migration_utils.retrieve_migration_configuration(do_args.file)
            target_format = migration_utils.retrieve_target_format(self.api, self.login, migration_config["target"]["builder"]["type"])
            image = migration_utils.retrieve_image(migration_config["target"]["builder"], target_format, self.api, self.login)
            if image is None:
                return 2
            cred_account = migration_utils.retrieve_account(self.api, self.login, migration_config["target"]["builder"]["account"]["name"])
            publish_image = migration_utils.build_publish_image(migration_config["target"]["builder"], target_format, cred_account)
            if publish_image is None:
                return 2

            migration = self.create_migration(migration_config["name"], migration_config["os"], target_format.name, image, publish_image)
            self.api.Users(self.login).Migrations.Create(body=migration, element_name="ns1:migration")

            local_uforge_migration_path = hammr_utils.download_binary_in_local_temp_dir(self.api, constants.TMP_WORKING_DIR, constants.URI_MIGRATION_BINARY, constants.MIGRATION_BINARY_NAME)

            self.upload_and_launch_migration_binary(self.login, uforge_password, migration_config, local_uforge_migration_path, self.api.getUrl())

            # delete temp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)

            printer.out("Migration launched successfully, please go to the platform to follow steps of the migration.", printer.OK)

        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_launch()
        except Exception as e:
            if hammr_utils.is_uforge_exception(e):
                return hammr_utils.handle_uforge_exception(e)
            printer.out(str(e), printer.ERROR)

        return 0
Exemplo n.º 4
0
    def test_retrieve_image_return_the_image_created(self):
        # given
        api = Api("url",
                  username="******",
                  password="******",
                  headers=None,
                  disable_ssl_certificate_validation=False,
                  timeout=constants.HTTP_TIMEOUT)
        target_format = uforge.TargetFormat()
        image_format = uforge.ImageFormat()
        image_format.name = "vcenter"
        target_format.format = image_format

        builder = {"hardwareSettings": {"memory": 512, "hwType": 4}}

        # when
        image_retrieved = migration_utils.retrieve_image(
            builder, target_format, api, "login")

        # then
        self.assertEqual(image_retrieved.installProfile.memorySize, 512)
        self.assertEqual(image_retrieved.installProfile.hwType, "4")
        self.assertFalse(image_retrieved.compress)
Exemplo n.º 5
0
    def test_retrieve_image_return_the_image_created(self):
        # given
        api = Api("url", username="******", password="******", headers=None,
                    disable_ssl_certificate_validation=False, timeout=constants.HTTP_TIMEOUT)
        target_format = uforge.TargetFormat()
        image_format = uforge.ImageFormat()
        image_format.name = "vcenter"
        target_format.format = image_format

        builder = {
            "hardwareSettings": {
                "memory": 512,
                "hwType": 4
            }
        }

        # when
        image_retrieved = migration_utils.retrieve_image(builder, target_format, api, "login")

        # then
        self.assertEqual(image_retrieved.installProfile.memorySize, 512)
        self.assertEqual(image_retrieved.installProfile.hwType, "4")
        self.assertFalse(image_retrieved.compress)
Exemplo n.º 6
0
    def do_launch(self, args):
        try:
            # add arguments
            do_parser = self.arg_launch()
            do_args = do_parser.parse_args(shlex.split(args))

            # if the help command is called, parse_args returns None object
            if not do_args:
                return 2

            try:
                uforge_password = self.password
            except AttributeError:
                printer.out(
                    "Using API keys with command 'hammr migration launch' is not yet supported. Please use password.",
                    printer.ERROR)
                return 2

            migration_config = migration_utils.retrieve_migration_configuration(
                do_args.file)
            target_format = migration_utils.retrieve_target_format(
                self.api, self.login,
                migration_config["target"]["builder"]["type"])
            image = migration_utils.retrieve_image(
                migration_config["target"]["builder"], target_format, self.api,
                self.login)
            if image is None:
                return 2
            cred_account = migration_utils.retrieve_account(
                self.api, self.login,
                migration_config["target"]["builder"]["account"]["name"])
            publish_image = migration_utils.build_publish_image(
                migration_config["target"]["builder"], target_format,
                cred_account)
            if publish_image is None:
                return 2

            migration = self.create_migration(migration_config["name"],
                                              migration_config["os"],
                                              target_format.name, image,
                                              publish_image)
            self.api.Users(self.login).Migrations.Create(
                body=migration, element_name="ns1:migration")

            local_uforge_migration_path = hammr_utils.download_binary_in_local_temp_dir(
                self.api, constants.TMP_WORKING_DIR,
                constants.URI_MIGRATION_BINARY,
                constants.MIGRATION_BINARY_NAME)

            self.upload_and_launch_migration_binary(
                self.login, uforge_password, migration_config,
                local_uforge_migration_path, self.api.getUrl())

            # delete temp dir
            shutil.rmtree(constants.TMP_WORKING_DIR)

            printer.out(
                "Migration launched successfully, please go to the platform to follow steps of the migration.",
                printer.OK)

        except ArgumentParserError as e:
            printer.out("ERROR: In Arguments: " + str(e), printer.ERROR)
            self.help_launch()
        except Exception as e:
            if hammr_utils.is_uforge_exception(e):
                return hammr_utils.handle_uforge_exception(e)
            printer.out(str(e), printer.ERROR)

        return 0