diff --git a/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/gapic_metadata.json b/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/gapic_metadata.json index 702c9830db93..c5e364801af7 100644 --- a/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/gapic_metadata.json +++ b/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/gapic_metadata.json @@ -476,6 +476,241 @@ ] } } + }, + "rest": { + "libraryClient": "VmMigrationClient", + "rpcs": { + "AddGroupMigration": { + "methods": [ + "add_group_migration" + ] + }, + "CancelCloneJob": { + "methods": [ + "cancel_clone_job" + ] + }, + "CancelCutoverJob": { + "methods": [ + "cancel_cutover_job" + ] + }, + "CreateCloneJob": { + "methods": [ + "create_clone_job" + ] + }, + "CreateCutoverJob": { + "methods": [ + "create_cutover_job" + ] + }, + "CreateDatacenterConnector": { + "methods": [ + "create_datacenter_connector" + ] + }, + "CreateGroup": { + "methods": [ + "create_group" + ] + }, + "CreateMigratingVm": { + "methods": [ + "create_migrating_vm" + ] + }, + "CreateSource": { + "methods": [ + "create_source" + ] + }, + "CreateTargetProject": { + "methods": [ + "create_target_project" + ] + }, + "CreateUtilizationReport": { + "methods": [ + "create_utilization_report" + ] + }, + "DeleteDatacenterConnector": { + "methods": [ + "delete_datacenter_connector" + ] + }, + "DeleteGroup": { + "methods": [ + "delete_group" + ] + }, + "DeleteMigratingVm": { + "methods": [ + "delete_migrating_vm" + ] + }, + "DeleteSource": { + "methods": [ + "delete_source" + ] + }, + "DeleteTargetProject": { + "methods": [ + "delete_target_project" + ] + }, + "DeleteUtilizationReport": { + "methods": [ + "delete_utilization_report" + ] + }, + "FetchInventory": { + "methods": [ + "fetch_inventory" + ] + }, + "FinalizeMigration": { + "methods": [ + "finalize_migration" + ] + }, + "GetCloneJob": { + "methods": [ + "get_clone_job" + ] + }, + "GetCutoverJob": { + "methods": [ + "get_cutover_job" + ] + }, + "GetDatacenterConnector": { + "methods": [ + "get_datacenter_connector" + ] + }, + "GetGroup": { + "methods": [ + "get_group" + ] + }, + "GetMigratingVm": { + "methods": [ + "get_migrating_vm" + ] + }, + "GetReplicationCycle": { + "methods": [ + "get_replication_cycle" + ] + }, + "GetSource": { + "methods": [ + "get_source" + ] + }, + "GetTargetProject": { + "methods": [ + "get_target_project" + ] + }, + "GetUtilizationReport": { + "methods": [ + "get_utilization_report" + ] + }, + "ListCloneJobs": { + "methods": [ + "list_clone_jobs" + ] + }, + "ListCutoverJobs": { + "methods": [ + "list_cutover_jobs" + ] + }, + "ListDatacenterConnectors": { + "methods": [ + "list_datacenter_connectors" + ] + }, + "ListGroups": { + "methods": [ + "list_groups" + ] + }, + "ListMigratingVms": { + "methods": [ + "list_migrating_vms" + ] + }, + "ListReplicationCycles": { + "methods": [ + "list_replication_cycles" + ] + }, + "ListSources": { + "methods": [ + "list_sources" + ] + }, + "ListTargetProjects": { + "methods": [ + "list_target_projects" + ] + }, + "ListUtilizationReports": { + "methods": [ + "list_utilization_reports" + ] + }, + "PauseMigration": { + "methods": [ + "pause_migration" + ] + }, + "RemoveGroupMigration": { + "methods": [ + "remove_group_migration" + ] + }, + "ResumeMigration": { + "methods": [ + "resume_migration" + ] + }, + "StartMigration": { + "methods": [ + "start_migration" + ] + }, + "UpdateGroup": { + "methods": [ + "update_group" + ] + }, + "UpdateMigratingVm": { + "methods": [ + "update_migrating_vm" + ] + }, + "UpdateSource": { + "methods": [ + "update_source" + ] + }, + "UpdateTargetProject": { + "methods": [ + "update_target_project" + ] + }, + "UpgradeAppliance": { + "methods": [ + "upgrade_appliance" + ] + } + } } } } diff --git a/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/client.py b/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/client.py index 3df5e9154ff5..3934e7512469 100644 --- a/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/client.py +++ b/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/client.py @@ -64,6 +64,7 @@ from .transports.base import DEFAULT_CLIENT_INFO, VmMigrationTransport from .transports.grpc import VmMigrationGrpcTransport from .transports.grpc_asyncio import VmMigrationGrpcAsyncIOTransport +from .transports.rest import VmMigrationRestTransport class VmMigrationClientMeta(type): @@ -77,6 +78,7 @@ class VmMigrationClientMeta(type): _transport_registry = OrderedDict() # type: Dict[str, Type[VmMigrationTransport]] _transport_registry["grpc"] = VmMigrationGrpcTransport _transport_registry["grpc_asyncio"] = VmMigrationGrpcAsyncIOTransport + _transport_registry["rest"] = VmMigrationRestTransport def get_transport_class( cls, diff --git a/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/transports/__init__.py b/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/transports/__init__.py index ed8354d8cce2..bf596f9187c4 100644 --- a/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/transports/__init__.py +++ b/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/transports/__init__.py @@ -19,14 +19,18 @@ from .base import VmMigrationTransport from .grpc import VmMigrationGrpcTransport from .grpc_asyncio import VmMigrationGrpcAsyncIOTransport +from .rest import VmMigrationRestInterceptor, VmMigrationRestTransport # Compile a registry of transports. _transport_registry = OrderedDict() # type: Dict[str, Type[VmMigrationTransport]] _transport_registry["grpc"] = VmMigrationGrpcTransport _transport_registry["grpc_asyncio"] = VmMigrationGrpcAsyncIOTransport +_transport_registry["rest"] = VmMigrationRestTransport __all__ = ( "VmMigrationTransport", "VmMigrationGrpcTransport", "VmMigrationGrpcAsyncIOTransport", + "VmMigrationRestTransport", + "VmMigrationRestInterceptor", ) diff --git a/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/transports/rest.py b/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/transports/rest.py new file mode 100644 index 000000000000..86a96f730b16 --- /dev/null +++ b/packages/google-cloud-vm-migration/google/cloud/vmmigration_v1/services/vm_migration/transports/rest.py @@ -0,0 +1,7021 @@ +# -*- coding: utf-8 -*- +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import dataclasses +import json # type: ignore +import re +from typing import Callable, Dict, List, Optional, Sequence, Tuple, Union +import warnings + +from google.api_core import ( + gapic_v1, + operations_v1, + path_template, + rest_helpers, + rest_streaming, +) +from google.api_core import exceptions as core_exceptions +from google.api_core import retry as retries +from google.auth import credentials as ga_credentials # type: ignore +from google.auth.transport.grpc import SslCredentials # type: ignore +from google.auth.transport.requests import AuthorizedSession # type: ignore +from google.cloud.location import locations_pb2 # type: ignore +from google.iam.v1 import iam_policy_pb2 # type: ignore +from google.iam.v1 import policy_pb2 # type: ignore +from google.longrunning import operations_pb2 +from google.protobuf import json_format +import grpc # type: ignore +from requests import __version__ as requests_version + +try: + OptionalRetry = Union[retries.Retry, gapic_v1.method._MethodDefault] +except AttributeError: # pragma: NO COVER + OptionalRetry = Union[retries.Retry, object] # type: ignore + + +from google.longrunning import operations_pb2 # type: ignore + +from google.cloud.vmmigration_v1.types import vmmigration + +from .base import DEFAULT_CLIENT_INFO as BASE_DEFAULT_CLIENT_INFO +from .base import VmMigrationTransport + +DEFAULT_CLIENT_INFO = gapic_v1.client_info.ClientInfo( + gapic_version=BASE_DEFAULT_CLIENT_INFO.gapic_version, + grpc_version=None, + rest_version=requests_version, +) + + +class VmMigrationRestInterceptor: + """Interceptor for VmMigration. + + Interceptors are used to manipulate requests, request metadata, and responses + in arbitrary ways. + Example use cases include: + * Logging + * Verifying requests according to service or custom semantics + * Stripping extraneous information from responses + + These use cases and more can be enabled by injecting an + instance of a custom subclass when constructing the VmMigrationRestTransport. + + .. code-block:: python + class MyCustomVmMigrationInterceptor(VmMigrationRestInterceptor): + def pre_add_group_migration(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_add_group_migration(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_cancel_clone_job(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_cancel_clone_job(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_cancel_cutover_job(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_cancel_cutover_job(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_clone_job(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_clone_job(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_cutover_job(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_cutover_job(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_datacenter_connector(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_datacenter_connector(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_group(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_group(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_migrating_vm(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_migrating_vm(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_source(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_source(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_target_project(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_target_project(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_create_utilization_report(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_create_utilization_report(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_datacenter_connector(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_datacenter_connector(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_group(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_group(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_migrating_vm(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_migrating_vm(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_source(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_source(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_target_project(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_target_project(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_delete_utilization_report(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_delete_utilization_report(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_fetch_inventory(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_fetch_inventory(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_finalize_migration(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_finalize_migration(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_clone_job(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_clone_job(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_cutover_job(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_cutover_job(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_datacenter_connector(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_datacenter_connector(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_group(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_group(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_migrating_vm(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_migrating_vm(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_replication_cycle(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_replication_cycle(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_source(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_source(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_target_project(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_target_project(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_get_utilization_report(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_get_utilization_report(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_clone_jobs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_clone_jobs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_cutover_jobs(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_cutover_jobs(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_datacenter_connectors(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_datacenter_connectors(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_groups(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_groups(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_migrating_vms(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_migrating_vms(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_replication_cycles(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_replication_cycles(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_sources(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_sources(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_target_projects(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_target_projects(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_list_utilization_reports(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_list_utilization_reports(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_pause_migration(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_pause_migration(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_remove_group_migration(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_remove_group_migration(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_resume_migration(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_resume_migration(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_start_migration(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_start_migration(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_group(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_group(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_migrating_vm(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_migrating_vm(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_source(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_source(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_update_target_project(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_update_target_project(self, response): + logging.log(f"Received response: {response}") + return response + + def pre_upgrade_appliance(self, request, metadata): + logging.log(f"Received request: {request}") + return request, metadata + + def post_upgrade_appliance(self, response): + logging.log(f"Received response: {response}") + return response + + transport = VmMigrationRestTransport(interceptor=MyCustomVmMigrationInterceptor()) + client = VmMigrationClient(transport=transport) + + + """ + + def pre_add_group_migration( + self, + request: vmmigration.AddGroupMigrationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.AddGroupMigrationRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for add_group_migration + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_add_group_migration( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for add_group_migration + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_cancel_clone_job( + self, + request: vmmigration.CancelCloneJobRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CancelCloneJobRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for cancel_clone_job + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_cancel_clone_job( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for cancel_clone_job + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_cancel_cutover_job( + self, + request: vmmigration.CancelCutoverJobRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CancelCutoverJobRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for cancel_cutover_job + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_cancel_cutover_job( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for cancel_cutover_job + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_create_clone_job( + self, + request: vmmigration.CreateCloneJobRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CreateCloneJobRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_clone_job + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_create_clone_job( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_clone_job + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_create_cutover_job( + self, + request: vmmigration.CreateCutoverJobRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CreateCutoverJobRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_cutover_job + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_create_cutover_job( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_cutover_job + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_create_datacenter_connector( + self, + request: vmmigration.CreateDatacenterConnectorRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CreateDatacenterConnectorRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_datacenter_connector + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_create_datacenter_connector( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_datacenter_connector + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_create_group( + self, + request: vmmigration.CreateGroupRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CreateGroupRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_group + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_create_group( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_group + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_create_migrating_vm( + self, + request: vmmigration.CreateMigratingVmRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CreateMigratingVmRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_migrating_vm + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_create_migrating_vm( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_migrating_vm + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_create_source( + self, + request: vmmigration.CreateSourceRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CreateSourceRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_source + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_create_source( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_source + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_create_target_project( + self, + request: vmmigration.CreateTargetProjectRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CreateTargetProjectRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_target_project + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_create_target_project( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_target_project + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_create_utilization_report( + self, + request: vmmigration.CreateUtilizationReportRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.CreateUtilizationReportRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for create_utilization_report + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_create_utilization_report( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for create_utilization_report + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_delete_datacenter_connector( + self, + request: vmmigration.DeleteDatacenterConnectorRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.DeleteDatacenterConnectorRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_datacenter_connector + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_delete_datacenter_connector( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_datacenter_connector + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_delete_group( + self, + request: vmmigration.DeleteGroupRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.DeleteGroupRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_group + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_delete_group( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_group + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_delete_migrating_vm( + self, + request: vmmigration.DeleteMigratingVmRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.DeleteMigratingVmRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_migrating_vm + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_delete_migrating_vm( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_migrating_vm + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_delete_source( + self, + request: vmmigration.DeleteSourceRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.DeleteSourceRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_source + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_delete_source( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_source + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_delete_target_project( + self, + request: vmmigration.DeleteTargetProjectRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.DeleteTargetProjectRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_target_project + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_delete_target_project( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_target_project + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_delete_utilization_report( + self, + request: vmmigration.DeleteUtilizationReportRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.DeleteUtilizationReportRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for delete_utilization_report + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_delete_utilization_report( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for delete_utilization_report + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_fetch_inventory( + self, + request: vmmigration.FetchInventoryRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.FetchInventoryRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for fetch_inventory + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_fetch_inventory( + self, response: vmmigration.FetchInventoryResponse + ) -> vmmigration.FetchInventoryResponse: + """Post-rpc interceptor for fetch_inventory + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_finalize_migration( + self, + request: vmmigration.FinalizeMigrationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.FinalizeMigrationRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for finalize_migration + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_finalize_migration( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for finalize_migration + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_clone_job( + self, + request: vmmigration.GetCloneJobRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.GetCloneJobRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_clone_job + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_clone_job( + self, response: vmmigration.CloneJob + ) -> vmmigration.CloneJob: + """Post-rpc interceptor for get_clone_job + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_cutover_job( + self, + request: vmmigration.GetCutoverJobRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.GetCutoverJobRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_cutover_job + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_cutover_job( + self, response: vmmigration.CutoverJob + ) -> vmmigration.CutoverJob: + """Post-rpc interceptor for get_cutover_job + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_datacenter_connector( + self, + request: vmmigration.GetDatacenterConnectorRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.GetDatacenterConnectorRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_datacenter_connector + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_datacenter_connector( + self, response: vmmigration.DatacenterConnector + ) -> vmmigration.DatacenterConnector: + """Post-rpc interceptor for get_datacenter_connector + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_group( + self, request: vmmigration.GetGroupRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[vmmigration.GetGroupRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_group + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_group(self, response: vmmigration.Group) -> vmmigration.Group: + """Post-rpc interceptor for get_group + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_migrating_vm( + self, + request: vmmigration.GetMigratingVmRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.GetMigratingVmRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_migrating_vm + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_migrating_vm( + self, response: vmmigration.MigratingVm + ) -> vmmigration.MigratingVm: + """Post-rpc interceptor for get_migrating_vm + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_replication_cycle( + self, + request: vmmigration.GetReplicationCycleRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.GetReplicationCycleRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_replication_cycle + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_replication_cycle( + self, response: vmmigration.ReplicationCycle + ) -> vmmigration.ReplicationCycle: + """Post-rpc interceptor for get_replication_cycle + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_source( + self, request: vmmigration.GetSourceRequest, metadata: Sequence[Tuple[str, str]] + ) -> Tuple[vmmigration.GetSourceRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_source + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_source(self, response: vmmigration.Source) -> vmmigration.Source: + """Post-rpc interceptor for get_source + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_target_project( + self, + request: vmmigration.GetTargetProjectRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.GetTargetProjectRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_target_project + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_target_project( + self, response: vmmigration.TargetProject + ) -> vmmigration.TargetProject: + """Post-rpc interceptor for get_target_project + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_utilization_report( + self, + request: vmmigration.GetUtilizationReportRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.GetUtilizationReportRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for get_utilization_report + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_utilization_report( + self, response: vmmigration.UtilizationReport + ) -> vmmigration.UtilizationReport: + """Post-rpc interceptor for get_utilization_report + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_clone_jobs( + self, + request: vmmigration.ListCloneJobsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListCloneJobsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_clone_jobs + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_clone_jobs( + self, response: vmmigration.ListCloneJobsResponse + ) -> vmmigration.ListCloneJobsResponse: + """Post-rpc interceptor for list_clone_jobs + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_cutover_jobs( + self, + request: vmmigration.ListCutoverJobsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListCutoverJobsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_cutover_jobs + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_cutover_jobs( + self, response: vmmigration.ListCutoverJobsResponse + ) -> vmmigration.ListCutoverJobsResponse: + """Post-rpc interceptor for list_cutover_jobs + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_datacenter_connectors( + self, + request: vmmigration.ListDatacenterConnectorsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListDatacenterConnectorsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_datacenter_connectors + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_datacenter_connectors( + self, response: vmmigration.ListDatacenterConnectorsResponse + ) -> vmmigration.ListDatacenterConnectorsResponse: + """Post-rpc interceptor for list_datacenter_connectors + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_groups( + self, + request: vmmigration.ListGroupsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListGroupsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_groups + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_groups( + self, response: vmmigration.ListGroupsResponse + ) -> vmmigration.ListGroupsResponse: + """Post-rpc interceptor for list_groups + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_migrating_vms( + self, + request: vmmigration.ListMigratingVmsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListMigratingVmsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_migrating_vms + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_migrating_vms( + self, response: vmmigration.ListMigratingVmsResponse + ) -> vmmigration.ListMigratingVmsResponse: + """Post-rpc interceptor for list_migrating_vms + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_replication_cycles( + self, + request: vmmigration.ListReplicationCyclesRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListReplicationCyclesRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_replication_cycles + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_replication_cycles( + self, response: vmmigration.ListReplicationCyclesResponse + ) -> vmmigration.ListReplicationCyclesResponse: + """Post-rpc interceptor for list_replication_cycles + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_sources( + self, + request: vmmigration.ListSourcesRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListSourcesRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_sources + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_sources( + self, response: vmmigration.ListSourcesResponse + ) -> vmmigration.ListSourcesResponse: + """Post-rpc interceptor for list_sources + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_target_projects( + self, + request: vmmigration.ListTargetProjectsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListTargetProjectsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_target_projects + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_target_projects( + self, response: vmmigration.ListTargetProjectsResponse + ) -> vmmigration.ListTargetProjectsResponse: + """Post-rpc interceptor for list_target_projects + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_utilization_reports( + self, + request: vmmigration.ListUtilizationReportsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ListUtilizationReportsRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for list_utilization_reports + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_utilization_reports( + self, response: vmmigration.ListUtilizationReportsResponse + ) -> vmmigration.ListUtilizationReportsResponse: + """Post-rpc interceptor for list_utilization_reports + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_pause_migration( + self, + request: vmmigration.PauseMigrationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.PauseMigrationRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for pause_migration + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_pause_migration( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for pause_migration + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_remove_group_migration( + self, + request: vmmigration.RemoveGroupMigrationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.RemoveGroupMigrationRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for remove_group_migration + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_remove_group_migration( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for remove_group_migration + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_resume_migration( + self, + request: vmmigration.ResumeMigrationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.ResumeMigrationRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for resume_migration + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_resume_migration( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for resume_migration + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_start_migration( + self, + request: vmmigration.StartMigrationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.StartMigrationRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for start_migration + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_start_migration( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for start_migration + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_update_group( + self, + request: vmmigration.UpdateGroupRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.UpdateGroupRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_group + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_update_group( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for update_group + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_update_migrating_vm( + self, + request: vmmigration.UpdateMigratingVmRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.UpdateMigratingVmRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_migrating_vm + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_update_migrating_vm( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for update_migrating_vm + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_update_source( + self, + request: vmmigration.UpdateSourceRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.UpdateSourceRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_source + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_update_source( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for update_source + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_update_target_project( + self, + request: vmmigration.UpdateTargetProjectRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.UpdateTargetProjectRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for update_target_project + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_update_target_project( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for update_target_project + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_upgrade_appliance( + self, + request: vmmigration.UpgradeApplianceRequest, + metadata: Sequence[Tuple[str, str]], + ) -> Tuple[vmmigration.UpgradeApplianceRequest, Sequence[Tuple[str, str]]]: + """Pre-rpc interceptor for upgrade_appliance + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_upgrade_appliance( + self, response: operations_pb2.Operation + ) -> operations_pb2.Operation: + """Post-rpc interceptor for upgrade_appliance + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_location( + self, + request: locations_pb2.GetLocationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> locations_pb2.Location: + """Pre-rpc interceptor for get_location + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_location( + self, response: locations_pb2.GetLocationRequest + ) -> locations_pb2.Location: + """Post-rpc interceptor for get_location + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_locations( + self, + request: locations_pb2.ListLocationsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> locations_pb2.ListLocationsResponse: + """Pre-rpc interceptor for list_locations + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_locations( + self, response: locations_pb2.ListLocationsRequest + ) -> locations_pb2.ListLocationsResponse: + """Post-rpc interceptor for list_locations + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_cancel_operation( + self, + request: operations_pb2.CancelOperationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> None: + """Pre-rpc interceptor for cancel_operation + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_cancel_operation( + self, response: operations_pb2.CancelOperationRequest + ) -> None: + """Post-rpc interceptor for cancel_operation + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_delete_operation( + self, + request: operations_pb2.DeleteOperationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> None: + """Pre-rpc interceptor for delete_operation + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_delete_operation( + self, response: operations_pb2.DeleteOperationRequest + ) -> None: + """Post-rpc interceptor for delete_operation + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_get_operation( + self, + request: operations_pb2.GetOperationRequest, + metadata: Sequence[Tuple[str, str]], + ) -> operations_pb2.Operation: + """Pre-rpc interceptor for get_operation + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_get_operation( + self, response: operations_pb2.GetOperationRequest + ) -> operations_pb2.Operation: + """Post-rpc interceptor for get_operation + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + def pre_list_operations( + self, + request: operations_pb2.ListOperationsRequest, + metadata: Sequence[Tuple[str, str]], + ) -> operations_pb2.ListOperationsResponse: + """Pre-rpc interceptor for list_operations + + Override in a subclass to manipulate the request or metadata + before they are sent to the VmMigration server. + """ + return request, metadata + + def post_list_operations( + self, response: operations_pb2.ListOperationsRequest + ) -> operations_pb2.ListOperationsResponse: + """Post-rpc interceptor for list_operations + + Override in a subclass to manipulate the response + after it is returned by the VmMigration server but before + it is returned to user code. + """ + return response + + +@dataclasses.dataclass +class VmMigrationRestStub: + _session: AuthorizedSession + _host: str + _interceptor: VmMigrationRestInterceptor + + +class VmMigrationRestTransport(VmMigrationTransport): + """REST backend transport for VmMigration. + + VM Migration Service + + This class defines the same methods as the primary client, so the + primary client can load the underlying transport implementation + and call it. + + It sends JSON representations of protocol buffers over HTTP/1.1 + + """ + + def __init__( + self, + *, + host: str = "vmmigration.googleapis.com", + credentials: Optional[ga_credentials.Credentials] = None, + credentials_file: Optional[str] = None, + scopes: Optional[Sequence[str]] = None, + client_cert_source_for_mtls: Optional[Callable[[], Tuple[bytes, bytes]]] = None, + quota_project_id: Optional[str] = None, + client_info: gapic_v1.client_info.ClientInfo = DEFAULT_CLIENT_INFO, + always_use_jwt_access: Optional[bool] = False, + url_scheme: str = "https", + interceptor: Optional[VmMigrationRestInterceptor] = None, + api_audience: Optional[str] = None, + ) -> None: + """Instantiate the transport. + + Args: + host (Optional[str]): + The hostname to connect to. + credentials (Optional[google.auth.credentials.Credentials]): The + authorization credentials to attach to requests. These + credentials identify the application to the service; if none + are specified, the client will attempt to ascertain the + credentials from the environment. + + credentials_file (Optional[str]): A file with credentials that can + be loaded with :func:`google.auth.load_credentials_from_file`. + This argument is ignored if ``channel`` is provided. + scopes (Optional(Sequence[str])): A list of scopes. This argument is + ignored if ``channel`` is provided. + client_cert_source_for_mtls (Callable[[], Tuple[bytes, bytes]]): Client + certificate to configure mutual TLS HTTP channel. It is ignored + if ``channel`` is provided. + quota_project_id (Optional[str]): An optional project to use for billing + and quota. + client_info (google.api_core.gapic_v1.client_info.ClientInfo): + The client info used to send a user-agent string along with + API requests. If ``None``, then default info will be used. + Generally, you only need to set this if you are developing + your own client library. + always_use_jwt_access (Optional[bool]): Whether self signed JWT should + be used for service account credentials. + url_scheme: the protocol scheme for the API endpoint. Normally + "https", but for testing or local servers, + "http" can be specified. + """ + # Run the base constructor + # TODO(yon-mg): resolve other ctor params i.e. scopes, quota, etc. + # TODO: When custom host (api_endpoint) is set, `scopes` must *also* be set on the + # credentials object + maybe_url_match = re.match("^(?Phttp(?:s)?://)?(?P.*)$", host) + if maybe_url_match is None: + raise ValueError( + f"Unexpected hostname structure: {host}" + ) # pragma: NO COVER + + url_match_items = maybe_url_match.groupdict() + + host = f"{url_scheme}://{host}" if not url_match_items["scheme"] else host + + super().__init__( + host=host, + credentials=credentials, + client_info=client_info, + always_use_jwt_access=always_use_jwt_access, + api_audience=api_audience, + ) + self._session = AuthorizedSession( + self._credentials, default_host=self.DEFAULT_HOST + ) + self._operations_client: Optional[operations_v1.AbstractOperationsClient] = None + if client_cert_source_for_mtls: + self._session.configure_mtls_channel(client_cert_source_for_mtls) + self._interceptor = interceptor or VmMigrationRestInterceptor() + self._prep_wrapped_messages(client_info) + + @property + def operations_client(self) -> operations_v1.AbstractOperationsClient: + """Create the client designed to process long-running operations. + + This property caches on the instance; repeated calls return the same + client. + """ + # Only create a new client if we do not already have one. + if self._operations_client is None: + http_options: Dict[str, List[Dict[str, str]]] = { + "google.longrunning.Operations.CancelOperation": [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/operations/*}:cancel", + "body": "*", + }, + ], + "google.longrunning.Operations.DeleteOperation": [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.GetOperation": [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ], + "google.longrunning.Operations.ListOperations": [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*}/operations", + }, + ], + } + + rest_transport = operations_v1.OperationsRestTransport( + host=self._host, + # use the credentials which are saved + credentials=self._credentials, + scopes=self._scopes, + http_options=http_options, + path_prefix="v1", + ) + + self._operations_client = operations_v1.AbstractOperationsClient( + transport=rest_transport + ) + + # Return the client from cache. + return self._operations_client + + class _AddGroupMigration(VmMigrationRestStub): + def __hash__(self): + return hash("AddGroupMigration") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.AddGroupMigrationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the add group migration method over HTTP. + + Args: + request (~.vmmigration.AddGroupMigrationRequest): + The request object. Request message for + 'AddGroupMigration' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{group=projects/*/locations/*/groups/*}:addGroupMigration", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_add_group_migration( + request, metadata + ) + pb_request = vmmigration.AddGroupMigrationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_add_group_migration(resp) + return resp + + class _CancelCloneJob(VmMigrationRestStub): + def __hash__(self): + return hash("CancelCloneJob") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CancelCloneJobRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the cancel clone job method over HTTP. + + Args: + request (~.vmmigration.CancelCloneJobRequest): + The request object. Request message for 'CancelCloneJob' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/cloneJobs/*}:cancel", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_cancel_clone_job( + request, metadata + ) + pb_request = vmmigration.CancelCloneJobRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_cancel_clone_job(resp) + return resp + + class _CancelCutoverJob(VmMigrationRestStub): + def __hash__(self): + return hash("CancelCutoverJob") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CancelCutoverJobRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the cancel cutover job method over HTTP. + + Args: + request (~.vmmigration.CancelCutoverJobRequest): + The request object. Request message for + 'CancelCutoverJob' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/cutoverJobs/*}:cancel", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_cancel_cutover_job( + request, metadata + ) + pb_request = vmmigration.CancelCutoverJobRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_cancel_cutover_job(resp) + return resp + + class _CreateCloneJob(VmMigrationRestStub): + def __hash__(self): + return hash("CreateCloneJob") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "cloneJobId": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CreateCloneJobRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create clone job method over HTTP. + + Args: + request (~.vmmigration.CreateCloneJobRequest): + The request object. Request message for 'CreateCloneJob' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/cloneJobs", + "body": "clone_job", + }, + ] + request, metadata = self._interceptor.pre_create_clone_job( + request, metadata + ) + pb_request = vmmigration.CreateCloneJobRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_clone_job(resp) + return resp + + class _CreateCutoverJob(VmMigrationRestStub): + def __hash__(self): + return hash("CreateCutoverJob") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "cutoverJobId": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CreateCutoverJobRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create cutover job method over HTTP. + + Args: + request (~.vmmigration.CreateCutoverJobRequest): + The request object. Request message for + 'CreateCutoverJob' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/cutoverJobs", + "body": "cutover_job", + }, + ] + request, metadata = self._interceptor.pre_create_cutover_job( + request, metadata + ) + pb_request = vmmigration.CreateCutoverJobRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_cutover_job(resp) + return resp + + class _CreateDatacenterConnector(VmMigrationRestStub): + def __hash__(self): + return hash("CreateDatacenterConnector") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "datacenterConnectorId": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CreateDatacenterConnectorRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create datacenter + connector method over HTTP. + + Args: + request (~.vmmigration.CreateDatacenterConnectorRequest): + The request object. Request message for + 'CreateDatacenterConnector' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*/sources/*}/datacenterConnectors", + "body": "datacenter_connector", + }, + ] + request, metadata = self._interceptor.pre_create_datacenter_connector( + request, metadata + ) + pb_request = vmmigration.CreateDatacenterConnectorRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_datacenter_connector(resp) + return resp + + class _CreateGroup(VmMigrationRestStub): + def __hash__(self): + return hash("CreateGroup") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "groupId": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CreateGroupRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create group method over HTTP. + + Args: + request (~.vmmigration.CreateGroupRequest): + The request object. Request message for 'CreateGroup' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*}/groups", + "body": "group", + }, + ] + request, metadata = self._interceptor.pre_create_group(request, metadata) + pb_request = vmmigration.CreateGroupRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_group(resp) + return resp + + class _CreateMigratingVm(VmMigrationRestStub): + def __hash__(self): + return hash("CreateMigratingVm") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "migratingVmId": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CreateMigratingVmRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create migrating vm method over HTTP. + + Args: + request (~.vmmigration.CreateMigratingVmRequest): + The request object. Request message for + 'CreateMigratingVm' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*/sources/*}/migratingVms", + "body": "migrating_vm", + }, + ] + request, metadata = self._interceptor.pre_create_migrating_vm( + request, metadata + ) + pb_request = vmmigration.CreateMigratingVmRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_migrating_vm(resp) + return resp + + class _CreateSource(VmMigrationRestStub): + def __hash__(self): + return hash("CreateSource") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "sourceId": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CreateSourceRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create source method over HTTP. + + Args: + request (~.vmmigration.CreateSourceRequest): + The request object. Request message for 'CreateSource' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*}/sources", + "body": "source", + }, + ] + request, metadata = self._interceptor.pre_create_source(request, metadata) + pb_request = vmmigration.CreateSourceRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_source(resp) + return resp + + class _CreateTargetProject(VmMigrationRestStub): + def __hash__(self): + return hash("CreateTargetProject") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "targetProjectId": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CreateTargetProjectRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create target project method over HTTP. + + Args: + request (~.vmmigration.CreateTargetProjectRequest): + The request object. Request message for + 'CreateTargetProject' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*}/targetProjects", + "body": "target_project", + }, + ] + request, metadata = self._interceptor.pre_create_target_project( + request, metadata + ) + pb_request = vmmigration.CreateTargetProjectRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_target_project(resp) + return resp + + class _CreateUtilizationReport(VmMigrationRestStub): + def __hash__(self): + return hash("CreateUtilizationReport") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "utilizationReportId": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.CreateUtilizationReportRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the create utilization report method over HTTP. + + Args: + request (~.vmmigration.CreateUtilizationReportRequest): + The request object. Request message for + 'CreateUtilizationReport' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{parent=projects/*/locations/*/sources/*}/utilizationReports", + "body": "utilization_report", + }, + ] + request, metadata = self._interceptor.pre_create_utilization_report( + request, metadata + ) + pb_request = vmmigration.CreateUtilizationReportRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_create_utilization_report(resp) + return resp + + class _DeleteDatacenterConnector(VmMigrationRestStub): + def __hash__(self): + return hash("DeleteDatacenterConnector") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.DeleteDatacenterConnectorRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete datacenter + connector method over HTTP. + + Args: + request (~.vmmigration.DeleteDatacenterConnectorRequest): + The request object. Request message for + 'DeleteDatacenterConnector' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/sources/*/datacenterConnectors/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_datacenter_connector( + request, metadata + ) + pb_request = vmmigration.DeleteDatacenterConnectorRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_datacenter_connector(resp) + return resp + + class _DeleteGroup(VmMigrationRestStub): + def __hash__(self): + return hash("DeleteGroup") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.DeleteGroupRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete group method over HTTP. + + Args: + request (~.vmmigration.DeleteGroupRequest): + The request object. Request message for 'DeleteGroup' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/groups/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_group(request, metadata) + pb_request = vmmigration.DeleteGroupRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_group(resp) + return resp + + class _DeleteMigratingVm(VmMigrationRestStub): + def __hash__(self): + return hash("DeleteMigratingVm") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.DeleteMigratingVmRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete migrating vm method over HTTP. + + Args: + request (~.vmmigration.DeleteMigratingVmRequest): + The request object. Request message for + 'DeleteMigratingVm' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/sources/*/migratingVms/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_migrating_vm( + request, metadata + ) + pb_request = vmmigration.DeleteMigratingVmRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_migrating_vm(resp) + return resp + + class _DeleteSource(VmMigrationRestStub): + def __hash__(self): + return hash("DeleteSource") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.DeleteSourceRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete source method over HTTP. + + Args: + request (~.vmmigration.DeleteSourceRequest): + The request object. Request message for 'DeleteSource' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/sources/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_source(request, metadata) + pb_request = vmmigration.DeleteSourceRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_source(resp) + return resp + + class _DeleteTargetProject(VmMigrationRestStub): + def __hash__(self): + return hash("DeleteTargetProject") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.DeleteTargetProjectRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete target project method over HTTP. + + Args: + request (~.vmmigration.DeleteTargetProjectRequest): + The request object. Request message for + 'DeleteTargetProject' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/targetProjects/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_target_project( + request, metadata + ) + pb_request = vmmigration.DeleteTargetProjectRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_target_project(resp) + return resp + + class _DeleteUtilizationReport(VmMigrationRestStub): + def __hash__(self): + return hash("DeleteUtilizationReport") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.DeleteUtilizationReportRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the delete utilization report method over HTTP. + + Args: + request (~.vmmigration.DeleteUtilizationReportRequest): + The request object. Request message for + 'DeleteUtilizationReport' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/sources/*/utilizationReports/*}", + }, + ] + request, metadata = self._interceptor.pre_delete_utilization_report( + request, metadata + ) + pb_request = vmmigration.DeleteUtilizationReportRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_delete_utilization_report(resp) + return resp + + class _FetchInventory(VmMigrationRestStub): + def __hash__(self): + return hash("FetchInventory") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.FetchInventoryRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.FetchInventoryResponse: + r"""Call the fetch inventory method over HTTP. + + Args: + request (~.vmmigration.FetchInventoryRequest): + The request object. Request message for + [fetchInventory][google.cloud.vmmigration.v1.VmMigration.FetchInventory]. + + 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: + ~.vmmigration.FetchInventoryResponse: + Response message for + [fetchInventory][google.cloud.vmmigration.v1.VmMigration.FetchInventory]. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{source=projects/*/locations/*/sources/*}:fetchInventory", + }, + ] + request, metadata = self._interceptor.pre_fetch_inventory(request, metadata) + pb_request = vmmigration.FetchInventoryRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.FetchInventoryResponse() + pb_resp = vmmigration.FetchInventoryResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_fetch_inventory(resp) + return resp + + class _FinalizeMigration(VmMigrationRestStub): + def __hash__(self): + return hash("FinalizeMigration") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.FinalizeMigrationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the finalize migration method over HTTP. + + Args: + request (~.vmmigration.FinalizeMigrationRequest): + The request object. Request message for + 'FinalizeMigration' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{migrating_vm=projects/*/locations/*/sources/*/migratingVms/*}:finalizeMigration", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_finalize_migration( + request, metadata + ) + pb_request = vmmigration.FinalizeMigrationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_finalize_migration(resp) + return resp + + class _GetCloneJob(VmMigrationRestStub): + def __hash__(self): + return hash("GetCloneJob") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetCloneJobRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.CloneJob: + r"""Call the get clone job method over HTTP. + + Args: + request (~.vmmigration.GetCloneJobRequest): + The request object. Request message for 'GetCloneJob' + request. + + 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: + ~.vmmigration.CloneJob: + CloneJob describes the process of creating a clone of a + [MigratingVM][google.cloud.vmmigration.v1.MigratingVm] + to the requested target based on the latest successful + uploaded snapshots. While the migration cycles of a + MigratingVm take place, it is possible to verify the + uploaded VM can be started in the cloud, by creating a + clone. The clone can be created without any downtime, + and it is created using the latest snapshots which are + already in the cloud. The cloneJob is only responsible + for its work, not its products, which means once it is + finished, it will never touch the instance it created. + It will only delete it in case of the CloneJob being + cancelled or upon failure to clone. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/cloneJobs/*}", + }, + ] + request, metadata = self._interceptor.pre_get_clone_job(request, metadata) + pb_request = vmmigration.GetCloneJobRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.CloneJob() + pb_resp = vmmigration.CloneJob.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_clone_job(resp) + return resp + + class _GetCutoverJob(VmMigrationRestStub): + def __hash__(self): + return hash("GetCutoverJob") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetCutoverJobRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.CutoverJob: + r"""Call the get cutover job method over HTTP. + + Args: + request (~.vmmigration.GetCutoverJobRequest): + The request object. Request message for 'GetCutoverJob' + request. + + 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: + ~.vmmigration.CutoverJob: + CutoverJob message describes a + cutover of a migrating VM. The + CutoverJob is the operation of shutting + down the VM, creating a snapshot and + clonning the VM using the replicated + snapshot. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/cutoverJobs/*}", + }, + ] + request, metadata = self._interceptor.pre_get_cutover_job(request, metadata) + pb_request = vmmigration.GetCutoverJobRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.CutoverJob() + pb_resp = vmmigration.CutoverJob.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_cutover_job(resp) + return resp + + class _GetDatacenterConnector(VmMigrationRestStub): + def __hash__(self): + return hash("GetDatacenterConnector") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetDatacenterConnectorRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.DatacenterConnector: + r"""Call the get datacenter connector method over HTTP. + + Args: + request (~.vmmigration.GetDatacenterConnectorRequest): + The request object. Request message for + 'GetDatacenterConnector' request. + + 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: + ~.vmmigration.DatacenterConnector: + DatacenterConnector message describes + a connector between the Source and + Google Cloud, which is installed on a + vmware datacenter (an OVA vm installed + by the user) to connect the Datacenter + to Google Cloud and support vm migration + data transfer. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/sources/*/datacenterConnectors/*}", + }, + ] + request, metadata = self._interceptor.pre_get_datacenter_connector( + request, metadata + ) + pb_request = vmmigration.GetDatacenterConnectorRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.DatacenterConnector() + pb_resp = vmmigration.DatacenterConnector.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_datacenter_connector(resp) + return resp + + class _GetGroup(VmMigrationRestStub): + def __hash__(self): + return hash("GetGroup") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetGroupRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.Group: + r"""Call the get group method over HTTP. + + Args: + request (~.vmmigration.GetGroupRequest): + The request object. Request message for 'GetGroup' + request. + + 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: + ~.vmmigration.Group: + Describes message for 'Group' + resource. The Group is a collections of + several MigratingVms. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/groups/*}", + }, + ] + request, metadata = self._interceptor.pre_get_group(request, metadata) + pb_request = vmmigration.GetGroupRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.Group() + pb_resp = vmmigration.Group.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_group(resp) + return resp + + class _GetMigratingVm(VmMigrationRestStub): + def __hash__(self): + return hash("GetMigratingVm") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetMigratingVmRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.MigratingVm: + r"""Call the get migrating vm method over HTTP. + + Args: + request (~.vmmigration.GetMigratingVmRequest): + The request object. Request message for 'GetMigratingVm' + request. + + 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: + ~.vmmigration.MigratingVm: + MigratingVm describes the VM that + will be migrated from a Source + environment and its replication state. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/sources/*/migratingVms/*}", + }, + ] + request, metadata = self._interceptor.pre_get_migrating_vm( + request, metadata + ) + pb_request = vmmigration.GetMigratingVmRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.MigratingVm() + pb_resp = vmmigration.MigratingVm.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_migrating_vm(resp) + return resp + + class _GetReplicationCycle(VmMigrationRestStub): + def __hash__(self): + return hash("GetReplicationCycle") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetReplicationCycleRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ReplicationCycle: + r"""Call the get replication cycle method over HTTP. + + Args: + request (~.vmmigration.GetReplicationCycleRequest): + The request object. Request message for + 'GetReplicationCycle' request. + + 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: + ~.vmmigration.ReplicationCycle: + ReplicationCycle contains information + about the current replication cycle + status. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/replicationCycles/*}", + }, + ] + request, metadata = self._interceptor.pre_get_replication_cycle( + request, metadata + ) + pb_request = vmmigration.GetReplicationCycleRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ReplicationCycle() + pb_resp = vmmigration.ReplicationCycle.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_replication_cycle(resp) + return resp + + class _GetSource(VmMigrationRestStub): + def __hash__(self): + return hash("GetSource") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetSourceRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.Source: + r"""Call the get source method over HTTP. + + Args: + request (~.vmmigration.GetSourceRequest): + The request object. Request message for 'GetSource' + request. + + 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: + ~.vmmigration.Source: + Source message describes a specific + vm migration Source resource. It + contains the source environment + information. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/sources/*}", + }, + ] + request, metadata = self._interceptor.pre_get_source(request, metadata) + pb_request = vmmigration.GetSourceRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.Source() + pb_resp = vmmigration.Source.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_source(resp) + return resp + + class _GetTargetProject(VmMigrationRestStub): + def __hash__(self): + return hash("GetTargetProject") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetTargetProjectRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.TargetProject: + r"""Call the get target project method over HTTP. + + Args: + request (~.vmmigration.GetTargetProjectRequest): + The request object. Request message for + 'GetTargetProject' call. + + 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: + ~.vmmigration.TargetProject: + TargetProject message represents a + target Compute Engine project for a + migration or a clone. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/targetProjects/*}", + }, + ] + request, metadata = self._interceptor.pre_get_target_project( + request, metadata + ) + pb_request = vmmigration.GetTargetProjectRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.TargetProject() + pb_resp = vmmigration.TargetProject.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_target_project(resp) + return resp + + class _GetUtilizationReport(VmMigrationRestStub): + def __hash__(self): + return hash("GetUtilizationReport") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.GetUtilizationReportRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.UtilizationReport: + r"""Call the get utilization report method over HTTP. + + Args: + request (~.vmmigration.GetUtilizationReportRequest): + The request object. Request message for + 'GetUtilizationReport' request. + + 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: + ~.vmmigration.UtilizationReport: + Utilization report details the + utilization (CPU, memory, etc.) of + selected source VMs. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/sources/*/utilizationReports/*}", + }, + ] + request, metadata = self._interceptor.pre_get_utilization_report( + request, metadata + ) + pb_request = vmmigration.GetUtilizationReportRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.UtilizationReport() + pb_resp = vmmigration.UtilizationReport.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_get_utilization_report(resp) + return resp + + class _ListCloneJobs(VmMigrationRestStub): + def __hash__(self): + return hash("ListCloneJobs") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListCloneJobsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListCloneJobsResponse: + r"""Call the list clone jobs method over HTTP. + + Args: + request (~.vmmigration.ListCloneJobsRequest): + The request object. Request message for + 'ListCloneJobsRequest' request. + + 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: + ~.vmmigration.ListCloneJobsResponse: + Response message for 'ListCloneJobs' + request. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/cloneJobs", + }, + ] + request, metadata = self._interceptor.pre_list_clone_jobs(request, metadata) + pb_request = vmmigration.ListCloneJobsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListCloneJobsResponse() + pb_resp = vmmigration.ListCloneJobsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_clone_jobs(resp) + return resp + + class _ListCutoverJobs(VmMigrationRestStub): + def __hash__(self): + return hash("ListCutoverJobs") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListCutoverJobsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListCutoverJobsResponse: + r"""Call the list cutover jobs method over HTTP. + + Args: + request (~.vmmigration.ListCutoverJobsRequest): + The request object. Request message for + 'ListCutoverJobsRequest' request. + + 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: + ~.vmmigration.ListCutoverJobsResponse: + Response message for + 'ListCutoverJobs' request. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/cutoverJobs", + }, + ] + request, metadata = self._interceptor.pre_list_cutover_jobs( + request, metadata + ) + pb_request = vmmigration.ListCutoverJobsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListCutoverJobsResponse() + pb_resp = vmmigration.ListCutoverJobsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_cutover_jobs(resp) + return resp + + class _ListDatacenterConnectors(VmMigrationRestStub): + def __hash__(self): + return hash("ListDatacenterConnectors") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListDatacenterConnectorsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListDatacenterConnectorsResponse: + r"""Call the list datacenter + connectors method over HTTP. + + Args: + request (~.vmmigration.ListDatacenterConnectorsRequest): + The request object. Request message for + 'ListDatacenterConnectors' request. + + 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: + ~.vmmigration.ListDatacenterConnectorsResponse: + Response message for + 'ListDatacenterConnectors' request. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/sources/*}/datacenterConnectors", + }, + ] + request, metadata = self._interceptor.pre_list_datacenter_connectors( + request, metadata + ) + pb_request = vmmigration.ListDatacenterConnectorsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListDatacenterConnectorsResponse() + pb_resp = vmmigration.ListDatacenterConnectorsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_datacenter_connectors(resp) + return resp + + class _ListGroups(VmMigrationRestStub): + def __hash__(self): + return hash("ListGroups") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListGroupsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListGroupsResponse: + r"""Call the list groups method over HTTP. + + Args: + request (~.vmmigration.ListGroupsRequest): + The request object. Request message for 'ListGroups' + request. + + 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: + ~.vmmigration.ListGroupsResponse: + Response message for 'ListGroups' + request. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*}/groups", + }, + ] + request, metadata = self._interceptor.pre_list_groups(request, metadata) + pb_request = vmmigration.ListGroupsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListGroupsResponse() + pb_resp = vmmigration.ListGroupsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_groups(resp) + return resp + + class _ListMigratingVms(VmMigrationRestStub): + def __hash__(self): + return hash("ListMigratingVms") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListMigratingVmsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListMigratingVmsResponse: + r"""Call the list migrating vms method over HTTP. + + Args: + request (~.vmmigration.ListMigratingVmsRequest): + The request object. Request message for + 'LisMigratingVmsRequest' request. + + 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: + ~.vmmigration.ListMigratingVmsResponse: + Response message for + 'ListMigratingVms' request. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/sources/*}/migratingVms", + }, + ] + request, metadata = self._interceptor.pre_list_migrating_vms( + request, metadata + ) + pb_request = vmmigration.ListMigratingVmsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListMigratingVmsResponse() + pb_resp = vmmigration.ListMigratingVmsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_migrating_vms(resp) + return resp + + class _ListReplicationCycles(VmMigrationRestStub): + def __hash__(self): + return hash("ListReplicationCycles") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListReplicationCyclesRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListReplicationCyclesResponse: + r"""Call the list replication cycles method over HTTP. + + Args: + request (~.vmmigration.ListReplicationCyclesRequest): + The request object. Request message for + 'LisReplicationCyclesRequest' request. + + 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: + ~.vmmigration.ListReplicationCyclesResponse: + Response message for + 'ListReplicationCycles' request. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/replicationCycles", + }, + ] + request, metadata = self._interceptor.pre_list_replication_cycles( + request, metadata + ) + pb_request = vmmigration.ListReplicationCyclesRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListReplicationCyclesResponse() + pb_resp = vmmigration.ListReplicationCyclesResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_replication_cycles(resp) + return resp + + class _ListSources(VmMigrationRestStub): + def __hash__(self): + return hash("ListSources") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListSourcesRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListSourcesResponse: + r"""Call the list sources method over HTTP. + + Args: + request (~.vmmigration.ListSourcesRequest): + The request object. Request message for 'ListSources' + request. + + 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: + ~.vmmigration.ListSourcesResponse: + Response message for 'ListSources' + request. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*}/sources", + }, + ] + request, metadata = self._interceptor.pre_list_sources(request, metadata) + pb_request = vmmigration.ListSourcesRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListSourcesResponse() + pb_resp = vmmigration.ListSourcesResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_sources(resp) + return resp + + class _ListTargetProjects(VmMigrationRestStub): + def __hash__(self): + return hash("ListTargetProjects") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListTargetProjectsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListTargetProjectsResponse: + r"""Call the list target projects method over HTTP. + + Args: + request (~.vmmigration.ListTargetProjectsRequest): + The request object. Request message for + 'ListTargetProjects' call. + + 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: + ~.vmmigration.ListTargetProjectsResponse: + Response message for + 'ListTargetProjects' call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*}/targetProjects", + }, + ] + request, metadata = self._interceptor.pre_list_target_projects( + request, metadata + ) + pb_request = vmmigration.ListTargetProjectsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListTargetProjectsResponse() + pb_resp = vmmigration.ListTargetProjectsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_target_projects(resp) + return resp + + class _ListUtilizationReports(VmMigrationRestStub): + def __hash__(self): + return hash("ListUtilizationReports") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = { + "pageToken": "", + } + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ListUtilizationReportsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> vmmigration.ListUtilizationReportsResponse: + r"""Call the list utilization reports method over HTTP. + + Args: + request (~.vmmigration.ListUtilizationReportsRequest): + The request object. Request message for + 'ListUtilizationReports' request. + + 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: + ~.vmmigration.ListUtilizationReportsResponse: + Response message for + 'ListUtilizationReports' request. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{parent=projects/*/locations/*/sources/*}/utilizationReports", + }, + ] + request, metadata = self._interceptor.pre_list_utilization_reports( + request, metadata + ) + pb_request = vmmigration.ListUtilizationReportsRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + ) + + # 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 + resp = vmmigration.ListUtilizationReportsResponse() + pb_resp = vmmigration.ListUtilizationReportsResponse.pb(resp) + + json_format.Parse(response.content, pb_resp, ignore_unknown_fields=True) + resp = self._interceptor.post_list_utilization_reports(resp) + return resp + + class _PauseMigration(VmMigrationRestStub): + def __hash__(self): + return hash("PauseMigration") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.PauseMigrationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the pause migration method over HTTP. + + Args: + request (~.vmmigration.PauseMigrationRequest): + The request object. Request message for 'PauseMigration' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{migrating_vm=projects/*/locations/*/sources/*/migratingVms/*}:pauseMigration", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_pause_migration(request, metadata) + pb_request = vmmigration.PauseMigrationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_pause_migration(resp) + return resp + + class _RemoveGroupMigration(VmMigrationRestStub): + def __hash__(self): + return hash("RemoveGroupMigration") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.RemoveGroupMigrationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the remove group migration method over HTTP. + + Args: + request (~.vmmigration.RemoveGroupMigrationRequest): + The request object. Request message for 'RemoveMigration' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{group=projects/*/locations/*/groups/*}:removeGroupMigration", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_remove_group_migration( + request, metadata + ) + pb_request = vmmigration.RemoveGroupMigrationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_remove_group_migration(resp) + return resp + + class _ResumeMigration(VmMigrationRestStub): + def __hash__(self): + return hash("ResumeMigration") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.ResumeMigrationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the resume migration method over HTTP. + + Args: + request (~.vmmigration.ResumeMigrationRequest): + The request object. Request message for 'ResumeMigration' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{migrating_vm=projects/*/locations/*/sources/*/migratingVms/*}:resumeMigration", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_resume_migration( + request, metadata + ) + pb_request = vmmigration.ResumeMigrationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_resume_migration(resp) + return resp + + class _StartMigration(VmMigrationRestStub): + def __hash__(self): + return hash("StartMigration") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.StartMigrationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the start migration method over HTTP. + + Args: + request (~.vmmigration.StartMigrationRequest): + The request object. Request message for + 'StartMigrationRequest' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{migrating_vm=projects/*/locations/*/sources/*/migratingVms/*}:startMigration", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_start_migration(request, metadata) + pb_request = vmmigration.StartMigrationRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_start_migration(resp) + return resp + + class _UpdateGroup(VmMigrationRestStub): + def __hash__(self): + return hash("UpdateGroup") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.UpdateGroupRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the update group method over HTTP. + + Args: + request (~.vmmigration.UpdateGroupRequest): + The request object. Update message for 'UpdateGroups' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1/{group.name=projects/*/locations/*/groups/*}", + "body": "group", + }, + ] + request, metadata = self._interceptor.pre_update_group(request, metadata) + pb_request = vmmigration.UpdateGroupRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_group(resp) + return resp + + class _UpdateMigratingVm(VmMigrationRestStub): + def __hash__(self): + return hash("UpdateMigratingVm") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.UpdateMigratingVmRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the update migrating vm method over HTTP. + + Args: + request (~.vmmigration.UpdateMigratingVmRequest): + The request object. Request message for + 'UpdateMigratingVm' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1/{migrating_vm.name=projects/*/locations/*/sources/*/migratingVms/*}", + "body": "migrating_vm", + }, + ] + request, metadata = self._interceptor.pre_update_migrating_vm( + request, metadata + ) + pb_request = vmmigration.UpdateMigratingVmRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_migrating_vm(resp) + return resp + + class _UpdateSource(VmMigrationRestStub): + def __hash__(self): + return hash("UpdateSource") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.UpdateSourceRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the update source method over HTTP. + + Args: + request (~.vmmigration.UpdateSourceRequest): + The request object. Update message for 'UpdateSources' + request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1/{source.name=projects/*/locations/*/sources/*}", + "body": "source", + }, + ] + request, metadata = self._interceptor.pre_update_source(request, metadata) + pb_request = vmmigration.UpdateSourceRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_source(resp) + return resp + + class _UpdateTargetProject(VmMigrationRestStub): + def __hash__(self): + return hash("UpdateTargetProject") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.UpdateTargetProjectRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the update target project method over HTTP. + + Args: + request (~.vmmigration.UpdateTargetProjectRequest): + The request object. Update message for + 'UpdateTargetProject' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "patch", + "uri": "/v1/{target_project.name=projects/*/locations/*/targetProjects/*}", + "body": "target_project", + }, + ] + request, metadata = self._interceptor.pre_update_target_project( + request, metadata + ) + pb_request = vmmigration.UpdateTargetProjectRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_update_target_project(resp) + return resp + + class _UpgradeAppliance(VmMigrationRestStub): + def __hash__(self): + return hash("UpgradeAppliance") + + __REQUIRED_FIELDS_DEFAULT_VALUES: Dict[str, str] = {} + + @classmethod + def _get_unset_required_fields(cls, message_dict): + return { + k: v + for k, v in cls.__REQUIRED_FIELDS_DEFAULT_VALUES.items() + if k not in message_dict + } + + def __call__( + self, + request: vmmigration.UpgradeApplianceRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + r"""Call the upgrade appliance method over HTTP. + + Args: + request (~.vmmigration.UpgradeApplianceRequest): + The request object. Request message for + 'UpgradeAppliance' request. + + 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: + ~.operations_pb2.Operation: + This resource represents a + long-running operation that is the + result of a network API call. + + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{datacenter_connector=projects/*/locations/*/sources/*/datacenterConnectors/*}:upgradeAppliance", + "body": "*", + }, + ] + request, metadata = self._interceptor.pre_upgrade_appliance( + request, metadata + ) + pb_request = vmmigration.UpgradeApplianceRequest.pb(request) + transcoded_request = path_template.transcode(http_options, pb_request) + + # Jsonify the request body + + body = json_format.MessageToJson( + transcoded_request["body"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads( + json_format.MessageToJson( + transcoded_request["query_params"], + including_default_value_fields=False, + use_integers_for_enums=True, + ) + ) + query_params.update(self._get_unset_required_fields(query_params)) + + query_params["$alt"] = "json;enum-encoding=int" + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params, strict=True), + data=body, + ) + + # 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 + resp = operations_pb2.Operation() + json_format.Parse(response.content, resp, ignore_unknown_fields=True) + resp = self._interceptor.post_upgrade_appliance(resp) + return resp + + @property + def add_group_migration( + self, + ) -> Callable[[vmmigration.AddGroupMigrationRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._AddGroupMigration(self._session, self._host, self._interceptor) # type: ignore + + @property + def cancel_clone_job( + self, + ) -> Callable[[vmmigration.CancelCloneJobRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CancelCloneJob(self._session, self._host, self._interceptor) # type: ignore + + @property + def cancel_cutover_job( + self, + ) -> Callable[[vmmigration.CancelCutoverJobRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CancelCutoverJob(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_clone_job( + self, + ) -> Callable[[vmmigration.CreateCloneJobRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateCloneJob(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_cutover_job( + self, + ) -> Callable[[vmmigration.CreateCutoverJobRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateCutoverJob(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_datacenter_connector( + self, + ) -> Callable[ + [vmmigration.CreateDatacenterConnectorRequest], operations_pb2.Operation + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateDatacenterConnector(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_group( + self, + ) -> Callable[[vmmigration.CreateGroupRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateGroup(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_migrating_vm( + self, + ) -> Callable[[vmmigration.CreateMigratingVmRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateMigratingVm(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_source( + self, + ) -> Callable[[vmmigration.CreateSourceRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateSource(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_target_project( + self, + ) -> Callable[[vmmigration.CreateTargetProjectRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateTargetProject(self._session, self._host, self._interceptor) # type: ignore + + @property + def create_utilization_report( + self, + ) -> Callable[ + [vmmigration.CreateUtilizationReportRequest], operations_pb2.Operation + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._CreateUtilizationReport(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_datacenter_connector( + self, + ) -> Callable[ + [vmmigration.DeleteDatacenterConnectorRequest], operations_pb2.Operation + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteDatacenterConnector(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_group( + self, + ) -> Callable[[vmmigration.DeleteGroupRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteGroup(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_migrating_vm( + self, + ) -> Callable[[vmmigration.DeleteMigratingVmRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteMigratingVm(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_source( + self, + ) -> Callable[[vmmigration.DeleteSourceRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteSource(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_target_project( + self, + ) -> Callable[[vmmigration.DeleteTargetProjectRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteTargetProject(self._session, self._host, self._interceptor) # type: ignore + + @property + def delete_utilization_report( + self, + ) -> Callable[ + [vmmigration.DeleteUtilizationReportRequest], operations_pb2.Operation + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._DeleteUtilizationReport(self._session, self._host, self._interceptor) # type: ignore + + @property + def fetch_inventory( + self, + ) -> Callable[ + [vmmigration.FetchInventoryRequest], vmmigration.FetchInventoryResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._FetchInventory(self._session, self._host, self._interceptor) # type: ignore + + @property + def finalize_migration( + self, + ) -> Callable[[vmmigration.FinalizeMigrationRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._FinalizeMigration(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_clone_job( + self, + ) -> Callable[[vmmigration.GetCloneJobRequest], vmmigration.CloneJob]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetCloneJob(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_cutover_job( + self, + ) -> Callable[[vmmigration.GetCutoverJobRequest], vmmigration.CutoverJob]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetCutoverJob(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_datacenter_connector( + self, + ) -> Callable[ + [vmmigration.GetDatacenterConnectorRequest], vmmigration.DatacenterConnector + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetDatacenterConnector(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_group(self) -> Callable[[vmmigration.GetGroupRequest], vmmigration.Group]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetGroup(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_migrating_vm( + self, + ) -> Callable[[vmmigration.GetMigratingVmRequest], vmmigration.MigratingVm]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetMigratingVm(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_replication_cycle( + self, + ) -> Callable[ + [vmmigration.GetReplicationCycleRequest], vmmigration.ReplicationCycle + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetReplicationCycle(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_source( + self, + ) -> Callable[[vmmigration.GetSourceRequest], vmmigration.Source]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetSource(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_target_project( + self, + ) -> Callable[[vmmigration.GetTargetProjectRequest], vmmigration.TargetProject]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetTargetProject(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_utilization_report( + self, + ) -> Callable[ + [vmmigration.GetUtilizationReportRequest], vmmigration.UtilizationReport + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._GetUtilizationReport(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_clone_jobs( + self, + ) -> Callable[ + [vmmigration.ListCloneJobsRequest], vmmigration.ListCloneJobsResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListCloneJobs(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_cutover_jobs( + self, + ) -> Callable[ + [vmmigration.ListCutoverJobsRequest], vmmigration.ListCutoverJobsResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListCutoverJobs(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_datacenter_connectors( + self, + ) -> Callable[ + [vmmigration.ListDatacenterConnectorsRequest], + vmmigration.ListDatacenterConnectorsResponse, + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListDatacenterConnectors(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_groups( + self, + ) -> Callable[[vmmigration.ListGroupsRequest], vmmigration.ListGroupsResponse]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListGroups(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_migrating_vms( + self, + ) -> Callable[ + [vmmigration.ListMigratingVmsRequest], vmmigration.ListMigratingVmsResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListMigratingVms(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_replication_cycles( + self, + ) -> Callable[ + [vmmigration.ListReplicationCyclesRequest], + vmmigration.ListReplicationCyclesResponse, + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListReplicationCycles(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_sources( + self, + ) -> Callable[[vmmigration.ListSourcesRequest], vmmigration.ListSourcesResponse]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListSources(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_target_projects( + self, + ) -> Callable[ + [vmmigration.ListTargetProjectsRequest], vmmigration.ListTargetProjectsResponse + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListTargetProjects(self._session, self._host, self._interceptor) # type: ignore + + @property + def list_utilization_reports( + self, + ) -> Callable[ + [vmmigration.ListUtilizationReportsRequest], + vmmigration.ListUtilizationReportsResponse, + ]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ListUtilizationReports(self._session, self._host, self._interceptor) # type: ignore + + @property + def pause_migration( + self, + ) -> Callable[[vmmigration.PauseMigrationRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._PauseMigration(self._session, self._host, self._interceptor) # type: ignore + + @property + def remove_group_migration( + self, + ) -> Callable[[vmmigration.RemoveGroupMigrationRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._RemoveGroupMigration(self._session, self._host, self._interceptor) # type: ignore + + @property + def resume_migration( + self, + ) -> Callable[[vmmigration.ResumeMigrationRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._ResumeMigration(self._session, self._host, self._interceptor) # type: ignore + + @property + def start_migration( + self, + ) -> Callable[[vmmigration.StartMigrationRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._StartMigration(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_group( + self, + ) -> Callable[[vmmigration.UpdateGroupRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateGroup(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_migrating_vm( + self, + ) -> Callable[[vmmigration.UpdateMigratingVmRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateMigratingVm(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_source( + self, + ) -> Callable[[vmmigration.UpdateSourceRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateSource(self._session, self._host, self._interceptor) # type: ignore + + @property + def update_target_project( + self, + ) -> Callable[[vmmigration.UpdateTargetProjectRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpdateTargetProject(self._session, self._host, self._interceptor) # type: ignore + + @property + def upgrade_appliance( + self, + ) -> Callable[[vmmigration.UpgradeApplianceRequest], operations_pb2.Operation]: + # The return type is fine, but mypy isn't sophisticated enough to determine what's going on here. + # In C++ this would require a dynamic_cast + return self._UpgradeAppliance(self._session, self._host, self._interceptor) # type: ignore + + @property + def get_location(self): + return self._GetLocation(self._session, self._host, self._interceptor) # type: ignore + + class _GetLocation(VmMigrationRestStub): + def __call__( + self, + request: locations_pb2.GetLocationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> locations_pb2.Location: + + r"""Call the get location method over HTTP. + + Args: + request (locations_pb2.GetLocationRequest): + The request object for GetLocation method. + 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: + locations_pb2.Location: Response from GetLocation method. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*}", + }, + ] + + request, metadata = self._interceptor.pre_get_location(request, metadata) + request_kwargs = json_format.MessageToDict(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(json.dumps(transcoded_request["query_params"])) + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + + response = getattr(self._session, method)( + "{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) + + resp = locations_pb2.Location() + resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = self._interceptor.post_get_location(resp) + return resp + + @property + def list_locations(self): + return self._ListLocations(self._session, self._host, self._interceptor) # type: ignore + + class _ListLocations(VmMigrationRestStub): + def __call__( + self, + request: locations_pb2.ListLocationsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> locations_pb2.ListLocationsResponse: + + r"""Call the list locations method over HTTP. + + Args: + request (locations_pb2.ListLocationsRequest): + The request object for ListLocations method. + 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: + locations_pb2.ListLocationsResponse: Response from ListLocations method. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*}/locations", + }, + ] + + request, metadata = self._interceptor.pre_list_locations(request, metadata) + request_kwargs = json_format.MessageToDict(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(json.dumps(transcoded_request["query_params"])) + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + + response = getattr(self._session, method)( + "{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) + + resp = locations_pb2.ListLocationsResponse() + resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = self._interceptor.post_list_locations(resp) + return resp + + @property + def cancel_operation(self): + return self._CancelOperation(self._session, self._host, self._interceptor) # type: ignore + + class _CancelOperation(VmMigrationRestStub): + def __call__( + self, + request: operations_pb2.CancelOperationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> None: + + r"""Call the cancel operation method over HTTP. + + Args: + request (operations_pb2.CancelOperationRequest): + The request object for CancelOperation method. + 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. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "post", + "uri": "/v1/{name=projects/*/locations/*/operations/*}:cancel", + "body": "*", + }, + ] + + request, metadata = self._interceptor.pre_cancel_operation( + request, metadata + ) + request_kwargs = json_format.MessageToDict(request) + transcoded_request = path_template.transcode(http_options, **request_kwargs) + + body = json.loads(json.dumps(transcoded_request["body"])) + uri = transcoded_request["uri"] + method = transcoded_request["method"] + + # Jsonify the query params + query_params = json.loads(json.dumps(transcoded_request["query_params"])) + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + + response = getattr(self._session, method)( + "{host}{uri}".format(host=self._host, uri=uri), + timeout=timeout, + headers=headers, + params=rest_helpers.flatten_query_params(query_params), + data=body, + ) + + # 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 self._interceptor.post_cancel_operation(None) + + @property + def delete_operation(self): + return self._DeleteOperation(self._session, self._host, self._interceptor) # type: ignore + + class _DeleteOperation(VmMigrationRestStub): + def __call__( + self, + request: operations_pb2.DeleteOperationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> None: + + r"""Call the delete operation method over HTTP. + + Args: + request (operations_pb2.DeleteOperationRequest): + The request object for DeleteOperation method. + 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. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "delete", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ] + + request, metadata = self._interceptor.pre_delete_operation( + request, metadata + ) + request_kwargs = json_format.MessageToDict(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(json.dumps(transcoded_request["query_params"])) + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + + response = getattr(self._session, method)( + "{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 self._interceptor.post_delete_operation(None) + + @property + def get_operation(self): + return self._GetOperation(self._session, self._host, self._interceptor) # type: ignore + + class _GetOperation(VmMigrationRestStub): + def __call__( + self, + request: operations_pb2.GetOperationRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.Operation: + + r"""Call the get operation method over HTTP. + + Args: + request (operations_pb2.GetOperationRequest): + The request object for GetOperation method. + 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: + operations_pb2.Operation: Response from GetOperation method. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*/operations/*}", + }, + ] + + request, metadata = self._interceptor.pre_get_operation(request, metadata) + request_kwargs = json_format.MessageToDict(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(json.dumps(transcoded_request["query_params"])) + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + + response = getattr(self._session, method)( + "{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) + + resp = operations_pb2.Operation() + resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = self._interceptor.post_get_operation(resp) + return resp + + @property + def list_operations(self): + return self._ListOperations(self._session, self._host, self._interceptor) # type: ignore + + class _ListOperations(VmMigrationRestStub): + def __call__( + self, + request: operations_pb2.ListOperationsRequest, + *, + retry: OptionalRetry = gapic_v1.method.DEFAULT, + timeout: Optional[float] = None, + metadata: Sequence[Tuple[str, str]] = (), + ) -> operations_pb2.ListOperationsResponse: + + r"""Call the list operations method over HTTP. + + Args: + request (operations_pb2.ListOperationsRequest): + The request object for ListOperations method. + 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: + operations_pb2.ListOperationsResponse: Response from ListOperations method. + """ + + http_options: List[Dict[str, str]] = [ + { + "method": "get", + "uri": "/v1/{name=projects/*/locations/*}/operations", + }, + ] + + request, metadata = self._interceptor.pre_list_operations(request, metadata) + request_kwargs = json_format.MessageToDict(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(json.dumps(transcoded_request["query_params"])) + + # Send the request + headers = dict(metadata) + headers["Content-Type"] = "application/json" + + response = getattr(self._session, method)( + "{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) + + resp = operations_pb2.ListOperationsResponse() + resp = json_format.Parse(response.content.decode("utf-8"), resp) + resp = self._interceptor.post_list_operations(resp) + return resp + + @property + def kind(self) -> str: + return "rest" + + def close(self): + self._session.close() + + +__all__ = ("VmMigrationRestTransport",) diff --git a/packages/google-cloud-vm-migration/tests/unit/gapic/vmmigration_v1/test_vm_migration.py b/packages/google-cloud-vm-migration/tests/unit/gapic/vmmigration_v1/test_vm_migration.py index c7880b2308aa..fd7728b3f0f7 100644 --- a/packages/google-cloud-vm-migration/tests/unit/gapic/vmmigration_v1/test_vm_migration.py +++ b/packages/google-cloud-vm-migration/tests/unit/gapic/vmmigration_v1/test_vm_migration.py @@ -22,6 +22,8 @@ except ImportError: # pragma: NO COVER import mock +from collections.abc import Iterable +import json import math from google.api_core import ( @@ -49,6 +51,7 @@ from google.protobuf import duration_pb2 # type: ignore from google.protobuf import empty_pb2 # type: ignore from google.protobuf import field_mask_pb2 # type: ignore +from google.protobuf import json_format from google.protobuf import timestamp_pb2 # type: ignore from google.rpc import status_pb2 # type: ignore import grpc @@ -56,6 +59,8 @@ from proto.marshal.rules import wrappers from proto.marshal.rules.dates import DurationRule, TimestampRule import pytest +from requests import PreparedRequest, Request, Response +from requests.sessions import Session from google.cloud.vmmigration_v1.services.vm_migration import ( VmMigrationAsyncClient, @@ -112,6 +117,7 @@ def test__get_default_mtls_endpoint(): [ (VmMigrationClient, "grpc"), (VmMigrationAsyncClient, "grpc_asyncio"), + (VmMigrationClient, "rest"), ], ) def test_vm_migration_client_from_service_account_info(client_class, transport_name): @@ -125,7 +131,11 @@ def test_vm_migration_client_from_service_account_info(client_class, transport_n assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("vmmigration.googleapis.com:443") + assert client.transport._host == ( + "vmmigration.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://vmmigration.googleapis.com" + ) @pytest.mark.parametrize( @@ -133,6 +143,7 @@ def test_vm_migration_client_from_service_account_info(client_class, transport_n [ (transports.VmMigrationGrpcTransport, "grpc"), (transports.VmMigrationGrpcAsyncIOTransport, "grpc_asyncio"), + (transports.VmMigrationRestTransport, "rest"), ], ) def test_vm_migration_client_service_account_always_use_jwt( @@ -158,6 +169,7 @@ def test_vm_migration_client_service_account_always_use_jwt( [ (VmMigrationClient, "grpc"), (VmMigrationAsyncClient, "grpc_asyncio"), + (VmMigrationClient, "rest"), ], ) def test_vm_migration_client_from_service_account_file(client_class, transport_name): @@ -178,13 +190,18 @@ def test_vm_migration_client_from_service_account_file(client_class, transport_n assert client.transport._credentials == creds assert isinstance(client, client_class) - assert client.transport._host == ("vmmigration.googleapis.com:443") + assert client.transport._host == ( + "vmmigration.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://vmmigration.googleapis.com" + ) def test_vm_migration_client_get_transport_class(): transport = VmMigrationClient.get_transport_class() available_transports = [ transports.VmMigrationGrpcTransport, + transports.VmMigrationRestTransport, ] assert transport in available_transports @@ -201,6 +218,7 @@ def test_vm_migration_client_get_transport_class(): transports.VmMigrationGrpcAsyncIOTransport, "grpc_asyncio", ), + (VmMigrationClient, transports.VmMigrationRestTransport, "rest"), ], ) @mock.patch.object( @@ -344,6 +362,8 @@ def test_vm_migration_client_client_options( "grpc_asyncio", "false", ), + (VmMigrationClient, transports.VmMigrationRestTransport, "rest", "true"), + (VmMigrationClient, transports.VmMigrationRestTransport, "rest", "false"), ], ) @mock.patch.object( @@ -537,6 +557,7 @@ def test_vm_migration_client_get_mtls_endpoint_and_cert_source(client_class): transports.VmMigrationGrpcAsyncIOTransport, "grpc_asyncio", ), + (VmMigrationClient, transports.VmMigrationRestTransport, "rest"), ], ) def test_vm_migration_client_client_options_scopes( @@ -572,6 +593,7 @@ def test_vm_migration_client_client_options_scopes( "grpc_asyncio", grpc_helpers_async, ), + (VmMigrationClient, transports.VmMigrationRestTransport, "rest", None), ], ) def test_vm_migration_client_client_options_credentials_file( @@ -13540,144 +13562,15038 @@ async def test_get_replication_cycle_flattened_error_async(): ) -def test_credentials_transport_error(): - # It is an error to provide credentials and a transport instance. - transport = transports.VmMigrationGrpcTransport( +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListSourcesRequest, + dict, + ], +) +def test_list_sources_rest(request_type): + client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - with pytest.raises(ValueError): - client = VmMigrationClient( - credentials=ga_credentials.AnonymousCredentials(), - transport=transport, + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListSourcesResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], ) - # It is an error to provide a credentials file and a transport instance. - transport = transports.VmMigrationGrpcTransport( + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListSourcesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_sources(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListSourcesPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_sources_rest_required_fields(request_type=vmmigration.ListSourcesRequest): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_sources._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_sources._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - with pytest.raises(ValueError): - client = VmMigrationClient( - client_options={"credentials_file": "credentials.json"}, - transport=transport, + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListSourcesResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListSourcesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_sources(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_sources_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_sources._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) + ) + & set( + ( + "parent", + "pageToken", + ) ) + ) - # It is an error to provide an api_key and a transport instance. - transport = transports.VmMigrationGrpcTransport( + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_sources_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), ) - options = client_options.ClientOptions() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = VmMigrationClient( - client_options=options, - transport=transport, + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_sources" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_sources" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListSourcesRequest.pb(vmmigration.ListSourcesRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ListSourcesResponse.to_json( + vmmigration.ListSourcesResponse() ) - # It is an error to provide an api_key and a credential. - options = mock.Mock() - options.api_key = "api_key" - with pytest.raises(ValueError): - client = VmMigrationClient( - client_options=options, credentials=ga_credentials.AnonymousCredentials() + request = vmmigration.ListSourcesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListSourcesResponse() + + client.list_sources( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) - # It is an error to provide scopes and a transport instance. - transport = transports.VmMigrationGrpcTransport( + pre.assert_called_once() + post.assert_called_once() + + +def test_list_sources_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListSourcesRequest +): + client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - with pytest.raises(ValueError): - client = VmMigrationClient( - client_options={"scopes": ["1", "2"]}, - transport=transport, - ) + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) -def test_transport_instance(): - # A client may be instantiated with a custom transport instance. - transport = transports.VmMigrationGrpcTransport( + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_sources(request) + + +def test_list_sources_rest_flattened(): + client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - client = VmMigrationClient(transport=transport) - assert client.transport is transport + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListSourcesResponse() -def test_transport_get_channel(): - # A client may be instantiated with a custom transport instance. - transport = transports.VmMigrationGrpcTransport( + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListSourcesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_sources(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/sources" % client.transport._host, + args[1], + ) + + +def test_list_sources_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel - transport = transports.VmMigrationGrpcAsyncIOTransport( + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_sources( + vmmigration.ListSourcesRequest(), + parent="parent_value", + ) + + +def test_list_sources_rest_pager(transport: str = "rest"): + client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), + transport=transport, ) - channel = transport.grpc_channel - assert channel + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListSourcesResponse( + sources=[ + vmmigration.Source(), + vmmigration.Source(), + vmmigration.Source(), + ], + next_page_token="abc", + ), + vmmigration.ListSourcesResponse( + sources=[], + next_page_token="def", + ), + vmmigration.ListSourcesResponse( + sources=[ + vmmigration.Source(), + ], + next_page_token="ghi", + ), + vmmigration.ListSourcesResponse( + sources=[ + vmmigration.Source(), + vmmigration.Source(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(vmmigration.ListSourcesResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values -@pytest.mark.parametrize( - "transport_class", - [ - transports.VmMigrationGrpcTransport, - transports.VmMigrationGrpcAsyncIOTransport, - ], -) -def test_transport_adc(transport_class): - # Test default credentials are used if not provided. - with mock.patch.object(google.auth, "default") as adc: - adc.return_value = (ga_credentials.AnonymousCredentials(), None) - transport_class() - adc.assert_called_once() + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_sources(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.Source) for i in results) + + pages = list(client.list_sources(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token @pytest.mark.parametrize( - "transport_name", + "request_type", [ - "grpc", + vmmigration.GetSourceRequest, + dict, ], ) -def test_transport_kind(transport_name): - transport = VmMigrationClient.get_transport_class(transport_name)( +def test_get_source_rest(request_type): + client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert transport.kind == transport_name + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.Source( + name="name_value", + description="description_value", + vmware=vmmigration.VmwareSourceDetails(username="username_value"), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.Source.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_source(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.Source) + assert response.name == "name_value" + assert response.description == "description_value" + + +def test_get_source_rest_required_fields(request_type=vmmigration.GetSourceRequest): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_source._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_source._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" -def test_transport_grpc_default(): - # A client should use the gRPC transport by default. client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), + transport="rest", ) - assert isinstance( - client.transport, - transports.VmMigrationGrpcTransport, + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.Source() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.Source.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_source(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_source_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials ) + unset_fields = transport.get_source._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) -def test_vm_migration_base_transport_error(): - # Passing both a credentials object and credentials_file should raise an error - with pytest.raises(core_exceptions.DuplicateCredentialArgs): - transport = transports.VmMigrationTransport( - credentials=ga_credentials.AnonymousCredentials(), - credentials_file="credentials.json", - ) +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_source_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_source" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_source" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetSourceRequest.pb(vmmigration.GetSourceRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.Source.to_json(vmmigration.Source()) + + request = vmmigration.GetSourceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.Source() -def test_vm_migration_base_transport(): - # Instantiate the base transport. - with mock.patch( - "google.cloud.vmmigration_v1.services.vm_migration.transports.VmMigrationTransport.__init__" - ) as Transport: - Transport.return_value = None - transport = transports.VmMigrationTransport( - credentials=ga_credentials.AnonymousCredentials(), + client.get_source( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], ) - # Every method on the transport should just blindly - # raise NotImplementedError. - methods = ( - "list_sources", + pre.assert_called_once() + post.assert_called_once() + + +def test_get_source_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetSourceRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_source(request) + + +def test_get_source_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.Source() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/sources/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.Source.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_source(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*}" % client.transport._host, + args[1], + ) + + +def test_get_source_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_source( + vmmigration.GetSourceRequest(), + name="name_value", + ) + + +def test_get_source_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CreateSourceRequest, + dict, + ], +) +def test_create_source_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["source"] = { + "vmware": { + "username": "username_value", + "password": "password_value", + "vcenter_ip": "vcenter_ip_value", + "thumbprint": "thumbprint_value", + }, + "aws": { + "access_key_creds": { + "access_key_id": "access_key_id_value", + "secret_access_key": "secret_access_key_value", + }, + "aws_region": "aws_region_value", + "state": 1, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "inventory_tag_list": [{"key": "key_value", "value": "value_value"}], + "inventory_security_group_names": [ + "inventory_security_group_names_value1", + "inventory_security_group_names_value2", + ], + "migration_resources_user_tags": {}, + "public_ip": "public_ip_value", + }, + "name": "name_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "labels": {}, + "description": "description_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_source(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_source_rest_required_fields( + request_type=vmmigration.CreateSourceRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["source_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "sourceId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_source._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "sourceId" in jsonified_request + assert jsonified_request["sourceId"] == request_init["source_id"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["sourceId"] = "source_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_source._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "source_id", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "sourceId" in jsonified_request + assert jsonified_request["sourceId"] == "source_id_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_source(request) + + expected_params = [ + ( + "sourceId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_source_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_source._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "sourceId", + ) + ) + & set( + ( + "parent", + "sourceId", + "source", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_source_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_create_source" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_create_source" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CreateSourceRequest.pb( + vmmigration.CreateSourceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CreateSourceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_source( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_source_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CreateSourceRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["source"] = { + "vmware": { + "username": "username_value", + "password": "password_value", + "vcenter_ip": "vcenter_ip_value", + "thumbprint": "thumbprint_value", + }, + "aws": { + "access_key_creds": { + "access_key_id": "access_key_id_value", + "secret_access_key": "secret_access_key_value", + }, + "aws_region": "aws_region_value", + "state": 1, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "inventory_tag_list": [{"key": "key_value", "value": "value_value"}], + "inventory_security_group_names": [ + "inventory_security_group_names_value1", + "inventory_security_group_names_value2", + ], + "migration_resources_user_tags": {}, + "public_ip": "public_ip_value", + }, + "name": "name_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "labels": {}, + "description": "description_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_source(request) + + +def test_create_source_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + source=vmmigration.Source( + vmware=vmmigration.VmwareSourceDetails(username="username_value") + ), + source_id="source_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_source(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/sources" % client.transport._host, + args[1], + ) + + +def test_create_source_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_source( + vmmigration.CreateSourceRequest(), + parent="parent_value", + source=vmmigration.Source( + vmware=vmmigration.VmwareSourceDetails(username="username_value") + ), + source_id="source_id_value", + ) + + +def test_create_source_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.UpdateSourceRequest, + dict, + ], +) +def test_update_source_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "source": {"name": "projects/sample1/locations/sample2/sources/sample3"} + } + request_init["source"] = { + "vmware": { + "username": "username_value", + "password": "password_value", + "vcenter_ip": "vcenter_ip_value", + "thumbprint": "thumbprint_value", + }, + "aws": { + "access_key_creds": { + "access_key_id": "access_key_id_value", + "secret_access_key": "secret_access_key_value", + }, + "aws_region": "aws_region_value", + "state": 1, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "inventory_tag_list": [{"key": "key_value", "value": "value_value"}], + "inventory_security_group_names": [ + "inventory_security_group_names_value1", + "inventory_security_group_names_value2", + ], + "migration_resources_user_tags": {}, + "public_ip": "public_ip_value", + }, + "name": "projects/sample1/locations/sample2/sources/sample3", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "labels": {}, + "description": "description_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_source(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_update_source_rest_required_fields( + request_type=vmmigration.UpdateSourceRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_source._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_source._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "update_mask", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_source(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_source_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_source._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "updateMask", + ) + ) + & set(("source",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_source_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_update_source" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_update_source" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.UpdateSourceRequest.pb( + vmmigration.UpdateSourceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.UpdateSourceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.update_source( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_source_rest_bad_request( + transport: str = "rest", request_type=vmmigration.UpdateSourceRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "source": {"name": "projects/sample1/locations/sample2/sources/sample3"} + } + request_init["source"] = { + "vmware": { + "username": "username_value", + "password": "password_value", + "vcenter_ip": "vcenter_ip_value", + "thumbprint": "thumbprint_value", + }, + "aws": { + "access_key_creds": { + "access_key_id": "access_key_id_value", + "secret_access_key": "secret_access_key_value", + }, + "aws_region": "aws_region_value", + "state": 1, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "inventory_tag_list": [{"key": "key_value", "value": "value_value"}], + "inventory_security_group_names": [ + "inventory_security_group_names_value1", + "inventory_security_group_names_value2", + ], + "migration_resources_user_tags": {}, + "public_ip": "public_ip_value", + }, + "name": "projects/sample1/locations/sample2/sources/sample3", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "labels": {}, + "description": "description_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_source(request) + + +def test_update_source_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "source": {"name": "projects/sample1/locations/sample2/sources/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + source=vmmigration.Source( + vmware=vmmigration.VmwareSourceDetails(username="username_value") + ), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_source(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{source.name=projects/*/locations/*/sources/*}" + % client.transport._host, + args[1], + ) + + +def test_update_source_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_source( + vmmigration.UpdateSourceRequest(), + source=vmmigration.Source( + vmware=vmmigration.VmwareSourceDetails(username="username_value") + ), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_source_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.DeleteSourceRequest, + dict, + ], +) +def test_delete_source_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_source(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_source_rest_required_fields( + request_type=vmmigration.DeleteSourceRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_source._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_source._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("request_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_source(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_source_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_source._get_unset_required_fields({}) + assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_source_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_delete_source" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_delete_source" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.DeleteSourceRequest.pb( + vmmigration.DeleteSourceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.DeleteSourceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_source( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_source_rest_bad_request( + transport: str = "rest", request_type=vmmigration.DeleteSourceRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_source(request) + + +def test_delete_source_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/sources/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_source(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*}" % client.transport._host, + args[1], + ) + + +def test_delete_source_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_source( + vmmigration.DeleteSourceRequest(), + name="name_value", + ) + + +def test_delete_source_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.FetchInventoryRequest, + dict, + ], +) +def test_fetch_inventory_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"source": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.FetchInventoryResponse( + vmware_vms=vmmigration.VmwareVmsDetails( + details=[vmmigration.VmwareVmDetails(vm_id="vm_id_value")] + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.FetchInventoryResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.fetch_inventory(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.FetchInventoryResponse) + + +def test_fetch_inventory_rest_required_fields( + request_type=vmmigration.FetchInventoryRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["source"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).fetch_inventory._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["source"] = "source_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).fetch_inventory._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("force_refresh",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "source" in jsonified_request + assert jsonified_request["source"] == "source_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.FetchInventoryResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.FetchInventoryResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.fetch_inventory(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_fetch_inventory_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.fetch_inventory._get_unset_required_fields({}) + assert set(unset_fields) == (set(("forceRefresh",)) & set(("source",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_fetch_inventory_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_fetch_inventory" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_fetch_inventory" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.FetchInventoryRequest.pb( + vmmigration.FetchInventoryRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.FetchInventoryResponse.to_json( + vmmigration.FetchInventoryResponse() + ) + + request = vmmigration.FetchInventoryRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.FetchInventoryResponse() + + client.fetch_inventory( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_fetch_inventory_rest_bad_request( + transport: str = "rest", request_type=vmmigration.FetchInventoryRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"source": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.fetch_inventory(request) + + +def test_fetch_inventory_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.FetchInventoryResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "source": "projects/sample1/locations/sample2/sources/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + source="source_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.FetchInventoryResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.fetch_inventory(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{source=projects/*/locations/*/sources/*}:fetchInventory" + % client.transport._host, + args[1], + ) + + +def test_fetch_inventory_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.fetch_inventory( + vmmigration.FetchInventoryRequest(), + source="source_value", + ) + + +def test_fetch_inventory_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListUtilizationReportsRequest, + dict, + ], +) +def test_list_utilization_reports_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListUtilizationReportsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListUtilizationReportsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_utilization_reports(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListUtilizationReportsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_utilization_reports_rest_required_fields( + request_type=vmmigration.ListUtilizationReportsRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_utilization_reports._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_utilization_reports._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + "view", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListUtilizationReportsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListUtilizationReportsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_utilization_reports(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_utilization_reports_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_utilization_reports._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + "view", + ) + ) + & set( + ( + "parent", + "pageToken", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_utilization_reports_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_utilization_reports" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_utilization_reports" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListUtilizationReportsRequest.pb( + vmmigration.ListUtilizationReportsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ListUtilizationReportsResponse.to_json( + vmmigration.ListUtilizationReportsResponse() + ) + + request = vmmigration.ListUtilizationReportsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListUtilizationReportsResponse() + + client.list_utilization_reports( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_utilization_reports_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListUtilizationReportsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_utilization_reports(request) + + +def test_list_utilization_reports_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListUtilizationReportsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListUtilizationReportsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_utilization_reports(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*}/utilizationReports" + % client.transport._host, + args[1], + ) + + +def test_list_utilization_reports_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_utilization_reports( + vmmigration.ListUtilizationReportsRequest(), + parent="parent_value", + ) + + +def test_list_utilization_reports_rest_pager(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListUtilizationReportsResponse( + utilization_reports=[ + vmmigration.UtilizationReport(), + vmmigration.UtilizationReport(), + vmmigration.UtilizationReport(), + ], + next_page_token="abc", + ), + vmmigration.ListUtilizationReportsResponse( + utilization_reports=[], + next_page_token="def", + ), + vmmigration.ListUtilizationReportsResponse( + utilization_reports=[ + vmmigration.UtilizationReport(), + ], + next_page_token="ghi", + ), + vmmigration.ListUtilizationReportsResponse( + utilization_reports=[ + vmmigration.UtilizationReport(), + vmmigration.UtilizationReport(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + vmmigration.ListUtilizationReportsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + pager = client.list_utilization_reports(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.UtilizationReport) for i in results) + + pages = list(client.list_utilization_reports(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.GetUtilizationReportRequest, + dict, + ], +) +def test_get_utilization_report_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/utilizationReports/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.UtilizationReport( + name="name_value", + display_name="display_name_value", + state=vmmigration.UtilizationReport.State.CREATING, + time_frame=vmmigration.UtilizationReport.TimeFrame.WEEK, + vm_count=875, + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.UtilizationReport.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_utilization_report(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.UtilizationReport) + assert response.name == "name_value" + assert response.display_name == "display_name_value" + assert response.state == vmmigration.UtilizationReport.State.CREATING + assert response.time_frame == vmmigration.UtilizationReport.TimeFrame.WEEK + assert response.vm_count == 875 + + +def test_get_utilization_report_rest_required_fields( + request_type=vmmigration.GetUtilizationReportRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_utilization_report._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_utilization_report._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("view",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.UtilizationReport() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.UtilizationReport.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_utilization_report(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_utilization_report_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_utilization_report._get_unset_required_fields({}) + assert set(unset_fields) == (set(("view",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_utilization_report_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_utilization_report" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_utilization_report" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetUtilizationReportRequest.pb( + vmmigration.GetUtilizationReportRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.UtilizationReport.to_json( + vmmigration.UtilizationReport() + ) + + request = vmmigration.GetUtilizationReportRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.UtilizationReport() + + client.get_utilization_report( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_utilization_report_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetUtilizationReportRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/utilizationReports/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_utilization_report(request) + + +def test_get_utilization_report_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.UtilizationReport() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/utilizationReports/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.UtilizationReport.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_utilization_report(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/utilizationReports/*}" + % client.transport._host, + args[1], + ) + + +def test_get_utilization_report_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_utilization_report( + vmmigration.GetUtilizationReportRequest(), + name="name_value", + ) + + +def test_get_utilization_report_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CreateUtilizationReportRequest, + dict, + ], +) +def test_create_utilization_report_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request_init["utilization_report"] = { + "name": "name_value", + "display_name": "display_name_value", + "state": 1, + "state_time": {"seconds": 751, "nanos": 543}, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "create_time": {}, + "time_frame": 1, + "frame_end_time": {}, + "vm_count": 875, + "vms": [ + { + "vmware_vm_details": { + "vm_id": "vm_id_value", + "datacenter_id": "datacenter_id_value", + "datacenter_description": "datacenter_description_value", + "uuid": "uuid_value", + "display_name": "display_name_value", + "power_state": 1, + "cpu_count": 976, + "memory_mb": 967, + "disk_count": 1075, + "committed_storage_mb": 2120, + "guest_description": "guest_description_value", + "boot_option": 1, + }, + "vm_id": "vm_id_value", + "utilization": { + "cpu_max_percent": 1597, + "cpu_average_percent": 2002, + "memory_max_percent": 1934, + "memory_average_percent": 2339, + "disk_io_rate_max_kbps": 2209, + "disk_io_rate_average_kbps": 2614, + "network_throughput_max_kbps": 2935, + "network_throughput_average_kbps": 3340, + }, + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_utilization_report(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_utilization_report_rest_required_fields( + request_type=vmmigration.CreateUtilizationReportRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["utilization_report_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "utilizationReportId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_utilization_report._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "utilizationReportId" in jsonified_request + assert ( + jsonified_request["utilizationReportId"] + == request_init["utilization_report_id"] + ) + + jsonified_request["parent"] = "parent_value" + jsonified_request["utilizationReportId"] = "utilization_report_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_utilization_report._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "utilization_report_id", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "utilizationReportId" in jsonified_request + assert jsonified_request["utilizationReportId"] == "utilization_report_id_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_utilization_report(request) + + expected_params = [ + ( + "utilizationReportId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_utilization_report_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_utilization_report._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "utilizationReportId", + ) + ) + & set( + ( + "parent", + "utilizationReport", + "utilizationReportId", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_utilization_report_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_create_utilization_report" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_create_utilization_report" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CreateUtilizationReportRequest.pb( + vmmigration.CreateUtilizationReportRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CreateUtilizationReportRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_utilization_report( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_utilization_report_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CreateUtilizationReportRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request_init["utilization_report"] = { + "name": "name_value", + "display_name": "display_name_value", + "state": 1, + "state_time": {"seconds": 751, "nanos": 543}, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "create_time": {}, + "time_frame": 1, + "frame_end_time": {}, + "vm_count": 875, + "vms": [ + { + "vmware_vm_details": { + "vm_id": "vm_id_value", + "datacenter_id": "datacenter_id_value", + "datacenter_description": "datacenter_description_value", + "uuid": "uuid_value", + "display_name": "display_name_value", + "power_state": 1, + "cpu_count": 976, + "memory_mb": 967, + "disk_count": 1075, + "committed_storage_mb": 2120, + "guest_description": "guest_description_value", + "boot_option": 1, + }, + "vm_id": "vm_id_value", + "utilization": { + "cpu_max_percent": 1597, + "cpu_average_percent": 2002, + "memory_max_percent": 1934, + "memory_average_percent": 2339, + "disk_io_rate_max_kbps": 2209, + "disk_io_rate_average_kbps": 2614, + "network_throughput_max_kbps": 2935, + "network_throughput_average_kbps": 3340, + }, + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_utilization_report(request) + + +def test_create_utilization_report_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + utilization_report=vmmigration.UtilizationReport(name="name_value"), + utilization_report_id="utilization_report_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_utilization_report(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*}/utilizationReports" + % client.transport._host, + args[1], + ) + + +def test_create_utilization_report_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_utilization_report( + vmmigration.CreateUtilizationReportRequest(), + parent="parent_value", + utilization_report=vmmigration.UtilizationReport(name="name_value"), + utilization_report_id="utilization_report_id_value", + ) + + +def test_create_utilization_report_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.DeleteUtilizationReportRequest, + dict, + ], +) +def test_delete_utilization_report_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/utilizationReports/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_utilization_report(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_utilization_report_rest_required_fields( + request_type=vmmigration.DeleteUtilizationReportRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_utilization_report._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_utilization_report._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("request_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_utilization_report(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_utilization_report_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_utilization_report._get_unset_required_fields({}) + assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_utilization_report_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_delete_utilization_report" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_delete_utilization_report" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.DeleteUtilizationReportRequest.pb( + vmmigration.DeleteUtilizationReportRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.DeleteUtilizationReportRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_utilization_report( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_utilization_report_rest_bad_request( + transport: str = "rest", request_type=vmmigration.DeleteUtilizationReportRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/utilizationReports/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_utilization_report(request) + + +def test_delete_utilization_report_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/utilizationReports/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_utilization_report(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/utilizationReports/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_utilization_report_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_utilization_report( + vmmigration.DeleteUtilizationReportRequest(), + name="name_value", + ) + + +def test_delete_utilization_report_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListDatacenterConnectorsRequest, + dict, + ], +) +def test_list_datacenter_connectors_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListDatacenterConnectorsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListDatacenterConnectorsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_datacenter_connectors(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListDatacenterConnectorsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_datacenter_connectors_rest_required_fields( + request_type=vmmigration.ListDatacenterConnectorsRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_datacenter_connectors._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_datacenter_connectors._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListDatacenterConnectorsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListDatacenterConnectorsResponse.pb( + return_value + ) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_datacenter_connectors(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_datacenter_connectors_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_datacenter_connectors._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) + ) + & set( + ( + "parent", + "pageToken", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_datacenter_connectors_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_datacenter_connectors" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_datacenter_connectors" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListDatacenterConnectorsRequest.pb( + vmmigration.ListDatacenterConnectorsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = ( + vmmigration.ListDatacenterConnectorsResponse.to_json( + vmmigration.ListDatacenterConnectorsResponse() + ) + ) + + request = vmmigration.ListDatacenterConnectorsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListDatacenterConnectorsResponse() + + client.list_datacenter_connectors( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_datacenter_connectors_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListDatacenterConnectorsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_datacenter_connectors(request) + + +def test_list_datacenter_connectors_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListDatacenterConnectorsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListDatacenterConnectorsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_datacenter_connectors(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*}/datacenterConnectors" + % client.transport._host, + args[1], + ) + + +def test_list_datacenter_connectors_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_datacenter_connectors( + vmmigration.ListDatacenterConnectorsRequest(), + parent="parent_value", + ) + + +def test_list_datacenter_connectors_rest_pager(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListDatacenterConnectorsResponse( + datacenter_connectors=[ + vmmigration.DatacenterConnector(), + vmmigration.DatacenterConnector(), + vmmigration.DatacenterConnector(), + ], + next_page_token="abc", + ), + vmmigration.ListDatacenterConnectorsResponse( + datacenter_connectors=[], + next_page_token="def", + ), + vmmigration.ListDatacenterConnectorsResponse( + datacenter_connectors=[ + vmmigration.DatacenterConnector(), + ], + next_page_token="ghi", + ), + vmmigration.ListDatacenterConnectorsResponse( + datacenter_connectors=[ + vmmigration.DatacenterConnector(), + vmmigration.DatacenterConnector(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + vmmigration.ListDatacenterConnectorsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + pager = client.list_datacenter_connectors(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.DatacenterConnector) for i in results) + + pages = list(client.list_datacenter_connectors(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.GetDatacenterConnectorRequest, + dict, + ], +) +def test_get_datacenter_connector_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/datacenterConnectors/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.DatacenterConnector( + name="name_value", + registration_id="registration_id_value", + service_account="service_account_value", + version="version_value", + bucket="bucket_value", + state=vmmigration.DatacenterConnector.State.PENDING, + appliance_infrastructure_version="appliance_infrastructure_version_value", + appliance_software_version="appliance_software_version_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.DatacenterConnector.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_datacenter_connector(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.DatacenterConnector) + assert response.name == "name_value" + assert response.registration_id == "registration_id_value" + assert response.service_account == "service_account_value" + assert response.version == "version_value" + assert response.bucket == "bucket_value" + assert response.state == vmmigration.DatacenterConnector.State.PENDING + assert ( + response.appliance_infrastructure_version + == "appliance_infrastructure_version_value" + ) + assert response.appliance_software_version == "appliance_software_version_value" + + +def test_get_datacenter_connector_rest_required_fields( + request_type=vmmigration.GetDatacenterConnectorRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_datacenter_connector._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_datacenter_connector._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.DatacenterConnector() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.DatacenterConnector.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_datacenter_connector(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_datacenter_connector_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_datacenter_connector._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_datacenter_connector_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_datacenter_connector" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_datacenter_connector" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetDatacenterConnectorRequest.pb( + vmmigration.GetDatacenterConnectorRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.DatacenterConnector.to_json( + vmmigration.DatacenterConnector() + ) + + request = vmmigration.GetDatacenterConnectorRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.DatacenterConnector() + + client.get_datacenter_connector( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_datacenter_connector_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetDatacenterConnectorRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/datacenterConnectors/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_datacenter_connector(request) + + +def test_get_datacenter_connector_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.DatacenterConnector() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/datacenterConnectors/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.DatacenterConnector.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_datacenter_connector(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/datacenterConnectors/*}" + % client.transport._host, + args[1], + ) + + +def test_get_datacenter_connector_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_datacenter_connector( + vmmigration.GetDatacenterConnectorRequest(), + name="name_value", + ) + + +def test_get_datacenter_connector_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CreateDatacenterConnectorRequest, + dict, + ], +) +def test_create_datacenter_connector_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request_init["datacenter_connector"] = { + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "name": "name_value", + "registration_id": "registration_id_value", + "service_account": "service_account_value", + "version": "version_value", + "bucket": "bucket_value", + "state": 1, + "state_time": {}, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "appliance_infrastructure_version": "appliance_infrastructure_version_value", + "appliance_software_version": "appliance_software_version_value", + "available_versions": { + "new_deployable_appliance": { + "version": "version_value", + "uri": "uri_value", + "critical": True, + "release_notes_uri": "release_notes_uri_value", + }, + "in_place_update": {}, + }, + "upgrade_status": { + "version": "version_value", + "state": 1, + "error": {}, + "start_time": {}, + "previous_version": "previous_version_value", + }, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_datacenter_connector(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_datacenter_connector_rest_required_fields( + request_type=vmmigration.CreateDatacenterConnectorRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["datacenter_connector_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "datacenterConnectorId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_datacenter_connector._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "datacenterConnectorId" in jsonified_request + assert ( + jsonified_request["datacenterConnectorId"] + == request_init["datacenter_connector_id"] + ) + + jsonified_request["parent"] = "parent_value" + jsonified_request["datacenterConnectorId"] = "datacenter_connector_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_datacenter_connector._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "datacenter_connector_id", + "request_id", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "datacenterConnectorId" in jsonified_request + assert jsonified_request["datacenterConnectorId"] == "datacenter_connector_id_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_datacenter_connector(request) + + expected_params = [ + ( + "datacenterConnectorId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_datacenter_connector_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_datacenter_connector._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "datacenterConnectorId", + "requestId", + ) + ) + & set( + ( + "parent", + "datacenterConnectorId", + "datacenterConnector", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_datacenter_connector_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_create_datacenter_connector" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_create_datacenter_connector" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CreateDatacenterConnectorRequest.pb( + vmmigration.CreateDatacenterConnectorRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CreateDatacenterConnectorRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_datacenter_connector( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_datacenter_connector_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CreateDatacenterConnectorRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request_init["datacenter_connector"] = { + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "name": "name_value", + "registration_id": "registration_id_value", + "service_account": "service_account_value", + "version": "version_value", + "bucket": "bucket_value", + "state": 1, + "state_time": {}, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "appliance_infrastructure_version": "appliance_infrastructure_version_value", + "appliance_software_version": "appliance_software_version_value", + "available_versions": { + "new_deployable_appliance": { + "version": "version_value", + "uri": "uri_value", + "critical": True, + "release_notes_uri": "release_notes_uri_value", + }, + "in_place_update": {}, + }, + "upgrade_status": { + "version": "version_value", + "state": 1, + "error": {}, + "start_time": {}, + "previous_version": "previous_version_value", + }, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_datacenter_connector(request) + + +def test_create_datacenter_connector_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + datacenter_connector=vmmigration.DatacenterConnector( + create_time=timestamp_pb2.Timestamp(seconds=751) + ), + datacenter_connector_id="datacenter_connector_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_datacenter_connector(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*}/datacenterConnectors" + % client.transport._host, + args[1], + ) + + +def test_create_datacenter_connector_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_datacenter_connector( + vmmigration.CreateDatacenterConnectorRequest(), + parent="parent_value", + datacenter_connector=vmmigration.DatacenterConnector( + create_time=timestamp_pb2.Timestamp(seconds=751) + ), + datacenter_connector_id="datacenter_connector_id_value", + ) + + +def test_create_datacenter_connector_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.DeleteDatacenterConnectorRequest, + dict, + ], +) +def test_delete_datacenter_connector_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/datacenterConnectors/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_datacenter_connector(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_datacenter_connector_rest_required_fields( + request_type=vmmigration.DeleteDatacenterConnectorRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_datacenter_connector._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_datacenter_connector._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("request_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_datacenter_connector(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_datacenter_connector_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_datacenter_connector._get_unset_required_fields({}) + assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_datacenter_connector_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_delete_datacenter_connector" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_delete_datacenter_connector" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.DeleteDatacenterConnectorRequest.pb( + vmmigration.DeleteDatacenterConnectorRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.DeleteDatacenterConnectorRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_datacenter_connector( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_datacenter_connector_rest_bad_request( + transport: str = "rest", request_type=vmmigration.DeleteDatacenterConnectorRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/datacenterConnectors/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_datacenter_connector(request) + + +def test_delete_datacenter_connector_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/datacenterConnectors/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_datacenter_connector(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/datacenterConnectors/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_datacenter_connector_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_datacenter_connector( + vmmigration.DeleteDatacenterConnectorRequest(), + name="name_value", + ) + + +def test_delete_datacenter_connector_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.UpgradeApplianceRequest, + dict, + ], +) +def test_upgrade_appliance_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "datacenter_connector": "projects/sample1/locations/sample2/sources/sample3/datacenterConnectors/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.upgrade_appliance(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_upgrade_appliance_rest_required_fields( + request_type=vmmigration.UpgradeApplianceRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["datacenter_connector"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).upgrade_appliance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["datacenterConnector"] = "datacenter_connector_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).upgrade_appliance._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "datacenterConnector" in jsonified_request + assert jsonified_request["datacenterConnector"] == "datacenter_connector_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.upgrade_appliance(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_upgrade_appliance_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.upgrade_appliance._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("datacenterConnector",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_upgrade_appliance_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_upgrade_appliance" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_upgrade_appliance" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.UpgradeApplianceRequest.pb( + vmmigration.UpgradeApplianceRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.UpgradeApplianceRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.upgrade_appliance( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_upgrade_appliance_rest_bad_request( + transport: str = "rest", request_type=vmmigration.UpgradeApplianceRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "datacenter_connector": "projects/sample1/locations/sample2/sources/sample3/datacenterConnectors/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.upgrade_appliance(request) + + +def test_upgrade_appliance_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CreateMigratingVmRequest, + dict, + ], +) +def test_create_migrating_vm_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request_init["migrating_vm"] = { + "compute_engine_target_defaults": { + "vm_name": "vm_name_value", + "target_project": "target_project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": [ + { + "network": "network_value", + "subnetwork": "subnetwork_value", + "internal_ip": "internal_ip_value", + "external_ip": "external_ip_value", + } + ], + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {"type_": 1, "os_license": "os_license_value"}, + "compute_scheduling": { + "on_host_maintenance": 1, + "restart_type": 1, + "node_affinities": [ + { + "key": "key_value", + "operator": 1, + "values": ["values_value1", "values_value2"], + } + ], + "min_node_cpus": 1379, + }, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "aws_source_vm_details": {"firmware": 1, "committed_storage_bytes": 2464}, + "name": "name_value", + "source_vm_id": "source_vm_id_value", + "display_name": "display_name_value", + "description": "description_value", + "policy": { + "idle_duration": {"seconds": 751, "nanos": 543}, + "skip_os_adaptation": True, + }, + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "last_sync": {"last_sync_time": {}}, + "state": 1, + "state_time": {}, + "current_sync_info": { + "name": "name_value", + "cycle_number": 1272, + "start_time": {}, + "end_time": {}, + "total_pause_duration": {}, + "progress_percent": 1733, + "steps": [ + { + "initializing_replication": {}, + "replicating": { + "total_bytes": 1194, + "replicated_bytes": 1699, + "last_two_minutes_average_bytes_per_second": 4370, + "last_thirty_minutes_average_bytes_per_second": 4700, + }, + "post_processing": {}, + "start_time": {}, + "end_time": {}, + } + ], + "state": 1, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + }, + "group": "group_value", + "labels": {}, + "recent_clone_jobs": [ + { + "compute_engine_target_details": { + "vm_name": "vm_name_value", + "project": "project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": {}, + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {}, + "compute_scheduling": {}, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "create_time": {}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "error": {}, + "steps": [ + { + "adapting_os": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + ], + "error": {}, + "recent_cutover_jobs": [ + { + "compute_engine_target_details": {}, + "create_time": {}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "progress_percent": 1733, + "error": {}, + "state_message": "state_message_value", + "steps": [ + { + "previous_replication_cycle": {}, + "shutting_down_source_vm": {}, + "final_sync": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_migrating_vm(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_migrating_vm_rest_required_fields( + request_type=vmmigration.CreateMigratingVmRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["migrating_vm_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "migratingVmId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_migrating_vm._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "migratingVmId" in jsonified_request + assert jsonified_request["migratingVmId"] == request_init["migrating_vm_id"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["migratingVmId"] = "migrating_vm_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_migrating_vm._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "migrating_vm_id", + "request_id", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "migratingVmId" in jsonified_request + assert jsonified_request["migratingVmId"] == "migrating_vm_id_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_migrating_vm(request) + + expected_params = [ + ( + "migratingVmId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_migrating_vm_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_migrating_vm._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "migratingVmId", + "requestId", + ) + ) + & set( + ( + "parent", + "migratingVmId", + "migratingVm", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_migrating_vm_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_create_migrating_vm" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_create_migrating_vm" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CreateMigratingVmRequest.pb( + vmmigration.CreateMigratingVmRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CreateMigratingVmRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_migrating_vm( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_migrating_vm_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CreateMigratingVmRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request_init["migrating_vm"] = { + "compute_engine_target_defaults": { + "vm_name": "vm_name_value", + "target_project": "target_project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": [ + { + "network": "network_value", + "subnetwork": "subnetwork_value", + "internal_ip": "internal_ip_value", + "external_ip": "external_ip_value", + } + ], + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {"type_": 1, "os_license": "os_license_value"}, + "compute_scheduling": { + "on_host_maintenance": 1, + "restart_type": 1, + "node_affinities": [ + { + "key": "key_value", + "operator": 1, + "values": ["values_value1", "values_value2"], + } + ], + "min_node_cpus": 1379, + }, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "aws_source_vm_details": {"firmware": 1, "committed_storage_bytes": 2464}, + "name": "name_value", + "source_vm_id": "source_vm_id_value", + "display_name": "display_name_value", + "description": "description_value", + "policy": { + "idle_duration": {"seconds": 751, "nanos": 543}, + "skip_os_adaptation": True, + }, + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "last_sync": {"last_sync_time": {}}, + "state": 1, + "state_time": {}, + "current_sync_info": { + "name": "name_value", + "cycle_number": 1272, + "start_time": {}, + "end_time": {}, + "total_pause_duration": {}, + "progress_percent": 1733, + "steps": [ + { + "initializing_replication": {}, + "replicating": { + "total_bytes": 1194, + "replicated_bytes": 1699, + "last_two_minutes_average_bytes_per_second": 4370, + "last_thirty_minutes_average_bytes_per_second": 4700, + }, + "post_processing": {}, + "start_time": {}, + "end_time": {}, + } + ], + "state": 1, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + }, + "group": "group_value", + "labels": {}, + "recent_clone_jobs": [ + { + "compute_engine_target_details": { + "vm_name": "vm_name_value", + "project": "project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": {}, + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {}, + "compute_scheduling": {}, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "create_time": {}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "error": {}, + "steps": [ + { + "adapting_os": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + ], + "error": {}, + "recent_cutover_jobs": [ + { + "compute_engine_target_details": {}, + "create_time": {}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "progress_percent": 1733, + "error": {}, + "state_message": "state_message_value", + "steps": [ + { + "previous_replication_cycle": {}, + "shutting_down_source_vm": {}, + "final_sync": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_migrating_vm(request) + + +def test_create_migrating_vm_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + migrating_vm=vmmigration.MigratingVm( + compute_engine_target_defaults=vmmigration.ComputeEngineTargetDefaults( + vm_name="vm_name_value" + ) + ), + migrating_vm_id="migrating_vm_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_migrating_vm(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*}/migratingVms" + % client.transport._host, + args[1], + ) + + +def test_create_migrating_vm_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_migrating_vm( + vmmigration.CreateMigratingVmRequest(), + parent="parent_value", + migrating_vm=vmmigration.MigratingVm( + compute_engine_target_defaults=vmmigration.ComputeEngineTargetDefaults( + vm_name="vm_name_value" + ) + ), + migrating_vm_id="migrating_vm_id_value", + ) + + +def test_create_migrating_vm_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListMigratingVmsRequest, + dict, + ], +) +def test_list_migrating_vms_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListMigratingVmsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListMigratingVmsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_migrating_vms(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListMigratingVmsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_migrating_vms_rest_required_fields( + request_type=vmmigration.ListMigratingVmsRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_migrating_vms._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_migrating_vms._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + "view", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListMigratingVmsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListMigratingVmsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_migrating_vms(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_migrating_vms_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_migrating_vms._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + "view", + ) + ) + & set( + ( + "parent", + "pageToken", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_migrating_vms_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_migrating_vms" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_migrating_vms" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListMigratingVmsRequest.pb( + vmmigration.ListMigratingVmsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ListMigratingVmsResponse.to_json( + vmmigration.ListMigratingVmsResponse() + ) + + request = vmmigration.ListMigratingVmsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListMigratingVmsResponse() + + client.list_migrating_vms( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_migrating_vms_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListMigratingVmsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2/sources/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_migrating_vms(request) + + +def test_list_migrating_vms_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListMigratingVmsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListMigratingVmsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_migrating_vms(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*}/migratingVms" + % client.transport._host, + args[1], + ) + + +def test_list_migrating_vms_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_migrating_vms( + vmmigration.ListMigratingVmsRequest(), + parent="parent_value", + ) + + +def test_list_migrating_vms_rest_pager(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListMigratingVmsResponse( + migrating_vms=[ + vmmigration.MigratingVm(), + vmmigration.MigratingVm(), + vmmigration.MigratingVm(), + ], + next_page_token="abc", + ), + vmmigration.ListMigratingVmsResponse( + migrating_vms=[], + next_page_token="def", + ), + vmmigration.ListMigratingVmsResponse( + migrating_vms=[ + vmmigration.MigratingVm(), + ], + next_page_token="ghi", + ), + vmmigration.ListMigratingVmsResponse( + migrating_vms=[ + vmmigration.MigratingVm(), + vmmigration.MigratingVm(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + vmmigration.ListMigratingVmsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3" + } + + pager = client.list_migrating_vms(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.MigratingVm) for i in results) + + pages = list(client.list_migrating_vms(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.GetMigratingVmRequest, + dict, + ], +) +def test_get_migrating_vm_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.MigratingVm( + name="name_value", + source_vm_id="source_vm_id_value", + display_name="display_name_value", + description="description_value", + state=vmmigration.MigratingVm.State.PENDING, + group="group_value", + compute_engine_target_defaults=vmmigration.ComputeEngineTargetDefaults( + vm_name="vm_name_value" + ), + aws_source_vm_details=vmmigration.AwsSourceVmDetails( + firmware=vmmigration.AwsSourceVmDetails.Firmware.EFI + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.MigratingVm.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_migrating_vm(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.MigratingVm) + assert response.name == "name_value" + assert response.source_vm_id == "source_vm_id_value" + assert response.display_name == "display_name_value" + assert response.description == "description_value" + assert response.state == vmmigration.MigratingVm.State.PENDING + assert response.group == "group_value" + + +def test_get_migrating_vm_rest_required_fields( + request_type=vmmigration.GetMigratingVmRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_migrating_vm._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_migrating_vm._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("view",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.MigratingVm() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.MigratingVm.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_migrating_vm(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_migrating_vm_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_migrating_vm._get_unset_required_fields({}) + assert set(unset_fields) == (set(("view",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_migrating_vm_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_migrating_vm" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_migrating_vm" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetMigratingVmRequest.pb( + vmmigration.GetMigratingVmRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.MigratingVm.to_json( + vmmigration.MigratingVm() + ) + + request = vmmigration.GetMigratingVmRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.MigratingVm() + + client.get_migrating_vm( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_migrating_vm_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetMigratingVmRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_migrating_vm(request) + + +def test_get_migrating_vm_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.MigratingVm() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.MigratingVm.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_migrating_vm(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/migratingVms/*}" + % client.transport._host, + args[1], + ) + + +def test_get_migrating_vm_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_migrating_vm( + vmmigration.GetMigratingVmRequest(), + name="name_value", + ) + + +def test_get_migrating_vm_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.UpdateMigratingVmRequest, + dict, + ], +) +def test_update_migrating_vm_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + } + request_init["migrating_vm"] = { + "compute_engine_target_defaults": { + "vm_name": "vm_name_value", + "target_project": "target_project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": [ + { + "network": "network_value", + "subnetwork": "subnetwork_value", + "internal_ip": "internal_ip_value", + "external_ip": "external_ip_value", + } + ], + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {"type_": 1, "os_license": "os_license_value"}, + "compute_scheduling": { + "on_host_maintenance": 1, + "restart_type": 1, + "node_affinities": [ + { + "key": "key_value", + "operator": 1, + "values": ["values_value1", "values_value2"], + } + ], + "min_node_cpus": 1379, + }, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "aws_source_vm_details": {"firmware": 1, "committed_storage_bytes": 2464}, + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4", + "source_vm_id": "source_vm_id_value", + "display_name": "display_name_value", + "description": "description_value", + "policy": { + "idle_duration": {"seconds": 751, "nanos": 543}, + "skip_os_adaptation": True, + }, + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "last_sync": {"last_sync_time": {}}, + "state": 1, + "state_time": {}, + "current_sync_info": { + "name": "name_value", + "cycle_number": 1272, + "start_time": {}, + "end_time": {}, + "total_pause_duration": {}, + "progress_percent": 1733, + "steps": [ + { + "initializing_replication": {}, + "replicating": { + "total_bytes": 1194, + "replicated_bytes": 1699, + "last_two_minutes_average_bytes_per_second": 4370, + "last_thirty_minutes_average_bytes_per_second": 4700, + }, + "post_processing": {}, + "start_time": {}, + "end_time": {}, + } + ], + "state": 1, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + }, + "group": "group_value", + "labels": {}, + "recent_clone_jobs": [ + { + "compute_engine_target_details": { + "vm_name": "vm_name_value", + "project": "project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": {}, + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {}, + "compute_scheduling": {}, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "create_time": {}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "error": {}, + "steps": [ + { + "adapting_os": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + ], + "error": {}, + "recent_cutover_jobs": [ + { + "compute_engine_target_details": {}, + "create_time": {}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "progress_percent": 1733, + "error": {}, + "state_message": "state_message_value", + "steps": [ + { + "previous_replication_cycle": {}, + "shutting_down_source_vm": {}, + "final_sync": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_migrating_vm(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_update_migrating_vm_rest_required_fields( + request_type=vmmigration.UpdateMigratingVmRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_migrating_vm._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_migrating_vm._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "update_mask", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_migrating_vm(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_migrating_vm_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_migrating_vm._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "updateMask", + ) + ) + & set(("migratingVm",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_migrating_vm_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_update_migrating_vm" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_update_migrating_vm" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.UpdateMigratingVmRequest.pb( + vmmigration.UpdateMigratingVmRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.UpdateMigratingVmRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.update_migrating_vm( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_migrating_vm_rest_bad_request( + transport: str = "rest", request_type=vmmigration.UpdateMigratingVmRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + } + request_init["migrating_vm"] = { + "compute_engine_target_defaults": { + "vm_name": "vm_name_value", + "target_project": "target_project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": [ + { + "network": "network_value", + "subnetwork": "subnetwork_value", + "internal_ip": "internal_ip_value", + "external_ip": "external_ip_value", + } + ], + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {"type_": 1, "os_license": "os_license_value"}, + "compute_scheduling": { + "on_host_maintenance": 1, + "restart_type": 1, + "node_affinities": [ + { + "key": "key_value", + "operator": 1, + "values": ["values_value1", "values_value2"], + } + ], + "min_node_cpus": 1379, + }, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "aws_source_vm_details": {"firmware": 1, "committed_storage_bytes": 2464}, + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4", + "source_vm_id": "source_vm_id_value", + "display_name": "display_name_value", + "description": "description_value", + "policy": { + "idle_duration": {"seconds": 751, "nanos": 543}, + "skip_os_adaptation": True, + }, + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "last_sync": {"last_sync_time": {}}, + "state": 1, + "state_time": {}, + "current_sync_info": { + "name": "name_value", + "cycle_number": 1272, + "start_time": {}, + "end_time": {}, + "total_pause_duration": {}, + "progress_percent": 1733, + "steps": [ + { + "initializing_replication": {}, + "replicating": { + "total_bytes": 1194, + "replicated_bytes": 1699, + "last_two_minutes_average_bytes_per_second": 4370, + "last_thirty_minutes_average_bytes_per_second": 4700, + }, + "post_processing": {}, + "start_time": {}, + "end_time": {}, + } + ], + "state": 1, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + }, + "group": "group_value", + "labels": {}, + "recent_clone_jobs": [ + { + "compute_engine_target_details": { + "vm_name": "vm_name_value", + "project": "project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": {}, + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {}, + "compute_scheduling": {}, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "create_time": {}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "error": {}, + "steps": [ + { + "adapting_os": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + ], + "error": {}, + "recent_cutover_jobs": [ + { + "compute_engine_target_details": {}, + "create_time": {}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "progress_percent": 1733, + "error": {}, + "state_message": "state_message_value", + "steps": [ + { + "previous_replication_cycle": {}, + "shutting_down_source_vm": {}, + "final_sync": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_migrating_vm(request) + + +def test_update_migrating_vm_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "migrating_vm": { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + } + + # get truthy value for each flattened field + mock_args = dict( + migrating_vm=vmmigration.MigratingVm( + compute_engine_target_defaults=vmmigration.ComputeEngineTargetDefaults( + vm_name="vm_name_value" + ) + ), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_migrating_vm(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{migrating_vm.name=projects/*/locations/*/sources/*/migratingVms/*}" + % client.transport._host, + args[1], + ) + + +def test_update_migrating_vm_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_migrating_vm( + vmmigration.UpdateMigratingVmRequest(), + migrating_vm=vmmigration.MigratingVm( + compute_engine_target_defaults=vmmigration.ComputeEngineTargetDefaults( + vm_name="vm_name_value" + ) + ), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_migrating_vm_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.DeleteMigratingVmRequest, + dict, + ], +) +def test_delete_migrating_vm_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_migrating_vm(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_migrating_vm_rest_required_fields( + request_type=vmmigration.DeleteMigratingVmRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_migrating_vm._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_migrating_vm._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_migrating_vm(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_migrating_vm_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_migrating_vm._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_migrating_vm_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_delete_migrating_vm" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_delete_migrating_vm" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.DeleteMigratingVmRequest.pb( + vmmigration.DeleteMigratingVmRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.DeleteMigratingVmRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_migrating_vm( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_migrating_vm_rest_bad_request( + transport: str = "rest", request_type=vmmigration.DeleteMigratingVmRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_migrating_vm(request) + + +def test_delete_migrating_vm_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_migrating_vm(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/migratingVms/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_migrating_vm_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_migrating_vm( + vmmigration.DeleteMigratingVmRequest(), + name="name_value", + ) + + +def test_delete_migrating_vm_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.StartMigrationRequest, + dict, + ], +) +def test_start_migration_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.start_migration(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_start_migration_rest_required_fields( + request_type=vmmigration.StartMigrationRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["migrating_vm"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).start_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["migratingVm"] = "migrating_vm_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).start_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "migratingVm" in jsonified_request + assert jsonified_request["migratingVm"] == "migrating_vm_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.start_migration(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_start_migration_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.start_migration._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("migratingVm",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_start_migration_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_start_migration" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_start_migration" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.StartMigrationRequest.pb( + vmmigration.StartMigrationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.StartMigrationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.start_migration( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_start_migration_rest_bad_request( + transport: str = "rest", request_type=vmmigration.StartMigrationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.start_migration(request) + + +def test_start_migration_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + migrating_vm="migrating_vm_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.start_migration(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{migrating_vm=projects/*/locations/*/sources/*/migratingVms/*}:startMigration" + % client.transport._host, + args[1], + ) + + +def test_start_migration_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.start_migration( + vmmigration.StartMigrationRequest(), + migrating_vm="migrating_vm_value", + ) + + +def test_start_migration_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ResumeMigrationRequest, + dict, + ], +) +def test_resume_migration_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.resume_migration(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_resume_migration_rest_required_fields( + request_type=vmmigration.ResumeMigrationRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["migrating_vm"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).resume_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["migratingVm"] = "migrating_vm_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).resume_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "migratingVm" in jsonified_request + assert jsonified_request["migratingVm"] == "migrating_vm_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.resume_migration(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_resume_migration_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.resume_migration._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("migratingVm",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_resume_migration_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_resume_migration" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_resume_migration" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ResumeMigrationRequest.pb( + vmmigration.ResumeMigrationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.ResumeMigrationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.resume_migration( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_resume_migration_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ResumeMigrationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.resume_migration(request) + + +def test_resume_migration_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.PauseMigrationRequest, + dict, + ], +) +def test_pause_migration_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.pause_migration(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_pause_migration_rest_required_fields( + request_type=vmmigration.PauseMigrationRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["migrating_vm"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).pause_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["migratingVm"] = "migrating_vm_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).pause_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "migratingVm" in jsonified_request + assert jsonified_request["migratingVm"] == "migrating_vm_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.pause_migration(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_pause_migration_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.pause_migration._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("migratingVm",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_pause_migration_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_pause_migration" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_pause_migration" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.PauseMigrationRequest.pb( + vmmigration.PauseMigrationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.PauseMigrationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.pause_migration( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_pause_migration_rest_bad_request( + transport: str = "rest", request_type=vmmigration.PauseMigrationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.pause_migration(request) + + +def test_pause_migration_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.FinalizeMigrationRequest, + dict, + ], +) +def test_finalize_migration_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.finalize_migration(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_finalize_migration_rest_required_fields( + request_type=vmmigration.FinalizeMigrationRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["migrating_vm"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).finalize_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["migratingVm"] = "migrating_vm_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).finalize_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "migratingVm" in jsonified_request + assert jsonified_request["migratingVm"] == "migrating_vm_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.finalize_migration(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_finalize_migration_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.finalize_migration._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("migratingVm",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_finalize_migration_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_finalize_migration" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_finalize_migration" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.FinalizeMigrationRequest.pb( + vmmigration.FinalizeMigrationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.FinalizeMigrationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.finalize_migration( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_finalize_migration_rest_bad_request( + transport: str = "rest", request_type=vmmigration.FinalizeMigrationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.finalize_migration(request) + + +def test_finalize_migration_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "migrating_vm": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + migrating_vm="migrating_vm_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.finalize_migration(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{migrating_vm=projects/*/locations/*/sources/*/migratingVms/*}:finalizeMigration" + % client.transport._host, + args[1], + ) + + +def test_finalize_migration_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.finalize_migration( + vmmigration.FinalizeMigrationRequest(), + migrating_vm="migrating_vm_value", + ) + + +def test_finalize_migration_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CreateCloneJobRequest, + dict, + ], +) +def test_create_clone_job_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request_init["clone_job"] = { + "compute_engine_target_details": { + "vm_name": "vm_name_value", + "project": "project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": [ + { + "network": "network_value", + "subnetwork": "subnetwork_value", + "internal_ip": "internal_ip_value", + "external_ip": "external_ip_value", + } + ], + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {"type_": 1, "os_license": "os_license_value"}, + "compute_scheduling": { + "on_host_maintenance": 1, + "restart_type": 1, + "node_affinities": [ + { + "key": "key_value", + "operator": 1, + "values": ["values_value1", "values_value2"], + } + ], + "min_node_cpus": 1379, + }, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "create_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "steps": [ + { + "adapting_os": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_clone_job(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_clone_job_rest_required_fields( + request_type=vmmigration.CreateCloneJobRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["clone_job_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "cloneJobId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_clone_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "cloneJobId" in jsonified_request + assert jsonified_request["cloneJobId"] == request_init["clone_job_id"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["cloneJobId"] = "clone_job_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_clone_job._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "clone_job_id", + "request_id", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "cloneJobId" in jsonified_request + assert jsonified_request["cloneJobId"] == "clone_job_id_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_clone_job(request) + + expected_params = [ + ( + "cloneJobId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_clone_job_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_clone_job._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "cloneJobId", + "requestId", + ) + ) + & set( + ( + "parent", + "cloneJobId", + "cloneJob", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_clone_job_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_create_clone_job" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_create_clone_job" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CreateCloneJobRequest.pb( + vmmigration.CreateCloneJobRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CreateCloneJobRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_clone_job( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_clone_job_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CreateCloneJobRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request_init["clone_job"] = { + "compute_engine_target_details": { + "vm_name": "vm_name_value", + "project": "project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": [ + { + "network": "network_value", + "subnetwork": "subnetwork_value", + "internal_ip": "internal_ip_value", + "external_ip": "external_ip_value", + } + ], + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {"type_": 1, "os_license": "os_license_value"}, + "compute_scheduling": { + "on_host_maintenance": 1, + "restart_type": 1, + "node_affinities": [ + { + "key": "key_value", + "operator": 1, + "values": ["values_value1", "values_value2"], + } + ], + "min_node_cpus": 1379, + }, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "create_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "steps": [ + { + "adapting_os": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_clone_job(request) + + +def test_create_clone_job_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + clone_job=vmmigration.CloneJob( + compute_engine_target_details=vmmigration.ComputeEngineTargetDetails( + vm_name="vm_name_value" + ) + ), + clone_job_id="clone_job_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_clone_job(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/cloneJobs" + % client.transport._host, + args[1], + ) + + +def test_create_clone_job_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_clone_job( + vmmigration.CreateCloneJobRequest(), + parent="parent_value", + clone_job=vmmigration.CloneJob( + compute_engine_target_details=vmmigration.ComputeEngineTargetDetails( + vm_name="vm_name_value" + ) + ), + clone_job_id="clone_job_id_value", + ) + + +def test_create_clone_job_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CancelCloneJobRequest, + dict, + ], +) +def test_cancel_clone_job_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cloneJobs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.cancel_clone_job(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_cancel_clone_job_rest_required_fields( + request_type=vmmigration.CancelCloneJobRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).cancel_clone_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).cancel_clone_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.cancel_clone_job(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_cancel_clone_job_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.cancel_clone_job._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_cancel_clone_job_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_cancel_clone_job" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_cancel_clone_job" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CancelCloneJobRequest.pb( + vmmigration.CancelCloneJobRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CancelCloneJobRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.cancel_clone_job( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_cancel_clone_job_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CancelCloneJobRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cloneJobs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.cancel_clone_job(request) + + +def test_cancel_clone_job_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cloneJobs/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.cancel_clone_job(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/cloneJobs/*}:cancel" + % client.transport._host, + args[1], + ) + + +def test_cancel_clone_job_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.cancel_clone_job( + vmmigration.CancelCloneJobRequest(), + name="name_value", + ) + + +def test_cancel_clone_job_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListCloneJobsRequest, + dict, + ], +) +def test_list_clone_jobs_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListCloneJobsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListCloneJobsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_clone_jobs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListCloneJobsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_clone_jobs_rest_required_fields( + request_type=vmmigration.ListCloneJobsRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_clone_jobs._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_clone_jobs._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListCloneJobsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListCloneJobsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_clone_jobs(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_clone_jobs_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_clone_jobs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) + ) + & set( + ( + "parent", + "pageToken", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_clone_jobs_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_clone_jobs" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_clone_jobs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListCloneJobsRequest.pb( + vmmigration.ListCloneJobsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ListCloneJobsResponse.to_json( + vmmigration.ListCloneJobsResponse() + ) + + request = vmmigration.ListCloneJobsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListCloneJobsResponse() + + client.list_clone_jobs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_clone_jobs_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListCloneJobsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_clone_jobs(request) + + +def test_list_clone_jobs_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListCloneJobsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListCloneJobsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_clone_jobs(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/cloneJobs" + % client.transport._host, + args[1], + ) + + +def test_list_clone_jobs_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_clone_jobs( + vmmigration.ListCloneJobsRequest(), + parent="parent_value", + ) + + +def test_list_clone_jobs_rest_pager(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListCloneJobsResponse( + clone_jobs=[ + vmmigration.CloneJob(), + vmmigration.CloneJob(), + vmmigration.CloneJob(), + ], + next_page_token="abc", + ), + vmmigration.ListCloneJobsResponse( + clone_jobs=[], + next_page_token="def", + ), + vmmigration.ListCloneJobsResponse( + clone_jobs=[ + vmmigration.CloneJob(), + ], + next_page_token="ghi", + ), + vmmigration.ListCloneJobsResponse( + clone_jobs=[ + vmmigration.CloneJob(), + vmmigration.CloneJob(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(vmmigration.ListCloneJobsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + pager = client.list_clone_jobs(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.CloneJob) for i in results) + + pages = list(client.list_clone_jobs(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.GetCloneJobRequest, + dict, + ], +) +def test_get_clone_job_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cloneJobs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.CloneJob( + name="name_value", + state=vmmigration.CloneJob.State.PENDING, + compute_engine_target_details=vmmigration.ComputeEngineTargetDetails( + vm_name="vm_name_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.CloneJob.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_clone_job(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.CloneJob) + assert response.name == "name_value" + assert response.state == vmmigration.CloneJob.State.PENDING + + +def test_get_clone_job_rest_required_fields( + request_type=vmmigration.GetCloneJobRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_clone_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_clone_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.CloneJob() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.CloneJob.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_clone_job(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_clone_job_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_clone_job._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_clone_job_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_clone_job" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_clone_job" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetCloneJobRequest.pb(vmmigration.GetCloneJobRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.CloneJob.to_json(vmmigration.CloneJob()) + + request = vmmigration.GetCloneJobRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.CloneJob() + + client.get_clone_job( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_clone_job_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetCloneJobRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cloneJobs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_clone_job(request) + + +def test_get_clone_job_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.CloneJob() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cloneJobs/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.CloneJob.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_clone_job(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/cloneJobs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_clone_job_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_clone_job( + vmmigration.GetCloneJobRequest(), + name="name_value", + ) + + +def test_get_clone_job_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CreateCutoverJobRequest, + dict, + ], +) +def test_create_cutover_job_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request_init["cutover_job"] = { + "compute_engine_target_details": { + "vm_name": "vm_name_value", + "project": "project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": [ + { + "network": "network_value", + "subnetwork": "subnetwork_value", + "internal_ip": "internal_ip_value", + "external_ip": "external_ip_value", + } + ], + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {"type_": 1, "os_license": "os_license_value"}, + "compute_scheduling": { + "on_host_maintenance": 1, + "restart_type": 1, + "node_affinities": [ + { + "key": "key_value", + "operator": 1, + "values": ["values_value1", "values_value2"], + } + ], + "min_node_cpus": 1379, + }, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "create_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "progress_percent": 1733, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "state_message": "state_message_value", + "steps": [ + { + "previous_replication_cycle": { + "name": "name_value", + "cycle_number": 1272, + "start_time": {}, + "end_time": {}, + "total_pause_duration": {"seconds": 751, "nanos": 543}, + "progress_percent": 1733, + "steps": [ + { + "initializing_replication": {}, + "replicating": { + "total_bytes": 1194, + "replicated_bytes": 1699, + "last_two_minutes_average_bytes_per_second": 4370, + "last_thirty_minutes_average_bytes_per_second": 4700, + }, + "post_processing": {}, + "start_time": {}, + "end_time": {}, + } + ], + "state": 1, + "error": {}, + }, + "shutting_down_source_vm": {}, + "final_sync": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_cutover_job(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_cutover_job_rest_required_fields( + request_type=vmmigration.CreateCutoverJobRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["cutover_job_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "cutoverJobId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_cutover_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "cutoverJobId" in jsonified_request + assert jsonified_request["cutoverJobId"] == request_init["cutover_job_id"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["cutoverJobId"] = "cutover_job_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_cutover_job._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "cutover_job_id", + "request_id", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "cutoverJobId" in jsonified_request + assert jsonified_request["cutoverJobId"] == "cutover_job_id_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_cutover_job(request) + + expected_params = [ + ( + "cutoverJobId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_cutover_job_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_cutover_job._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "cutoverJobId", + "requestId", + ) + ) + & set( + ( + "parent", + "cutoverJobId", + "cutoverJob", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_cutover_job_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_create_cutover_job" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_create_cutover_job" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CreateCutoverJobRequest.pb( + vmmigration.CreateCutoverJobRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CreateCutoverJobRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_cutover_job( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_cutover_job_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CreateCutoverJobRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request_init["cutover_job"] = { + "compute_engine_target_details": { + "vm_name": "vm_name_value", + "project": "project_value", + "zone": "zone_value", + "machine_type_series": "machine_type_series_value", + "machine_type": "machine_type_value", + "network_tags": ["network_tags_value1", "network_tags_value2"], + "network_interfaces": [ + { + "network": "network_value", + "subnetwork": "subnetwork_value", + "internal_ip": "internal_ip_value", + "external_ip": "external_ip_value", + } + ], + "service_account": "service_account_value", + "disk_type": 1, + "labels": {}, + "license_type": 1, + "applied_license": {"type_": 1, "os_license": "os_license_value"}, + "compute_scheduling": { + "on_host_maintenance": 1, + "restart_type": 1, + "node_affinities": [ + { + "key": "key_value", + "operator": 1, + "values": ["values_value1", "values_value2"], + } + ], + "min_node_cpus": 1379, + }, + "secure_boot": True, + "boot_option": 1, + "metadata": {}, + "additional_licenses": [ + "additional_licenses_value1", + "additional_licenses_value2", + ], + "hostname": "hostname_value", + }, + "create_time": {"seconds": 751, "nanos": 543}, + "end_time": {}, + "name": "name_value", + "state": 1, + "state_time": {}, + "progress_percent": 1733, + "error": { + "code": 411, + "message": "message_value", + "details": [ + { + "type_url": "type.googleapis.com/google.protobuf.Duration", + "value": b"\x08\x0c\x10\xdb\x07", + } + ], + }, + "state_message": "state_message_value", + "steps": [ + { + "previous_replication_cycle": { + "name": "name_value", + "cycle_number": 1272, + "start_time": {}, + "end_time": {}, + "total_pause_duration": {"seconds": 751, "nanos": 543}, + "progress_percent": 1733, + "steps": [ + { + "initializing_replication": {}, + "replicating": { + "total_bytes": 1194, + "replicated_bytes": 1699, + "last_two_minutes_average_bytes_per_second": 4370, + "last_thirty_minutes_average_bytes_per_second": 4700, + }, + "post_processing": {}, + "start_time": {}, + "end_time": {}, + } + ], + "state": 1, + "error": {}, + }, + "shutting_down_source_vm": {}, + "final_sync": {}, + "preparing_vm_disks": {}, + "instantiating_migrated_vm": {}, + "start_time": {}, + "end_time": {}, + } + ], + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_cutover_job(request) + + +def test_create_cutover_job_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + cutover_job=vmmigration.CutoverJob( + compute_engine_target_details=vmmigration.ComputeEngineTargetDetails( + vm_name="vm_name_value" + ) + ), + cutover_job_id="cutover_job_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_cutover_job(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/cutoverJobs" + % client.transport._host, + args[1], + ) + + +def test_create_cutover_job_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_cutover_job( + vmmigration.CreateCutoverJobRequest(), + parent="parent_value", + cutover_job=vmmigration.CutoverJob( + compute_engine_target_details=vmmigration.ComputeEngineTargetDetails( + vm_name="vm_name_value" + ) + ), + cutover_job_id="cutover_job_id_value", + ) + + +def test_create_cutover_job_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CancelCutoverJobRequest, + dict, + ], +) +def test_cancel_cutover_job_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cutoverJobs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.cancel_cutover_job(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_cancel_cutover_job_rest_required_fields( + request_type=vmmigration.CancelCutoverJobRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).cancel_cutover_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).cancel_cutover_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.cancel_cutover_job(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_cancel_cutover_job_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.cancel_cutover_job._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_cancel_cutover_job_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_cancel_cutover_job" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_cancel_cutover_job" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CancelCutoverJobRequest.pb( + vmmigration.CancelCutoverJobRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CancelCutoverJobRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.cancel_cutover_job( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_cancel_cutover_job_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CancelCutoverJobRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cutoverJobs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.cancel_cutover_job(request) + + +def test_cancel_cutover_job_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cutoverJobs/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.cancel_cutover_job(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/cutoverJobs/*}:cancel" + % client.transport._host, + args[1], + ) + + +def test_cancel_cutover_job_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.cancel_cutover_job( + vmmigration.CancelCutoverJobRequest(), + name="name_value", + ) + + +def test_cancel_cutover_job_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListCutoverJobsRequest, + dict, + ], +) +def test_list_cutover_jobs_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListCutoverJobsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListCutoverJobsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_cutover_jobs(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListCutoverJobsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_cutover_jobs_rest_required_fields( + request_type=vmmigration.ListCutoverJobsRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_cutover_jobs._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_cutover_jobs._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListCutoverJobsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListCutoverJobsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_cutover_jobs(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_cutover_jobs_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_cutover_jobs._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) + ) + & set( + ( + "parent", + "pageToken", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_cutover_jobs_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_cutover_jobs" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_cutover_jobs" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListCutoverJobsRequest.pb( + vmmigration.ListCutoverJobsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ListCutoverJobsResponse.to_json( + vmmigration.ListCutoverJobsResponse() + ) + + request = vmmigration.ListCutoverJobsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListCutoverJobsResponse() + + client.list_cutover_jobs( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_cutover_jobs_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListCutoverJobsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_cutover_jobs(request) + + +def test_list_cutover_jobs_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListCutoverJobsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListCutoverJobsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_cutover_jobs(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/cutoverJobs" + % client.transport._host, + args[1], + ) + + +def test_list_cutover_jobs_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_cutover_jobs( + vmmigration.ListCutoverJobsRequest(), + parent="parent_value", + ) + + +def test_list_cutover_jobs_rest_pager(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListCutoverJobsResponse( + cutover_jobs=[ + vmmigration.CutoverJob(), + vmmigration.CutoverJob(), + vmmigration.CutoverJob(), + ], + next_page_token="abc", + ), + vmmigration.ListCutoverJobsResponse( + cutover_jobs=[], + next_page_token="def", + ), + vmmigration.ListCutoverJobsResponse( + cutover_jobs=[ + vmmigration.CutoverJob(), + ], + next_page_token="ghi", + ), + vmmigration.ListCutoverJobsResponse( + cutover_jobs=[ + vmmigration.CutoverJob(), + vmmigration.CutoverJob(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + vmmigration.ListCutoverJobsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + pager = client.list_cutover_jobs(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.CutoverJob) for i in results) + + pages = list(client.list_cutover_jobs(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.GetCutoverJobRequest, + dict, + ], +) +def test_get_cutover_job_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cutoverJobs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.CutoverJob( + name="name_value", + state=vmmigration.CutoverJob.State.PENDING, + progress_percent=1733, + state_message="state_message_value", + compute_engine_target_details=vmmigration.ComputeEngineTargetDetails( + vm_name="vm_name_value" + ), + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.CutoverJob.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_cutover_job(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.CutoverJob) + assert response.name == "name_value" + assert response.state == vmmigration.CutoverJob.State.PENDING + assert response.progress_percent == 1733 + assert response.state_message == "state_message_value" + + +def test_get_cutover_job_rest_required_fields( + request_type=vmmigration.GetCutoverJobRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_cutover_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_cutover_job._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.CutoverJob() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.CutoverJob.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_cutover_job(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_cutover_job_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_cutover_job._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_cutover_job_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_cutover_job" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_cutover_job" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetCutoverJobRequest.pb( + vmmigration.GetCutoverJobRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.CutoverJob.to_json( + vmmigration.CutoverJob() + ) + + request = vmmigration.GetCutoverJobRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.CutoverJob() + + client.get_cutover_job( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_cutover_job_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetCutoverJobRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cutoverJobs/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_cutover_job(request) + + +def test_get_cutover_job_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.CutoverJob() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/cutoverJobs/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.CutoverJob.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_cutover_job(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/cutoverJobs/*}" + % client.transport._host, + args[1], + ) + + +def test_get_cutover_job_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_cutover_job( + vmmigration.GetCutoverJobRequest(), + name="name_value", + ) + + +def test_get_cutover_job_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListGroupsRequest, + dict, + ], +) +def test_list_groups_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListGroupsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListGroupsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_groups(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListGroupsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_groups_rest_required_fields(request_type=vmmigration.ListGroupsRequest): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_groups._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_groups._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListGroupsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListGroupsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_groups(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_groups_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_groups._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) + ) + & set( + ( + "parent", + "pageToken", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_groups_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_groups" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_groups" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListGroupsRequest.pb(vmmigration.ListGroupsRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ListGroupsResponse.to_json( + vmmigration.ListGroupsResponse() + ) + + request = vmmigration.ListGroupsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListGroupsResponse() + + client.list_groups( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_groups_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListGroupsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_groups(request) + + +def test_list_groups_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListGroupsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListGroupsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_groups(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/groups" % client.transport._host, + args[1], + ) + + +def test_list_groups_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_groups( + vmmigration.ListGroupsRequest(), + parent="parent_value", + ) + + +def test_list_groups_rest_pager(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListGroupsResponse( + groups=[ + vmmigration.Group(), + vmmigration.Group(), + vmmigration.Group(), + ], + next_page_token="abc", + ), + vmmigration.ListGroupsResponse( + groups=[], + next_page_token="def", + ), + vmmigration.ListGroupsResponse( + groups=[ + vmmigration.Group(), + ], + next_page_token="ghi", + ), + vmmigration.ListGroupsResponse( + groups=[ + vmmigration.Group(), + vmmigration.Group(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(vmmigration.ListGroupsResponse.to_json(x) for x in response) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_groups(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.Group) for i in results) + + pages = list(client.list_groups(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.GetGroupRequest, + dict, + ], +) +def test_get_group_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/groups/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.Group( + name="name_value", + description="description_value", + display_name="display_name_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.Group.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_group(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.Group) + assert response.name == "name_value" + assert response.description == "description_value" + assert response.display_name == "display_name_value" + + +def test_get_group_rest_required_fields(request_type=vmmigration.GetGroupRequest): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_group._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_group._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.Group() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.Group.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_group(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_group_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_group._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_group_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_group" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_group" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetGroupRequest.pb(vmmigration.GetGroupRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.Group.to_json(vmmigration.Group()) + + request = vmmigration.GetGroupRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.Group() + + client.get_group( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_group_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetGroupRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/groups/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_group(request) + + +def test_get_group_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.Group() + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/groups/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.Group.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_group(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/groups/*}" % client.transport._host, + args[1], + ) + + +def test_get_group_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_group( + vmmigration.GetGroupRequest(), + name="name_value", + ) + + +def test_get_group_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CreateGroupRequest, + dict, + ], +) +def test_create_group_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["group"] = { + "name": "name_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "description": "description_value", + "display_name": "display_name_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_group(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_group_rest_required_fields(request_type=vmmigration.CreateGroupRequest): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["group_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "groupId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_group._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "groupId" in jsonified_request + assert jsonified_request["groupId"] == request_init["group_id"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["groupId"] = "group_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_group._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "group_id", + "request_id", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "groupId" in jsonified_request + assert jsonified_request["groupId"] == "group_id_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_group(request) + + expected_params = [ + ( + "groupId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_group_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_group._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "groupId", + "requestId", + ) + ) + & set( + ( + "parent", + "groupId", + "group", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_group_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_create_group" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_create_group" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CreateGroupRequest.pb(vmmigration.CreateGroupRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CreateGroupRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_group( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_group_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CreateGroupRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["group"] = { + "name": "name_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "description": "description_value", + "display_name": "display_name_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_group(request) + + +def test_create_group_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + group=vmmigration.Group(name="name_value"), + group_id="group_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_group(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/groups" % client.transport._host, + args[1], + ) + + +def test_create_group_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_group( + vmmigration.CreateGroupRequest(), + parent="parent_value", + group=vmmigration.Group(name="name_value"), + group_id="group_id_value", + ) + + +def test_create_group_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.UpdateGroupRequest, + dict, + ], +) +def test_update_group_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "group": {"name": "projects/sample1/locations/sample2/groups/sample3"} + } + request_init["group"] = { + "name": "projects/sample1/locations/sample2/groups/sample3", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "description": "description_value", + "display_name": "display_name_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_group(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_update_group_rest_required_fields(request_type=vmmigration.UpdateGroupRequest): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_group._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_group._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "update_mask", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_group(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_group_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_group._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "updateMask", + ) + ) + & set(("group",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_group_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_update_group" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_update_group" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.UpdateGroupRequest.pb(vmmigration.UpdateGroupRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.UpdateGroupRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.update_group( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_group_rest_bad_request( + transport: str = "rest", request_type=vmmigration.UpdateGroupRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "group": {"name": "projects/sample1/locations/sample2/groups/sample3"} + } + request_init["group"] = { + "name": "projects/sample1/locations/sample2/groups/sample3", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + "description": "description_value", + "display_name": "display_name_value", + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_group(request) + + +def test_update_group_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "group": {"name": "projects/sample1/locations/sample2/groups/sample3"} + } + + # get truthy value for each flattened field + mock_args = dict( + group=vmmigration.Group(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_group(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{group.name=projects/*/locations/*/groups/*}" + % client.transport._host, + args[1], + ) + + +def test_update_group_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_group( + vmmigration.UpdateGroupRequest(), + group=vmmigration.Group(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_group_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.DeleteGroupRequest, + dict, + ], +) +def test_delete_group_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/groups/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_group(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_group_rest_required_fields(request_type=vmmigration.DeleteGroupRequest): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_group._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_group._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("request_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_group(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_group_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_group._get_unset_required_fields({}) + assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_group_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_delete_group" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_delete_group" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.DeleteGroupRequest.pb(vmmigration.DeleteGroupRequest()) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.DeleteGroupRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_group( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_group_rest_bad_request( + transport: str = "rest", request_type=vmmigration.DeleteGroupRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/groups/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_group(request) + + +def test_delete_group_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"name": "projects/sample1/locations/sample2/groups/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_group(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/groups/*}" % client.transport._host, + args[1], + ) + + +def test_delete_group_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_group( + vmmigration.DeleteGroupRequest(), + name="name_value", + ) + + +def test_delete_group_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.AddGroupMigrationRequest, + dict, + ], +) +def test_add_group_migration_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"group": "projects/sample1/locations/sample2/groups/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.add_group_migration(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_add_group_migration_rest_required_fields( + request_type=vmmigration.AddGroupMigrationRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["group"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).add_group_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["group"] = "group_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).add_group_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "group" in jsonified_request + assert jsonified_request["group"] == "group_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.add_group_migration(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_add_group_migration_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.add_group_migration._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("group",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_add_group_migration_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_add_group_migration" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_add_group_migration" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.AddGroupMigrationRequest.pb( + vmmigration.AddGroupMigrationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.AddGroupMigrationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.add_group_migration( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_add_group_migration_rest_bad_request( + transport: str = "rest", request_type=vmmigration.AddGroupMigrationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"group": "projects/sample1/locations/sample2/groups/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.add_group_migration(request) + + +def test_add_group_migration_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"group": "projects/sample1/locations/sample2/groups/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + group="group_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.add_group_migration(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{group=projects/*/locations/*/groups/*}:addGroupMigration" + % client.transport._host, + args[1], + ) + + +def test_add_group_migration_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.add_group_migration( + vmmigration.AddGroupMigrationRequest(), + group="group_value", + ) + + +def test_add_group_migration_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.RemoveGroupMigrationRequest, + dict, + ], +) +def test_remove_group_migration_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"group": "projects/sample1/locations/sample2/groups/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.remove_group_migration(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_remove_group_migration_rest_required_fields( + request_type=vmmigration.RemoveGroupMigrationRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["group"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).remove_group_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["group"] = "group_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).remove_group_migration._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "group" in jsonified_request + assert jsonified_request["group"] == "group_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.remove_group_migration(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_remove_group_migration_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.remove_group_migration._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("group",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_remove_group_migration_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_remove_group_migration" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_remove_group_migration" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.RemoveGroupMigrationRequest.pb( + vmmigration.RemoveGroupMigrationRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.RemoveGroupMigrationRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.remove_group_migration( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_remove_group_migration_rest_bad_request( + transport: str = "rest", request_type=vmmigration.RemoveGroupMigrationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"group": "projects/sample1/locations/sample2/groups/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.remove_group_migration(request) + + +def test_remove_group_migration_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"group": "projects/sample1/locations/sample2/groups/sample3"} + + # get truthy value for each flattened field + mock_args = dict( + group="group_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.remove_group_migration(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{group=projects/*/locations/*/groups/*}:removeGroupMigration" + % client.transport._host, + args[1], + ) + + +def test_remove_group_migration_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.remove_group_migration( + vmmigration.RemoveGroupMigrationRequest(), + group="group_value", + ) + + +def test_remove_group_migration_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListTargetProjectsRequest, + dict, + ], +) +def test_list_target_projects_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListTargetProjectsResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListTargetProjectsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_target_projects(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListTargetProjectsPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_target_projects_rest_required_fields( + request_type=vmmigration.ListTargetProjectsRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_target_projects._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_target_projects._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListTargetProjectsResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListTargetProjectsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_target_projects(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_target_projects_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_target_projects._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) + ) + & set( + ( + "parent", + "pageToken", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_target_projects_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_target_projects" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_target_projects" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListTargetProjectsRequest.pb( + vmmigration.ListTargetProjectsRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ListTargetProjectsResponse.to_json( + vmmigration.ListTargetProjectsResponse() + ) + + request = vmmigration.ListTargetProjectsRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListTargetProjectsResponse() + + client.list_target_projects( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_target_projects_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListTargetProjectsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_target_projects(request) + + +def test_list_target_projects_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListTargetProjectsResponse() + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListTargetProjectsResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_target_projects(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/targetProjects" + % client.transport._host, + args[1], + ) + + +def test_list_target_projects_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_target_projects( + vmmigration.ListTargetProjectsRequest(), + parent="parent_value", + ) + + +def test_list_target_projects_rest_pager(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListTargetProjectsResponse( + target_projects=[ + vmmigration.TargetProject(), + vmmigration.TargetProject(), + vmmigration.TargetProject(), + ], + next_page_token="abc", + ), + vmmigration.ListTargetProjectsResponse( + target_projects=[], + next_page_token="def", + ), + vmmigration.ListTargetProjectsResponse( + target_projects=[ + vmmigration.TargetProject(), + ], + next_page_token="ghi", + ), + vmmigration.ListTargetProjectsResponse( + target_projects=[ + vmmigration.TargetProject(), + vmmigration.TargetProject(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + vmmigration.ListTargetProjectsResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = {"parent": "projects/sample1/locations/sample2"} + + pager = client.list_target_projects(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.TargetProject) for i in results) + + pages = list(client.list_target_projects(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.GetTargetProjectRequest, + dict, + ], +) +def test_get_target_project_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/targetProjects/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.TargetProject( + name="name_value", + project="project_value", + description="description_value", + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.TargetProject.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_target_project(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.TargetProject) + assert response.name == "name_value" + assert response.project == "project_value" + assert response.description == "description_value" + + +def test_get_target_project_rest_required_fields( + request_type=vmmigration.GetTargetProjectRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_target_project._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_target_project._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.TargetProject() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.TargetProject.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_target_project(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_target_project_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_target_project._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_target_project_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_target_project" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_target_project" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetTargetProjectRequest.pb( + vmmigration.GetTargetProjectRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.TargetProject.to_json( + vmmigration.TargetProject() + ) + + request = vmmigration.GetTargetProjectRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.TargetProject() + + client.get_target_project( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_target_project_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetTargetProjectRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/targetProjects/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_target_project(request) + + +def test_get_target_project_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.TargetProject() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/targetProjects/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.TargetProject.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_target_project(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/targetProjects/*}" + % client.transport._host, + args[1], + ) + + +def test_get_target_project_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_target_project( + vmmigration.GetTargetProjectRequest(), + name="name_value", + ) + + +def test_get_target_project_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.CreateTargetProjectRequest, + dict, + ], +) +def test_create_target_project_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["target_project"] = { + "name": "name_value", + "project": "project_value", + "description": "description_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.create_target_project(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_create_target_project_rest_required_fields( + request_type=vmmigration.CreateTargetProjectRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["target_project_id"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "targetProjectId" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_target_project._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "targetProjectId" in jsonified_request + assert jsonified_request["targetProjectId"] == request_init["target_project_id"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["targetProjectId"] = "target_project_id_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).create_target_project._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "target_project_id", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "targetProjectId" in jsonified_request + assert jsonified_request["targetProjectId"] == "target_project_id_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "post", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.create_target_project(request) + + expected_params = [ + ( + "targetProjectId", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_create_target_project_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.create_target_project._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "targetProjectId", + ) + ) + & set( + ( + "parent", + "targetProjectId", + "targetProject", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_create_target_project_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_create_target_project" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_create_target_project" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.CreateTargetProjectRequest.pb( + vmmigration.CreateTargetProjectRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.CreateTargetProjectRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.create_target_project( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_create_target_project_rest_bad_request( + transport: str = "rest", request_type=vmmigration.CreateTargetProjectRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"parent": "projects/sample1/locations/sample2"} + request_init["target_project"] = { + "name": "name_value", + "project": "project_value", + "description": "description_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.create_target_project(request) + + +def test_create_target_project_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = {"parent": "projects/sample1/locations/sample2"} + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + target_project=vmmigration.TargetProject(name="name_value"), + target_project_id="target_project_id_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.create_target_project(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*}/targetProjects" + % client.transport._host, + args[1], + ) + + +def test_create_target_project_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.create_target_project( + vmmigration.CreateTargetProjectRequest(), + parent="parent_value", + target_project=vmmigration.TargetProject(name="name_value"), + target_project_id="target_project_id_value", + ) + + +def test_create_target_project_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.UpdateTargetProjectRequest, + dict, + ], +) +def test_update_target_project_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "target_project": { + "name": "projects/sample1/locations/sample2/targetProjects/sample3" + } + } + request_init["target_project"] = { + "name": "projects/sample1/locations/sample2/targetProjects/sample3", + "project": "project_value", + "description": "description_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.update_target_project(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_update_target_project_rest_required_fields( + request_type=vmmigration.UpdateTargetProjectRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_target_project._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).update_target_project._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "request_id", + "update_mask", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "patch", + "query_params": pb_request, + } + transcode_result["body"] = pb_request + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.update_target_project(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_update_target_project_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.update_target_project._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "requestId", + "updateMask", + ) + ) + & set(("targetProject",)) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_update_target_project_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_update_target_project" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_update_target_project" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.UpdateTargetProjectRequest.pb( + vmmigration.UpdateTargetProjectRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.UpdateTargetProjectRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.update_target_project( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_update_target_project_rest_bad_request( + transport: str = "rest", request_type=vmmigration.UpdateTargetProjectRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "target_project": { + "name": "projects/sample1/locations/sample2/targetProjects/sample3" + } + } + request_init["target_project"] = { + "name": "projects/sample1/locations/sample2/targetProjects/sample3", + "project": "project_value", + "description": "description_value", + "create_time": {"seconds": 751, "nanos": 543}, + "update_time": {}, + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.update_target_project(request) + + +def test_update_target_project_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "target_project": { + "name": "projects/sample1/locations/sample2/targetProjects/sample3" + } + } + + # get truthy value for each flattened field + mock_args = dict( + target_project=vmmigration.TargetProject(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.update_target_project(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{target_project.name=projects/*/locations/*/targetProjects/*}" + % client.transport._host, + args[1], + ) + + +def test_update_target_project_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.update_target_project( + vmmigration.UpdateTargetProjectRequest(), + target_project=vmmigration.TargetProject(name="name_value"), + update_mask=field_mask_pb2.FieldMask(paths=["paths_value"]), + ) + + +def test_update_target_project_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.DeleteTargetProjectRequest, + dict, + ], +) +def test_delete_target_project_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/targetProjects/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.delete_target_project(request) + + # Establish that the response is the type that we expect. + assert response.operation.name == "operations/spam" + + +def test_delete_target_project_rest_required_fields( + request_type=vmmigration.DeleteTargetProjectRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_target_project._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).delete_target_project._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set(("request_id",)) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "delete", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_target_project(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_delete_target_project_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.delete_target_project._get_unset_required_fields({}) + assert set(unset_fields) == (set(("requestId",)) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_delete_target_project_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + operation.Operation, "_set_result_from_operation" + ), mock.patch.object( + transports.VmMigrationRestInterceptor, "post_delete_target_project" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_delete_target_project" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.DeleteTargetProjectRequest.pb( + vmmigration.DeleteTargetProjectRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = json_format.MessageToJson( + operations_pb2.Operation() + ) + + request = vmmigration.DeleteTargetProjectRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = operations_pb2.Operation() + + client.delete_target_project( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_delete_target_project_rest_bad_request( + transport: str = "rest", request_type=vmmigration.DeleteTargetProjectRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = {"name": "projects/sample1/locations/sample2/targetProjects/sample3"} + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_target_project(request) + + +def test_delete_target_project_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation(name="operations/spam") + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/targetProjects/sample3" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.delete_target_project(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/targetProjects/*}" + % client.transport._host, + args[1], + ) + + +def test_delete_target_project_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.delete_target_project( + vmmigration.DeleteTargetProjectRequest(), + name="name_value", + ) + + +def test_delete_target_project_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.ListReplicationCyclesRequest, + dict, + ], +) +def test_list_replication_cycles_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListReplicationCyclesResponse( + next_page_token="next_page_token_value", + unreachable=["unreachable_value"], + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListReplicationCyclesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.list_replication_cycles(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, pagers.ListReplicationCyclesPager) + assert response.next_page_token == "next_page_token_value" + assert response.unreachable == ["unreachable_value"] + + +def test_list_replication_cycles_rest_required_fields( + request_type=vmmigration.ListReplicationCyclesRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["parent"] = "" + request_init["page_token"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + assert "pageToken" not in jsonified_request + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_replication_cycles._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == request_init["page_token"] + + jsonified_request["parent"] = "parent_value" + jsonified_request["pageToken"] = "page_token_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).list_replication_cycles._get_unset_required_fields(jsonified_request) + # Check that path parameters and body parameters are not mixing in. + assert not set(unset_fields) - set( + ( + "filter", + "order_by", + "page_size", + "page_token", + ) + ) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "parent" in jsonified_request + assert jsonified_request["parent"] == "parent_value" + assert "pageToken" in jsonified_request + assert jsonified_request["pageToken"] == "page_token_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListReplicationCyclesResponse() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ListReplicationCyclesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_replication_cycles(request) + + expected_params = [ + ( + "pageToken", + "", + ), + ("$alt", "json;enum-encoding=int"), + ] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_list_replication_cycles_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.list_replication_cycles._get_unset_required_fields({}) + assert set(unset_fields) == ( + set( + ( + "filter", + "orderBy", + "pageSize", + "pageToken", + ) + ) + & set( + ( + "parent", + "pageToken", + ) + ) + ) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_list_replication_cycles_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_list_replication_cycles" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_list_replication_cycles" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.ListReplicationCyclesRequest.pb( + vmmigration.ListReplicationCyclesRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ListReplicationCyclesResponse.to_json( + vmmigration.ListReplicationCyclesResponse() + ) + + request = vmmigration.ListReplicationCyclesRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ListReplicationCyclesResponse() + + client.list_replication_cycles( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_list_replication_cycles_rest_bad_request( + transport: str = "rest", request_type=vmmigration.ListReplicationCyclesRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_replication_cycles(request) + + +def test_list_replication_cycles_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ListReplicationCyclesResponse() + + # get arguments that satisfy an http rule for this method + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + # get truthy value for each flattened field + mock_args = dict( + parent="parent_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ListReplicationCyclesResponse.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.list_replication_cycles(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{parent=projects/*/locations/*/sources/*/migratingVms/*}/replicationCycles" + % client.transport._host, + args[1], + ) + + +def test_list_replication_cycles_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.list_replication_cycles( + vmmigration.ListReplicationCyclesRequest(), + parent="parent_value", + ) + + +def test_list_replication_cycles_rest_pager(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # TODO(kbandes): remove this mock unless there's a good reason for it. + # with mock.patch.object(path_template, 'transcode') as transcode: + # Set the response as a series of pages + response = ( + vmmigration.ListReplicationCyclesResponse( + replication_cycles=[ + vmmigration.ReplicationCycle(), + vmmigration.ReplicationCycle(), + vmmigration.ReplicationCycle(), + ], + next_page_token="abc", + ), + vmmigration.ListReplicationCyclesResponse( + replication_cycles=[], + next_page_token="def", + ), + vmmigration.ListReplicationCyclesResponse( + replication_cycles=[ + vmmigration.ReplicationCycle(), + ], + next_page_token="ghi", + ), + vmmigration.ListReplicationCyclesResponse( + replication_cycles=[ + vmmigration.ReplicationCycle(), + vmmigration.ReplicationCycle(), + ], + ), + ) + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + vmmigration.ListReplicationCyclesResponse.to_json(x) for x in response + ) + return_values = tuple(Response() for i in response) + for return_val, response_val in zip(return_values, response): + return_val._content = response_val.encode("UTF-8") + return_val.status_code = 200 + req.side_effect = return_values + + sample_request = { + "parent": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4" + } + + pager = client.list_replication_cycles(request=sample_request) + + results = list(pager) + assert len(results) == 6 + assert all(isinstance(i, vmmigration.ReplicationCycle) for i in results) + + pages = list(client.list_replication_cycles(request=sample_request).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + +@pytest.mark.parametrize( + "request_type", + [ + vmmigration.GetReplicationCycleRequest, + dict, + ], +) +def test_get_replication_cycle_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/replicationCycles/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ReplicationCycle( + name="name_value", + cycle_number=1272, + progress_percent=1733, + state=vmmigration.ReplicationCycle.State.RUNNING, + ) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ReplicationCycle.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + response = client.get_replication_cycle(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, vmmigration.ReplicationCycle) + assert response.name == "name_value" + assert response.cycle_number == 1272 + assert response.progress_percent == 1733 + assert response.state == vmmigration.ReplicationCycle.State.RUNNING + + +def test_get_replication_cycle_rest_required_fields( + request_type=vmmigration.GetReplicationCycleRequest, +): + transport_class = transports.VmMigrationRestTransport + + request_init = {} + request_init["name"] = "" + request = request_type(**request_init) + pb_request = request_type.pb(request) + jsonified_request = json.loads( + json_format.MessageToJson( + pb_request, + including_default_value_fields=False, + use_integers_for_enums=False, + ) + ) + + # verify fields with default values are dropped + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_replication_cycle._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with default values are now present + + jsonified_request["name"] = "name_value" + + unset_fields = transport_class( + credentials=ga_credentials.AnonymousCredentials() + ).get_replication_cycle._get_unset_required_fields(jsonified_request) + jsonified_request.update(unset_fields) + + # verify required fields with non-default values are left alone + assert "name" in jsonified_request + assert jsonified_request["name"] == "name_value" + + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request = request_type(**request_init) + + # Designate an appropriate value for the returned response. + return_value = vmmigration.ReplicationCycle() + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # We need to mock transcode() because providing default values + # for required fields will fail the real version if the http_options + # expect actual values for those fields. + with mock.patch.object(path_template, "transcode") as transcode: + # A uri without fields and an empty body will force all the + # request fields to show up in the query_params. + pb_request = request_type.pb(request) + transcode_result = { + "uri": "v1/sample_method", + "method": "get", + "query_params": pb_request, + } + transcode.return_value = transcode_result + + response_value = Response() + response_value.status_code = 200 + + pb_return_value = vmmigration.ReplicationCycle.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_replication_cycle(request) + + expected_params = [("$alt", "json;enum-encoding=int")] + actual_params = req.call_args.kwargs["params"] + assert expected_params == actual_params + + +def test_get_replication_cycle_rest_unset_required_fields(): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials + ) + + unset_fields = transport.get_replication_cycle._get_unset_required_fields({}) + assert set(unset_fields) == (set(()) & set(("name",))) + + +@pytest.mark.parametrize("null_interceptor", [True, False]) +def test_get_replication_cycle_rest_interceptors(null_interceptor): + transport = transports.VmMigrationRestTransport( + credentials=ga_credentials.AnonymousCredentials(), + interceptor=None + if null_interceptor + else transports.VmMigrationRestInterceptor(), + ) + client = VmMigrationClient(transport=transport) + with mock.patch.object( + type(client.transport._session), "request" + ) as req, mock.patch.object( + path_template, "transcode" + ) as transcode, mock.patch.object( + transports.VmMigrationRestInterceptor, "post_get_replication_cycle" + ) as post, mock.patch.object( + transports.VmMigrationRestInterceptor, "pre_get_replication_cycle" + ) as pre: + pre.assert_not_called() + post.assert_not_called() + pb_message = vmmigration.GetReplicationCycleRequest.pb( + vmmigration.GetReplicationCycleRequest() + ) + transcode.return_value = { + "method": "post", + "uri": "my_uri", + "body": pb_message, + "query_params": pb_message, + } + + req.return_value = Response() + req.return_value.status_code = 200 + req.return_value.request = PreparedRequest() + req.return_value._content = vmmigration.ReplicationCycle.to_json( + vmmigration.ReplicationCycle() + ) + + request = vmmigration.GetReplicationCycleRequest() + metadata = [ + ("key", "val"), + ("cephalopod", "squid"), + ] + pre.return_value = request, metadata + post.return_value = vmmigration.ReplicationCycle() + + client.get_replication_cycle( + request, + metadata=[ + ("key", "val"), + ("cephalopod", "squid"), + ], + ) + + pre.assert_called_once() + post.assert_called_once() + + +def test_get_replication_cycle_rest_bad_request( + transport: str = "rest", request_type=vmmigration.GetReplicationCycleRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # send a request that will satisfy transcoding + request_init = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/replicationCycles/sample5" + } + request = request_type(**request_init) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_replication_cycle(request) + + +def test_get_replication_cycle_rest_flattened(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = vmmigration.ReplicationCycle() + + # get arguments that satisfy an http rule for this method + sample_request = { + "name": "projects/sample1/locations/sample2/sources/sample3/migratingVms/sample4/replicationCycles/sample5" + } + + # get truthy value for each flattened field + mock_args = dict( + name="name_value", + ) + mock_args.update(sample_request) + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + pb_return_value = vmmigration.ReplicationCycle.pb(return_value) + json_return_value = json_format.MessageToJson(pb_return_value) + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + client.get_replication_cycle(**mock_args) + + # Establish that the underlying call was made with the expected + # request object values. + assert len(req.mock_calls) == 1 + _, args, _ = req.mock_calls[0] + assert path_template.validate( + "%s/v1/{name=projects/*/locations/*/sources/*/migratingVms/*/replicationCycles/*}" + % client.transport._host, + args[1], + ) + + +def test_get_replication_cycle_rest_flattened_error(transport: str = "rest"): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # Attempting to call a method with both a request object and flattened + # fields is an error. + with pytest.raises(ValueError): + client.get_replication_cycle( + vmmigration.GetReplicationCycleRequest(), + name="name_value", + ) + + +def test_get_replication_cycle_rest_error(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), transport="rest" + ) + + +def test_credentials_transport_error(): + # It is an error to provide credentials and a transport instance. + transport = transports.VmMigrationGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + # It is an error to provide a credentials file and a transport instance. + transport = transports.VmMigrationGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = VmMigrationClient( + client_options={"credentials_file": "credentials.json"}, + transport=transport, + ) + + # It is an error to provide an api_key and a transport instance. + transport = transports.VmMigrationGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + options = client_options.ClientOptions() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = VmMigrationClient( + client_options=options, + transport=transport, + ) + + # It is an error to provide an api_key and a credential. + options = mock.Mock() + options.api_key = "api_key" + with pytest.raises(ValueError): + client = VmMigrationClient( + client_options=options, credentials=ga_credentials.AnonymousCredentials() + ) + + # It is an error to provide scopes and a transport instance. + transport = transports.VmMigrationGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + with pytest.raises(ValueError): + client = VmMigrationClient( + client_options={"scopes": ["1", "2"]}, + transport=transport, + ) + + +def test_transport_instance(): + # A client may be instantiated with a custom transport instance. + transport = transports.VmMigrationGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + client = VmMigrationClient(transport=transport) + assert client.transport is transport + + +def test_transport_get_channel(): + # A client may be instantiated with a custom transport instance. + transport = transports.VmMigrationGrpcTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + transport = transports.VmMigrationGrpcAsyncIOTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + channel = transport.grpc_channel + assert channel + + +@pytest.mark.parametrize( + "transport_class", + [ + transports.VmMigrationGrpcTransport, + transports.VmMigrationGrpcAsyncIOTransport, + transports.VmMigrationRestTransport, + ], +) +def test_transport_adc(transport_class): + # Test default credentials are used if not provided. + with mock.patch.object(google.auth, "default") as adc: + adc.return_value = (ga_credentials.AnonymousCredentials(), None) + transport_class() + adc.assert_called_once() + + +@pytest.mark.parametrize( + "transport_name", + [ + "grpc", + "rest", + ], +) +def test_transport_kind(transport_name): + transport = VmMigrationClient.get_transport_class(transport_name)( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert transport.kind == transport_name + + +def test_transport_grpc_default(): + # A client should use the gRPC transport by default. + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + ) + assert isinstance( + client.transport, + transports.VmMigrationGrpcTransport, + ) + + +def test_vm_migration_base_transport_error(): + # Passing both a credentials object and credentials_file should raise an error + with pytest.raises(core_exceptions.DuplicateCredentialArgs): + transport = transports.VmMigrationTransport( + credentials=ga_credentials.AnonymousCredentials(), + credentials_file="credentials.json", + ) + + +def test_vm_migration_base_transport(): + # Instantiate the base transport. + with mock.patch( + "google.cloud.vmmigration_v1.services.vm_migration.transports.VmMigrationTransport.__init__" + ) as Transport: + Transport.return_value = None + transport = transports.VmMigrationTransport( + credentials=ga_credentials.AnonymousCredentials(), + ) + + # Every method on the transport should just blindly + # raise NotImplementedError. + methods = ( + "list_sources", "get_source", "create_source", "update_source", @@ -13820,6 +28736,7 @@ def test_vm_migration_transport_auth_adc(transport_class): [ transports.VmMigrationGrpcTransport, transports.VmMigrationGrpcAsyncIOTransport, + transports.VmMigrationRestTransport, ], ) def test_vm_migration_transport_auth_gdch_credentials(transport_class): @@ -13914,11 +28831,40 @@ def test_vm_migration_grpc_transport_client_cert_source_for_mtls(transport_class ) +def test_vm_migration_http_transport_client_cert_source_for_mtls(): + cred = ga_credentials.AnonymousCredentials() + with mock.patch( + "google.auth.transport.requests.AuthorizedSession.configure_mtls_channel" + ) as mock_configure_mtls_channel: + transports.VmMigrationRestTransport( + credentials=cred, client_cert_source_for_mtls=client_cert_source_callback + ) + mock_configure_mtls_channel.assert_called_once_with(client_cert_source_callback) + + +def test_vm_migration_rest_lro_client(): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + transport = client.transport + + # Ensure that we have a api-core operations client. + assert isinstance( + transport.operations_client, + operations_v1.AbstractOperationsClient, + ) + + # Ensure that subsequent calls to the property send the exact same object. + assert transport.operations_client is transport.operations_client + + @pytest.mark.parametrize( "transport_name", [ "grpc", "grpc_asyncio", + "rest", ], ) def test_vm_migration_host_no_port(transport_name): @@ -13929,7 +28875,11 @@ def test_vm_migration_host_no_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("vmmigration.googleapis.com:443") + assert client.transport._host == ( + "vmmigration.googleapis.com:443" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://vmmigration.googleapis.com" + ) @pytest.mark.parametrize( @@ -13937,6 +28887,7 @@ def test_vm_migration_host_no_port(transport_name): [ "grpc", "grpc_asyncio", + "rest", ], ) def test_vm_migration_host_with_port(transport_name): @@ -13947,7 +28898,168 @@ def test_vm_migration_host_with_port(transport_name): ), transport=transport_name, ) - assert client.transport._host == ("vmmigration.googleapis.com:8000") + assert client.transport._host == ( + "vmmigration.googleapis.com:8000" + if transport_name in ["grpc", "grpc_asyncio"] + else "https://vmmigration.googleapis.com:8000" + ) + + +@pytest.mark.parametrize( + "transport_name", + [ + "rest", + ], +) +def test_vm_migration_client_transport_session_collision(transport_name): + creds1 = ga_credentials.AnonymousCredentials() + creds2 = ga_credentials.AnonymousCredentials() + client1 = VmMigrationClient( + credentials=creds1, + transport=transport_name, + ) + client2 = VmMigrationClient( + credentials=creds2, + transport=transport_name, + ) + session1 = client1.transport.list_sources._session + session2 = client2.transport.list_sources._session + assert session1 != session2 + session1 = client1.transport.get_source._session + session2 = client2.transport.get_source._session + assert session1 != session2 + session1 = client1.transport.create_source._session + session2 = client2.transport.create_source._session + assert session1 != session2 + session1 = client1.transport.update_source._session + session2 = client2.transport.update_source._session + assert session1 != session2 + session1 = client1.transport.delete_source._session + session2 = client2.transport.delete_source._session + assert session1 != session2 + session1 = client1.transport.fetch_inventory._session + session2 = client2.transport.fetch_inventory._session + assert session1 != session2 + session1 = client1.transport.list_utilization_reports._session + session2 = client2.transport.list_utilization_reports._session + assert session1 != session2 + session1 = client1.transport.get_utilization_report._session + session2 = client2.transport.get_utilization_report._session + assert session1 != session2 + session1 = client1.transport.create_utilization_report._session + session2 = client2.transport.create_utilization_report._session + assert session1 != session2 + session1 = client1.transport.delete_utilization_report._session + session2 = client2.transport.delete_utilization_report._session + assert session1 != session2 + session1 = client1.transport.list_datacenter_connectors._session + session2 = client2.transport.list_datacenter_connectors._session + assert session1 != session2 + session1 = client1.transport.get_datacenter_connector._session + session2 = client2.transport.get_datacenter_connector._session + assert session1 != session2 + session1 = client1.transport.create_datacenter_connector._session + session2 = client2.transport.create_datacenter_connector._session + assert session1 != session2 + session1 = client1.transport.delete_datacenter_connector._session + session2 = client2.transport.delete_datacenter_connector._session + assert session1 != session2 + session1 = client1.transport.upgrade_appliance._session + session2 = client2.transport.upgrade_appliance._session + assert session1 != session2 + session1 = client1.transport.create_migrating_vm._session + session2 = client2.transport.create_migrating_vm._session + assert session1 != session2 + session1 = client1.transport.list_migrating_vms._session + session2 = client2.transport.list_migrating_vms._session + assert session1 != session2 + session1 = client1.transport.get_migrating_vm._session + session2 = client2.transport.get_migrating_vm._session + assert session1 != session2 + session1 = client1.transport.update_migrating_vm._session + session2 = client2.transport.update_migrating_vm._session + assert session1 != session2 + session1 = client1.transport.delete_migrating_vm._session + session2 = client2.transport.delete_migrating_vm._session + assert session1 != session2 + session1 = client1.transport.start_migration._session + session2 = client2.transport.start_migration._session + assert session1 != session2 + session1 = client1.transport.resume_migration._session + session2 = client2.transport.resume_migration._session + assert session1 != session2 + session1 = client1.transport.pause_migration._session + session2 = client2.transport.pause_migration._session + assert session1 != session2 + session1 = client1.transport.finalize_migration._session + session2 = client2.transport.finalize_migration._session + assert session1 != session2 + session1 = client1.transport.create_clone_job._session + session2 = client2.transport.create_clone_job._session + assert session1 != session2 + session1 = client1.transport.cancel_clone_job._session + session2 = client2.transport.cancel_clone_job._session + assert session1 != session2 + session1 = client1.transport.list_clone_jobs._session + session2 = client2.transport.list_clone_jobs._session + assert session1 != session2 + session1 = client1.transport.get_clone_job._session + session2 = client2.transport.get_clone_job._session + assert session1 != session2 + session1 = client1.transport.create_cutover_job._session + session2 = client2.transport.create_cutover_job._session + assert session1 != session2 + session1 = client1.transport.cancel_cutover_job._session + session2 = client2.transport.cancel_cutover_job._session + assert session1 != session2 + session1 = client1.transport.list_cutover_jobs._session + session2 = client2.transport.list_cutover_jobs._session + assert session1 != session2 + session1 = client1.transport.get_cutover_job._session + session2 = client2.transport.get_cutover_job._session + assert session1 != session2 + session1 = client1.transport.list_groups._session + session2 = client2.transport.list_groups._session + assert session1 != session2 + session1 = client1.transport.get_group._session + session2 = client2.transport.get_group._session + assert session1 != session2 + session1 = client1.transport.create_group._session + session2 = client2.transport.create_group._session + assert session1 != session2 + session1 = client1.transport.update_group._session + session2 = client2.transport.update_group._session + assert session1 != session2 + session1 = client1.transport.delete_group._session + session2 = client2.transport.delete_group._session + assert session1 != session2 + session1 = client1.transport.add_group_migration._session + session2 = client2.transport.add_group_migration._session + assert session1 != session2 + session1 = client1.transport.remove_group_migration._session + session2 = client2.transport.remove_group_migration._session + assert session1 != session2 + session1 = client1.transport.list_target_projects._session + session2 = client2.transport.list_target_projects._session + assert session1 != session2 + session1 = client1.transport.get_target_project._session + session2 = client2.transport.get_target_project._session + assert session1 != session2 + session1 = client1.transport.create_target_project._session + session2 = client2.transport.create_target_project._session + assert session1 != session2 + session1 = client1.transport.update_target_project._session + session2 = client2.transport.update_target_project._session + assert session1 != session2 + session1 = client1.transport.delete_target_project._session + session2 = client2.transport.delete_target_project._session + assert session1 != session2 + session1 = client1.transport.list_replication_cycles._session + session2 = client2.transport.list_replication_cycles._session + assert session1 != session2 + session1 = client1.transport.get_replication_cycle._session + session2 = client2.transport.get_replication_cycle._session + assert session1 != session2 def test_vm_migration_grpc_transport_channel(): @@ -14515,6 +29627,352 @@ async def test_transport_close_async(): close.assert_called_once() +def test_get_location_rest_bad_request( + transport: str = "rest", request_type=locations_pb2.GetLocationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_location(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.GetLocationRequest, + dict, + ], +) +def test_get_location_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.Location() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_location(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.Location) + + +def test_list_locations_rest_bad_request( + transport: str = "rest", request_type=locations_pb2.ListLocationsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict({"name": "projects/sample1"}, request) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_locations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + locations_pb2.ListLocationsRequest, + dict, + ], +) +def test_list_locations_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "projects/sample1"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = locations_pb2.ListLocationsResponse() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_locations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, locations_pb2.ListLocationsResponse) + + +def test_cancel_operation_rest_bad_request( + transport: str = "rest", request_type=operations_pb2.CancelOperationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.cancel_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.CancelOperationRequest, + dict, + ], +) +def test_cancel_operation_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "{}" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.cancel_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_delete_operation_rest_bad_request( + transport: str = "rest", request_type=operations_pb2.DeleteOperationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.delete_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.DeleteOperationRequest, + dict, + ], +) +def test_delete_operation_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = None + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = "{}" + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.delete_operation(request) + + # Establish that the response is the type that we expect. + assert response is None + + +def test_get_operation_rest_bad_request( + transport: str = "rest", request_type=operations_pb2.GetOperationRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2/operations/sample3"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.get_operation(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.GetOperationRequest, + dict, + ], +) +def test_get_operation_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "projects/sample1/locations/sample2/operations/sample3"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.Operation() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.get_operation(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.Operation) + + +def test_list_operations_rest_bad_request( + transport: str = "rest", request_type=operations_pb2.ListOperationsRequest +): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport=transport, + ) + + request = request_type() + request = json_format.ParseDict( + {"name": "projects/sample1/locations/sample2"}, request + ) + + # Mock the http request call within the method and fake a BadRequest error. + with mock.patch.object(Session, "request") as req, pytest.raises( + core_exceptions.BadRequest + ): + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 400 + response_value.request = Request() + req.return_value = response_value + client.list_operations(request) + + +@pytest.mark.parametrize( + "request_type", + [ + operations_pb2.ListOperationsRequest, + dict, + ], +) +def test_list_operations_rest(request_type): + client = VmMigrationClient( + credentials=ga_credentials.AnonymousCredentials(), + transport="rest", + ) + request_init = {"name": "projects/sample1/locations/sample2"} + request = request_type(**request_init) + # Mock the http request call within the method and fake a response. + with mock.patch.object(type(client.transport._session), "request") as req: + # Designate an appropriate value for the returned response. + return_value = operations_pb2.ListOperationsResponse() + + # Wrap the value into a proper Response obj + response_value = Response() + response_value.status_code = 200 + json_return_value = json_format.MessageToJson(return_value) + + response_value._content = json_return_value.encode("UTF-8") + req.return_value = response_value + + response = client.list_operations(request) + + # Establish that the response is the type that we expect. + assert isinstance(response, operations_pb2.ListOperationsResponse) + + def test_delete_operation(transport: str = "grpc"): client = VmMigrationClient( credentials=ga_credentials.AnonymousCredentials(), @@ -15371,6 +30829,7 @@ async def test_get_location_from_dict_async(): def test_transport_close(): transports = { + "rest": "_session", "grpc": "_grpc_channel", } @@ -15388,6 +30847,7 @@ def test_transport_close(): def test_client_ctx(): transports = [ + "rest", "grpc", ] for transport in transports: