コード例 #1
0
 def __init__(
     self,
     *,
     location: str,
     body: Dict,
     project_id: Optional[str] = None,
     gcp_conn_id: str = 'google_cloud_default',
     api_version: str = 'v1',
     zip_path: Optional[str] = None,
     validate_body: bool = True,
     impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
     **kwargs,
 ) -> None:
     self.project_id = project_id
     self.location = location
     self.body = body
     self.gcp_conn_id = gcp_conn_id
     self.api_version = api_version
     self.zip_path = zip_path
     self.zip_path_preprocessor = ZipPathPreprocessor(body, zip_path)
     self._field_validator = None  # type: Optional[GcpBodyFieldValidator]
     self.impersonation_chain = impersonation_chain
     if validate_body:
         self._field_validator = GcpBodyFieldValidator(
             CLOUD_FUNCTION_VALIDATION, api_version=api_version)
     self._validate_inputs()
     super().__init__(**kwargs)
コード例 #2
0
    def test_validate_should_interpret_optional_clause(self):
        specification = [dict(name="name", allow_empty=False, optional=True)]
        body = {}

        validator = GcpBodyFieldValidator(specification, 'v1')

        self.assertIsNone(validator.validate(body))
コード例 #3
0
ファイル: compute.py プロジェクト: youngyjd/incubator-airflow
 def __init__(
     self,
     *,
     resource_id: str,
     body_patch: dict,
     project_id: Optional[str] = None,
     request_id: Optional[str] = None,
     gcp_conn_id: str = 'google_cloud_default',
     api_version: str = 'v1',
     validate_body: bool = True,
     impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
     **kwargs,
 ) -> None:
     self.body_patch = body_patch
     self.request_id = request_id
     self._field_validator = None  # Optional[GcpBodyFieldValidator]
     if 'name' not in self.body_patch:
         raise AirflowException(
             f"The body '{body_patch}' should contain at least name for the new operator "
             f"in the 'name' field")
     if validate_body:
         self._field_validator = GcpBodyFieldValidator(
             GCE_INSTANCE_TEMPLATE_VALIDATION_PATCH_SPECIFICATION,
             api_version=api_version)
     self._field_sanitizer = GcpBodyFieldSanitizer(
         GCE_INSTANCE_TEMPLATE_FIELDS_TO_SANITIZE)
     super().__init__(
         project_id=project_id,
         zone='global',
         resource_id=resource_id,
         gcp_conn_id=gcp_conn_id,
         api_version=api_version,
         impersonation_chain=impersonation_chain,
         **kwargs,
     )
コード例 #4
0
    def test_validate_should_allow_type_and_optional_in_a_spec(self):
        specification = [dict(name="labels", optional=True, type="dict")]
        body = {"labels": {}}

        validator = GcpBodyFieldValidator(specification, 'v1')

        assert validator.validate(body) is None
コード例 #5
0
    def test_validate_should_not_raise_exception_if_field_and_body_are_both_empty(self):
        specification = []
        body = {}

        validator = GcpBodyFieldValidator(specification, 'v1')

        assert validator.validate(body) is None
コード例 #6
0
    def test_validate_should_interpret_dict_type(self):
        specification = [dict(name="labels", optional=True, type="dict")]
        body = {"labels": {"one": "value"}}

        validator = GcpBodyFieldValidator(specification, 'v1')

        assert validator.validate(body) is None
コード例 #7
0
ファイル: compute.py プロジェクト: youngyjd/incubator-airflow
 def __init__(
     self,
     *,
     zone: str,
     resource_id: str,
     body: dict,
     project_id: Optional[str] = None,
     gcp_conn_id: str = 'google_cloud_default',
     api_version: str = 'v1',
     validate_body: bool = True,
     impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
     **kwargs,
 ) -> None:
     self.body = body
     self._field_validator = None  # type: Optional[GcpBodyFieldValidator]
     if validate_body:
         self._field_validator = GcpBodyFieldValidator(
             SET_MACHINE_TYPE_VALIDATION_SPECIFICATION,
             api_version=api_version)
     super().__init__(
         project_id=project_id,
         zone=zone,
         resource_id=resource_id,
         gcp_conn_id=gcp_conn_id,
         api_version=api_version,
         impersonation_chain=impersonation_chain,
         **kwargs,
     )
