예제 #1
0
    async def run_trigger(
        self,
        name: str,
        tag_name: str = None,
        branch_name: str = None,
        commit_sha: str = None,
        project_id: str = None,
    ) -> TriggerType:
        params = [tag_name, branch_name, commit_sha]
        if len([x for x in params if x]) != 1:
            raise exceptions.ValidationError(
                "Only one of {tag_name, branch_name, commit_sha} must be provided"
            )

        event = cloudbuild_v1.RepoSource(
            project_id=project_id or self.project_id,
            tag_name=tag_name,
            branch_name=branch_name,
            commit_sha=commit_sha,
        )
        response = self.client.run_build_trigger(
            trigger_id=name,
            source=event,
            project_id=project_id or self.project_id,
        )
        return response
예제 #2
0
    async def execute(
        self,
        sql: str,
        params: Dict[str, Any] = None,
        destination_table_name: str = None,
        destination_dataset_name: str = None,
        destination_project: str = None,
        truncate: bool = None,
    ):
        job_config = bigquery.QueryJobConfig()
        if destination_table_name or destination_dataset_name:
            if not destination_table_name and destination_dataset_name:
                raise exceptions.ValidationError(
                    "Both destination_dataset_name and destination_table_name must be provided."
                )
            destination_project = destination_project or self.project_id
            destination_dataset = destination_dataset_name
            destination = f"{destination_project}.{destination_dataset}.{destination_table_name}"
            job_config.destination = destination
            if truncate:
                job_config.write_disposition = bigquery.WriteDisposition.WRITE_TRUNCATE
            elif truncate is False:
                job_config.write_disposition = bigquery.WriteDisposition.WRITE_APPEND

        if params:
            query_params = [
                _BigQueryParam.parse(key, value)
                for key, value in params.items()
            ]
            job_config.query_parameters = query_params

        query_job = self.client.query(sql, job_config=job_config)
        return self._wait_for_job(job=query_job)
예제 #3
0
 def _get_event_param():
     valid_events = {
         "trigger_template": cloudbuild_v1.RepoSource,
         "github": cloudbuild_v1.GitHubEventsConfig,
     }
     for key, klass in valid_events.items():
         if isinstance(event, klass):
             return key
     raise exceptions.ValidationError(
         f"Unsupported event type {event.__class__.__name__,}")
예제 #4
0
    def _change_record(
        self,
        action: str,
        zone_name: str,
        zone_dns: str,
        name: str,
        record_type: RecordType,
        record_data: List[str] = None,
        ttl: int = 60 * 60,
        wait: bool = True,
    ) -> dns.ResourceRecordSet:
        zone = self._build_zone(name=zone_name, dns_name=zone_dns)

        parsed_record_data = ([
            RecordType.prepare(record_type=record_type, record=record)
            for record in record_data
        ] if record_data else [])

        record_set = zone.resource_record_set(
            name=RecordType.build_dns_name(name=name),
            record_type=record_type.name,
            ttl=ttl,
            rrdatas=parsed_record_data,
        )
        changes = zone.changes()

        if action == "add":
            changes.add_record_set(record_set)
        elif action == "delete":
            changes.delete_record_set(record_set)
        else:
            raise exceptions.ValidationError(
                f"Action {action} is not support for record sets")

        try:
            changes.create()
        except BadRequest as exc:
            if "is only permitted to have one record" in exc.message:
                raise exceptions.AlreadyExists(exc.message) from exc
            raise
        except Conflict as exc:
            raise exceptions.AlreadyExists(exc.message) from exc

        if wait:
            while changes.status != "done":
                print("Waiting for changes to complete")
                time.sleep(60)
                changes.reload()
        return record_set
예제 #5
0
    def reset_password(self, email: str, new_password: str, old_password: str = None, oob_code: str = None):
        data = {
            "newPassword": new_password,
            "email": email,
        }

        if oob_code:
            data["oobCode"] = oob_code
        elif old_password:
            data["oldPassword"] = old_password
        else:
            raise exceptions.ValidationError("Either `old_password` or `oob_code` must be provided")

        response = self._execute(method=self.client.accounts().resetPassword, body=data)
        return response
