コード例 #1
0
def test_get_rest_flattened_error():
    client = GlobalAddressesClient(
        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.GetGlobalAddressRequest(),
            project="project_value",
            address="address_value",
        )
コード例 #2
0
ファイル: client.py プロジェクト: vam-google/python-compute
    def get(
            self,
            request: compute.GetGlobalAddressRequest = None,
            *,
            project: str = None,
            address: str = None,
            retry: retries.Retry = gapic_v1.method.DEFAULT,
            timeout: float = None,
            metadata: Sequence[Tuple[str, str]] = (),
    ) -> compute.Address:
        r"""Returns the specified address resource. Gets a list
        of available addresses by making a list() request.

        Args:
            request (google.cloud.compute_v1.types.GetGlobalAddressRequest):
                The request object. A request message for
                GlobalAddresses.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.
            address (str):
                Name of the address resource to
                return.

                This corresponds to the ``address`` 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.Address:
                Use global external addresses for GFE-based external
                HTTP(S) load balancers in Premium Tier.

                   Use global internal addresses for reserved peering
                   network range.

                   Use regional external addresses for the following
                   resources:

                   -  External IP addresses for VM instances - Regional
                      external forwarding rules - Cloud NAT external IP
                      addresses - GFE based LBs in Standard Tier -
                      Network LBs in Premium or Standard Tier - Cloud
                      VPN gateways (both Classic and HA)

                   Use regional internal IP addresses for subnet IP
                   ranges (primary and secondary). This includes:

                   -  Internal IP addresses for VM instances - Alias IP
                      ranges of VM instances (/32 only) - Regional
                      internal forwarding rules - Internal TCP/UDP load
                      balancer addresses - Internal HTTP(S) load
                      balancer addresses - Cloud DNS inbound forwarding
                      IP addresses

                   For more information, read reserved IP address.

                   (== resource_for {$api_version}.addresses ==) (==
                   resource_for {$api_version}.globalAddresses ==)

        """
        # 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, address])
        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.GetGlobalAddressRequest.
        # There's no risk of modifying the input as we've already verified
        # there are no flattened fields.
        if not isinstance(request, compute.GetGlobalAddressRequest):
            request = compute.GetGlobalAddressRequest(request)
            # If we have keyword arguments corresponding to fields on the
            # request, apply these.
            if project is not None:
                request.project = project
            if address is not None:
                request.address = address

        # 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
コード例 #3
0
ファイル: client.py プロジェクト: googleapis/python-compute
    def get(
            self,
            request: Union[compute.GetGlobalAddressRequest, dict] = None,
            *,
            project: str = None,
            address: str = None,
            retry: OptionalRetry = gapic_v1.method.DEFAULT,
            timeout: float = None,
            metadata: Sequence[Tuple[str, str]] = (),
    ) -> compute.Address:
        r"""Returns the specified address resource. Gets a list
        of available addresses by making a list() request.

        Args:
            request (Union[google.cloud.compute_v1.types.GetGlobalAddressRequest, dict]):
                The request object. A request message for
                GlobalAddresses.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.
            address (str):
                Name of the address resource to
                return.

                This corresponds to the ``address`` 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.Address:
                Represents an IP Address resource. Google Compute Engine
                has two IP Address resources: \* [Global (external and
                internal)](\ https://cloud.google.com/compute/docs/reference/rest/v1/globalAddresses)
                \* [Regional (external and
                internal)](\ https://cloud.google.com/compute/docs/reference/rest/v1/addresses)
                For more information, see Reserving a static external IP
                address.

        """
        # 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, address])
        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.GetGlobalAddressRequest.
        # There's no risk of modifying the input as we've already verified
        # there are no flattened fields.
        if not isinstance(request, compute.GetGlobalAddressRequest):
            request = compute.GetGlobalAddressRequest(request)
            # If we have keyword arguments corresponding to fields on the
            # request, apply these.
            if project is not None:
                request.project = project
            if address is not None:
                request.address = address

        # 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
コード例 #4
0
        def __call__(
                self,
                request: compute.GetGlobalAddressRequest,
                *,
                retry: OptionalRetry = gapic_v1.method.DEFAULT,
                timeout: float = None,
                metadata: Sequence[Tuple[str, str]] = (),
        ) -> compute.Address:
            r"""Call the get method over HTTP.

            Args:
                request (~.compute.GetGlobalAddressRequest):
                    The request object. A request message for
                GlobalAddresses.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.Address:
                    Represents an IP Address resource. Google Compute Engine
                has two IP Address resources: \* `Global (external and
                internal) <https://cloud.google.com/compute/docs/reference/rest/v1/globalAddresses>`__
                \* `Regional (external and
                internal) <https://cloud.google.com/compute/docs/reference/rest/v1/addresses>`__
                For more information, see Reserving a static external IP
                address.

            """

            http_options = [
                {
                    "method":
                    "get",
                    "uri":
                    "/compute/v1/projects/{project}/global/addresses/{address}",
                },
            ]

            request_kwargs = compute.GetGlobalAddressRequest.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.GetGlobalAddressRequest.to_json(
                    compute.GetGlobalAddressRequest(
                        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.Address.from_json(response.content,
                                             ignore_unknown_fields=True)