Пример #1
0
def test_get_rest_flattened_error():
    client = DiskTypesClient(
        credentials=ga_credentials.AnonymousCredentials(), )

    # Attempting to call a method with both a request object and flattened
    # fields is an error.
    with pytest.raises(ValueError):
        client.get(
            compute.GetDiskTypeRequest(),
            project="project_value",
            zone="zone_value",
            disk_type="disk_type_value",
        )
Пример #2
0
        def __call__(
            self,
            request: compute.GetDiskTypeRequest,
            *,
            retry: OptionalRetry = gapic_v1.method.DEFAULT,
            timeout: float = None,
            metadata: Sequence[Tuple[str, str]] = (),
        ) -> compute.DiskType:
            r"""Call the get method over HTTP.

            Args:
                request (~.compute.GetDiskTypeRequest):
                    The request object. A request message for DiskTypes.Get.
                See the method description for details.

                retry (google.api_core.retry.Retry): Designation of what errors, if any,
                    should be retried.
                timeout (float): The timeout for this request.
                metadata (Sequence[Tuple[str, str]]): Strings which should be
                    sent along with the request as metadata.

            Returns:
                ~.compute.DiskType:
                    Represents a Disk Type resource. Google Compute Engine
                has two Disk Type resources: \*
                `Regional </compute/docs/reference/rest/v1/regionDiskTypes>`__
                \* `Zonal </compute/docs/reference/rest/v1/diskTypes>`__
                You can choose from a variety of disk types based on
                your needs. For more information, read Storage options.
                The diskTypes resource represents disk types for a zonal
                persistent disk. For more information, read Zonal
                persistent disks. The regionDiskTypes resource
                represents disk types for a regional persistent disk.
                For more information, read Regional persistent disks.

            """

            http_options = [
                {
                    "method": "get",
                    "uri": "/compute/v1/projects/{project}/zones/{zone}/diskTypes/{disk_type}",
                },
            ]

            request_kwargs = compute.GetDiskTypeRequest.to_dict(request)
            transcoded_request = path_template.transcode(http_options, **request_kwargs)

            uri = transcoded_request["uri"]
            method = transcoded_request["method"]

            # Jsonify the query params
            query_params = json.loads(
                compute.GetDiskTypeRequest.to_json(
                    compute.GetDiskTypeRequest(transcoded_request["query_params"]),
                    including_default_value_fields=False,
                    use_integers_for_enums=False,
                )
            )

            query_params.update(self._get_unset_required_fields(query_params))

            # Send the request
            headers = dict(metadata)
            headers["Content-Type"] = "application/json"
            response = getattr(self._session, method)(
                # Replace with proper schema configuration (http/https) logic
                "https://{host}{uri}".format(host=self._host, uri=uri),
                timeout=timeout,
                headers=headers,
                params=rest_helpers.flatten_query_params(query_params),
            )

            # In case of error, raise the appropriate core_exceptions.GoogleAPICallError exception
            # subclass.
            if response.status_code >= 400:
                raise core_exceptions.from_http_response(response)
            # Return the response
            return compute.DiskType.from_json(
                response.content, ignore_unknown_fields=True
            )
Пример #3
0
    def get(
            self,
            request: compute.GetDiskTypeRequest = None,
            *,
            project: str = None,
            zone: str = None,
            disk_type: str = None,
            retry: retries.Retry = gapic_v1.method.DEFAULT,
            timeout: float = None,
            metadata: Sequence[Tuple[str, str]] = (),
    ) -> compute.DiskType:
        r"""Returns the specified disk type. Gets a list of
        available disk types by making a list() request.

        Args:
            request (google.cloud.compute_v1.types.GetDiskTypeRequest):
                The request object. A request message for DiskTypes.Get.
                See the method description for details.
            project (str):
                Project ID for this request.
                This corresponds to the ``project`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.
            zone (str):
                The name of the zone for this
                request.

                This corresponds to the ``zone`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.
            disk_type (str):
                Name of the disk type to return.
                This corresponds to the ``disk_type`` field
                on the ``request`` instance; if ``request`` is provided, this
                should not be set.
            retry (google.api_core.retry.Retry): Designation of what errors, if any,
                should be retried.
            timeout (float): The timeout for this request.
            metadata (Sequence[Tuple[str, str]]): Strings which should be
                sent along with the request as metadata.

        Returns:
            google.cloud.compute_v1.types.DiskType:
                Represents a Disk Type resource.

                   Google Compute Engine has two Disk Type resources:

                   -  [Regional](/compute/docs/reference/rest/{$api_version}/regionDiskTypes)
                      \*
                      [Zonal](/compute/docs/reference/rest/{$api_version}/diskTypes)

                   You can choose from a variety of disk types based on
                   your needs. For more information, read Storage
                   options.

                   The diskTypes resource represents disk types for a
                   zonal persistent disk. For more information, read
                   Zonal persistent disks.

                   The regionDiskTypes resource represents disk types
                   for a regional persistent disk. For more information,
                   read Regional persistent disks. (== resource_for
                   {$api_version}.diskTypes ==) (== resource_for
                   {$api_version}.regionDiskTypes ==)

        """
        # Create or coerce a protobuf request object.
        # Sanity check: If we got a request object, we should *not* have
        # gotten any keyword arguments that map to the request.
        has_flattened_params = any([project, zone, disk_type])
        if request is not None and has_flattened_params:
            raise ValueError("If the `request` argument is set, then none of "
                             "the individual field arguments should be set.")

        # Minor optimization to avoid making a copy if the user passes
        # in a compute.GetDiskTypeRequest.
        # There's no risk of modifying the input as we've already verified
        # there are no flattened fields.
        if not isinstance(request, compute.GetDiskTypeRequest):
            request = compute.GetDiskTypeRequest(request)
            # If we have keyword arguments corresponding to fields on the
            # request, apply these.
            if project is not None:
                request.project = project
            if zone is not None:
                request.zone = zone
            if disk_type is not None:
                request.disk_type = disk_type

        # Wrap the RPC method; this adds retry and timeout information,
        # and friendly error handling.
        rpc = self._transport._wrapped_methods[self._transport.get]

        # Send the request.
        response = rpc(
            request,
            retry=retry,
            timeout=timeout,
            metadata=metadata,
        )

        # Done; return the response.
        return response