예제 #6
0
    def lookup(self, email: str = None, phone_number: str = None, project_id: str = None) -> Iterator[User]:
        if not email and not phone_number:
            raise exceptions.ValidationError("Either `email` or `phone_number` must be provided")

        data = {
            "target_project_id": project_id or self.project_id,
        }
        if email:
            data["email"] = email
        if phone_number:
            if not phone_number.startswith("+"):
                phone_number = f"+{phone_number}"
            data["phone_number"] = phone_number

        response = self._execute(method=self.client.accounts().lookup, body=data)
        for item in response.get("users", []):
            yield User.create(data=item)
예제 #7
0
    def _get_type(cls, variable: Any) -> str:
        TYPES = {  # pylint: disable=invalid-name
            "int": "INT64",
            "str": "STRING",
            "datetime": "DATETIME",
            "date": "DATE",
            "bool": "BOOL",
            "float": "FLOAT64",
            "Decimal": "FLOAT64",
        }

        python_type = type(variable).__name__
        param_type = TYPES.get(python_type, None)
        if param_type is None:
            raise exceptions.ValidationError(
                f"Parameter with type {param_type} not supported")
        return param_type
예제 #8
0
    def _build_filter(self, key: str,
                      value: Any) -> List[Tuple[str, str, Any]]:
        operator = None

        *field_parts, lookup = key.split("__")
        if lookup:
            try:
                operator = self.lookup_operators[lookup]

                if callable(operator):
                    field_name = ".".join(field_parts)
                    return operator(field_name, value)
            except KeyError:
                field_parts.append(lookup)

        if len(field_parts) > 1 and field_parts[0] not in self.fields:
            raise exceptions.ValidationError(
                f"{field_parts[0]} is not a valid field. Excepted one of {' | '.join(self.fields)}"
            )

        field_name = ".".join(field_parts)
        return [(field_name, (operator or "="), value)]
예제 #9
0
    def create_or_update_resource(
        self,
        data: Dict[str, Any],
        resource_type: str,
        store_name: str,
        dataset_name: str,
        query: Dict[str, Any] = None,
        project_id: str = None,
        location: str = None,
    ) -> ResourceType:
        if not query:
            identifiers = data.get("identifier")
            if not identifiers:
                raise exceptions.ValidationError(
                    "Either `query` or identifiers must be provided to create-or-update"
                )

            query_values = []
            for identifier in identifiers:
                query_values.append(identifier["system"])
                query_values.append(identifier["value"])
            query = {"identifier": "|".join(query_values)}

        parent = self._store_path(
            name=store_name,
            dataset_name=dataset_name,
            project_id=project_id,
            location=location,
        )
        url = f"{self._base_url}/{parent}/fhir/{resource_type}"

        params = dict(
            url=url,
            params=query,
            headers={"Content-Type": "application/fhir+json;charset=utf-8"},
            json=data,
        )

        return self._execute(method=self._session.put, **params)
예제 #10
0
    def build_repo_source(
        cls,
        name,
        branch: str = None,
        commit: str = None,
        tag: str = None,
        directory: str = None,
        project_id: str = None,
    ) -> Dict[str, Any]:

        valid_references = [ref for ref in [branch, commit, tag] if ref]
        if len(valid_references) > 1:
            raise exceptions.ValidationError(
                "Only one of branch|tag|commit must be provided")

        if branch:
            ref = f"moveable-aliases/{branch}"
        elif commit:
            ref = f"revisions{commit}"
        elif tag:
            ref = f"fixed-aliases/{tag}"
        else:
            ref = "moveable-aliases/master"

        path = f"/paths/{directory}" if directory else ""

        if "/" in name:
            # Assume it's a repository integrated through Github App
            org, repo = name.split("/")
            repo_name = f"github_{org}_{repo}"
        else:
            repo_name = name

        REPO_SOURCE_URL = "https://source.developers.google.com"  # pylint: disable=invalid-name
        url = f"{REPO_SOURCE_URL}/projects/{project_id}/repos/{repo_name}/{ref}{path}"
        repo = dict(url=url)

        return repo