Ejemplo n.º 1
0
    def test_services_list_command_spatial(self, mock_mtd, mock_spatial,
                                           mock_pretty_output, mock_print):
        """
        Test for services_list_command
        Only spatial is set
        :param mock_mtd:  mock for model_to_dict to return a dictionary
        :param mock_spatial:  mock for SpatialDatasetService
        :param mock_pretty_output:  mock for pretty_output text
        :param mock_stdout:  mock for text written with print statements
        :return:
        """
        mock_mtd.return_value = self.my_dict
        mock_args = mock.MagicMock()
        mock_args.spatial = True
        mock_args.persistent = False
        mock_args.dataset = False
        mock_args.wps = False
        mock_spatial.objects.order_by('id').all.return_value = [
            mock.MagicMock(),
            mock.MagicMock(),
            mock.MagicMock()
        ]

        services_list_command(mock_args)

        # Check expected pretty_output
        po_call_args = mock_pretty_output().__enter__().write.call_args_list
        self.assertEqual(2, len(po_call_args))
        self.assertIn('Spatial Dataset Services:', po_call_args[0][0][0])
        self.assertIn('ID', po_call_args[1][0][0])
        self.assertIn('Name', po_call_args[1][0][0])
        self.assertNotIn('Host', po_call_args[1][0][0])
        self.assertNotIn('Port', po_call_args[1][0][0])
        self.assertIn('Endpoint', po_call_args[1][0][0])
        self.assertIn('Public Endpoint', po_call_args[1][0][0])
        self.assertIn('API Key', po_call_args[1][0][0])

        # Check text written with Python's print
        rts_call_args = mock_print.call_args_list

        self.assertIn(self.my_dict['id'], rts_call_args[2][0][0])
        self.assertIn(self.my_dict['name'], rts_call_args[2][0][0])
        self.assertNotIn(self.my_dict['host'], rts_call_args[2][0][0])
        self.assertNotIn(self.my_dict['port'], rts_call_args[2][0][0])
        self.assertIn(self.my_dict['endpoint'], rts_call_args[2][0][0])
        self.assertIn(self.my_dict['public_endpoint'], rts_call_args[2][0][0])
        self.assertIn(self.my_dict['apikey'], rts_call_args[2][0][0])
def get_service_options(service_type):
    # # List existing services
    args = Namespace()

    for conf in ['spatial', 'persistent', 'wps', 'dataset']:
        setattr(args, conf, False)

    setattr(args, service_type, True)

    existing_services_list = services_list_command(args)[0]
    existing_services = []

    if (len(existing_services_list)):
        for service in existing_services_list:
            existing_services.append({"name": service.name, "id": service.id})
    return existing_services
Ejemplo n.º 3
0
def run_interactive_services(app_name):
    write_msg(
        'Running Interactive Service Mode. '
        'Any configuration options in services.yml or portal_config.yml will be ignored...'
    )
    write_msg('Hit return at any time to skip a step.')

    app_settings = get_app_settings(app_name)

    # In the case the app isn't installed, has no settings, or it is an extension,
    # skip configuring services/settings
    if not app_settings:
        write_msg(
            f'No settings found for app "{app_name}". Skipping interactive configuration...'
        )
        return

    unlinked_settings = app_settings['unlinked_settings']

    for setting in unlinked_settings:
        valid = False
        configure_text = "Configuring {}".format(setting.name)
        star_out = '*' * len(configure_text)
        write_msg(f"\n{star_out}\n{configure_text}\n{star_out}")
        write_msg(f"Type: {setting.__class__.__name__}\n"
                  f"Description: {setting.description}\n"
                  f"Required: {setting.required}")
        if hasattr(setting, 'value'):
            while not valid:
                write_msg(
                    '\nEnter the desired value for the current custom setting: {}'
                    .format(setting.name))
                try:
                    value = get_interactive_input()
                    if value != "":
                        try:
                            setting.value = value
                            setting.clean()
                            setting.save()
                            valid = True
                            write_success(
                                "{} successfully set with value: {}.".format(
                                    setting.name, value))
                        except ValidationError:
                            write_error(
                                "Incorrect value type given for custom setting '{}'. Please try again"
                                .format(setting.name))

                    else:
                        write_msg("Skipping setup of {}".format(setting.name))
                        valid = True

                except (KeyboardInterrupt, SystemExit):
                    write_msg('\nInstall Command cancelled.')
                    exit(0)
        else:
            # List existing services
            args = Namespace()

            for conf in ['spatial', 'persistent', 'wps', 'dataset']:
                setattr(args, conf, False)

            setattr(args, get_setting_type(setting), True)
            services = services_list_command(args)[0]

            if len(services) <= 0:
                write_warning(
                    'No compatible services found. See:\n\n  tethys services create {} -h\n'
                    .format(get_setting_type(setting)))
                continue

            while not valid:
                write_msg(
                    '\nEnter the service ID/Name to link to the current service setting: {}.'
                    .format(setting.name))
                try:
                    service_id = get_interactive_input()
                    if service_id != "":
                        try:
                            setting_type = get_setting_type_from_setting(
                                setting)
                            service_type = get_service_type_from_setting(
                                setting)
                        except RuntimeError as e:
                            write_error(str(e) + ' Skipping...')
                            break

                        # Validate the given service id
                        valid_service = validate_service_id(
                            service_type, service_id)

                        if valid_service:
                            link_service_to_app_setting(
                                service_type, service_id, app_name,
                                setting_type, setting.name)

                            valid = True
                        else:
                            write_error(
                                'Incorrect service ID/Name. Please try again.')

                    else:
                        write_msg("Skipping setup of {}".format(setting.name))
                        valid = True

                except (KeyboardInterrupt, SystemExit):
                    write_msg('\nInstall Command cancelled.')
                    exit(0)