コード例 #8
0
    def test_validate_should_validate_a_single_field(self):
        specification = [dict(name="name", allow_empty=False)]
        body = {"name": "bigquery"}

        validator = GcpBodyFieldValidator(specification, 'v1')

        assert validator.validate(body) is None
コード例 #9
0
    def test_validate_should_interpret_allow_empty_clause(self):
        specification = [dict(name="name", allow_empty=True)]
        body = {"name": ""}

        validator = GcpBodyFieldValidator(specification, 'v1')

        assert validator.validate(body) is None
コード例 #10
0
    def test_validate_should_interpret_optional_irrespective_of_allow_empty(self):
        specification = [dict(name="name", allow_empty=False, optional=True)]
        body = {"name": None}

        validator = GcpBodyFieldValidator(specification, 'v1')

        assert validator.validate(body) is None
コード例 #11
0
    def test_validate_should_raise_if_version_mismatch_is_found(self):
        specification = [dict(name="name", allow_empty=False, api_version='v2')]
        body = {"name": "value"}

        validator = GcpBodyFieldValidator(specification, 'v1')

        validator.validate(body)
コード例 #12
0
    def test_validate_should_raise_exception_name_attribute_is_missing_from_specs(self):
        specification = [dict(allow_empty=False)]
        body = {}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(KeyError):
            validator.validate(body)
コード例 #13
0
    def test_validate_should_fail_if_specification_is_none(self):
        specification = None
        body = {}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(TypeError):
            validator.validate(body)
コード例 #14
0
    def test_validate_should_fail_if_body_is_none(self):
        specification = []
        body = None

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(AttributeError):
            validator.validate(body)
コード例 #15
0
    def test_validate_should_interpret_union_with_one_field(self):
        specification = [
            dict(name="an_union", type="union", fields=[dict(name="variant_1", regexp=r'^.+$'),])
        ]
        body = {"variant_1": "abc", "variant_2": "def"}

        validator = GcpBodyFieldValidator(specification, 'v1')
        self.assertIsNone(validator.validate(body))
コード例 #16
0
    def test_validate_should_fail_if_there_is_no_nested_field_for_union(self):
        specification = [dict(name="an_union", type="union", optional=False, fields=[])]
        body = {}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(GcpValidationSpecificationException):
            validator.validate(body)
コード例 #17
0
    def test_validate_should_not_allow_both_type_and_allow_empty_in_a_spec(self):
        specification = [dict(name="labels", optional=True, type="dict", allow_empty=True)]
        body = {"labels": 1}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(GcpValidationSpecificationException):
            validator.validate(body)
コード例 #18
0
    def test_validate_should_fail_if_value_is_not_dict_as_per_specs(self):
        specification = [dict(name="labels", optional=True, type="dict")]
        body = {"labels": 1}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(GcpFieldValidationException):
            validator.validate(body)
コード例 #19
0
    def test_validate_should_raise_if_empty_clause_is_false(self):
        specification = [dict(name="name", allow_empty=False)]
        body = {"name": None}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(GcpFieldValidationException):
            validator.validate(body)
コード例 #20
0
    def test_validate_should_raise_exception_if_field_is_not_present(self):
        specification = [dict(name="name", allow_empty=False)]
        body = {}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(GcpFieldValidationException):
            validator.validate(body)
コード例 #21
0
    def test_validate_should_raise_exception_if_optional_clause_is_false_and_field_not_present(self):
        specification = [dict(name="name", allow_empty=False, optional=False)]
        body = {}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with self.assertRaises(GcpFieldValidationException):
            validator.validate(body)
