def test_template_field_default_and_non_valid_spec_name(self): default_template_config = "value with {{ non_exist.my_key }}" spec_config = {"spec": {"other": "linux"}, "user_spec": {}} expected_result = "" self.assertEqual( expected_result, FormUtils.template_field(default_template_config, spec_config))
def test_template_field_default_and_valid_spec_list(self): default_template_config = "value with {{ spec.os[0] }}" spec_config = {"spec": {"os": ["linux", "windows"]}, "user_spec": {}} expected_result = "value with linux" self.assertEqual( expected_result, FormUtils.template_field(default_template_config, spec_config))
def test_template_field_default_and_valid_spec_dict(self): default_template_config = "value with {{ spec.os['linux'] }}" spec_config = {"spec": {"os": {"linux": "ubuntu"}}, "user_spec": {}} expected_result = "value with ubuntu" self.assertEqual( expected_result, FormUtils.template_field(default_template_config, spec_config))
def test_template_field_default_no_spec(self): default_template_config = "my_default_value" spec_config = "" expected_result = "my_default_value" self.assertEqual( expected_result, FormUtils.template_field(default_template_config, spec_config))
def test_template_field_default_and_valid_spec_contains_integer(self): default_template_config = "value with {{ spec.os }}" spec_config = {"spec": {"os": 12}, "user_spec": {}} expected_result = "value with 12" self.assertEqual( expected_result, FormUtils.template_field(default_template_config, spec_config))
def test_get_available_fields(self): expected_result = { "name": "test-survey", "description": "test-survey-description", "spec": [{ "choices": "", "default": "", "max": 1024, "min": 0, "new_question": True, "question_description": "", "question_name": "String variable", "required": True, "type": "text", "variable": "text_variable" }] } self.assertEqual( expected_result, FormUtils.get_available_fields( job_template_survey=self.job_template_test.survey, operation_survey=self.create_operation_test.tower_survey_fields ))
def test_template_field_default_and_spec_with_filter(self): default_template_config = "{{ spec.multiple | join('\n') }}" spec_config = { "spec": { "multiple": ["value1", "value2"] }, "user_spec": {} } expected_result = "value1\nvalue2" self.assertEqual( expected_result, FormUtils.template_field(default_template_config, spec_config))
def test_apply_spec_template_to_survey_with_spec_variable(self): self.job_template_test.survey = { "name": "test-survey", "description": "test-survey-description", "spec": [{ "choices": "", "default": "this_was_the_default", "max": 1024, "min": 0, "new_question": True, "question_description": "", "question_name": "String variable", "required": True, "type": "text", "variable": "text_variable" }] } self.job_template_test.save() # test with en empty string target_field = TowerSurveyField.objects.get( name="text_variable", operation=self.create_operation_test) target_field.default = "{{ spec.os }}" target_field.save() admin_spec = {"os": "linux"} expected_result = { "name": "test-survey", "description": "test-survey-description", "spec": [{ "choices": "", "default": "linux", "max": 1024, "min": 0, "new_question": True, "question_description": "", "question_name": "String variable", "required": True, "type": "text", "variable": "text_variable" }] } self.assertEqual( expected_result, FormUtils.apply_spec_template_to_survey( job_template_survey=self.job_template_test.survey, operation_survey=self.create_operation_test. tower_survey_fields, admin_spec=admin_spec))
def test_template_field_default_and_valid_spec_and_user_spec(self): default_template_config = "value with {{ spec.os }} and {{ user_spec.key1 }}" spec_config = { "spec": { "os": "linux" }, "user_spec": { "key1": "value1" } } expected_result = "value with linux and value1" self.assertEqual( expected_result, FormUtils.template_field(default_template_config, spec_config))
def __init__(self, *args, **kwargs): context = kwargs.get('context', None) self.view = context.get('view', None) self.service_id = self.view.kwargs.get('pk', None) self.request = context.get('request', None) super(ServiceRequestSerializer, self).__init__(*args, **kwargs) if self.service_id is not None: self.service = get_object_or_404( Service.objects.filter(enabled=True), id=self.service_id) # get the create operation of this service self.create_operation = Operation.objects.get( service=self.service, type=OperationType.CREATE) # get all field that are not disabled by the admin purged_survey = FormUtils.get_available_fields( job_template_survey=self.create_operation.job_template.survey, operation_survey=self.create_operation.tower_survey_fields) self.fields['fill_in_survey'] = DynamicSurveySerializer( fill_in_survey=purged_survey)
def __init__(self, *args, **kwargs): context = kwargs.get('context', None) self.view = context.get('view', None) self.request = context.get('request', None) operation_id = self.view.kwargs.get('operation_id', None) instance_id = self.view.kwargs.get('instance_id', None) super(OperationRequestSerializer, self).__init__(*args, **kwargs) if operation_id is not None and instance_id is not None: allowed_operations = Operation.objects.filter( enabled=True, type__in=[OperationType.UPDATE, OperationType.DELETE]) self.target_operation = get_object_or_404(allowed_operations, id=operation_id) self.target_instance = get_object_or_404(get_objects_for_user( self.request.user, 'service_catalog.view_instance'), id=instance_id) # get all field that are not disabled by the admin purged_survey = FormUtils.get_available_fields( job_template_survey=self.target_operation.job_template.survey, operation_survey=self.target_operation.tower_survey_fields) self.fields['fill_in_survey'] = DynamicSurveySerializer( fill_in_survey=purged_survey)