コード例 #22
0
    def test_validate_should_fail_for_set_allow_empty_when_field_is_none(self):
        specification = [dict(name="name", allow_empty=True)]
        body = {"name": None}

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(GcpFieldValidationException):
            validator.validate(body)
コード例 #23
0
    def test_validate_should_fail_if_body_is_not_a_dict(self):
        specification = [dict(name="name", allow_empty=False)]
        body = [{"name": "bigquery"}]

        validator = GcpBodyFieldValidator(specification, 'v1')

        with pytest.raises(AttributeError):
            validator.validate(body)
コード例 #24
0
    def test_validate_should_validate_when_value_matches_regex(self):
        specification = [
            dict(name="an_union", type="union", fields=[dict(name="variant_1", regexp=r'[^a-z]'),])
        ]
        body = {"variant_1": "12"}

        validator = GcpBodyFieldValidator(specification, 'v1')
        self.assertIsNone(validator.validate(body))
コード例 #25
0
    def test_validate_should_fail_when_value_does_not_match_regex(self):
        specification = [
            dict(name="an_union", type="union", fields=[dict(name="variant_1", regexp=r'[^a-z]'),])
        ]
        body = {"variant_1": "abc"}

        validator = GcpBodyFieldValidator(specification, 'v1')
        with self.assertRaises(GcpFieldValidationException):
            validator.validate(body)
コード例 #26
0
    def test_validate_should_not_raise_if_custom_validation_is_true(self):
        def _int_equal_to_zero(value):
            if int(value) != 0:
                raise GcpFieldValidationException("The available memory has to be equal to 0")

        specification = [dict(name="availableMemoryMb", custom_validation=_int_equal_to_zero)]
        body = {"availableMemoryMb": 0}

        validator = GcpBodyFieldValidator(specification, 'v1')
        assert validator.validate(body) is None
コード例 #27
0
    def test_validate_should_fail_if_union_field_is_not_found(self):
        specification = [
            dict(
                name="an_union",
                type="union",
                optional=False,
                fields=[dict(name="variant_1", regexp=r'^.+$', optional=False, allow_empty=False),],
            )
        ]
        body = {}

        validator = GcpBodyFieldValidator(specification, 'v1')
        self.assertIsNone(validator.validate(body))
コード例 #28
0
    def test_validate_should_fail_if_both_field_of_union_is_present(self):
        specification = [
            dict(
                name="an_union",
                type="union",
                fields=[dict(name="variant_1", regexp=r'^.+$'), dict(name="variant_2", regexp=r'^.+$'),],
            )
        ]
        body = {"variant_1": "abc", "variant_2": "def"}

        validator = GcpBodyFieldValidator(specification, 'v1')
        with self.assertRaises(GcpFieldValidationException):
            validator.validate(body)
コード例 #29
0
    def test_validate_should_validate_group_of_specs(self):
        specification = [
            dict(name="name", allow_empty=False),
            dict(name="description", allow_empty=False, optional=True),
            dict(name="labels", optional=True, type="dict"),
            dict(
                name="an_union",
                type="union",
                fields=[
                    dict(name="variant_1", regexp=r'^.+$'),
                    dict(name="variant_2", regexp=r'^.+$', api_version='v1beta2'),
                    dict(name="variant_3", type="dict", fields=[dict(name="url", regexp=r'^.+$')]),
                    dict(name="variant_4"),
                ],
            ),
        ]
        body = {"variant_1": "abc", "name": "bigquery"}

        validator = GcpBodyFieldValidator(specification, 'v1')
        validator.validate(body)
コード例 #30
0
ファイル: cloud_sql.py プロジェクト: nsenno-dbr/airflow
 def _validate_body_fields(self) -> None:
     if self.validate_body:
         GcpBodyFieldValidator(CLOUD_SQL_EXPORT_VALIDATION, api_version=self.api_version).validate(
             self.body
         )