diff --git a/packages/google-cloud-compute/.kokoro/docs/common.cfg b/packages/google-cloud-compute/.kokoro/docs/common.cfg index 90a255bf8abe..fd5cbe28bdda 100644 --- a/packages/google-cloud-compute/.kokoro/docs/common.cfg +++ b/packages/google-cloud-compute/.kokoro/docs/common.cfg @@ -33,10 +33,10 @@ env_vars: { value: "docs-staging-v2" } -# Don't update the docker image from this build +# It will upload the docker image after successful builds. env_vars: { key: "TRAMPOLINE_IMAGE_UPLOAD" - value: "false" + value: "true" } # It will always build the docker image. diff --git a/packages/google-cloud-compute/.kokoro/docs/docs-presubmit.cfg b/packages/google-cloud-compute/.kokoro/docs/docs-presubmit.cfg index b8c8a7a42315..8fd4ab105d4b 100644 --- a/packages/google-cloud-compute/.kokoro/docs/docs-presubmit.cfg +++ b/packages/google-cloud-compute/.kokoro/docs/docs-presubmit.cfg @@ -10,6 +10,12 @@ env_vars: { value: "gcloud-python-test" } +# We only upload the image in the main `docs` build. +env_vars: { + key: "TRAMPOLINE_IMAGE_UPLOAD" + value: "false" +} + env_vars: { key: "TRAMPOLINE_BUILD_FILE" value: "github/python-compute/.kokoro/build.sh" diff --git a/packages/google-cloud-compute/.kokoro/docs/docs.cfg b/packages/google-cloud-compute/.kokoro/docs/docs.cfg index 18a4c35325b8..8f43917d92fe 100644 --- a/packages/google-cloud-compute/.kokoro/docs/docs.cfg +++ b/packages/google-cloud-compute/.kokoro/docs/docs.cfg @@ -1 +1 @@ -# Format: //devtools/kokoro/config/proto/build.proto +# Format: //devtools/kokoro/config/proto/build.proto \ No newline at end of file diff --git a/packages/google-cloud-compute/.kokoro/test-samples.sh b/packages/google-cloud-compute/.kokoro/test-samples.sh index 25f00b020f18..93993ef7cec7 100755 --- a/packages/google-cloud-compute/.kokoro/test-samples.sh +++ b/packages/google-cloud-compute/.kokoro/test-samples.sh @@ -87,11 +87,11 @@ for file in samples/**/requirements.txt; do python3.6 -m nox -s "$RUN_TESTS_SESSION" EXIT=$? - # If this is a periodic build, send the test log to the FlakyBot. - # See https://github.com/googleapis/repo-automation-bots/tree/master/packages/flakybot. + # If this is a periodic build, send the test log to the Build Cop Bot. + # See https://github.com/googleapis/repo-automation-bots/tree/master/packages/buildcop. if [[ $KOKORO_BUILD_ARTIFACTS_SUBDIR = *"periodic"* ]]; then - chmod +x $KOKORO_GFILE_DIR/linux_amd64/flakybot - $KOKORO_GFILE_DIR/linux_amd64/flakybot + chmod +x $KOKORO_GFILE_DIR/linux_amd64/buildcop + $KOKORO_GFILE_DIR/linux_amd64/buildcop fi if [[ $EXIT -ne 0 ]]; then diff --git a/packages/google-cloud-compute/.kokoro/trampoline_v2.sh b/packages/google-cloud-compute/.kokoro/trampoline_v2.sh index 4af6cdc26dbc..719bcd5ba84d 100755 --- a/packages/google-cloud-compute/.kokoro/trampoline_v2.sh +++ b/packages/google-cloud-compute/.kokoro/trampoline_v2.sh @@ -159,7 +159,7 @@ if [[ -n "${KOKORO_BUILD_ID:-}" ]]; then "KOKORO_GITHUB_COMMIT" "KOKORO_GITHUB_PULL_REQUEST_NUMBER" "KOKORO_GITHUB_PULL_REQUEST_COMMIT" - # For FlakyBot + # For Build Cop Bot "KOKORO_GITHUB_COMMIT_URL" "KOKORO_GITHUB_PULL_REQUEST_URL" ) diff --git a/packages/google-cloud-compute/docs/compute_v1/accelerator_types.rst b/packages/google-cloud-compute/docs/compute_v1/accelerator_types.rst index 8788df128c50..5eb8673b0ad9 100644 --- a/packages/google-cloud-compute/docs/compute_v1/accelerator_types.rst +++ b/packages/google-cloud-compute/docs/compute_v1/accelerator_types.rst @@ -4,3 +4,8 @@ AcceleratorTypes .. automodule:: google.cloud.compute_v1.services.accelerator_types :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.accelerator_types.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/addresses.rst b/packages/google-cloud-compute/docs/compute_v1/addresses.rst index ff9f390e0e0a..0579da50349f 100644 --- a/packages/google-cloud-compute/docs/compute_v1/addresses.rst +++ b/packages/google-cloud-compute/docs/compute_v1/addresses.rst @@ -4,3 +4,8 @@ Addresses .. automodule:: google.cloud.compute_v1.services.addresses :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.addresses.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/autoscalers.rst b/packages/google-cloud-compute/docs/compute_v1/autoscalers.rst index b1934613f279..7e7acb14c0d6 100644 --- a/packages/google-cloud-compute/docs/compute_v1/autoscalers.rst +++ b/packages/google-cloud-compute/docs/compute_v1/autoscalers.rst @@ -4,3 +4,8 @@ Autoscalers .. automodule:: google.cloud.compute_v1.services.autoscalers :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.autoscalers.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/backend_buckets.rst b/packages/google-cloud-compute/docs/compute_v1/backend_buckets.rst index 5ae49505b8c7..6b71dfbb60c3 100644 --- a/packages/google-cloud-compute/docs/compute_v1/backend_buckets.rst +++ b/packages/google-cloud-compute/docs/compute_v1/backend_buckets.rst @@ -4,3 +4,8 @@ BackendBuckets .. automodule:: google.cloud.compute_v1.services.backend_buckets :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.backend_buckets.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/backend_services.rst b/packages/google-cloud-compute/docs/compute_v1/backend_services.rst index 4ae802216286..9d387953b5d2 100644 --- a/packages/google-cloud-compute/docs/compute_v1/backend_services.rst +++ b/packages/google-cloud-compute/docs/compute_v1/backend_services.rst @@ -4,3 +4,8 @@ BackendServices .. automodule:: google.cloud.compute_v1.services.backend_services :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.backend_services.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/disk_types.rst b/packages/google-cloud-compute/docs/compute_v1/disk_types.rst index 685a6985e57e..a4853da43416 100644 --- a/packages/google-cloud-compute/docs/compute_v1/disk_types.rst +++ b/packages/google-cloud-compute/docs/compute_v1/disk_types.rst @@ -4,3 +4,8 @@ DiskTypes .. automodule:: google.cloud.compute_v1.services.disk_types :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.disk_types.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/disks.rst b/packages/google-cloud-compute/docs/compute_v1/disks.rst index b3979d150ee8..154429e8ad0c 100644 --- a/packages/google-cloud-compute/docs/compute_v1/disks.rst +++ b/packages/google-cloud-compute/docs/compute_v1/disks.rst @@ -4,3 +4,8 @@ Disks .. automodule:: google.cloud.compute_v1.services.disks :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.disks.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/external_vpn_gateways.rst b/packages/google-cloud-compute/docs/compute_v1/external_vpn_gateways.rst index 0945902af345..ff28cc2eabd3 100644 --- a/packages/google-cloud-compute/docs/compute_v1/external_vpn_gateways.rst +++ b/packages/google-cloud-compute/docs/compute_v1/external_vpn_gateways.rst @@ -4,3 +4,8 @@ ExternalVpnGateways .. automodule:: google.cloud.compute_v1.services.external_vpn_gateways :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.external_vpn_gateways.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/firewalls.rst b/packages/google-cloud-compute/docs/compute_v1/firewalls.rst index 81ac2d1660ad..ef6049d7cda6 100644 --- a/packages/google-cloud-compute/docs/compute_v1/firewalls.rst +++ b/packages/google-cloud-compute/docs/compute_v1/firewalls.rst @@ -4,3 +4,8 @@ Firewalls .. automodule:: google.cloud.compute_v1.services.firewalls :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.firewalls.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/forwarding_rules.rst b/packages/google-cloud-compute/docs/compute_v1/forwarding_rules.rst index 7d16f9de536f..08b543649963 100644 --- a/packages/google-cloud-compute/docs/compute_v1/forwarding_rules.rst +++ b/packages/google-cloud-compute/docs/compute_v1/forwarding_rules.rst @@ -4,3 +4,8 @@ ForwardingRules .. automodule:: google.cloud.compute_v1.services.forwarding_rules :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.forwarding_rules.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/global_addresses.rst b/packages/google-cloud-compute/docs/compute_v1/global_addresses.rst index 7ca81d5c53e5..8755a1855c45 100644 --- a/packages/google-cloud-compute/docs/compute_v1/global_addresses.rst +++ b/packages/google-cloud-compute/docs/compute_v1/global_addresses.rst @@ -4,3 +4,8 @@ GlobalAddresses .. automodule:: google.cloud.compute_v1.services.global_addresses :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.global_addresses.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/global_forwarding_rules.rst b/packages/google-cloud-compute/docs/compute_v1/global_forwarding_rules.rst index b4a2a8381f4e..a54eb4b0ac44 100644 --- a/packages/google-cloud-compute/docs/compute_v1/global_forwarding_rules.rst +++ b/packages/google-cloud-compute/docs/compute_v1/global_forwarding_rules.rst @@ -4,3 +4,8 @@ GlobalForwardingRules .. automodule:: google.cloud.compute_v1.services.global_forwarding_rules :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.global_forwarding_rules.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/global_network_endpoint_groups.rst b/packages/google-cloud-compute/docs/compute_v1/global_network_endpoint_groups.rst index c4962be837ec..430a202f2fd2 100644 --- a/packages/google-cloud-compute/docs/compute_v1/global_network_endpoint_groups.rst +++ b/packages/google-cloud-compute/docs/compute_v1/global_network_endpoint_groups.rst @@ -4,3 +4,8 @@ GlobalNetworkEndpointGroups .. automodule:: google.cloud.compute_v1.services.global_network_endpoint_groups :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.global_network_endpoint_groups.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/global_operations.rst b/packages/google-cloud-compute/docs/compute_v1/global_operations.rst index c9280a004e11..6c4cef346f1a 100644 --- a/packages/google-cloud-compute/docs/compute_v1/global_operations.rst +++ b/packages/google-cloud-compute/docs/compute_v1/global_operations.rst @@ -4,3 +4,8 @@ GlobalOperations .. automodule:: google.cloud.compute_v1.services.global_operations :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.global_operations.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/global_organization_operations.rst b/packages/google-cloud-compute/docs/compute_v1/global_organization_operations.rst index 5a415898b5a7..2fb034517031 100644 --- a/packages/google-cloud-compute/docs/compute_v1/global_organization_operations.rst +++ b/packages/google-cloud-compute/docs/compute_v1/global_organization_operations.rst @@ -4,3 +4,8 @@ GlobalOrganizationOperations .. automodule:: google.cloud.compute_v1.services.global_organization_operations :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.global_organization_operations.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/health_checks.rst b/packages/google-cloud-compute/docs/compute_v1/health_checks.rst index cfa091319626..68e2841a2c2e 100644 --- a/packages/google-cloud-compute/docs/compute_v1/health_checks.rst +++ b/packages/google-cloud-compute/docs/compute_v1/health_checks.rst @@ -4,3 +4,8 @@ HealthChecks .. automodule:: google.cloud.compute_v1.services.health_checks :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.health_checks.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/images.rst b/packages/google-cloud-compute/docs/compute_v1/images.rst index 3ad9732dd03c..318fd80733e6 100644 --- a/packages/google-cloud-compute/docs/compute_v1/images.rst +++ b/packages/google-cloud-compute/docs/compute_v1/images.rst @@ -4,3 +4,8 @@ Images .. automodule:: google.cloud.compute_v1.services.images :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.images.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/instance_group_managers.rst b/packages/google-cloud-compute/docs/compute_v1/instance_group_managers.rst index aed206b3b3d8..0010acecf191 100644 --- a/packages/google-cloud-compute/docs/compute_v1/instance_group_managers.rst +++ b/packages/google-cloud-compute/docs/compute_v1/instance_group_managers.rst @@ -4,3 +4,8 @@ InstanceGroupManagers .. automodule:: google.cloud.compute_v1.services.instance_group_managers :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.instance_group_managers.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/instance_groups.rst b/packages/google-cloud-compute/docs/compute_v1/instance_groups.rst index 35a4b178d51d..094150adb3f2 100644 --- a/packages/google-cloud-compute/docs/compute_v1/instance_groups.rst +++ b/packages/google-cloud-compute/docs/compute_v1/instance_groups.rst @@ -4,3 +4,8 @@ InstanceGroups .. automodule:: google.cloud.compute_v1.services.instance_groups :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.instance_groups.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/instance_templates.rst b/packages/google-cloud-compute/docs/compute_v1/instance_templates.rst index 4f0effd30704..35a6f2f0976f 100644 --- a/packages/google-cloud-compute/docs/compute_v1/instance_templates.rst +++ b/packages/google-cloud-compute/docs/compute_v1/instance_templates.rst @@ -4,3 +4,8 @@ InstanceTemplates .. automodule:: google.cloud.compute_v1.services.instance_templates :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.instance_templates.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/instances.rst b/packages/google-cloud-compute/docs/compute_v1/instances.rst index 2d63480dabcc..f8ba5f34b8ce 100644 --- a/packages/google-cloud-compute/docs/compute_v1/instances.rst +++ b/packages/google-cloud-compute/docs/compute_v1/instances.rst @@ -4,3 +4,8 @@ Instances .. automodule:: google.cloud.compute_v1.services.instances :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.instances.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/interconnect_attachments.rst b/packages/google-cloud-compute/docs/compute_v1/interconnect_attachments.rst index 0aa91012585d..3356e3459693 100644 --- a/packages/google-cloud-compute/docs/compute_v1/interconnect_attachments.rst +++ b/packages/google-cloud-compute/docs/compute_v1/interconnect_attachments.rst @@ -4,3 +4,8 @@ InterconnectAttachments .. automodule:: google.cloud.compute_v1.services.interconnect_attachments :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.interconnect_attachments.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/interconnect_locations.rst b/packages/google-cloud-compute/docs/compute_v1/interconnect_locations.rst index f78790ca778a..bfb2d7c8794d 100644 --- a/packages/google-cloud-compute/docs/compute_v1/interconnect_locations.rst +++ b/packages/google-cloud-compute/docs/compute_v1/interconnect_locations.rst @@ -4,3 +4,8 @@ InterconnectLocations .. automodule:: google.cloud.compute_v1.services.interconnect_locations :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.interconnect_locations.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/interconnects.rst b/packages/google-cloud-compute/docs/compute_v1/interconnects.rst index 7324ff16c9af..d33ff5d3cf5d 100644 --- a/packages/google-cloud-compute/docs/compute_v1/interconnects.rst +++ b/packages/google-cloud-compute/docs/compute_v1/interconnects.rst @@ -4,3 +4,8 @@ Interconnects .. automodule:: google.cloud.compute_v1.services.interconnects :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.interconnects.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/licenses.rst b/packages/google-cloud-compute/docs/compute_v1/licenses.rst index f3807c747aee..4684df852cf0 100644 --- a/packages/google-cloud-compute/docs/compute_v1/licenses.rst +++ b/packages/google-cloud-compute/docs/compute_v1/licenses.rst @@ -4,3 +4,8 @@ Licenses .. automodule:: google.cloud.compute_v1.services.licenses :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.licenses.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/machine_types.rst b/packages/google-cloud-compute/docs/compute_v1/machine_types.rst index aad7d73784c8..768b2d4493ae 100644 --- a/packages/google-cloud-compute/docs/compute_v1/machine_types.rst +++ b/packages/google-cloud-compute/docs/compute_v1/machine_types.rst @@ -4,3 +4,8 @@ MachineTypes .. automodule:: google.cloud.compute_v1.services.machine_types :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.machine_types.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/network_endpoint_groups.rst b/packages/google-cloud-compute/docs/compute_v1/network_endpoint_groups.rst index cc230e4cbe84..3a1a44deea60 100644 --- a/packages/google-cloud-compute/docs/compute_v1/network_endpoint_groups.rst +++ b/packages/google-cloud-compute/docs/compute_v1/network_endpoint_groups.rst @@ -4,3 +4,8 @@ NetworkEndpointGroups .. automodule:: google.cloud.compute_v1.services.network_endpoint_groups :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.network_endpoint_groups.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/networks.rst b/packages/google-cloud-compute/docs/compute_v1/networks.rst index ba328fd42bd6..91273a803710 100644 --- a/packages/google-cloud-compute/docs/compute_v1/networks.rst +++ b/packages/google-cloud-compute/docs/compute_v1/networks.rst @@ -4,3 +4,8 @@ Networks .. automodule:: google.cloud.compute_v1.services.networks :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.networks.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/node_groups.rst b/packages/google-cloud-compute/docs/compute_v1/node_groups.rst index ef9e7e5a57ea..95b5c580b3c7 100644 --- a/packages/google-cloud-compute/docs/compute_v1/node_groups.rst +++ b/packages/google-cloud-compute/docs/compute_v1/node_groups.rst @@ -4,3 +4,8 @@ NodeGroups .. automodule:: google.cloud.compute_v1.services.node_groups :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.node_groups.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/node_templates.rst b/packages/google-cloud-compute/docs/compute_v1/node_templates.rst index 1ef57c0c8a3d..d6b5c39db2b3 100644 --- a/packages/google-cloud-compute/docs/compute_v1/node_templates.rst +++ b/packages/google-cloud-compute/docs/compute_v1/node_templates.rst @@ -4,3 +4,8 @@ NodeTemplates .. automodule:: google.cloud.compute_v1.services.node_templates :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.node_templates.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/node_types.rst b/packages/google-cloud-compute/docs/compute_v1/node_types.rst index 7ee2ecbcecf6..f5723f0fc4c9 100644 --- a/packages/google-cloud-compute/docs/compute_v1/node_types.rst +++ b/packages/google-cloud-compute/docs/compute_v1/node_types.rst @@ -4,3 +4,8 @@ NodeTypes .. automodule:: google.cloud.compute_v1.services.node_types :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.node_types.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/packet_mirrorings.rst b/packages/google-cloud-compute/docs/compute_v1/packet_mirrorings.rst index 4f6a68cb0c06..77d59a21710f 100644 --- a/packages/google-cloud-compute/docs/compute_v1/packet_mirrorings.rst +++ b/packages/google-cloud-compute/docs/compute_v1/packet_mirrorings.rst @@ -4,3 +4,8 @@ PacketMirrorings .. automodule:: google.cloud.compute_v1.services.packet_mirrorings :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.packet_mirrorings.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/projects.rst b/packages/google-cloud-compute/docs/compute_v1/projects.rst index 7df93144741a..deb8641a1068 100644 --- a/packages/google-cloud-compute/docs/compute_v1/projects.rst +++ b/packages/google-cloud-compute/docs/compute_v1/projects.rst @@ -4,3 +4,8 @@ Projects .. automodule:: google.cloud.compute_v1.services.projects :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.projects.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_autoscalers.rst b/packages/google-cloud-compute/docs/compute_v1/region_autoscalers.rst index df6b30705347..b491e22e2132 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_autoscalers.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_autoscalers.rst @@ -4,3 +4,8 @@ RegionAutoscalers .. automodule:: google.cloud.compute_v1.services.region_autoscalers :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_autoscalers.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_backend_services.rst b/packages/google-cloud-compute/docs/compute_v1/region_backend_services.rst index 3a19c6104703..bbfda5cf4f38 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_backend_services.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_backend_services.rst @@ -4,3 +4,8 @@ RegionBackendServices .. automodule:: google.cloud.compute_v1.services.region_backend_services :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_backend_services.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_commitments.rst b/packages/google-cloud-compute/docs/compute_v1/region_commitments.rst index c9e747a37964..141ad66e728f 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_commitments.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_commitments.rst @@ -4,3 +4,8 @@ RegionCommitments .. automodule:: google.cloud.compute_v1.services.region_commitments :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_commitments.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_disk_types.rst b/packages/google-cloud-compute/docs/compute_v1/region_disk_types.rst index 0eb6fc286ad4..f46e6258e28e 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_disk_types.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_disk_types.rst @@ -4,3 +4,8 @@ RegionDiskTypes .. automodule:: google.cloud.compute_v1.services.region_disk_types :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_disk_types.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_disks.rst b/packages/google-cloud-compute/docs/compute_v1/region_disks.rst index 8812bc1420f2..689b917a236d 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_disks.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_disks.rst @@ -4,3 +4,8 @@ RegionDisks .. automodule:: google.cloud.compute_v1.services.region_disks :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_disks.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_health_check_services.rst b/packages/google-cloud-compute/docs/compute_v1/region_health_check_services.rst index 3020a1d36dcd..8c342c0ad7a4 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_health_check_services.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_health_check_services.rst @@ -4,3 +4,8 @@ RegionHealthCheckServices .. automodule:: google.cloud.compute_v1.services.region_health_check_services :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_health_check_services.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_health_checks.rst b/packages/google-cloud-compute/docs/compute_v1/region_health_checks.rst index bc916ce1a037..631de0dfc922 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_health_checks.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_health_checks.rst @@ -4,3 +4,8 @@ RegionHealthChecks .. automodule:: google.cloud.compute_v1.services.region_health_checks :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_health_checks.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_instance_group_managers.rst b/packages/google-cloud-compute/docs/compute_v1/region_instance_group_managers.rst index 7cc14091dbe6..fd3af5961e49 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_instance_group_managers.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_instance_group_managers.rst @@ -4,3 +4,8 @@ RegionInstanceGroupManagers .. automodule:: google.cloud.compute_v1.services.region_instance_group_managers :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_instance_group_managers.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_instance_groups.rst b/packages/google-cloud-compute/docs/compute_v1/region_instance_groups.rst index 22161a072383..473dc65e936d 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_instance_groups.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_instance_groups.rst @@ -4,3 +4,8 @@ RegionInstanceGroups .. automodule:: google.cloud.compute_v1.services.region_instance_groups :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_instance_groups.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_network_endpoint_groups.rst b/packages/google-cloud-compute/docs/compute_v1/region_network_endpoint_groups.rst index 74ccc559b46d..d16d391cf371 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_network_endpoint_groups.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_network_endpoint_groups.rst @@ -4,3 +4,8 @@ RegionNetworkEndpointGroups .. automodule:: google.cloud.compute_v1.services.region_network_endpoint_groups :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_network_endpoint_groups.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_notification_endpoints.rst b/packages/google-cloud-compute/docs/compute_v1/region_notification_endpoints.rst index 6924c1768506..4baf0ced51f9 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_notification_endpoints.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_notification_endpoints.rst @@ -4,3 +4,8 @@ RegionNotificationEndpoints .. automodule:: google.cloud.compute_v1.services.region_notification_endpoints :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_notification_endpoints.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_operations.rst b/packages/google-cloud-compute/docs/compute_v1/region_operations.rst index 9bf0248adeef..be2ef7bf9d7f 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_operations.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_operations.rst @@ -4,3 +4,8 @@ RegionOperations .. automodule:: google.cloud.compute_v1.services.region_operations :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_operations.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_ssl_certificates.rst b/packages/google-cloud-compute/docs/compute_v1/region_ssl_certificates.rst index 1b45f988bfc9..6de5c7c4b713 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_ssl_certificates.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_ssl_certificates.rst @@ -4,3 +4,8 @@ RegionSslCertificates .. automodule:: google.cloud.compute_v1.services.region_ssl_certificates :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_ssl_certificates.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_target_http_proxies.rst b/packages/google-cloud-compute/docs/compute_v1/region_target_http_proxies.rst index 8677488249e7..f157d3bb7d11 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_target_http_proxies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_target_http_proxies.rst @@ -4,3 +4,8 @@ RegionTargetHttpProxies .. automodule:: google.cloud.compute_v1.services.region_target_http_proxies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_target_http_proxies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_target_https_proxies.rst b/packages/google-cloud-compute/docs/compute_v1/region_target_https_proxies.rst index 0eb410395944..3b5c715b4f88 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_target_https_proxies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_target_https_proxies.rst @@ -4,3 +4,8 @@ RegionTargetHttpsProxies .. automodule:: google.cloud.compute_v1.services.region_target_https_proxies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_target_https_proxies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/region_url_maps.rst b/packages/google-cloud-compute/docs/compute_v1/region_url_maps.rst index 2a7d379044f1..e01a03f0d3d7 100644 --- a/packages/google-cloud-compute/docs/compute_v1/region_url_maps.rst +++ b/packages/google-cloud-compute/docs/compute_v1/region_url_maps.rst @@ -4,3 +4,8 @@ RegionUrlMaps .. automodule:: google.cloud.compute_v1.services.region_url_maps :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.region_url_maps.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/regions.rst b/packages/google-cloud-compute/docs/compute_v1/regions.rst index d547521f604f..f1128793b888 100644 --- a/packages/google-cloud-compute/docs/compute_v1/regions.rst +++ b/packages/google-cloud-compute/docs/compute_v1/regions.rst @@ -4,3 +4,8 @@ Regions .. automodule:: google.cloud.compute_v1.services.regions :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.regions.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/reservations.rst b/packages/google-cloud-compute/docs/compute_v1/reservations.rst index f9913af5e447..14f4dce81457 100644 --- a/packages/google-cloud-compute/docs/compute_v1/reservations.rst +++ b/packages/google-cloud-compute/docs/compute_v1/reservations.rst @@ -4,3 +4,8 @@ Reservations .. automodule:: google.cloud.compute_v1.services.reservations :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.reservations.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/resource_policies.rst b/packages/google-cloud-compute/docs/compute_v1/resource_policies.rst index eceb888d6267..6ca37afc2fba 100644 --- a/packages/google-cloud-compute/docs/compute_v1/resource_policies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/resource_policies.rst @@ -4,3 +4,8 @@ ResourcePolicies .. automodule:: google.cloud.compute_v1.services.resource_policies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.resource_policies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/routers.rst b/packages/google-cloud-compute/docs/compute_v1/routers.rst index b35977a2c15b..dac1975e4c3a 100644 --- a/packages/google-cloud-compute/docs/compute_v1/routers.rst +++ b/packages/google-cloud-compute/docs/compute_v1/routers.rst @@ -4,3 +4,8 @@ Routers .. automodule:: google.cloud.compute_v1.services.routers :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.routers.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/routes.rst b/packages/google-cloud-compute/docs/compute_v1/routes.rst index b6c784132507..8a14f50e6fbe 100644 --- a/packages/google-cloud-compute/docs/compute_v1/routes.rst +++ b/packages/google-cloud-compute/docs/compute_v1/routes.rst @@ -4,3 +4,8 @@ Routes .. automodule:: google.cloud.compute_v1.services.routes :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.routes.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/security_policies.rst b/packages/google-cloud-compute/docs/compute_v1/security_policies.rst index bf60efa18ee3..c4bc386901b6 100644 --- a/packages/google-cloud-compute/docs/compute_v1/security_policies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/security_policies.rst @@ -4,3 +4,8 @@ SecurityPolicies .. automodule:: google.cloud.compute_v1.services.security_policies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.security_policies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/snapshots.rst b/packages/google-cloud-compute/docs/compute_v1/snapshots.rst index 8265d6d19b87..1af9eed276dc 100644 --- a/packages/google-cloud-compute/docs/compute_v1/snapshots.rst +++ b/packages/google-cloud-compute/docs/compute_v1/snapshots.rst @@ -4,3 +4,8 @@ Snapshots .. automodule:: google.cloud.compute_v1.services.snapshots :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.snapshots.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/ssl_certificates.rst b/packages/google-cloud-compute/docs/compute_v1/ssl_certificates.rst index ca48ca54cdba..c63ade587ded 100644 --- a/packages/google-cloud-compute/docs/compute_v1/ssl_certificates.rst +++ b/packages/google-cloud-compute/docs/compute_v1/ssl_certificates.rst @@ -4,3 +4,8 @@ SslCertificates .. automodule:: google.cloud.compute_v1.services.ssl_certificates :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.ssl_certificates.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/ssl_policies.rst b/packages/google-cloud-compute/docs/compute_v1/ssl_policies.rst index 094179f70a57..ddb6a78b6975 100644 --- a/packages/google-cloud-compute/docs/compute_v1/ssl_policies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/ssl_policies.rst @@ -4,3 +4,8 @@ SslPolicies .. automodule:: google.cloud.compute_v1.services.ssl_policies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.ssl_policies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/subnetworks.rst b/packages/google-cloud-compute/docs/compute_v1/subnetworks.rst index 1469d7ba4e0c..c99a21d05289 100644 --- a/packages/google-cloud-compute/docs/compute_v1/subnetworks.rst +++ b/packages/google-cloud-compute/docs/compute_v1/subnetworks.rst @@ -4,3 +4,8 @@ Subnetworks .. automodule:: google.cloud.compute_v1.services.subnetworks :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.subnetworks.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/target_grpc_proxies.rst b/packages/google-cloud-compute/docs/compute_v1/target_grpc_proxies.rst index d36a5ed3d98c..302c723981c4 100644 --- a/packages/google-cloud-compute/docs/compute_v1/target_grpc_proxies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/target_grpc_proxies.rst @@ -4,3 +4,8 @@ TargetGrpcProxies .. automodule:: google.cloud.compute_v1.services.target_grpc_proxies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.target_grpc_proxies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/target_http_proxies.rst b/packages/google-cloud-compute/docs/compute_v1/target_http_proxies.rst index dd082c59bb22..ce0cd5a42dcb 100644 --- a/packages/google-cloud-compute/docs/compute_v1/target_http_proxies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/target_http_proxies.rst @@ -4,3 +4,8 @@ TargetHttpProxies .. automodule:: google.cloud.compute_v1.services.target_http_proxies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.target_http_proxies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/target_https_proxies.rst b/packages/google-cloud-compute/docs/compute_v1/target_https_proxies.rst index d05206031f8b..0331656faf96 100644 --- a/packages/google-cloud-compute/docs/compute_v1/target_https_proxies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/target_https_proxies.rst @@ -4,3 +4,8 @@ TargetHttpsProxies .. automodule:: google.cloud.compute_v1.services.target_https_proxies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.target_https_proxies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/target_instances.rst b/packages/google-cloud-compute/docs/compute_v1/target_instances.rst index c9421cf2de56..840d1903eaa4 100644 --- a/packages/google-cloud-compute/docs/compute_v1/target_instances.rst +++ b/packages/google-cloud-compute/docs/compute_v1/target_instances.rst @@ -4,3 +4,8 @@ TargetInstances .. automodule:: google.cloud.compute_v1.services.target_instances :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.target_instances.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/target_pools.rst b/packages/google-cloud-compute/docs/compute_v1/target_pools.rst index 719aa36610f7..3898ef46dc8f 100644 --- a/packages/google-cloud-compute/docs/compute_v1/target_pools.rst +++ b/packages/google-cloud-compute/docs/compute_v1/target_pools.rst @@ -4,3 +4,8 @@ TargetPools .. automodule:: google.cloud.compute_v1.services.target_pools :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.target_pools.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/target_ssl_proxies.rst b/packages/google-cloud-compute/docs/compute_v1/target_ssl_proxies.rst index 21b5d9edbf96..f7a2d95cee26 100644 --- a/packages/google-cloud-compute/docs/compute_v1/target_ssl_proxies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/target_ssl_proxies.rst @@ -4,3 +4,8 @@ TargetSslProxies .. automodule:: google.cloud.compute_v1.services.target_ssl_proxies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.target_ssl_proxies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/target_tcp_proxies.rst b/packages/google-cloud-compute/docs/compute_v1/target_tcp_proxies.rst index 2f259b3090eb..66b3d186fd03 100644 --- a/packages/google-cloud-compute/docs/compute_v1/target_tcp_proxies.rst +++ b/packages/google-cloud-compute/docs/compute_v1/target_tcp_proxies.rst @@ -4,3 +4,8 @@ TargetTcpProxies .. automodule:: google.cloud.compute_v1.services.target_tcp_proxies :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.target_tcp_proxies.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/target_vpn_gateways.rst b/packages/google-cloud-compute/docs/compute_v1/target_vpn_gateways.rst index f9690ebc949a..f1f42c8adbd3 100644 --- a/packages/google-cloud-compute/docs/compute_v1/target_vpn_gateways.rst +++ b/packages/google-cloud-compute/docs/compute_v1/target_vpn_gateways.rst @@ -4,3 +4,8 @@ TargetVpnGateways .. automodule:: google.cloud.compute_v1.services.target_vpn_gateways :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.target_vpn_gateways.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/url_maps.rst b/packages/google-cloud-compute/docs/compute_v1/url_maps.rst index 259390224ef1..4c82c849c138 100644 --- a/packages/google-cloud-compute/docs/compute_v1/url_maps.rst +++ b/packages/google-cloud-compute/docs/compute_v1/url_maps.rst @@ -4,3 +4,8 @@ UrlMaps .. automodule:: google.cloud.compute_v1.services.url_maps :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.url_maps.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/vpn_gateways.rst b/packages/google-cloud-compute/docs/compute_v1/vpn_gateways.rst index b5921da6f2fc..d08ca6794dbd 100644 --- a/packages/google-cloud-compute/docs/compute_v1/vpn_gateways.rst +++ b/packages/google-cloud-compute/docs/compute_v1/vpn_gateways.rst @@ -4,3 +4,8 @@ VpnGateways .. automodule:: google.cloud.compute_v1.services.vpn_gateways :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.vpn_gateways.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/vpn_tunnels.rst b/packages/google-cloud-compute/docs/compute_v1/vpn_tunnels.rst index e513996c8bdf..91549354b9f9 100644 --- a/packages/google-cloud-compute/docs/compute_v1/vpn_tunnels.rst +++ b/packages/google-cloud-compute/docs/compute_v1/vpn_tunnels.rst @@ -4,3 +4,8 @@ VpnTunnels .. automodule:: google.cloud.compute_v1.services.vpn_tunnels :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.vpn_tunnels.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/zone_operations.rst b/packages/google-cloud-compute/docs/compute_v1/zone_operations.rst index 83847994f87b..1b966fd772a1 100644 --- a/packages/google-cloud-compute/docs/compute_v1/zone_operations.rst +++ b/packages/google-cloud-compute/docs/compute_v1/zone_operations.rst @@ -4,3 +4,8 @@ ZoneOperations .. automodule:: google.cloud.compute_v1.services.zone_operations :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.zone_operations.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/docs/compute_v1/zones.rst b/packages/google-cloud-compute/docs/compute_v1/zones.rst index d56b0f4897a0..55af2eafcafe 100644 --- a/packages/google-cloud-compute/docs/compute_v1/zones.rst +++ b/packages/google-cloud-compute/docs/compute_v1/zones.rst @@ -4,3 +4,8 @@ Zones .. automodule:: google.cloud.compute_v1.services.zones :members: :inherited-members: + + +.. automodule:: google.cloud.compute_v1.services.zones.pagers + :members: + :inherited-members: diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/__init__.py b/packages/google-cloud-compute/google/cloud/compute_v1/__init__.py index 9d541fb8295d..367e6c4ce45b 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/__init__.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/__init__.py @@ -1152,6 +1152,7 @@ "AcceleratorType", "AcceleratorTypeAggregatedList", "AcceleratorTypeList", + "AcceleratorTypesClient", "AcceleratorTypesScopedList", "Accelerators", "AccessConfig", @@ -1360,7 +1361,6 @@ "DiskTypesClient", "DiskTypesScopedList", "DisksAddResourcePoliciesRequest", - "DisksClient", "DisksRemoveResourcePoliciesRequest", "DisksResizeRequest", "DisksScopedList", @@ -2271,5 +2271,5 @@ "ZoneSetLabelsRequest", "ZoneSetPolicyRequest", "ZonesClient", - "AcceleratorTypesClient", + "DisksClient", ) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/client.py index 6ec97673a92a..b55bab926e41 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.accelerator_types import pagers from google.cloud.compute_v1.types import compute from .transports.base import AcceleratorTypesTransport, DEFAULT_CLIENT_INFO @@ -334,7 +335,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.AcceleratorTypeAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of accelerator types. Args: @@ -355,7 +356,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.AcceleratorTypeAggregatedList: + google.cloud.compute_v1.services.accelerator_types.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -388,6 +392,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -493,7 +503,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.AcceleratorTypeList: + ) -> pagers.ListPager: r"""Retrieves a list of accelerator types that are available to the specified project. @@ -522,8 +532,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.AcceleratorTypeList: + google.cloud.compute_v1.services.accelerator_types.pagers.ListPager: Contains a list of accelerator types. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -557,6 +571,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/pagers.py new file mode 100644 index 000000000000..284b35519a28 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.AcceleratorTypeAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.AcceleratorTypeAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.AcceleratorTypeAggregatedList], + request: compute.AggregatedListAcceleratorTypesRequest, + response: compute.AcceleratorTypeAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListAcceleratorTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.AcceleratorTypeAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListAcceleratorTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.AcceleratorTypeAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.AcceleratorTypesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.AcceleratorTypesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.AcceleratorTypeList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.AcceleratorTypeList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.AcceleratorTypeList], + request: compute.ListAcceleratorTypesRequest, + response: compute.AcceleratorTypeList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListAcceleratorTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.AcceleratorTypeList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListAcceleratorTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.AcceleratorTypeList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.AcceleratorType]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/transports/rest.py index 86666635f449..d60589b40f2e 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/accelerator_types/transports/rest.py @@ -127,11 +127,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -243,10 +243,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/client.py index a56d414b6846..fd38a15d3815 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.addresses import pagers from google.cloud.compute_v1.types import compute from .transports.base import AddressesTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.AddressAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of addresses. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.AddressAggregatedList: + google.cloud.compute_v1.services.addresses.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -722,7 +732,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.AddressList: + ) -> pagers.ListPager: r"""Retrieves a list of addresses contained within the specified region. @@ -748,8 +758,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.AddressList: + google.cloud.compute_v1.services.addresses.pagers.ListPager: Contains a list of addresses. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -783,6 +797,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/pagers.py new file mode 100644 index 000000000000..53c4a0b49a58 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.AddressAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.AddressAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.AddressAggregatedList], + request: compute.AggregatedListAddressesRequest, + response: compute.AddressAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListAddressesRequest): + The initial request object. + response (google.cloud.compute_v1.types.AddressAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListAddressesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.AddressAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.AddressesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.AddressesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.AddressList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.AddressList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.AddressList], + request: compute.ListAddressesRequest, + response: compute.AddressList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListAddressesRequest): + The initial request object. + response (google.cloud.compute_v1.types.AddressList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListAddressesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.AddressList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Address]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/transports/rest.py index 4995ab78e956..cd3f80cf6bac 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/addresses/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -363,7 +363,9 @@ def insert( # Jsonify the request body body = compute.Address.to_json( - request.address_resource, including_default_value_fields=False + request.address_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -386,7 +388,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -426,10 +428,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/client.py index 57ea6a58cdde..a110b9dbdf9d 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.autoscalers import pagers from google.cloud.compute_v1.types import compute from .transports.base import AutoscalersTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.AutoscalerAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of autoscalers. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.AutoscalerAggregatedList: + google.cloud.compute_v1.services.autoscalers.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -711,7 +721,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.AutoscalerList: + ) -> pagers.ListPager: r"""Retrieves a list of autoscalers contained within the specified zone. @@ -738,9 +748,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.AutoscalerList: + google.cloud.compute_v1.services.autoscalers.pagers.ListPager: Contains a list of Autoscaler resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -775,6 +788,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/pagers.py new file mode 100644 index 000000000000..7fab36d45fac --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.AutoscalerAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.AutoscalerAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.AutoscalerAggregatedList], + request: compute.AggregatedListAutoscalersRequest, + response: compute.AutoscalerAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListAutoscalersRequest): + The initial request object. + response (google.cloud.compute_v1.types.AutoscalerAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListAutoscalersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.AutoscalerAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.AutoscalersScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.AutoscalersScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.AutoscalerList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.AutoscalerList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.AutoscalerList], + request: compute.ListAutoscalersRequest, + response: compute.AutoscalerList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListAutoscalersRequest): + The initial request object. + response (google.cloud.compute_v1.types.AutoscalerList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListAutoscalersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.AutoscalerList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Autoscaler]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/transports/rest.py index e31193fca9fe..e5cc0414a22a 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/autoscalers/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -356,7 +356,9 @@ def insert( # Jsonify the request body body = compute.Autoscaler.to_json( - request.autoscaler_resource, including_default_value_fields=False + request.autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -379,7 +381,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -422,10 +424,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -497,7 +499,9 @@ def patch( # Jsonify the request body body = compute.Autoscaler.to_json( - request.autoscaler_resource, including_default_value_fields=False + request.autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -521,7 +525,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -582,7 +586,9 @@ def update( # Jsonify the request body body = compute.Autoscaler.to_json( - request.autoscaler_resource, including_default_value_fields=False + request.autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -606,7 +612,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/client.py index 4aeee20feb1c..bafee1faf785 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.backend_buckets import pagers from google.cloud.compute_v1.types import compute from .transports.base import BackendBucketsTransport, DEFAULT_CLIENT_INFO @@ -838,7 +839,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.BackendBucketList: + ) -> pagers.ListPager: r"""Retrieves the list of BackendBucket resources available to the specified project. @@ -860,9 +861,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.BackendBucketList: + google.cloud.compute_v1.services.backend_buckets.pagers.ListPager: Contains a list of BackendBucket resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -895,6 +899,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/pagers.py new file mode 100644 index 000000000000..a71b74ff8db5 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.BackendBucketList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.BackendBucketList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.BackendBucketList], + request: compute.ListBackendBucketsRequest, + response: compute.BackendBucketList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListBackendBucketsRequest): + The initial request object. + response (google.cloud.compute_v1.types.BackendBucketList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListBackendBucketsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.BackendBucketList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.BackendBucket]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/transports/rest.py index d8adca3ba291..c896529b1ca2 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_buckets/transports/rest.py @@ -146,7 +146,9 @@ def add_signed_url_key( # Jsonify the request body body = compute.SignedUrlKey.to_json( - request.signed_url_key_resource, including_default_value_fields=False + request.signed_url_key_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -171,7 +173,7 @@ def add_signed_url_key( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -452,7 +454,9 @@ def insert( # Jsonify the request body body = compute.BackendBucket.to_json( - request.backend_bucket_resource, including_default_value_fields=False + request.backend_bucket_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -475,7 +479,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -518,10 +522,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -593,7 +597,9 @@ def patch( # Jsonify the request body body = compute.BackendBucket.to_json( - request.backend_bucket_resource, including_default_value_fields=False + request.backend_bucket_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -618,7 +624,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -679,7 +685,9 @@ def update( # Jsonify the request body body = compute.BackendBucket.to_json( - request.backend_bucket_resource, including_default_value_fields=False + request.backend_bucket_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -704,7 +712,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/client.py index 8d84d19a701a..01c186923b24 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.backend_services import pagers from google.cloud.compute_v1.types import compute from .transports.base import BackendServicesTransport, DEFAULT_CLIENT_INFO @@ -443,7 +444,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.BackendServiceAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of all BackendService resources, regional and global, available to the specified project. @@ -467,9 +468,12 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.BackendServiceAggregatedList: + google.cloud.compute_v1.services.backend_services.pagers.AggregatedListPager: Contains a list of BackendServicesScopedList. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -502,6 +506,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1016,7 +1026,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.BackendServiceList: + ) -> pagers.ListPager: r"""Retrieves the list of BackendService resources available to the specified project. @@ -1038,9 +1048,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.BackendServiceList: + google.cloud.compute_v1.services.backend_services.pagers.ListPager: Contains a list of BackendService resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1073,6 +1086,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/pagers.py new file mode 100644 index 000000000000..5b3c487f8ccf --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.BackendServiceAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.BackendServiceAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.BackendServiceAggregatedList], + request: compute.AggregatedListBackendServicesRequest, + response: compute.BackendServiceAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListBackendServicesRequest): + The initial request object. + response (google.cloud.compute_v1.types.BackendServiceAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListBackendServicesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.BackendServiceAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.BackendServicesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.BackendServicesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.BackendServiceList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.BackendServiceList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.BackendServiceList], + request: compute.ListBackendServicesRequest, + response: compute.BackendServiceList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListBackendServicesRequest): + The initial request object. + response (google.cloud.compute_v1.types.BackendServiceList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListBackendServicesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.BackendServiceList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.BackendService]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/transports/rest.py index 17262bbdbf78..080c741124ed 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/backend_services/transports/rest.py @@ -146,7 +146,9 @@ def add_signed_url_key( # Jsonify the request body body = compute.SignedUrlKey.to_json( - request.signed_url_key_resource, including_default_value_fields=False + request.signed_url_key_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -171,7 +173,7 @@ def add_signed_url_key( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -214,11 +216,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -500,6 +502,7 @@ def get_health( body = compute.ResourceGroupReference.to_json( request.resource_group_reference_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -522,7 +525,7 @@ def get_health( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -583,7 +586,9 @@ def insert( # Jsonify the request body body = compute.BackendService.to_json( - request.backend_service_resource, including_default_value_fields=False + request.backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -606,7 +611,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -649,10 +654,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -724,7 +729,9 @@ def patch( # Jsonify the request body body = compute.BackendService.to_json( - request.backend_service_resource, including_default_value_fields=False + request.backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -749,7 +756,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -812,6 +819,7 @@ def set_security_policy( body = compute.SecurityPolicyReference.to_json( request.security_policy_reference_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -836,7 +844,7 @@ def set_security_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -897,7 +905,9 @@ def update( # Jsonify the request body body = compute.BackendService.to_json( - request.backend_service_resource, including_default_value_fields=False + request.backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -922,7 +932,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/client.py index 17fde2ffb08f..296c597b3201 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.disk_types import pagers from google.cloud.compute_v1.types import compute from .transports.base import DiskTypesTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.DiskTypeAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of disk types. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.DiskTypeAggregatedList: + google.cloud.compute_v1.services.disk_types.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -500,7 +510,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.DiskTypeList: + ) -> pagers.ListPager: r"""Retrieves a list of disk types available to the specified project. @@ -528,8 +538,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.DiskTypeList: + google.cloud.compute_v1.services.disk_types.pagers.ListPager: Contains a list of disk types. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -563,6 +577,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/pagers.py new file mode 100644 index 000000000000..5c0dbb19dd20 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.DiskTypeAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.DiskTypeAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.DiskTypeAggregatedList], + request: compute.AggregatedListDiskTypesRequest, + response: compute.DiskTypeAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListDiskTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.DiskTypeAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListDiskTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.DiskTypeAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.DiskTypesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.DiskTypesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.DiskTypeList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.DiskTypeList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.DiskTypeList], + request: compute.ListDiskTypesRequest, + response: compute.DiskTypeList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListDiskTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.DiskTypeList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListDiskTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.DiskTypeList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.DiskType]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/transports/rest.py index 5553b03b8fc3..212eeadbfda6 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/disk_types/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -252,10 +252,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/client.py index f74d1af590bc..c5dad0ae3a75 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.disks import pagers from google.cloud.compute_v1.types import compute from .transports.base import DisksTransport, DEFAULT_CLIENT_INFO @@ -452,7 +453,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.DiskAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of persistent disks. Args: @@ -473,7 +474,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.DiskAggregatedList: + google.cloud.compute_v1.services.disks.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -506,6 +510,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1106,7 +1116,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.DiskList: + ) -> pagers.ListPager: r"""Retrieves a list of persistent disks contained within the specified zone. @@ -1134,8 +1144,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.DiskList: + google.cloud.compute_v1.services.disks.pagers.ListPager: A list of Disk resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -1169,6 +1183,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/pagers.py new file mode 100644 index 000000000000..665157ce91b7 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.DiskAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.DiskAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.DiskAggregatedList], + request: compute.AggregatedListDisksRequest, + response: compute.DiskAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListDisksRequest): + The initial request object. + response (google.cloud.compute_v1.types.DiskAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListDisksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.DiskAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.DisksScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.DisksScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.DiskList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.DiskList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.DiskList], + request: compute.ListDisksRequest, + response: compute.DiskList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListDisksRequest): + The initial request object. + response (google.cloud.compute_v1.types.DiskList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListDisksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.DiskList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Disk]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/transports/rest.py index a1a4c6fbbdc8..a45810aef8a7 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/disks/transports/rest.py @@ -148,6 +148,7 @@ def add_resource_policies( body = compute.DisksAddResourcePoliciesRequest.to_json( request.disks_add_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -173,7 +174,7 @@ def add_resource_policies( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -214,11 +215,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -290,7 +291,9 @@ def create_snapshot( # Jsonify the request body body = compute.Snapshot.to_json( - request.snapshot_resource, including_default_value_fields=False + request.snapshot_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -317,7 +320,7 @@ def create_snapshot( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -633,7 +636,9 @@ def insert( # Jsonify the request body body = compute.Disk.to_json( - request.disk_resource, including_default_value_fields=False + request.disk_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -645,8 +650,8 @@ def insert( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "sourceImage": request.source_image, "requestId": request.request_id, + "sourceImage": request.source_image, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -657,7 +662,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -697,10 +702,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -774,6 +779,7 @@ def remove_resource_policies( body = compute.DisksRemoveResourcePoliciesRequest.to_json( request.disks_remove_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -799,7 +805,7 @@ def remove_resource_policies( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -859,7 +865,9 @@ def resize( # Jsonify the request body body = compute.DisksResizeRequest.to_json( - request.disks_resize_request_resource, including_default_value_fields=False + request.disks_resize_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -885,7 +893,7 @@ def resize( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -970,6 +978,7 @@ def set_iam_policy( body = compute.ZoneSetPolicyRequest.to_json( request.zone_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -993,7 +1002,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1056,6 +1065,7 @@ def set_labels( body = compute.ZoneSetLabelsRequest.to_json( request.zone_set_labels_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1081,7 +1091,7 @@ def set_labels( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1116,6 +1126,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1139,7 +1150,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/client.py index e9cd85006081..f541a900f609 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.external_vpn_gateways import pagers from google.cloud.compute_v1.types import compute from .transports.base import ExternalVpnGatewaysTransport, DEFAULT_CLIENT_INFO @@ -626,7 +627,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ExternalVpnGatewayList: + ) -> pagers.ListPager: r"""Retrieves the list of ExternalVpnGateway available to the specified project. @@ -648,9 +649,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ExternalVpnGatewayList: + google.cloud.compute_v1.services.external_vpn_gateways.pagers.ListPager: Response to the list request, and contains a list of externalVpnGateways. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -683,6 +687,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/pagers.py new file mode 100644 index 000000000000..f60ab239f1ed --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ExternalVpnGatewayList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ExternalVpnGatewayList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ExternalVpnGatewayList], + request: compute.ListExternalVpnGatewaysRequest, + response: compute.ExternalVpnGatewayList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListExternalVpnGatewaysRequest): + The initial request object. + response (google.cloud.compute_v1.types.ExternalVpnGatewayList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListExternalVpnGatewaysRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ExternalVpnGatewayList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.ExternalVpnGateway]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/transports/rest.py index a26a00088377..43da3694e783 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/external_vpn_gateways/transports/rest.py @@ -293,7 +293,9 @@ def insert( # Jsonify the request body body = compute.ExternalVpnGateway.to_json( - request.external_vpn_gateway_resource, including_default_value_fields=False + request.external_vpn_gateway_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -316,7 +318,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -359,10 +361,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -436,6 +438,7 @@ def set_labels( body = compute.GlobalSetLabelsRequest.to_json( request.global_set_labels_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -456,7 +459,7 @@ def set_labels( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -491,6 +494,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -511,7 +515,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/client.py index d2f059d61b79..c5522207646d 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.firewalls import pagers from google.cloud.compute_v1.types import compute from .transports.base import FirewallsTransport, DEFAULT_CLIENT_INFO @@ -604,7 +605,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.FirewallList: + ) -> pagers.ListPager: r"""Retrieves the list of firewall rules available to the specified project. @@ -625,8 +626,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.FirewallList: + google.cloud.compute_v1.services.firewalls.pagers.ListPager: Contains a list of firewalls. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -658,6 +663,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/pagers.py new file mode 100644 index 000000000000..ff2937ad2e88 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.FirewallList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.FirewallList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.FirewallList], + request: compute.ListFirewallsRequest, + response: compute.FirewallList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListFirewallsRequest): + The initial request object. + response (google.cloud.compute_v1.types.FirewallList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListFirewallsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.FirewallList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Firewall]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/transports/rest.py index 99c6a00ba321..2018ae10d650 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/firewalls/transports/rest.py @@ -278,7 +278,9 @@ def insert( # Jsonify the request body body = compute.Firewall.to_json( - request.firewall_resource, including_default_value_fields=False + request.firewall_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -301,7 +303,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -341,10 +343,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -416,7 +418,9 @@ def patch( # Jsonify the request body body = compute.Firewall.to_json( - request.firewall_resource, including_default_value_fields=False + request.firewall_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -439,7 +443,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -500,7 +504,9 @@ def update( # Jsonify the request body body = compute.Firewall.to_json( - request.firewall_resource, including_default_value_fields=False + request.firewall_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -523,7 +529,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/client.py index d2bb9604e3bb..f8f13b563b09 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.forwarding_rules import pagers from google.cloud.compute_v1.types import compute from .transports.base import ForwardingRulesTransport, DEFAULT_CLIENT_INFO @@ -332,7 +333,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ForwardingRuleAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of forwarding rules. Args: @@ -353,7 +354,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ForwardingRuleAggregatedList: + google.cloud.compute_v1.services.forwarding_rules.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -386,6 +390,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -725,7 +735,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ForwardingRuleList: + ) -> pagers.ListPager: r"""Retrieves a list of ForwardingRule resources available to the specified project and region. @@ -754,9 +764,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ForwardingRuleList: + google.cloud.compute_v1.services.forwarding_rules.pagers.ListPager: Contains a list of ForwardingRule resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -791,6 +804,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/pagers.py new file mode 100644 index 000000000000..0320a5ac521c --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ForwardingRuleAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ForwardingRuleAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ForwardingRuleAggregatedList], + request: compute.AggregatedListForwardingRulesRequest, + response: compute.ForwardingRuleAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListForwardingRulesRequest): + The initial request object. + response (google.cloud.compute_v1.types.ForwardingRuleAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListForwardingRulesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ForwardingRuleAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.ForwardingRulesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.ForwardingRulesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ForwardingRuleList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ForwardingRuleList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ForwardingRuleList], + request: compute.ListForwardingRulesRequest, + response: compute.ForwardingRuleList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListForwardingRulesRequest): + The initial request object. + response (google.cloud.compute_v1.types.ForwardingRuleList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListForwardingRulesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ForwardingRuleList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.ForwardingRule]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/transports/rest.py index 7c0ea446072f..ee40d69f9e06 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/forwarding_rules/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -358,7 +358,9 @@ def insert( # Jsonify the request body body = compute.ForwardingRule.to_json( - request.forwarding_rule_resource, including_default_value_fields=False + request.forwarding_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -381,7 +383,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -424,10 +426,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -499,7 +501,9 @@ def patch( # Jsonify the request body body = compute.ForwardingRule.to_json( - request.forwarding_rule_resource, including_default_value_fields=False + request.forwarding_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -525,7 +529,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -586,7 +590,9 @@ def set_target( # Jsonify the request body body = compute.TargetReference.to_json( - request.target_reference_resource, including_default_value_fields=False + request.target_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -612,7 +618,7 @@ def set_target( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/client.py index 90d0f6c37218..618993128b95 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.global_addresses import pagers from google.cloud.compute_v1.types import compute from .transports.base import GlobalAddressesTransport, DEFAULT_CLIENT_INFO @@ -636,7 +637,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.AddressList: + ) -> pagers.ListPager: r"""Retrieves a list of global addresses. Args: @@ -657,8 +658,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.AddressList: + google.cloud.compute_v1.services.global_addresses.pagers.ListPager: Contains a list of addresses. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -690,6 +695,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/pagers.py new file mode 100644 index 000000000000..2428e6bed0c9 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.AddressList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.AddressList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.AddressList], + request: compute.ListGlobalAddressesRequest, + response: compute.AddressList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListGlobalAddressesRequest): + The initial request object. + response (google.cloud.compute_v1.types.AddressList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListGlobalAddressesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.AddressList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Address]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/transports/rest.py index 7bef6d7a6705..b9c2f07964ca 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_addresses/transports/rest.py @@ -302,7 +302,9 @@ def insert( # Jsonify the request body body = compute.Address.to_json( - request.address_resource, including_default_value_fields=False + request.address_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -325,7 +327,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -366,10 +368,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/client.py index 2f3db25baf95..a9f7c1a2d0c2 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.global_forwarding_rules import pagers from google.cloud.compute_v1.types import compute from .transports.base import GlobalForwardingRulesTransport, DEFAULT_CLIENT_INFO @@ -633,7 +634,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ForwardingRuleList: + ) -> pagers.ListPager: r"""Retrieves a list of GlobalForwardingRule resources available to the specified project. @@ -655,9 +656,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ForwardingRuleList: + google.cloud.compute_v1.services.global_forwarding_rules.pagers.ListPager: Contains a list of ForwardingRule resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -690,6 +694,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/pagers.py new file mode 100644 index 000000000000..f606d1c7e9a7 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ForwardingRuleList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ForwardingRuleList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ForwardingRuleList], + request: compute.ListGlobalForwardingRulesRequest, + response: compute.ForwardingRuleList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListGlobalForwardingRulesRequest): + The initial request object. + response (google.cloud.compute_v1.types.ForwardingRuleList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListGlobalForwardingRulesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ForwardingRuleList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.ForwardingRule]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/transports/rest.py index 06b470341bd0..7d26ef92b5be 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_forwarding_rules/transports/rest.py @@ -300,7 +300,9 @@ def insert( # Jsonify the request body body = compute.ForwardingRule.to_json( - request.forwarding_rule_resource, including_default_value_fields=False + request.forwarding_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -323,7 +325,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -366,10 +368,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -441,7 +443,9 @@ def patch( # Jsonify the request body body = compute.ForwardingRule.to_json( - request.forwarding_rule_resource, including_default_value_fields=False + request.forwarding_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -466,7 +470,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -527,7 +531,9 @@ def set_target( # Jsonify the request body body = compute.TargetReference.to_json( - request.target_reference_resource, including_default_value_fields=False + request.target_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -552,7 +558,7 @@ def set_target( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/client.py index 696d6d9ddd1c..d4a70db19aa7 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.global_network_endpoint_groups import pagers from google.cloud.compute_v1.types import compute from .transports.base import GlobalNetworkEndpointGroupsTransport, DEFAULT_CLIENT_INFO @@ -884,7 +885,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NetworkEndpointGroupList: + ) -> pagers.ListPager: r"""Retrieves the list of network endpoint groups that are located in the specified project. @@ -906,7 +907,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NetworkEndpointGroupList: + google.cloud.compute_v1.services.global_network_endpoint_groups.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -939,6 +943,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -951,7 +961,7 @@ def list_network_endpoints( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NetworkEndpointGroupsListNetworkEndpoints: + ) -> pagers.ListNetworkEndpointsPager: r"""Lists the network endpoints in the specified network endpoint group. @@ -982,7 +992,10 @@ def list_network_endpoints( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NetworkEndpointGroupsListNetworkEndpoints: + google.cloud.compute_v1.services.global_network_endpoint_groups.pagers.ListNetworkEndpointsPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1021,6 +1034,12 @@ def list_network_endpoints( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListNetworkEndpointsPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/pagers.py new file mode 100644 index 000000000000..551f206683a4 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/pagers.py @@ -0,0 +1,155 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NetworkEndpointGroupList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NetworkEndpointGroupList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NetworkEndpointGroupList], + request: compute.ListGlobalNetworkEndpointGroupsRequest, + response: compute.NetworkEndpointGroupList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListGlobalNetworkEndpointGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NetworkEndpointGroupList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListGlobalNetworkEndpointGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NetworkEndpointGroupList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NetworkEndpointGroup]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListNetworkEndpointsPager: + """A pager for iterating through ``list_network_endpoints`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NetworkEndpointGroupsListNetworkEndpoints` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListNetworkEndpoints`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NetworkEndpointGroupsListNetworkEndpoints` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NetworkEndpointGroupsListNetworkEndpoints], + request: compute.ListNetworkEndpointsGlobalNetworkEndpointGroupsRequest, + response: compute.NetworkEndpointGroupsListNetworkEndpoints, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListNetworkEndpointsGlobalNetworkEndpointGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NetworkEndpointGroupsListNetworkEndpoints): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListNetworkEndpointsGlobalNetworkEndpointGroupsRequest( + request + ) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NetworkEndpointGroupsListNetworkEndpoints]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NetworkEndpointWithHealthStatus]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/transports/rest.py index 5dd073cdfeed..eedebf997717 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_network_endpoint_groups/transports/rest.py @@ -148,6 +148,7 @@ def attach_network_endpoints( body = compute.GlobalNetworkEndpointGroupsAttachEndpointsRequest.to_json( request.global_network_endpoint_groups_attach_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -172,7 +173,7 @@ def attach_network_endpoints( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -316,6 +317,7 @@ def detach_network_endpoints( body = compute.GlobalNetworkEndpointGroupsDetachEndpointsRequest.to_json( request.global_network_endpoint_groups_detach_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -340,7 +342,7 @@ def detach_network_endpoints( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -467,6 +469,7 @@ def insert( body = compute.NetworkEndpointGroup.to_json( request.network_endpoint_group_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -489,7 +492,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -530,10 +533,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -587,10 +590,10 @@ def list_network_endpoints( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/client.py index 4d71a8f7ea53..c434d82152d7 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.global_operations import pagers from google.cloud.compute_v1.types import compute from .transports.base import GlobalOperationsTransport, DEFAULT_CLIENT_INFO @@ -332,7 +333,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.OperationAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of all operations. Args: @@ -353,7 +354,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.OperationAggregatedList: + google.cloud.compute_v1.services.global_operations.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -386,6 +390,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -576,7 +586,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.OperationList: + ) -> pagers.ListPager: r"""Retrieves a list of Operation resources contained within the specified project. @@ -598,9 +608,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.OperationList: + google.cloud.compute_v1.services.global_operations.pagers.ListPager: Contains a list of Operation resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -633,6 +646,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/pagers.py new file mode 100644 index 000000000000..3acc037f21b0 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.OperationAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.OperationAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.OperationAggregatedList], + request: compute.AggregatedListGlobalOperationsRequest, + response: compute.OperationAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListGlobalOperationsRequest): + The initial request object. + response (google.cloud.compute_v1.types.OperationAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListGlobalOperationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.OperationAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.OperationsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.OperationsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.OperationList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.OperationList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.OperationList], + request: compute.ListGlobalOperationsRequest, + response: compute.OperationList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListGlobalOperationsRequest): + The initial request object. + response (google.cloud.compute_v1.types.OperationList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListGlobalOperationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.OperationList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Operation]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/transports/rest.py index 28755b5fff4a..e7afb894a3b8 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_operations/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -313,10 +313,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/client.py index 107ea4f8a27f..6ca1466bc3ba 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.global_organization_operations import pagers from google.cloud.compute_v1.types import compute from .transports.base import GlobalOrganizationOperationsTransport, DEFAULT_CLIENT_INFO @@ -498,7 +499,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.OperationList: + ) -> pagers.ListPager: r"""Retrieves a list of Operation resources contained within the specified organization. @@ -515,9 +516,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.OperationList: + google.cloud.compute_v1.services.global_organization_operations.pagers.ListPager: Contains a list of Operation resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -536,6 +540,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/pagers.py new file mode 100644 index 000000000000..30b420d47877 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.OperationList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.OperationList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.OperationList], + request: compute.ListGlobalOrganizationOperationsRequest, + response: compute.OperationList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListGlobalOrganizationOperationsRequest): + The initial request object. + response (google.cloud.compute_v1.types.OperationList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListGlobalOrganizationOperationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.OperationList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Operation]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/transports/rest.py index 76e798e1bb36..5909c89a5349 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/global_organization_operations/transports/rest.py @@ -262,12 +262,12 @@ def list( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "orderBy": request.order_by, "filter": request.filter, - "parentId": request.parent_id, + "maxResults": request.max_results, + "orderBy": request.order_by, "pageToken": request.page_token, + "parentId": request.parent_id, "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/client.py index 0654fed4a709..de3f512b57fc 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.health_checks import pagers from google.cloud.compute_v1.types import compute from .transports.base import HealthChecksTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.HealthChecksAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of all HealthCheck resources, regional and global, available to the specified project. @@ -354,7 +355,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.HealthChecksAggregatedList: + google.cloud.compute_v1.services.health_checks.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -387,6 +391,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -702,7 +712,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.HealthCheckList: + ) -> pagers.ListPager: r"""Retrieves the list of HealthCheck resources available to the specified project. @@ -724,9 +734,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.HealthCheckList: + google.cloud.compute_v1.services.health_checks.pagers.ListPager: Contains a list of HealthCheck resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -759,6 +772,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/pagers.py new file mode 100644 index 000000000000..6a2d05b87266 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.HealthChecksAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.HealthChecksAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.HealthChecksAggregatedList], + request: compute.AggregatedListHealthChecksRequest, + response: compute.HealthChecksAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListHealthChecksRequest): + The initial request object. + response (google.cloud.compute_v1.types.HealthChecksAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListHealthChecksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.HealthChecksAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.HealthChecksScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.HealthChecksScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.HealthCheckList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.HealthCheckList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.HealthCheckList], + request: compute.ListHealthChecksRequest, + response: compute.HealthCheckList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListHealthChecksRequest): + The initial request object. + response (google.cloud.compute_v1.types.HealthCheckList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListHealthChecksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.HealthCheckList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.HealthCheck]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/transports/rest.py index 6c852b513c32..1c785f29a103 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/health_checks/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -358,7 +358,9 @@ def insert( # Jsonify the request body body = compute.HealthCheck.to_json( - request.health_check_resource, including_default_value_fields=False + request.health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -381,7 +383,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -424,10 +426,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -499,7 +501,9 @@ def patch( # Jsonify the request body body = compute.HealthCheck.to_json( - request.health_check_resource, including_default_value_fields=False + request.health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -522,7 +526,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -583,7 +587,9 @@ def update( # Jsonify the request body body = compute.HealthCheck.to_json( - request.health_check_resource, including_default_value_fields=False + request.health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -606,7 +612,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/images/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/images/client.py index 75c9c3186481..3566dfa73866 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/images/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/images/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.images import pagers from google.cloud.compute_v1.types import compute from .transports.base import ImagesTransport, DEFAULT_CLIENT_INFO @@ -919,7 +920,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ImageList: + ) -> pagers.ListPager: r"""Retrieves the list of custom images available to the specified project. Custom images are images you create that belong to your project. This method does not get @@ -946,8 +947,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ImageList: + google.cloud.compute_v1.services.images.pagers.ListPager: Contains a list of images. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -979,6 +984,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/images/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/images/pagers.py new file mode 100644 index 000000000000..00ab356829c9 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/images/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ImageList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ImageList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ImageList], + request: compute.ListImagesRequest, + response: compute.ImageList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListImagesRequest): + The initial request object. + response (google.cloud.compute_v1.types.ImageList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListImagesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ImageList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Image]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/images/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/images/transports/rest.py index 2531e913d827..79a1180033bb 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/images/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/images/transports/rest.py @@ -224,7 +224,9 @@ def deprecate( # Jsonify the request body body = compute.DeprecationStatus.to_json( - request.deprecation_status_resource, including_default_value_fields=False + request.deprecation_status_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -247,7 +249,7 @@ def deprecate( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -515,7 +517,9 @@ def insert( # Jsonify the request body body = compute.Image.to_json( - request.image_resource, including_default_value_fields=False + request.image_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -539,7 +543,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -579,10 +583,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -653,7 +657,9 @@ def patch( # Jsonify the request body body = compute.Image.to_json( - request.image_resource, including_default_value_fields=False + request.image_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -676,7 +682,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -761,6 +767,7 @@ def set_iam_policy( body = compute.GlobalSetPolicyRequest.to_json( request.global_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -781,7 +788,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -844,6 +851,7 @@ def set_labels( body = compute.GlobalSetLabelsRequest.to_json( request.global_set_labels_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -864,7 +872,7 @@ def set_labels( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -899,6 +907,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -919,7 +928,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/client.py index d7522b241093..240ed9f14eb6 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.instance_group_managers import pagers from google.cloud.compute_v1.types import compute from .transports.base import InstanceGroupManagersTransport, DEFAULT_CLIENT_INFO @@ -477,7 +478,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceGroupManagerAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of managed instance groups and groups them by zone. @@ -499,7 +500,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceGroupManagerAggregatedList: + google.cloud.compute_v1.services.instance_group_managers.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -532,6 +536,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1428,7 +1438,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceGroupManagerList: + ) -> pagers.ListPager: r"""Retrieves a list of managed instance groups that are contained within the specified project and zone. @@ -1457,8 +1467,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceGroupManagerList: + google.cloud.compute_v1.services.instance_group_managers.pagers.ListPager: [Output Only] A list of managed instance groups. + + Iterating over this object will yield results and + resolve additional pages automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -1492,6 +1506,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1505,7 +1525,7 @@ def list_errors( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceGroupManagersListErrorsResponse: + ) -> pagers.ListErrorsPager: r"""Lists all errors thrown by actions on instances for a given managed instance group. The filter and orderBy query parameters are not supported. @@ -1545,7 +1565,10 @@ def list_errors( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceGroupManagersListErrorsResponse: + google.cloud.compute_v1.services.instance_group_managers.pagers.ListErrorsPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1582,6 +1605,12 @@ def list_errors( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListErrorsPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1595,7 +1624,7 @@ def list_managed_instances( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceGroupManagersListManagedInstancesResponse: + ) -> pagers.ListManagedInstancesPager: r"""Lists all of the instances in the managed instance group. Each instance in the list has a currentAction, which indicates the action that the managed instance @@ -1637,7 +1666,10 @@ def list_managed_instances( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceGroupManagersListManagedInstancesResponse: + google.cloud.compute_v1.services.instance_group_managers.pagers.ListManagedInstancesPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1676,6 +1708,12 @@ def list_managed_instances( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListManagedInstancesPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1689,7 +1727,7 @@ def list_per_instance_configs( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceGroupManagersListPerInstanceConfigsResp: + ) -> pagers.ListPerInstanceConfigsPager: r"""Lists all of the per-instance configs defined for the managed instance group. The orderBy query parameter is not supported. @@ -1727,7 +1765,10 @@ def list_per_instance_configs( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceGroupManagersListPerInstanceConfigsResp: + google.cloud.compute_v1.services.instance_group_managers.pagers.ListPerInstanceConfigsPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1770,6 +1811,12 @@ def list_per_instance_configs( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPerInstanceConfigsPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/pagers.py new file mode 100644 index 000000000000..e5200c6f83e2 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/pagers.py @@ -0,0 +1,352 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceGroupManagerAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceGroupManagerAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceGroupManagerAggregatedList], + request: compute.AggregatedListInstanceGroupManagersRequest, + response: compute.InstanceGroupManagerAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceGroupManagerAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListInstanceGroupManagersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceGroupManagerAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.InstanceGroupManagersScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.InstanceGroupManagersScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceGroupManagerList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceGroupManagerList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceGroupManagerList], + request: compute.ListInstanceGroupManagersRequest, + response: compute.InstanceGroupManagerList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceGroupManagerList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInstanceGroupManagersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceGroupManagerList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceGroupManager]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListErrorsPager: + """A pager for iterating through ``list_errors`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceGroupManagersListErrorsResponse` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListErrors`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceGroupManagersListErrorsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceGroupManagersListErrorsResponse], + request: compute.ListErrorsInstanceGroupManagersRequest, + response: compute.InstanceGroupManagersListErrorsResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListErrorsInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceGroupManagersListErrorsResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListErrorsInstanceGroupManagersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceGroupManagersListErrorsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceManagedByIgmError]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListManagedInstancesPager: + """A pager for iterating through ``list_managed_instances`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceGroupManagersListManagedInstancesResponse` object, and + provides an ``__iter__`` method to iterate through its + ``managed_instances`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListManagedInstances`` requests and continue to iterate + through the ``managed_instances`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceGroupManagersListManagedInstancesResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[ + ..., compute.InstanceGroupManagersListManagedInstancesResponse + ], + request: compute.ListManagedInstancesInstanceGroupManagersRequest, + response: compute.InstanceGroupManagersListManagedInstancesResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListManagedInstancesInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceGroupManagersListManagedInstancesResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListManagedInstancesInstanceGroupManagersRequest( + request + ) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages( + self, + ) -> Iterable[compute.InstanceGroupManagersListManagedInstancesResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.ManagedInstance]: + for page in self.pages: + yield from page.managed_instances + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPerInstanceConfigsPager: + """A pager for iterating through ``list_per_instance_configs`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceGroupManagersListPerInstanceConfigsResp` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListPerInstanceConfigs`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceGroupManagersListPerInstanceConfigsResp` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceGroupManagersListPerInstanceConfigsResp], + request: compute.ListPerInstanceConfigsInstanceGroupManagersRequest, + response: compute.InstanceGroupManagersListPerInstanceConfigsResp, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListPerInstanceConfigsInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceGroupManagersListPerInstanceConfigsResp): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListPerInstanceConfigsInstanceGroupManagersRequest( + request + ) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages( + self, + ) -> Iterable[compute.InstanceGroupManagersListPerInstanceConfigsResp]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.PerInstanceConfig]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/transports/rest.py index 138e2165190a..c3b5f1bc30ae 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_group_managers/transports/rest.py @@ -148,6 +148,7 @@ def abandon_instances( body = compute.InstanceGroupManagersAbandonInstancesRequest.to_json( request.instance_group_managers_abandon_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -173,7 +174,7 @@ def abandon_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -214,11 +215,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -293,6 +294,7 @@ def apply_updates_to_instances( body = compute.InstanceGroupManagersApplyUpdatesRequest.to_json( request.instance_group_managers_apply_updates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -316,7 +318,7 @@ def apply_updates_to_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -379,6 +381,7 @@ def create_instances( body = compute.InstanceGroupManagersCreateInstancesRequest.to_json( request.instance_group_managers_create_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -404,7 +407,7 @@ def create_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -549,6 +552,7 @@ def delete_instances( body = compute.InstanceGroupManagersDeleteInstancesRequest.to_json( request.instance_group_managers_delete_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -574,7 +578,7 @@ def delete_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -638,6 +642,7 @@ def delete_per_instance_configs( body = compute.InstanceGroupManagersDeletePerInstanceConfigsReq.to_json( request.instance_group_managers_delete_per_instance_configs_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -661,7 +666,7 @@ def delete_per_instance_configs( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -790,6 +795,7 @@ def insert( body = compute.InstanceGroupManager.to_json( request.instance_group_manager_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -812,7 +818,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -853,10 +859,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -911,10 +917,10 @@ def list_errors( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -971,10 +977,10 @@ def list_managed_instances( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1031,10 +1037,10 @@ def list_per_instance_configs( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1110,6 +1116,7 @@ def patch( body = compute.InstanceGroupManager.to_json( request.instance_group_manager_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1135,7 +1142,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1199,6 +1206,7 @@ def patch_per_instance_configs( body = compute.InstanceGroupManagersPatchPerInstanceConfigsReq.to_json( request.instance_group_managers_patch_per_instance_configs_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1224,7 +1232,7 @@ def patch_per_instance_configs( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1287,6 +1295,7 @@ def recreate_instances( body = compute.InstanceGroupManagersRecreateInstancesRequest.to_json( request.instance_group_managers_recreate_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1312,7 +1321,7 @@ def recreate_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1383,8 +1392,8 @@ def resize( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "size": request.size, "requestId": request.request_id, + "size": request.size, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1458,6 +1467,7 @@ def set_instance_template( body = compute.InstanceGroupManagersSetInstanceTemplateRequest.to_json( request.instance_group_managers_set_instance_template_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1483,7 +1493,7 @@ def set_instance_template( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1546,6 +1556,7 @@ def set_target_pools( body = compute.InstanceGroupManagersSetTargetPoolsRequest.to_json( request.instance_group_managers_set_target_pools_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1571,7 +1582,7 @@ def set_target_pools( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1635,6 +1646,7 @@ def update_per_instance_configs( body = compute.InstanceGroupManagersUpdatePerInstanceConfigsReq.to_json( request.instance_group_managers_update_per_instance_configs_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1660,7 +1672,7 @@ def update_per_instance_configs( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/client.py index c8fefe5521dc..b42d42a87b0a 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.instance_groups import pagers from google.cloud.compute_v1.types import compute from .transports.base import InstanceGroupsTransport, DEFAULT_CLIENT_INFO @@ -462,7 +463,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceGroupAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of instance groups and sorts them by zone. @@ -484,7 +485,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceGroupAggregatedList: + google.cloud.compute_v1.services.instance_groups.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -517,6 +521,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -860,7 +870,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceGroupList: + ) -> pagers.ListPager: r"""Retrieves the list of zonal instance group resources contained within the specified zone. For managed instance groups, use the @@ -892,8 +902,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceGroupList: + google.cloud.compute_v1.services.instance_groups.pagers.ListPager: A list of InstanceGroup resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -927,6 +941,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -941,7 +961,7 @@ def list_instances( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceGroupsListInstances: + ) -> pagers.ListInstancesPager: r"""Lists the instances in the specified instance group. The orderBy query parameter is not supported. @@ -983,7 +1003,10 @@ def list_instances( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceGroupsListInstances: + google.cloud.compute_v1.services.instance_groups.pagers.ListInstancesPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1031,6 +1054,12 @@ def list_instances( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListInstancesPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/pagers.py new file mode 100644 index 000000000000..a19c7f07fe3d --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/pagers.py @@ -0,0 +1,218 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceGroupAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceGroupAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceGroupAggregatedList], + request: compute.AggregatedListInstanceGroupsRequest, + response: compute.InstanceGroupAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListInstanceGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceGroupAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListInstanceGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceGroupAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.InstanceGroupsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.InstanceGroupsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceGroupList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceGroupList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceGroupList], + request: compute.ListInstanceGroupsRequest, + response: compute.InstanceGroupList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInstanceGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceGroupList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInstanceGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceGroupList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceGroup]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListInstancesPager: + """A pager for iterating through ``list_instances`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceGroupsListInstances` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListInstances`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceGroupsListInstances` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceGroupsListInstances], + request: compute.ListInstancesInstanceGroupsRequest, + response: compute.InstanceGroupsListInstances, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInstancesInstanceGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceGroupsListInstances): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInstancesInstanceGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceGroupsListInstances]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceWithNamedPorts]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/transports/rest.py index fd57d4a82b44..c1dc1f2f4430 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_groups/transports/rest.py @@ -148,6 +148,7 @@ def add_instances( body = compute.InstanceGroupsAddInstancesRequest.to_json( request.instance_groups_add_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -173,7 +174,7 @@ def add_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -214,11 +215,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -444,7 +445,9 @@ def insert( # Jsonify the request body body = compute.InstanceGroup.to_json( - request.instance_group_resource, including_default_value_fields=False + request.instance_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -467,7 +470,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -508,10 +511,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -557,6 +560,7 @@ def list_instances( body = compute.InstanceGroupsListInstancesRequest.to_json( request.instance_groups_list_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -572,10 +576,10 @@ def list_instances( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -586,7 +590,7 @@ def list_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -649,6 +653,7 @@ def remove_instances( body = compute.InstanceGroupsRemoveInstancesRequest.to_json( request.instance_groups_remove_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -674,7 +679,7 @@ def remove_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -737,6 +742,7 @@ def set_named_ports( body = compute.InstanceGroupsSetNamedPortsRequest.to_json( request.instance_groups_set_named_ports_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -762,7 +768,7 @@ def set_named_ports( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/client.py index 9a4c3eef0cae..014c7b7e22f5 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.instance_templates import pagers from google.cloud.compute_v1.types import compute from .transports.base import InstanceTemplatesTransport, DEFAULT_CLIENT_INFO @@ -748,7 +749,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceTemplateList: + ) -> pagers.ListPager: r"""Retrieves a list of instance templates that are contained within the specified project. @@ -770,8 +771,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceTemplateList: + google.cloud.compute_v1.services.instance_templates.pagers.ListPager: A list of instance templates. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -803,6 +808,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/pagers.py new file mode 100644 index 000000000000..5f767a6cb836 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceTemplateList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceTemplateList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceTemplateList], + request: compute.ListInstanceTemplatesRequest, + response: compute.InstanceTemplateList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInstanceTemplatesRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceTemplateList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInstanceTemplatesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceTemplateList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceTemplate]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/transports/rest.py index 5ab3375c183e..97b0a2e8dfb8 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instance_templates/transports/rest.py @@ -385,7 +385,9 @@ def insert( # Jsonify the request body body = compute.InstanceTemplate.to_json( - request.instance_template_resource, including_default_value_fields=False + request.instance_template_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -408,7 +410,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -449,10 +451,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -548,6 +550,7 @@ def set_iam_policy( body = compute.GlobalSetPolicyRequest.to_json( request.global_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -568,7 +571,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -603,6 +606,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -623,7 +627,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/client.py index d267596b5a52..fd5da4d87855 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.instances import pagers from google.cloud.compute_v1.types import compute from .transports.base import InstancesTransport, DEFAULT_CLIENT_INFO @@ -581,7 +582,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves aggregated list of all of the instances in your project across all regions and zones. @@ -603,7 +604,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceAggregatedList: + google.cloud.compute_v1.services.instances.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -636,6 +640,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1808,7 +1818,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceList: + ) -> pagers.ListPager: r"""Retrieves the list of instances contained within the specified zone. @@ -1836,8 +1846,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceList: + google.cloud.compute_v1.services.instances.pagers.ListPager: Contains a list of instances. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -1871,6 +1885,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1884,7 +1904,7 @@ def list_referrers( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InstanceListReferrers: + ) -> pagers.ListReferrersPager: r"""Retrieves a list of resources that refer to the VM instance specified in the request. For example, if the VM instance is part of a managed or unmanaged instance @@ -1926,9 +1946,12 @@ def list_referrers( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InstanceListReferrers: + google.cloud.compute_v1.services.instances.pagers.ListReferrersPager: Contains a list of instance referrers. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1965,6 +1988,12 @@ def list_referrers( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListReferrersPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/pagers.py new file mode 100644 index 000000000000..3907eddb3a11 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/pagers.py @@ -0,0 +1,218 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceAggregatedList], + request: compute.AggregatedListInstancesRequest, + response: compute.InstanceAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListInstancesRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListInstancesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.InstancesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.InstancesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceList], + request: compute.ListInstancesRequest, + response: compute.InstanceList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInstancesRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInstancesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Instance]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListReferrersPager: + """A pager for iterating through ``list_referrers`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InstanceListReferrers` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListReferrers`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InstanceListReferrers` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InstanceListReferrers], + request: compute.ListReferrersInstancesRequest, + response: compute.InstanceListReferrers, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListReferrersInstancesRequest): + The initial request object. + response (google.cloud.compute_v1.types.InstanceListReferrers): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListReferrersInstancesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InstanceListReferrers]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Reference]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/transports/rest.py index d8113002e15b..745de97d50e4 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/instances/transports/rest.py @@ -146,7 +146,9 @@ def add_access_config( # Jsonify the request body body = compute.AccessConfig.to_json( - request.access_config_resource, including_default_value_fields=False + request.access_config_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -173,7 +175,7 @@ def add_access_config( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -236,6 +238,7 @@ def add_resource_policies( body = compute.InstancesAddResourcePoliciesRequest.to_json( request.instances_add_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -261,7 +264,7 @@ def add_resource_policies( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -302,11 +305,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -378,7 +381,9 @@ def attach_disk( # Jsonify the request body body = compute.AttachedDisk.to_json( - request.attached_disk_resource, including_default_value_fields=False + request.attached_disk_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -405,7 +410,7 @@ def attach_disk( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -558,9 +563,9 @@ def delete_access_config( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { + "accessConfig": request.access_config, "networkInterface": request.network_interface, "requestId": request.request_id, - "accessConfig": request.access_config, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -642,8 +647,8 @@ def detach_disk( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "requestId": request.request_id, "deviceName": request.device_name, + "requestId": request.request_id, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -754,8 +759,8 @@ def get_guest_attributes( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "variableKey": request.variable_key, "queryPath": request.query_path, + "variableKey": request.variable_key, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1091,7 +1096,9 @@ def insert( # Jsonify the request body body = compute.Instance.to_json( - request.instance_resource, including_default_value_fields=False + request.instance_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1103,8 +1110,8 @@ def insert( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "sourceInstanceTemplate": request.source_instance_template, "requestId": request.request_id, + "sourceInstanceTemplate": request.source_instance_template, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1115,7 +1122,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1155,10 +1162,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1215,10 +1222,10 @@ def list_referrers( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1292,6 +1299,7 @@ def remove_resource_policies( body = compute.InstancesRemoveResourcePoliciesRequest.to_json( request.instances_remove_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1317,7 +1325,7 @@ def remove_resource_policies( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1554,8 +1562,8 @@ def set_disk_auto_delete( # not required for GCE query_params = { "autoDelete": request.auto_delete, - "requestId": request.request_id, "deviceName": request.device_name, + "requestId": request.request_id, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1651,6 +1659,7 @@ def set_iam_policy( body = compute.ZoneSetPolicyRequest.to_json( request.zone_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1674,7 +1683,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1737,6 +1746,7 @@ def set_labels( body = compute.InstancesSetLabelsRequest.to_json( request.instances_set_labels_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1762,7 +1772,7 @@ def set_labels( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1825,6 +1835,7 @@ def set_machine_resources( body = compute.InstancesSetMachineResourcesRequest.to_json( request.instances_set_machine_resources_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1850,7 +1861,7 @@ def set_machine_resources( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1913,6 +1924,7 @@ def set_machine_type( body = compute.InstancesSetMachineTypeRequest.to_json( request.instances_set_machine_type_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1938,7 +1950,7 @@ def set_machine_type( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1999,7 +2011,9 @@ def set_metadata( # Jsonify the request body body = compute.Metadata.to_json( - request.metadata_resource, including_default_value_fields=False + request.metadata_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2025,7 +2039,7 @@ def set_metadata( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -2088,6 +2102,7 @@ def set_min_cpu_platform( body = compute.InstancesSetMinCpuPlatformRequest.to_json( request.instances_set_min_cpu_platform_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2113,7 +2128,7 @@ def set_min_cpu_platform( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -2174,7 +2189,9 @@ def set_scheduling( # Jsonify the request body body = compute.Scheduling.to_json( - request.scheduling_resource, including_default_value_fields=False + request.scheduling_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2200,7 +2217,7 @@ def set_scheduling( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -2263,6 +2280,7 @@ def set_service_account( body = compute.InstancesSetServiceAccountRequest.to_json( request.instances_set_service_account_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2288,7 +2306,7 @@ def set_service_account( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -2352,6 +2370,7 @@ def set_shielded_instance_integrity_policy( body = compute.ShieldedInstanceIntegrityPolicy.to_json( request.shielded_instance_integrity_policy_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2377,7 +2396,7 @@ def set_shielded_instance_integrity_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -2438,7 +2457,9 @@ def set_tags( # Jsonify the request body body = compute.Tags.to_json( - request.tags_resource, including_default_value_fields=False + request.tags_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2464,7 +2485,7 @@ def set_tags( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -2690,6 +2711,7 @@ def start_with_encryption_key( body = compute.InstancesStartWithEncryptionKeyRequest.to_json( request.instances_start_with_encryption_key_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2715,7 +2737,7 @@ def start_with_encryption_key( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -2831,6 +2853,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2854,7 +2877,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -2915,7 +2938,9 @@ def update( # Jsonify the request body body = compute.Instance.to_json( - request.instance_resource, including_default_value_fields=False + request.instance_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -2930,9 +2955,9 @@ def update( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "requestId": request.request_id, - "mostDisruptiveAllowedAction": request.most_disruptive_allowed_action, "minimalAction": request.minimal_action, + "mostDisruptiveAllowedAction": request.most_disruptive_allowed_action, + "requestId": request.request_id, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -2943,7 +2968,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -3004,7 +3029,9 @@ def update_access_config( # Jsonify the request body body = compute.AccessConfig.to_json( - request.access_config_resource, including_default_value_fields=False + request.access_config_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -3031,7 +3058,7 @@ def update_access_config( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -3092,7 +3119,9 @@ def update_display_device( # Jsonify the request body body = compute.DisplayDevice.to_json( - request.display_device_resource, including_default_value_fields=False + request.display_device_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -3118,7 +3147,7 @@ def update_display_device( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -3179,7 +3208,9 @@ def update_network_interface( # Jsonify the request body body = compute.NetworkInterface.to_json( - request.network_interface_resource, including_default_value_fields=False + request.network_interface_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -3206,7 +3237,7 @@ def update_network_interface( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -3270,6 +3301,7 @@ def update_shielded_instance_config( body = compute.ShieldedInstanceConfig.to_json( request.shielded_instance_config_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -3295,7 +3327,7 @@ def update_shielded_instance_config( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/client.py index 37227afcb661..8a4ed7dfae40 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.interconnect_attachments import pagers from google.cloud.compute_v1.types import compute from .transports.base import InterconnectAttachmentsTransport, DEFAULT_CLIENT_INFO @@ -334,7 +335,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InterconnectAttachmentAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of interconnect attachments. @@ -356,7 +357,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InterconnectAttachmentAggregatedList: + google.cloud.compute_v1.services.interconnect_attachments.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -391,6 +395,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -711,7 +721,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InterconnectAttachmentList: + ) -> pagers.ListPager: r"""Retrieves the list of interconnect attachments contained within the specified region. @@ -738,10 +748,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InterconnectAttachmentList: + google.cloud.compute_v1.services.interconnect_attachments.pagers.ListPager: Response to the list request, and contains a list of interconnect - attachments. + attachments. Iterating over this object + will yield results and resolve + additional pages automatically. """ # Create or coerce a protobuf request object. @@ -776,6 +788,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/pagers.py new file mode 100644 index 000000000000..ae4acf937b7f --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/pagers.py @@ -0,0 +1,158 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InterconnectAttachmentAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InterconnectAttachmentAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InterconnectAttachmentAggregatedList], + request: compute.AggregatedListInterconnectAttachmentsRequest, + response: compute.InterconnectAttachmentAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListInterconnectAttachmentsRequest): + The initial request object. + response (google.cloud.compute_v1.types.InterconnectAttachmentAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListInterconnectAttachmentsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InterconnectAttachmentAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__( + self, + ) -> Iterable[Tuple[str, compute.InterconnectAttachmentsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.InterconnectAttachmentsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InterconnectAttachmentList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InterconnectAttachmentList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InterconnectAttachmentList], + request: compute.ListInterconnectAttachmentsRequest, + response: compute.InterconnectAttachmentList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInterconnectAttachmentsRequest): + The initial request object. + response (google.cloud.compute_v1.types.InterconnectAttachmentList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInterconnectAttachmentsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InterconnectAttachmentList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InterconnectAttachment]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/transports/rest.py index 8c0627ebb707..4e4f1b9a1964 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_attachments/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -345,6 +345,7 @@ def insert( body = compute.InterconnectAttachment.to_json( request.interconnect_attachment_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -356,8 +357,8 @@ def insert( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "validateOnly": request.validate_only, "requestId": request.request_id, + "validateOnly": request.validate_only, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -368,7 +369,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -412,10 +413,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -489,6 +490,7 @@ def patch( body = compute.InterconnectAttachment.to_json( request.interconnect_attachment_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -514,7 +516,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/client.py index 49b12749db36..64f9a72a2b78 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.interconnect_locations import pagers from google.cloud.compute_v1.types import compute from .transports.base import InterconnectLocationsTransport, DEFAULT_CLIENT_INFO @@ -418,7 +419,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InterconnectLocationList: + ) -> pagers.ListPager: r"""Retrieves the list of interconnect locations available to the specified project. @@ -440,10 +441,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InterconnectLocationList: + google.cloud.compute_v1.services.interconnect_locations.pagers.ListPager: Response to the list request, and contains a list of interconnect - locations. + locations. Iterating over this object + will yield results and resolve + additional pages automatically. """ # Create or coerce a protobuf request object. @@ -476,6 +479,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/pagers.py new file mode 100644 index 000000000000..176d029154ed --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InterconnectLocationList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InterconnectLocationList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InterconnectLocationList], + request: compute.ListInterconnectLocationsRequest, + response: compute.InterconnectLocationList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInterconnectLocationsRequest): + The initial request object. + response (google.cloud.compute_v1.types.InterconnectLocationList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInterconnectLocationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InterconnectLocationList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InterconnectLocation]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/transports/rest.py index 300aeddafe8a..7a860089892b 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnect_locations/transports/rest.py @@ -187,10 +187,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/client.py index 60dcc732df83..1a28e42b3091 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.interconnects import pagers from google.cloud.compute_v1.types import compute from .transports.base import InterconnectsTransport, DEFAULT_CLIENT_INFO @@ -686,7 +687,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.InterconnectList: + ) -> pagers.ListPager: r"""Retrieves the list of interconnect available to the specified project. @@ -708,9 +709,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.InterconnectList: + google.cloud.compute_v1.services.interconnects.pagers.ListPager: Response to the list request, and contains a list of interconnects. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -743,6 +747,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/pagers.py new file mode 100644 index 000000000000..4ce5501ed16f --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.InterconnectList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.InterconnectList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.InterconnectList], + request: compute.ListInterconnectsRequest, + response: compute.InterconnectList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInterconnectsRequest): + The initial request object. + response (google.cloud.compute_v1.types.InterconnectList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInterconnectsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.InterconnectList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Interconnect]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/transports/rest.py index 2e719280076b..b56e966f2551 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/interconnects/transports/rest.py @@ -332,7 +332,9 @@ def insert( # Jsonify the request body body = compute.Interconnect.to_json( - request.interconnect_resource, including_default_value_fields=False + request.interconnect_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -355,7 +357,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -398,10 +400,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -473,7 +475,9 @@ def patch( # Jsonify the request body body = compute.Interconnect.to_json( - request.interconnect_resource, including_default_value_fields=False + request.interconnect_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -496,7 +500,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/license_codes/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/license_codes/transports/rest.py index 0ecb2372ad3b..bb0746b91816 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/license_codes/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/license_codes/transports/rest.py @@ -176,6 +176,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -196,7 +197,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/client.py index 16d404fd54d4..58f6f8177707 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.licenses import pagers from google.cloud.compute_v1.types import compute from .transports.base import LicensesTransport, DEFAULT_CLIENT_INFO @@ -743,7 +744,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.LicensesListResponse: + ) -> pagers.ListPager: r"""Retrieves the list of licenses available in the specified project. This method does not get any licenses that belong to other projects, including licenses @@ -772,7 +773,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.LicensesListResponse: + google.cloud.compute_v1.services.licenses.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -805,6 +809,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/pagers.py new file mode 100644 index 000000000000..b6c5bd63afd1 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.LicensesListResponse` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.LicensesListResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.LicensesListResponse], + request: compute.ListLicensesRequest, + response: compute.LicensesListResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListLicensesRequest): + The initial request object. + response (google.cloud.compute_v1.types.LicensesListResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListLicensesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.LicensesListResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.License]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/transports/rest.py index aec08db081b6..cba33ef8d868 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/licenses/transports/rest.py @@ -153,8 +153,8 @@ def delete( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "requestId": request.request_id, "license": request.license_, + "requestId": request.request_id, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -384,7 +384,9 @@ def insert( # Jsonify the request body body = compute.License.to_json( - request.license_resource, including_default_value_fields=False + request.license_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -407,7 +409,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -447,10 +449,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -546,6 +548,7 @@ def set_iam_policy( body = compute.GlobalSetPolicyRequest.to_json( request.global_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -566,7 +569,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -601,6 +604,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -621,7 +625,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/client.py index 8231e343a79b..b04341968e8d 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.machine_types import pagers from google.cloud.compute_v1.types import compute from .transports.base import MachineTypesTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.MachineTypeAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of machine types. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.MachineTypeAggregatedList: + google.cloud.compute_v1.services.machine_types.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -487,7 +497,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.MachineTypeList: + ) -> pagers.ListPager: r"""Retrieves a list of machine types available to the specified project. @@ -516,8 +526,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.MachineTypeList: + google.cloud.compute_v1.services.machine_types.pagers.ListPager: Contains a list of machine types. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -551,6 +565,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/pagers.py new file mode 100644 index 000000000000..8221af685381 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.MachineTypeAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.MachineTypeAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.MachineTypeAggregatedList], + request: compute.AggregatedListMachineTypesRequest, + response: compute.MachineTypeAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListMachineTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.MachineTypeAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListMachineTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.MachineTypeAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.MachineTypesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.MachineTypesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.MachineTypeList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.MachineTypeList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.MachineTypeList], + request: compute.ListMachineTypesRequest, + response: compute.MachineTypeList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListMachineTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.MachineTypeList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListMachineTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.MachineTypeList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.MachineType]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/transports/rest.py index 79996c2d4c60..e20b1f06b46f 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/machine_types/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -240,10 +240,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/client.py index c14d225a629e..67b7509d09b9 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.network_endpoint_groups import pagers from google.cloud.compute_v1.types import compute from .transports.base import NetworkEndpointGroupsTransport, DEFAULT_CLIENT_INFO @@ -334,7 +335,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NetworkEndpointGroupAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of network endpoint groups and sorts them by zone. @@ -356,7 +357,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NetworkEndpointGroupAggregatedList: + google.cloud.compute_v1.services.network_endpoint_groups.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -389,6 +393,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -998,7 +1008,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NetworkEndpointGroupList: + ) -> pagers.ListPager: r"""Retrieves the list of network endpoint groups that are located in the specified project and zone. @@ -1028,7 +1038,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NetworkEndpointGroupList: + google.cloud.compute_v1.services.network_endpoint_groups.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1063,6 +1076,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1077,7 +1096,7 @@ def list_network_endpoints( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NetworkEndpointGroupsListNetworkEndpoints: + ) -> pagers.ListNetworkEndpointsPager: r"""Lists the network endpoints in the specified network endpoint group. @@ -1121,7 +1140,10 @@ def list_network_endpoints( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NetworkEndpointGroupsListNetworkEndpoints: + google.cloud.compute_v1.services.network_endpoint_groups.pagers.ListNetworkEndpointsPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1171,6 +1193,12 @@ def list_network_endpoints( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListNetworkEndpointsPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/pagers.py new file mode 100644 index 000000000000..6a377efbd79d --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/pagers.py @@ -0,0 +1,220 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NetworkEndpointGroupAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NetworkEndpointGroupAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NetworkEndpointGroupAggregatedList], + request: compute.AggregatedListNetworkEndpointGroupsRequest, + response: compute.NetworkEndpointGroupAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListNetworkEndpointGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NetworkEndpointGroupAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListNetworkEndpointGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NetworkEndpointGroupAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.NetworkEndpointGroupsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.NetworkEndpointGroupsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NetworkEndpointGroupList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NetworkEndpointGroupList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NetworkEndpointGroupList], + request: compute.ListNetworkEndpointGroupsRequest, + response: compute.NetworkEndpointGroupList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListNetworkEndpointGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NetworkEndpointGroupList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListNetworkEndpointGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NetworkEndpointGroupList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NetworkEndpointGroup]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListNetworkEndpointsPager: + """A pager for iterating through ``list_network_endpoints`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NetworkEndpointGroupsListNetworkEndpoints` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListNetworkEndpoints`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NetworkEndpointGroupsListNetworkEndpoints` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NetworkEndpointGroupsListNetworkEndpoints], + request: compute.ListNetworkEndpointsNetworkEndpointGroupsRequest, + response: compute.NetworkEndpointGroupsListNetworkEndpoints, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListNetworkEndpointsNetworkEndpointGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NetworkEndpointGroupsListNetworkEndpoints): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListNetworkEndpointsNetworkEndpointGroupsRequest( + request + ) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NetworkEndpointGroupsListNetworkEndpoints]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NetworkEndpointWithHealthStatus]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/transports/rest.py index b02ede1c9199..105a67afece5 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/network_endpoint_groups/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -204,6 +204,7 @@ def attach_network_endpoints( body = compute.NetworkEndpointGroupsAttachEndpointsRequest.to_json( request.network_endpoint_groups_attach_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -229,7 +230,7 @@ def attach_network_endpoints( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -374,6 +375,7 @@ def detach_network_endpoints( body = compute.NetworkEndpointGroupsDetachEndpointsRequest.to_json( request.network_endpoint_groups_detach_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -399,7 +401,7 @@ def detach_network_endpoints( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -527,6 +529,7 @@ def insert( body = compute.NetworkEndpointGroup.to_json( request.network_endpoint_group_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -549,7 +552,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -590,10 +593,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -639,6 +642,7 @@ def list_network_endpoints( body = compute.NetworkEndpointGroupsListEndpointsRequest.to_json( request.network_endpoint_groups_list_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -654,10 +658,10 @@ def list_network_endpoints( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -668,7 +672,7 @@ def list_network_endpoints( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -705,6 +709,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -728,7 +733,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/client.py index 9c34f5f107c6..e569f7f57041 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.networks import pagers from google.cloud.compute_v1.types import compute from .transports.base import NetworksTransport, DEFAULT_CLIENT_INFO @@ -716,7 +717,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NetworkList: + ) -> pagers.ListPager: r"""Retrieves the list of networks available to the specified project. @@ -737,8 +738,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NetworkList: + google.cloud.compute_v1.services.networks.pagers.ListPager: Contains a list of networks. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -770,6 +775,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -782,7 +793,7 @@ def list_peering_routes( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ExchangedPeeringRoutesList: + ) -> pagers.ListPeeringRoutesPager: r"""Lists the peering routes exchanged over peering connection. @@ -809,7 +820,10 @@ def list_peering_routes( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ExchangedPeeringRoutesList: + google.cloud.compute_v1.services.networks.pagers.ListPeeringRoutesPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -844,6 +858,12 @@ def list_peering_routes( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPeeringRoutesPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/pagers.py new file mode 100644 index 000000000000..3866c6c47e42 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/pagers.py @@ -0,0 +1,153 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NetworkList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NetworkList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NetworkList], + request: compute.ListNetworksRequest, + response: compute.NetworkList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListNetworksRequest): + The initial request object. + response (google.cloud.compute_v1.types.NetworkList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListNetworksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NetworkList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Network]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPeeringRoutesPager: + """A pager for iterating through ``list_peering_routes`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ExchangedPeeringRoutesList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListPeeringRoutes`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ExchangedPeeringRoutesList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ExchangedPeeringRoutesList], + request: compute.ListPeeringRoutesNetworksRequest, + response: compute.ExchangedPeeringRoutesList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListPeeringRoutesNetworksRequest): + The initial request object. + response (google.cloud.compute_v1.types.ExchangedPeeringRoutesList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListPeeringRoutesNetworksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ExchangedPeeringRoutesList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.ExchangedPeeringRoute]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/transports/rest.py index c5965687fc29..3615fc004e5b 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/networks/transports/rest.py @@ -148,6 +148,7 @@ def add_peering( body = compute.NetworksAddPeeringRequest.to_json( request.networks_add_peering_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -170,7 +171,7 @@ def add_peering( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -364,7 +365,9 @@ def insert( # Jsonify the request body body = compute.Network.to_json( - request.network_resource, including_default_value_fields=False + request.network_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -387,7 +390,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -427,10 +430,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -481,14 +484,14 @@ def list_peering_routes( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "filter": request.filter, - "region": request.region, - "pageToken": request.page_token, "direction": request.direction, - "returnPartialSuccess": request.return_partial_success, + "filter": request.filter, "maxResults": request.max_results, - "peeringName": request.peering_name, "orderBy": request.order_by, + "pageToken": request.page_token, + "peeringName": request.peering_name, + "region": request.region, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -559,7 +562,9 @@ def patch( # Jsonify the request body body = compute.Network.to_json( - request.network_resource, including_default_value_fields=False + request.network_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -582,7 +587,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -645,6 +650,7 @@ def remove_peering( body = compute.NetworksRemovePeeringRequest.to_json( request.networks_remove_peering_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -667,7 +673,7 @@ def remove_peering( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -809,6 +815,7 @@ def update_peering( body = compute.NetworksUpdatePeeringRequest.to_json( request.networks_update_peering_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -831,7 +838,7 @@ def update_peering( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/client.py index a980520b8f8b..14d60b3fc0ec 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.node_groups import pagers from google.cloud.compute_v1.types import compute from .transports.base import NodeGroupsTransport, DEFAULT_CLIENT_INFO @@ -450,7 +451,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NodeGroupAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of node groups. Note: use nodeGroups.listNodes for more details about each group. @@ -473,7 +474,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NodeGroupAggregatedList: + google.cloud.compute_v1.services.node_groups.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -506,6 +510,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1103,7 +1113,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NodeGroupList: + ) -> pagers.ListPager: r"""Retrieves a list of node groups available to the specified project. Note: use nodeGroups.listNodes for more details about each group. @@ -1132,8 +1142,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NodeGroupList: + google.cloud.compute_v1.services.node_groups.pagers.ListPager: Contains a list of nodeGroups. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -1167,6 +1181,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1180,7 +1200,7 @@ def list_nodes( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NodeGroupsListNodes: + ) -> pagers.ListNodesPager: r"""Lists nodes in the node group. Args: @@ -1215,7 +1235,10 @@ def list_nodes( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NodeGroupsListNodes: + google.cloud.compute_v1.services.node_groups.pagers.ListNodesPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1252,6 +1275,12 @@ def list_nodes( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListNodesPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/pagers.py new file mode 100644 index 000000000000..3bedd0f975c0 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/pagers.py @@ -0,0 +1,218 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NodeGroupAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NodeGroupAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NodeGroupAggregatedList], + request: compute.AggregatedListNodeGroupsRequest, + response: compute.NodeGroupAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListNodeGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NodeGroupAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListNodeGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NodeGroupAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.NodeGroupsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.NodeGroupsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NodeGroupList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NodeGroupList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NodeGroupList], + request: compute.ListNodeGroupsRequest, + response: compute.NodeGroupList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListNodeGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NodeGroupList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListNodeGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NodeGroupList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NodeGroup]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListNodesPager: + """A pager for iterating through ``list_nodes`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NodeGroupsListNodes` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListNodes`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NodeGroupsListNodes` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NodeGroupsListNodes], + request: compute.ListNodesNodeGroupsRequest, + response: compute.NodeGroupsListNodes, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListNodesNodeGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NodeGroupsListNodes): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListNodesNodeGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NodeGroupsListNodes]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NodeGroupNode]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/transports/rest.py index 7b39d94b8643..5494762011b5 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_groups/transports/rest.py @@ -148,6 +148,7 @@ def add_nodes( body = compute.NodeGroupsAddNodesRequest.to_json( request.node_groups_add_nodes_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -173,7 +174,7 @@ def add_nodes( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -214,11 +215,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -374,6 +375,7 @@ def delete_nodes( body = compute.NodeGroupsDeleteNodesRequest.to_json( request.node_groups_delete_nodes_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -399,7 +401,7 @@ def delete_nodes( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -624,7 +626,9 @@ def insert( # Jsonify the request body body = compute.NodeGroup.to_json( - request.node_group_resource, including_default_value_fields=False + request.node_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -648,7 +652,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -689,10 +693,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -747,10 +751,10 @@ def list_nodes( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -822,7 +826,9 @@ def patch( # Jsonify the request body body = compute.NodeGroup.to_json( - request.node_group_resource, including_default_value_fields=False + request.node_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -848,7 +854,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -933,6 +939,7 @@ def set_iam_policy( body = compute.ZoneSetPolicyRequest.to_json( request.zone_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -956,7 +963,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1019,6 +1026,7 @@ def set_node_template( body = compute.NodeGroupsSetNodeTemplateRequest.to_json( request.node_groups_set_node_template_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1044,7 +1052,7 @@ def set_node_template( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1079,6 +1087,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1102,7 +1111,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/client.py index 7cb04fa26b83..27bff671304e 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.node_templates import pagers from google.cloud.compute_v1.types import compute from .transports.base import NodeTemplatesTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NodeTemplateAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of node templates. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NodeTemplateAggregatedList: + google.cloud.compute_v1.services.node_templates.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -842,7 +852,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NodeTemplateList: + ) -> pagers.ListPager: r"""Retrieves a list of node templates available to the specified project. @@ -871,8 +881,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NodeTemplateList: + google.cloud.compute_v1.services.node_templates.pagers.ListPager: Contains a list of node templates. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -906,6 +920,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/pagers.py new file mode 100644 index 000000000000..16570a82881f --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NodeTemplateAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NodeTemplateAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NodeTemplateAggregatedList], + request: compute.AggregatedListNodeTemplatesRequest, + response: compute.NodeTemplateAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListNodeTemplatesRequest): + The initial request object. + response (google.cloud.compute_v1.types.NodeTemplateAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListNodeTemplatesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NodeTemplateAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.NodeTemplatesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.NodeTemplatesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NodeTemplateList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NodeTemplateList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NodeTemplateList], + request: compute.ListNodeTemplatesRequest, + response: compute.NodeTemplateList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListNodeTemplatesRequest): + The initial request object. + response (google.cloud.compute_v1.types.NodeTemplateList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListNodeTemplatesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NodeTemplateList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NodeTemplate]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/transports/rest.py index bc92942fef59..840c034ddb74 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_templates/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -446,7 +446,9 @@ def insert( # Jsonify the request body body = compute.NodeTemplate.to_json( - request.node_template_resource, including_default_value_fields=False + request.node_template_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -469,7 +471,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -510,10 +512,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -609,6 +611,7 @@ def set_iam_policy( body = compute.RegionSetPolicyRequest.to_json( request.region_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -632,7 +635,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -667,6 +670,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -690,7 +694,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/client.py index a75012cdb23b..c33817efbf16 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.node_types import pagers from google.cloud.compute_v1.types import compute from .transports.base import NodeTypesTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NodeTypeAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of node types. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NodeTypeAggregatedList: + google.cloud.compute_v1.services.node_types.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -488,7 +498,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NodeTypeList: + ) -> pagers.ListPager: r"""Retrieves a list of node types available to the specified project. @@ -516,8 +526,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NodeTypeList: + google.cloud.compute_v1.services.node_types.pagers.ListPager: Contains a list of node types. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -551,6 +565,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/pagers.py new file mode 100644 index 000000000000..1c5849e80950 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NodeTypeAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NodeTypeAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NodeTypeAggregatedList], + request: compute.AggregatedListNodeTypesRequest, + response: compute.NodeTypeAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListNodeTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.NodeTypeAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListNodeTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NodeTypeAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.NodeTypesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.NodeTypesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NodeTypeList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NodeTypeList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NodeTypeList], + request: compute.ListNodeTypesRequest, + response: compute.NodeTypeList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListNodeTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.NodeTypeList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListNodeTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NodeTypeList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NodeType]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/transports/rest.py index f906e1049d9b..711ddebb02ef 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/node_types/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -241,10 +241,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/client.py index bd357ab52892..62063109258c 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.packet_mirrorings import pagers from google.cloud.compute_v1.types import compute from .transports.base import PacketMirroringsTransport, DEFAULT_CLIENT_INFO @@ -332,7 +333,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.PacketMirroringAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of packetMirrorings. Args: @@ -353,8 +354,12 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.PacketMirroringAggregatedList: + google.cloud.compute_v1.services.packet_mirrorings.pagers.AggregatedListPager: Contains a list of packetMirrorings. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -386,6 +391,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -707,7 +718,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.PacketMirroringList: + ) -> pagers.ListPager: r"""Retrieves a list of PacketMirroring resources available to the specified project and region. @@ -734,9 +745,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.PacketMirroringList: + google.cloud.compute_v1.services.packet_mirrorings.pagers.ListPager: Contains a list of PacketMirroring resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -771,6 +785,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/pagers.py new file mode 100644 index 000000000000..40b54f2d3b28 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.PacketMirroringAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.PacketMirroringAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.PacketMirroringAggregatedList], + request: compute.AggregatedListPacketMirroringsRequest, + response: compute.PacketMirroringAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListPacketMirroringsRequest): + The initial request object. + response (google.cloud.compute_v1.types.PacketMirroringAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListPacketMirroringsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.PacketMirroringAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.PacketMirroringsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.PacketMirroringsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.PacketMirroringList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.PacketMirroringList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.PacketMirroringList], + request: compute.ListPacketMirroringsRequest, + response: compute.PacketMirroringList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListPacketMirroringsRequest): + The initial request object. + response (google.cloud.compute_v1.types.PacketMirroringList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListPacketMirroringsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.PacketMirroringList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.PacketMirroring]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/transports/rest.py index 558af6236420..a73754a93f22 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/packet_mirrorings/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -345,7 +345,9 @@ def insert( # Jsonify the request body body = compute.PacketMirroring.to_json( - request.packet_mirroring_resource, including_default_value_fields=False + request.packet_mirroring_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -368,7 +370,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -411,10 +413,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -486,7 +488,9 @@ def patch( # Jsonify the request body body = compute.PacketMirroring.to_json( - request.packet_mirroring_resource, including_default_value_fields=False + request.packet_mirroring_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -512,7 +516,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -547,6 +551,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -570,7 +575,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/client.py index d377e0a4be4c..d8105efb138b 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.projects import pagers from google.cloud.compute_v1.types import compute from .transports.base import ProjectsTransport, DEFAULT_CLIENT_INFO @@ -859,7 +860,7 @@ def get_xpn_resources( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ProjectsGetXpnResources: + ) -> pagers.GetXpnResourcesPager: r"""Gets service resources (a.k.a service project) associated with this host project. @@ -881,7 +882,10 @@ def get_xpn_resources( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ProjectsGetXpnResources: + google.cloud.compute_v1.services.projects.pagers.GetXpnResourcesPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -914,6 +918,12 @@ def get_xpn_resources( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.GetXpnResourcesPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -926,7 +936,7 @@ def list_xpn_hosts( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.XpnHostList: + ) -> pagers.ListXpnHostsPager: r"""Lists all shared VPC host projects visible to the user in an organization. @@ -953,7 +963,10 @@ def list_xpn_hosts( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.XpnHostList: + google.cloud.compute_v1.services.projects.pagers.ListXpnHostsPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -990,6 +1003,12 @@ def list_xpn_hosts( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListXpnHostsPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/pagers.py new file mode 100644 index 000000000000..f94918d8553d --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/pagers.py @@ -0,0 +1,153 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class GetXpnResourcesPager: + """A pager for iterating through ``get_xpn_resources`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ProjectsGetXpnResources` object, and + provides an ``__iter__`` method to iterate through its + ``resources`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``GetXpnResources`` requests and continue to iterate + through the ``resources`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ProjectsGetXpnResources` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ProjectsGetXpnResources], + request: compute.GetXpnResourcesProjectsRequest, + response: compute.ProjectsGetXpnResources, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.GetXpnResourcesProjectsRequest): + The initial request object. + response (google.cloud.compute_v1.types.ProjectsGetXpnResources): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.GetXpnResourcesProjectsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ProjectsGetXpnResources]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.XpnResourceId]: + for page in self.pages: + yield from page.resources + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListXpnHostsPager: + """A pager for iterating through ``list_xpn_hosts`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.XpnHostList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListXpnHosts`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.XpnHostList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.XpnHostList], + request: compute.ListXpnHostsProjectsRequest, + response: compute.XpnHostList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListXpnHostsProjectsRequest): + The initial request object. + response (google.cloud.compute_v1.types.XpnHostList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListXpnHostsProjectsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.XpnHostList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Project]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/transports/rest.py index a74a22345d23..b33374d53402 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/projects/transports/rest.py @@ -227,6 +227,7 @@ def disable_xpn_resource( body = compute.ProjectsDisableXpnResourceRequest.to_json( request.projects_disable_xpn_resource_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -249,7 +250,7 @@ def disable_xpn_resource( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -391,6 +392,7 @@ def enable_xpn_resource( body = compute.ProjectsEnableXpnResourceRequest.to_json( request.projects_enable_xpn_resource_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -413,7 +415,7 @@ def enable_xpn_resource( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -563,10 +565,10 @@ def get_xpn_resources( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -612,6 +614,7 @@ def list_xpn_hosts( body = compute.ProjectsListXpnHostsRequest.to_json( request.projects_list_xpn_hosts_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -624,10 +627,10 @@ def list_xpn_hosts( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -638,7 +641,7 @@ def list_xpn_hosts( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -699,7 +702,9 @@ def move_disk( # Jsonify the request body body = compute.DiskMoveRequest.to_json( - request.disk_move_request_resource, including_default_value_fields=False + request.disk_move_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -722,7 +727,7 @@ def move_disk( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -783,7 +788,9 @@ def move_instance( # Jsonify the request body body = compute.InstanceMoveRequest.to_json( - request.instance_move_request_resource, including_default_value_fields=False + request.instance_move_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -806,7 +813,7 @@ def move_instance( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -868,7 +875,9 @@ def set_common_instance_metadata( # Jsonify the request body body = compute.Metadata.to_json( - request.metadata_resource, including_default_value_fields=False + request.metadata_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -891,7 +900,7 @@ def set_common_instance_metadata( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -954,6 +963,7 @@ def set_default_network_tier( body = compute.ProjectsSetDefaultNetworkTierRequest.to_json( request.projects_set_default_network_tier_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -976,7 +986,7 @@ def set_default_network_tier( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1037,7 +1047,9 @@ def set_usage_export_bucket( # Jsonify the request body body = compute.UsageExportLocation.to_json( - request.usage_export_location_resource, including_default_value_fields=False + request.usage_export_location_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1060,7 +1072,7 @@ def set_usage_export_bucket( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/client.py index 4c1100caea18..8f0d64cab241 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_autoscalers import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionAutoscalersTransport, DEFAULT_CLIENT_INFO @@ -655,7 +656,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionAutoscalerList: + ) -> pagers.ListPager: r"""Retrieves a list of autoscalers contained within the specified region. @@ -684,8 +685,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionAutoscalerList: + google.cloud.compute_v1.services.region_autoscalers.pagers.ListPager: Contains a list of autoscalers. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -719,6 +724,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/pagers.py new file mode 100644 index 000000000000..6b12d3bafe35 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionAutoscalerList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionAutoscalerList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RegionAutoscalerList], + request: compute.ListRegionAutoscalersRequest, + response: compute.RegionAutoscalerList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionAutoscalersRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionAutoscalerList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionAutoscalersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RegionAutoscalerList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Autoscaler]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/transports/rest.py index 1ed3ae4fa90b..3a39dd6a21f7 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_autoscalers/transports/rest.py @@ -300,7 +300,9 @@ def insert( # Jsonify the request body body = compute.Autoscaler.to_json( - request.autoscaler_resource, including_default_value_fields=False + request.autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -323,7 +325,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -364,10 +366,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -439,7 +441,9 @@ def patch( # Jsonify the request body body = compute.Autoscaler.to_json( - request.autoscaler_resource, including_default_value_fields=False + request.autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -463,7 +467,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -524,7 +528,9 @@ def update( # Jsonify the request body body = compute.Autoscaler.to_json( - request.autoscaler_resource, including_default_value_fields=False + request.autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -548,7 +554,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/client.py index 49acf63d6000..7f0d5ab1476c 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_backend_services import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionBackendServicesTransport, DEFAULT_CLIENT_INFO @@ -763,7 +764,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.BackendServiceList: + ) -> pagers.ListPager: r"""Retrieves the list of regional BackendService resources available to the specified project in the given region. @@ -793,9 +794,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.BackendServiceList: + google.cloud.compute_v1.services.region_backend_services.pagers.ListPager: Contains a list of BackendService resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -830,6 +834,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/pagers.py new file mode 100644 index 000000000000..df369b0cae49 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.BackendServiceList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.BackendServiceList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.BackendServiceList], + request: compute.ListRegionBackendServicesRequest, + response: compute.BackendServiceList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionBackendServicesRequest): + The initial request object. + response (google.cloud.compute_v1.types.BackendServiceList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionBackendServicesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.BackendServiceList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.BackendService]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/transports/rest.py index 6454a7b9f4bd..0e30895e725c 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_backend_services/transports/rest.py @@ -276,6 +276,7 @@ def get_health( body = compute.ResourceGroupReference.to_json( request.resource_group_reference_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -299,7 +300,7 @@ def get_health( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -360,7 +361,9 @@ def insert( # Jsonify the request body body = compute.BackendService.to_json( - request.backend_service_resource, including_default_value_fields=False + request.backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -383,7 +386,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -426,10 +429,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -501,7 +504,9 @@ def patch( # Jsonify the request body body = compute.BackendService.to_json( - request.backend_service_resource, including_default_value_fields=False + request.backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -527,7 +532,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -588,7 +593,9 @@ def update( # Jsonify the request body body = compute.BackendService.to_json( - request.backend_service_resource, including_default_value_fields=False + request.backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -614,7 +621,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/client.py index 76268f052ece..37cb7eaa2699 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_commitments import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionCommitmentsTransport, DEFAULT_CLIENT_INFO @@ -334,7 +335,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.CommitmentAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of commitments. Args: @@ -355,7 +356,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.CommitmentAggregatedList: + google.cloud.compute_v1.services.region_commitments.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -388,6 +392,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -599,7 +609,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.CommitmentList: + ) -> pagers.ListPager: r"""Retrieves a list of commitments contained within the specified region. @@ -626,9 +636,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.CommitmentList: + google.cloud.compute_v1.services.region_commitments.pagers.ListPager: Contains a list of Commitment resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -663,6 +676,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/pagers.py new file mode 100644 index 000000000000..54dc0eb1c9f8 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.CommitmentAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.CommitmentAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.CommitmentAggregatedList], + request: compute.AggregatedListRegionCommitmentsRequest, + response: compute.CommitmentAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListRegionCommitmentsRequest): + The initial request object. + response (google.cloud.compute_v1.types.CommitmentAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListRegionCommitmentsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.CommitmentAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.CommitmentsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.CommitmentsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.CommitmentList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.CommitmentList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.CommitmentList], + request: compute.ListRegionCommitmentsRequest, + response: compute.CommitmentList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionCommitmentsRequest): + The initial request object. + response (google.cloud.compute_v1.types.CommitmentList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionCommitmentsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.CommitmentList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Commitment]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/transports/rest.py index dac9a0aedd0b..6b2d56cc5ef4 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_commitments/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -263,7 +263,9 @@ def insert( # Jsonify the request body body = compute.Commitment.to_json( - request.commitment_resource, including_default_value_fields=False + request.commitment_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -286,7 +288,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -329,10 +331,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/client.py index f4f0fb23b384..101d8eadac34 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_disk_types import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionDiskTypesTransport, DEFAULT_CLIENT_INFO @@ -438,7 +439,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionDiskTypeList: + ) -> pagers.ListPager: r"""Retrieves a list of regional disk types available to the specified project. @@ -467,7 +468,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionDiskTypeList: + google.cloud.compute_v1.services.region_disk_types.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -502,6 +506,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/pagers.py new file mode 100644 index 000000000000..a0ca2f635638 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionDiskTypeList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionDiskTypeList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RegionDiskTypeList], + request: compute.ListRegionDiskTypesRequest, + response: compute.RegionDiskTypeList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionDiskTypesRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionDiskTypeList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionDiskTypesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RegionDiskTypeList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.DiskType]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/transports/rest.py index c9e358b2a715..77cb03576ded 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disk_types/transports/rest.py @@ -198,10 +198,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/client.py index 85a4252b7b6c..c75fe341ec96 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_disks import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionDisksTransport, DEFAULT_CLIENT_INFO @@ -1029,7 +1030,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.DiskList: + ) -> pagers.ListPager: r"""Retrieves the list of persistent disks contained within the specified region. @@ -1056,8 +1057,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.DiskList: + google.cloud.compute_v1.services.region_disks.pagers.ListPager: A list of Disk resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -1091,6 +1096,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/pagers.py new file mode 100644 index 000000000000..e370537fa714 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.DiskList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.DiskList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.DiskList], + request: compute.ListRegionDisksRequest, + response: compute.DiskList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionDisksRequest): + The initial request object. + response (google.cloud.compute_v1.types.DiskList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionDisksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.DiskList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Disk]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/transports/rest.py index 8ca35e3c4ce7..693cf83b38e9 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_disks/transports/rest.py @@ -148,6 +148,7 @@ def add_resource_policies( body = compute.RegionDisksAddResourcePoliciesRequest.to_json( request.region_disks_add_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -173,7 +174,7 @@ def add_resource_policies( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -234,7 +235,9 @@ def create_snapshot( # Jsonify the request body body = compute.Snapshot.to_json( - request.snapshot_resource, including_default_value_fields=False + request.snapshot_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -260,7 +263,7 @@ def create_snapshot( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -579,7 +582,9 @@ def insert( # Jsonify the request body body = compute.Disk.to_json( - request.disk_resource, including_default_value_fields=False + request.disk_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -591,8 +596,8 @@ def insert( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "sourceImage": request.source_image, "requestId": request.request_id, + "sourceImage": request.source_image, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -603,7 +608,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -644,10 +649,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -721,6 +726,7 @@ def remove_resource_policies( body = compute.RegionDisksRemoveResourcePoliciesRequest.to_json( request.region_disks_remove_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -746,7 +752,7 @@ def remove_resource_policies( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -809,6 +815,7 @@ def resize( body = compute.RegionDisksResizeRequest.to_json( request.region_disks_resize_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -834,7 +841,7 @@ def resize( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -919,6 +926,7 @@ def set_iam_policy( body = compute.RegionSetPolicyRequest.to_json( request.region_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -942,7 +950,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1005,6 +1013,7 @@ def set_labels( body = compute.RegionSetLabelsRequest.to_json( request.region_set_labels_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1030,7 +1039,7 @@ def set_labels( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1065,6 +1074,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1088,7 +1098,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/client.py index 5a7c2be189fc..28a58ad0a63f 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_health_check_services import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionHealthCheckServicesTransport, DEFAULT_CLIENT_INFO @@ -647,7 +648,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.HealthCheckServicesList: + ) -> pagers.ListPager: r"""Lists all the HealthCheckService resources that have been configured for the specified project in the given region. @@ -677,7 +678,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.HealthCheckServicesList: + google.cloud.compute_v1.services.region_health_check_services.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -712,6 +716,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/pagers.py new file mode 100644 index 000000000000..b0fd2a06b156 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.HealthCheckServicesList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.HealthCheckServicesList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.HealthCheckServicesList], + request: compute.ListRegionHealthCheckServicesRequest, + response: compute.HealthCheckServicesList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionHealthCheckServicesRequest): + The initial request object. + response (google.cloud.compute_v1.types.HealthCheckServicesList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionHealthCheckServicesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.HealthCheckServicesList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.HealthCheckService]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/transports/rest.py index 224221666e26..28b958da5cd6 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_check_services/transports/rest.py @@ -284,7 +284,9 @@ def insert( # Jsonify the request body body = compute.HealthCheckService.to_json( - request.health_check_service_resource, including_default_value_fields=False + request.health_check_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -307,7 +309,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -348,10 +350,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -423,7 +425,9 @@ def patch( # Jsonify the request body body = compute.HealthCheckService.to_json( - request.health_check_service_resource, including_default_value_fields=False + request.health_check_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -449,7 +453,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/client.py index 6e66d7696a09..d0a49b9035f4 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_health_checks import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionHealthChecksTransport, DEFAULT_CLIENT_INFO @@ -669,7 +670,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.HealthCheckList: + ) -> pagers.ListPager: r"""Retrieves the list of HealthCheck resources available to the specified project. @@ -698,9 +699,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.HealthCheckList: + google.cloud.compute_v1.services.region_health_checks.pagers.ListPager: Contains a list of HealthCheck resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -735,6 +739,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/pagers.py new file mode 100644 index 000000000000..8e65a25b14b7 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.HealthCheckList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.HealthCheckList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.HealthCheckList], + request: compute.ListRegionHealthChecksRequest, + response: compute.HealthCheckList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionHealthChecksRequest): + The initial request object. + response (google.cloud.compute_v1.types.HealthCheckList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionHealthChecksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.HealthCheckList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.HealthCheck]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/transports/rest.py index 62ef85dad256..05c7c23821fd 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_health_checks/transports/rest.py @@ -308,7 +308,9 @@ def insert( # Jsonify the request body body = compute.HealthCheck.to_json( - request.health_check_resource, including_default_value_fields=False + request.health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -331,7 +333,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -374,10 +376,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -449,7 +451,9 @@ def patch( # Jsonify the request body body = compute.HealthCheck.to_json( - request.health_check_resource, including_default_value_fields=False + request.health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -475,7 +479,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -536,7 +540,9 @@ def update( # Jsonify the request body body = compute.HealthCheck.to_json( - request.health_check_resource, including_default_value_fields=False + request.health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -562,7 +568,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/client.py index a12fc302e1dd..6e97236e4e67 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_instance_group_managers import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionInstanceGroupManagersTransport, DEFAULT_CLIENT_INFO @@ -1372,7 +1373,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionInstanceGroupManagerList: + ) -> pagers.ListPager: r"""Retrieves the list of managed instance groups that are contained within the specified region. @@ -1401,9 +1402,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionInstanceGroupManagerList: + google.cloud.compute_v1.services.region_instance_group_managers.pagers.ListPager: Contains a list of managed instance groups. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1438,6 +1442,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1451,7 +1461,7 @@ def list_errors( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionInstanceGroupManagersListErrorsResponse: + ) -> pagers.ListErrorsPager: r"""Lists all errors thrown by actions on instances for a given regional managed instance group. The filter and orderBy query parameters are not supported. @@ -1490,7 +1500,10 @@ def list_errors( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionInstanceGroupManagersListErrorsResponse: + google.cloud.compute_v1.services.region_instance_group_managers.pagers.ListErrorsPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1529,6 +1542,12 @@ def list_errors( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListErrorsPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1542,7 +1561,7 @@ def list_managed_instances( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionInstanceGroupManagersListInstancesResponse: + ) -> pagers.ListManagedInstancesPager: r"""Lists the instances in the managed instance group and instances that are scheduled to be created. The list includes any current actions that the group has @@ -1581,7 +1600,10 @@ def list_managed_instances( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionInstanceGroupManagersListInstancesResponse: + google.cloud.compute_v1.services.region_instance_group_managers.pagers.ListManagedInstancesPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1622,6 +1644,12 @@ def list_managed_instances( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListManagedInstancesPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1635,7 +1663,7 @@ def list_per_instance_configs( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionInstanceGroupManagersListInstanceConfigsResp: + ) -> pagers.ListPerInstanceConfigsPager: r"""Lists all of the per-instance configs defined for the managed instance group. The orderBy query parameter is not supported. @@ -1672,7 +1700,10 @@ def list_per_instance_configs( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionInstanceGroupManagersListInstanceConfigsResp: + google.cloud.compute_v1.services.region_instance_group_managers.pagers.ListPerInstanceConfigsPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1715,6 +1746,12 @@ def list_per_instance_configs( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPerInstanceConfigsPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/pagers.py new file mode 100644 index 000000000000..190a93fd72ca --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/pagers.py @@ -0,0 +1,287 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionInstanceGroupManagerList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionInstanceGroupManagerList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RegionInstanceGroupManagerList], + request: compute.ListRegionInstanceGroupManagersRequest, + response: compute.RegionInstanceGroupManagerList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionInstanceGroupManagerList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionInstanceGroupManagersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RegionInstanceGroupManagerList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceGroupManager]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListErrorsPager: + """A pager for iterating through ``list_errors`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionInstanceGroupManagersListErrorsResponse` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListErrors`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionInstanceGroupManagersListErrorsResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RegionInstanceGroupManagersListErrorsResponse], + request: compute.ListErrorsRegionInstanceGroupManagersRequest, + response: compute.RegionInstanceGroupManagersListErrorsResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListErrorsRegionInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionInstanceGroupManagersListErrorsResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListErrorsRegionInstanceGroupManagersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RegionInstanceGroupManagersListErrorsResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceManagedByIgmError]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListManagedInstancesPager: + """A pager for iterating through ``list_managed_instances`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionInstanceGroupManagersListInstancesResponse` object, and + provides an ``__iter__`` method to iterate through its + ``managed_instances`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListManagedInstances`` requests and continue to iterate + through the ``managed_instances`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionInstanceGroupManagersListInstancesResponse` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RegionInstanceGroupManagersListInstancesResponse], + request: compute.ListManagedInstancesRegionInstanceGroupManagersRequest, + response: compute.RegionInstanceGroupManagersListInstancesResponse, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListManagedInstancesRegionInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionInstanceGroupManagersListInstancesResponse): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListManagedInstancesRegionInstanceGroupManagersRequest( + request + ) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages( + self, + ) -> Iterable[compute.RegionInstanceGroupManagersListInstancesResponse]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.ManagedInstance]: + for page in self.pages: + yield from page.managed_instances + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPerInstanceConfigsPager: + """A pager for iterating through ``list_per_instance_configs`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionInstanceGroupManagersListInstanceConfigsResp` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListPerInstanceConfigs`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionInstanceGroupManagersListInstanceConfigsResp` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[ + ..., compute.RegionInstanceGroupManagersListInstanceConfigsResp + ], + request: compute.ListPerInstanceConfigsRegionInstanceGroupManagersRequest, + response: compute.RegionInstanceGroupManagersListInstanceConfigsResp, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListPerInstanceConfigsRegionInstanceGroupManagersRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionInstanceGroupManagersListInstanceConfigsResp): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListPerInstanceConfigsRegionInstanceGroupManagersRequest( + request + ) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages( + self, + ) -> Iterable[compute.RegionInstanceGroupManagersListInstanceConfigsResp]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.PerInstanceConfig]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/transports/rest.py index e18d9fada079..af94f0a4807a 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_group_managers/transports/rest.py @@ -148,6 +148,7 @@ def abandon_instances( body = compute.RegionInstanceGroupManagersAbandonInstancesRequest.to_json( request.region_instance_group_managers_abandon_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -173,7 +174,7 @@ def abandon_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -237,6 +238,7 @@ def apply_updates_to_instances( body = compute.RegionInstanceGroupManagersApplyUpdatesRequest.to_json( request.region_instance_group_managers_apply_updates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -260,7 +262,7 @@ def apply_updates_to_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -323,6 +325,7 @@ def create_instances( body = compute.RegionInstanceGroupManagersCreateInstancesRequest.to_json( request.region_instance_group_managers_create_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -348,7 +351,7 @@ def create_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -493,6 +496,7 @@ def delete_instances( body = compute.RegionInstanceGroupManagersDeleteInstancesRequest.to_json( request.region_instance_group_managers_delete_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -518,7 +522,7 @@ def delete_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -582,6 +586,7 @@ def delete_per_instance_configs( body = compute.RegionInstanceGroupManagerDeleteInstanceConfigReq.to_json( request.region_instance_group_manager_delete_instance_config_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -605,7 +610,7 @@ def delete_per_instance_configs( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -734,6 +739,7 @@ def insert( body = compute.InstanceGroupManager.to_json( request.instance_group_manager_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -756,7 +762,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -799,10 +805,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -857,10 +863,10 @@ def list_errors( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -917,10 +923,10 @@ def list_managed_instances( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -977,10 +983,10 @@ def list_per_instance_configs( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1056,6 +1062,7 @@ def patch( body = compute.InstanceGroupManager.to_json( request.instance_group_manager_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1081,7 +1088,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1145,6 +1152,7 @@ def patch_per_instance_configs( body = compute.RegionInstanceGroupManagerPatchInstanceConfigReq.to_json( request.region_instance_group_manager_patch_instance_config_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1170,7 +1178,7 @@ def patch_per_instance_configs( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1233,6 +1241,7 @@ def recreate_instances( body = compute.RegionInstanceGroupManagersRecreateRequest.to_json( request.region_instance_group_managers_recreate_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1258,7 +1267,7 @@ def recreate_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1329,8 +1338,8 @@ def resize( # TODO(yon-mg): handle nested fields corerctly rather than using only top level fields # not required for GCE query_params = { - "size": request.size, "requestId": request.request_id, + "size": request.size, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -1404,6 +1413,7 @@ def set_instance_template( body = compute.RegionInstanceGroupManagersSetTemplateRequest.to_json( request.region_instance_group_managers_set_template_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1429,7 +1439,7 @@ def set_instance_template( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1492,6 +1502,7 @@ def set_target_pools( body = compute.RegionInstanceGroupManagersSetTargetPoolsRequest.to_json( request.region_instance_group_managers_set_target_pools_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1517,7 +1528,7 @@ def set_target_pools( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -1581,6 +1592,7 @@ def update_per_instance_configs( body = compute.RegionInstanceGroupManagerUpdateInstanceConfigReq.to_json( request.region_instance_group_manager_update_instance_config_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1606,7 +1618,7 @@ def update_per_instance_configs( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/client.py index cf091cccdba2..7e5a877d7dfc 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_instance_groups import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionInstanceGroupsTransport, DEFAULT_CLIENT_INFO @@ -442,7 +443,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionInstanceGroupList: + ) -> pagers.ListPager: r"""Retrieves the list of instance group resources contained within the specified region. @@ -471,9 +472,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionInstanceGroupList: + google.cloud.compute_v1.services.region_instance_groups.pagers.ListPager: Contains a list of InstanceGroup resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -508,6 +512,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -522,7 +532,7 @@ def list_instances( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionInstanceGroupsListInstances: + ) -> pagers.ListInstancesPager: r"""Lists the instances in the specified instance group and displays information about the named ports. Depending on the specified options, this method can list @@ -566,7 +576,10 @@ def list_instances( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionInstanceGroupsListInstances: + google.cloud.compute_v1.services.region_instance_groups.pagers.ListInstancesPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -614,6 +627,12 @@ def list_instances( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListInstancesPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/pagers.py new file mode 100644 index 000000000000..99e176e4b63f --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/pagers.py @@ -0,0 +1,153 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionInstanceGroupList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionInstanceGroupList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RegionInstanceGroupList], + request: compute.ListRegionInstanceGroupsRequest, + response: compute.RegionInstanceGroupList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionInstanceGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionInstanceGroupList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionInstanceGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RegionInstanceGroupList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceGroup]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListInstancesPager: + """A pager for iterating through ``list_instances`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionInstanceGroupsListInstances` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListInstances`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionInstanceGroupsListInstances` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RegionInstanceGroupsListInstances], + request: compute.ListInstancesRegionInstanceGroupsRequest, + response: compute.RegionInstanceGroupsListInstances, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListInstancesRegionInstanceGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionInstanceGroupsListInstances): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListInstancesRegionInstanceGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RegionInstanceGroupsListInstances]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.InstanceWithNamedPorts]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/transports/rest.py index 1e73f961a88e..5a9bbd38ae8c 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_instance_groups/transports/rest.py @@ -200,10 +200,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -249,6 +249,7 @@ def list_instances( body = compute.RegionInstanceGroupsListInstancesRequest.to_json( request.region_instance_groups_list_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -264,10 +265,10 @@ def list_instances( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -278,7 +279,7 @@ def list_instances( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -341,6 +342,7 @@ def set_named_ports( body = compute.RegionInstanceGroupsSetNamedPortsRequest.to_json( request.region_instance_groups_set_named_ports_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -366,7 +368,7 @@ def set_named_ports( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/client.py index 96ab91b67102..5d97de41819d 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_network_endpoint_groups import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionNetworkEndpointGroupsTransport, DEFAULT_CLIENT_INFO @@ -666,7 +667,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NetworkEndpointGroupList: + ) -> pagers.ListPager: r"""Retrieves the list of regional network endpoint groups available to the specified project in the given region. @@ -697,7 +698,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NetworkEndpointGroupList: + google.cloud.compute_v1.services.region_network_endpoint_groups.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -732,6 +736,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/pagers.py new file mode 100644 index 000000000000..66fb6aa055d7 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NetworkEndpointGroupList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NetworkEndpointGroupList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NetworkEndpointGroupList], + request: compute.ListRegionNetworkEndpointGroupsRequest, + response: compute.NetworkEndpointGroupList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionNetworkEndpointGroupsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NetworkEndpointGroupList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionNetworkEndpointGroupsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NetworkEndpointGroupList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NetworkEndpointGroup]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/transports/rest.py index 352b70f12513..a395f4264cd7 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_network_endpoint_groups/transports/rest.py @@ -295,6 +295,7 @@ def insert( body = compute.NetworkEndpointGroup.to_json( request.network_endpoint_group_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -317,7 +318,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -358,10 +359,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/client.py index 8161acfecd55..45bb0d59a4a1 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_notification_endpoints import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionNotificationEndpointsTransport, DEFAULT_CLIENT_INFO @@ -655,7 +656,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.NotificationEndpointList: + ) -> pagers.ListPager: r"""Lists the NotificationEndpoints for a project in the given region. @@ -684,7 +685,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.NotificationEndpointList: + google.cloud.compute_v1.services.region_notification_endpoints.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -719,6 +723,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/pagers.py new file mode 100644 index 000000000000..dc3331ed7d51 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.NotificationEndpointList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.NotificationEndpointList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.NotificationEndpointList], + request: compute.ListRegionNotificationEndpointsRequest, + response: compute.NotificationEndpointList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionNotificationEndpointsRequest): + The initial request object. + response (google.cloud.compute_v1.types.NotificationEndpointList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionNotificationEndpointsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.NotificationEndpointList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.NotificationEndpoint]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/transports/rest.py index 5a740dda55fe..b1af05fbe3a8 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_notification_endpoints/transports/rest.py @@ -290,7 +290,9 @@ def insert( # Jsonify the request body body = compute.NotificationEndpoint.to_json( - request.notification_endpoint_resource, including_default_value_fields=False + request.notification_endpoint_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -313,7 +315,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -354,10 +356,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/client.py index 7543b0bb689c..98fa6d571f8a 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_operations import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionOperationsTransport, DEFAULT_CLIENT_INFO @@ -529,7 +530,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.OperationList: + ) -> pagers.ListPager: r"""Retrieves a list of Operation resources contained within the specified region. @@ -556,9 +557,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.OperationList: + google.cloud.compute_v1.services.region_operations.pagers.ListPager: Contains a list of Operation resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -593,6 +597,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/pagers.py new file mode 100644 index 000000000000..fc7a72dc09d8 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.OperationList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.OperationList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.OperationList], + request: compute.ListRegionOperationsRequest, + response: compute.OperationList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionOperationsRequest): + The initial request object. + response (google.cloud.compute_v1.types.OperationList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionOperationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.OperationList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Operation]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/transports/rest.py index 5a0b17033f98..037c2c31f1fa 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_operations/transports/rest.py @@ -263,10 +263,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/client.py index 0d745532704a..3355d6a5abe3 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_ssl_certificates import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionSslCertificatesTransport, DEFAULT_CLIENT_INFO @@ -668,7 +669,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.SslCertificateList: + ) -> pagers.ListPager: r"""Retrieves the list of SslCertificate resources available to the specified project in the specified region. @@ -698,9 +699,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.SslCertificateList: + google.cloud.compute_v1.services.region_ssl_certificates.pagers.ListPager: Contains a list of SslCertificate resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -735,6 +739,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/pagers.py new file mode 100644 index 000000000000..bbe705086931 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.SslCertificateList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.SslCertificateList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.SslCertificateList], + request: compute.ListRegionSslCertificatesRequest, + response: compute.SslCertificateList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionSslCertificatesRequest): + The initial request object. + response (google.cloud.compute_v1.types.SslCertificateList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionSslCertificatesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.SslCertificateList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.SslCertificate]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/transports/rest.py index 6930448e93ca..6878e48c3c46 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_ssl_certificates/transports/rest.py @@ -306,7 +306,9 @@ def insert( # Jsonify the request body body = compute.SslCertificate.to_json( - request.ssl_certificate_resource, including_default_value_fields=False + request.ssl_certificate_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -329,7 +331,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -372,10 +374,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/client.py index 2e839fc113c2..fcd3f671cf58 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_target_http_proxies import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionTargetHttpProxiesTransport, DEFAULT_CLIENT_INFO @@ -665,7 +666,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetHttpProxyList: + ) -> pagers.ListPager: r"""Retrieves the list of TargetHttpProxy resources available to the specified project in the specified region. @@ -695,8 +696,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetHttpProxyList: + google.cloud.compute_v1.services.region_target_http_proxies.pagers.ListPager: A list of TargetHttpProxy resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -730,6 +735,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/pagers.py new file mode 100644 index 000000000000..25a545b2cc80 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetHttpProxyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetHttpProxyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetHttpProxyList], + request: compute.ListRegionTargetHttpProxiesRequest, + response: compute.TargetHttpProxyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionTargetHttpProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetHttpProxyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionTargetHttpProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetHttpProxyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetHttpProxy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/transports/rest.py index 9e56b2c9e761..c7d1662ddf24 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_http_proxies/transports/rest.py @@ -303,7 +303,9 @@ def insert( # Jsonify the request body body = compute.TargetHttpProxy.to_json( - request.target_http_proxy_resource, including_default_value_fields=False + request.target_http_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -326,7 +328,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -367,10 +369,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -442,7 +444,9 @@ def set_url_map( # Jsonify the request body body = compute.UrlMapReference.to_json( - request.url_map_reference_resource, including_default_value_fields=False + request.url_map_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -468,7 +472,7 @@ def set_url_map( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/client.py index 85f53803d15d..71935d61ffef 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_target_https_proxies import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionTargetHttpsProxiesTransport, DEFAULT_CLIENT_INFO @@ -665,7 +666,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetHttpsProxyList: + ) -> pagers.ListPager: r"""Retrieves the list of TargetHttpsProxy resources available to the specified project in the specified region. @@ -695,9 +696,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetHttpsProxyList: + google.cloud.compute_v1.services.region_target_https_proxies.pagers.ListPager: Contains a list of TargetHttpsProxy resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -732,6 +736,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/pagers.py new file mode 100644 index 000000000000..2e38cef7d313 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetHttpsProxyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetHttpsProxyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetHttpsProxyList], + request: compute.ListRegionTargetHttpsProxiesRequest, + response: compute.TargetHttpsProxyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionTargetHttpsProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetHttpsProxyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionTargetHttpsProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetHttpsProxyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetHttpsProxy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/transports/rest.py index c6872c6e5032..861a7215050c 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_target_https_proxies/transports/rest.py @@ -302,7 +302,9 @@ def insert( # Jsonify the request body body = compute.TargetHttpsProxy.to_json( - request.target_https_proxy_resource, including_default_value_fields=False + request.target_https_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -325,7 +327,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -368,10 +370,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -445,6 +447,7 @@ def set_ssl_certificates( body = compute.RegionTargetHttpsProxiesSetSslCertificatesRequest.to_json( request.region_target_https_proxies_set_ssl_certificates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -470,7 +473,7 @@ def set_ssl_certificates( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -531,7 +534,9 @@ def set_url_map( # Jsonify the request body body = compute.UrlMapReference.to_json( - request.url_map_reference_resource, including_default_value_fields=False + request.url_map_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -557,7 +562,7 @@ def set_url_map( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/client.py index 53add9473bb9..45b175910a7f 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.region_url_maps import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionUrlMapsTransport, DEFAULT_CLIENT_INFO @@ -669,7 +670,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.UrlMapList: + ) -> pagers.ListPager: r"""Retrieves the list of UrlMap resources available to the specified project in the specified region. @@ -698,8 +699,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.UrlMapList: + google.cloud.compute_v1.services.region_url_maps.pagers.ListPager: Contains a list of UrlMap resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -733,6 +738,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/pagers.py new file mode 100644 index 000000000000..132a85b4a075 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.UrlMapList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.UrlMapList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.UrlMapList], + request: compute.ListRegionUrlMapsRequest, + response: compute.UrlMapList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionUrlMapsRequest): + The initial request object. + response (google.cloud.compute_v1.types.UrlMapList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionUrlMapsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.UrlMapList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.UrlMap]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/transports/rest.py index 521bd1a3c731..a01cc8d55c0a 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/region_url_maps/transports/rest.py @@ -312,7 +312,9 @@ def insert( # Jsonify the request body body = compute.UrlMap.to_json( - request.url_map_resource, including_default_value_fields=False + request.url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -335,7 +337,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -376,10 +378,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -451,7 +453,9 @@ def patch( # Jsonify the request body body = compute.UrlMap.to_json( - request.url_map_resource, including_default_value_fields=False + request.url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -477,7 +481,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -538,7 +542,9 @@ def update( # Jsonify the request body body = compute.UrlMap.to_json( - request.url_map_resource, including_default_value_fields=False + request.url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -564,7 +570,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -599,6 +605,7 @@ def validate( body = compute.RegionUrlMapsValidateRequest.to_json( request.region_url_maps_validate_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -622,7 +629,7 @@ def validate( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/client.py index e7e961b38cbd..9213cbb0b2c1 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.regions import pagers from google.cloud.compute_v1.types import compute from .transports.base import RegionsTransport, DEFAULT_CLIENT_INFO @@ -410,7 +411,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RegionList: + ) -> pagers.ListPager: r"""Retrieves the list of region resources available to the specified project. @@ -431,8 +432,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RegionList: + google.cloud.compute_v1.services.regions.pagers.ListPager: Contains a list of region resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -464,6 +469,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/pagers.py new file mode 100644 index 000000000000..6e0a595aa3a2 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RegionList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RegionList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RegionList], + request: compute.ListRegionsRequest, + response: compute.RegionList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRegionsRequest): + The initial request object. + response (google.cloud.compute_v1.types.RegionList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRegionsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RegionList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Region]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/transports/rest.py index 6289b168a212..5902801b3706 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/regions/transports/rest.py @@ -178,10 +178,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/client.py index bd5561271a66..0db7fb89791e 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.reservations import pagers from google.cloud.compute_v1.types import compute from .transports.base import ReservationsTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ReservationAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of reservations. Args: @@ -351,8 +352,12 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ReservationAggregatedList: + google.cloud.compute_v1.services.reservations.pagers.AggregatedListPager: Contains a list of reservations. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -384,6 +389,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -833,7 +844,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ReservationList: + ) -> pagers.ListPager: r"""A list of all the reservations that have been configured for the specified project in specified zone. @@ -860,7 +871,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ReservationList: + google.cloud.compute_v1.services.reservations.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -895,6 +909,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/pagers.py new file mode 100644 index 000000000000..bf1275a2ac46 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ReservationAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ReservationAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ReservationAggregatedList], + request: compute.AggregatedListReservationsRequest, + response: compute.ReservationAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListReservationsRequest): + The initial request object. + response (google.cloud.compute_v1.types.ReservationAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListReservationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ReservationAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.ReservationsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.ReservationsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ReservationList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ReservationList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ReservationList], + request: compute.ListReservationsRequest, + response: compute.ReservationList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListReservationsRequest): + The initial request object. + response (google.cloud.compute_v1.types.ReservationList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListReservationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ReservationList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Reservation]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/transports/rest.py index 27b75613929f..96a8aa664484 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/reservations/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -445,7 +445,9 @@ def insert( # Jsonify the request body body = compute.Reservation.to_json( - request.reservation_resource, including_default_value_fields=False + request.reservation_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -468,7 +470,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -509,10 +511,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -586,6 +588,7 @@ def resize( body = compute.ReservationsResizeRequest.to_json( request.reservations_resize_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -611,7 +614,7 @@ def resize( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -696,6 +699,7 @@ def set_iam_policy( body = compute.ZoneSetPolicyRequest.to_json( request.zone_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -719,7 +723,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -754,6 +758,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -777,7 +782,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/client.py index 33263124f8ce..e9e02fcceb1f 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.resource_policies import pagers from google.cloud.compute_v1.types import compute from .transports.base import ResourcePoliciesTransport, DEFAULT_CLIENT_INFO @@ -332,7 +333,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ResourcePolicyAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of resource policies. Args: @@ -353,8 +354,12 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ResourcePolicyAggregatedList: + google.cloud.compute_v1.services.resource_policies.pagers.AggregatedListPager: Contains a list of resourcePolicies. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -386,6 +391,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -839,7 +850,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ResourcePolicyList: + ) -> pagers.ListPager: r"""A list all the resource policies that have been configured for the specified project in specified region. @@ -867,7 +878,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ResourcePolicyList: + google.cloud.compute_v1.services.resource_policies.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -902,6 +916,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/pagers.py new file mode 100644 index 000000000000..79376d997f8b --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ResourcePolicyAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ResourcePolicyAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ResourcePolicyAggregatedList], + request: compute.AggregatedListResourcePoliciesRequest, + response: compute.ResourcePolicyAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListResourcePoliciesRequest): + The initial request object. + response (google.cloud.compute_v1.types.ResourcePolicyAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListResourcePoliciesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ResourcePolicyAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.ResourcePoliciesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.ResourcePoliciesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ResourcePolicyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ResourcePolicyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ResourcePolicyList], + request: compute.ListResourcePoliciesRequest, + response: compute.ResourcePolicyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListResourcePoliciesRequest): + The initial request object. + response (google.cloud.compute_v1.types.ResourcePolicyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListResourcePoliciesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ResourcePolicyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.ResourcePolicy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/transports/rest.py index 00ca26484332..3336db8de65a 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/resource_policies/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -446,7 +446,9 @@ def insert( # Jsonify the request body body = compute.ResourcePolicy.to_json( - request.resource_policy_resource, including_default_value_fields=False + request.resource_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -469,7 +471,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -510,10 +512,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -609,6 +611,7 @@ def set_iam_policy( body = compute.RegionSetPolicyRequest.to_json( request.region_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -632,7 +635,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -667,6 +670,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -690,7 +694,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/client.py index b05597c0dfa2..e7f60dc33049 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.routers import pagers from google.cloud.compute_v1.types import compute from .transports.base import RoutersTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RouterAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of routers. Args: @@ -351,8 +352,12 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RouterAggregatedList: + google.cloud.compute_v1.services.routers.pagers.AggregatedListPager: Contains a list of routers. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -384,6 +389,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -590,7 +601,7 @@ def get_nat_mapping_info( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.VmEndpointNatMappingsList: + ) -> pagers.GetNatMappingInfoPager: r"""Retrieves runtime Nat mapping information of VM endpoints. @@ -625,9 +636,12 @@ def get_nat_mapping_info( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.VmEndpointNatMappingsList: + google.cloud.compute_v1.services.routers.pagers.GetNatMappingInfoPager: Contains a list of VmEndpointNatMappings. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -664,6 +678,12 @@ def get_nat_mapping_info( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.GetNatMappingInfoPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -864,7 +884,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RouterList: + ) -> pagers.ListPager: r"""Retrieves a list of Router resources available to the specified project. @@ -890,8 +910,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RouterList: + google.cloud.compute_v1.services.routers.pagers.ListPager: Contains a list of Router resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -925,6 +949,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/pagers.py new file mode 100644 index 000000000000..d342a7bc25da --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/pagers.py @@ -0,0 +1,218 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RouterAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RouterAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RouterAggregatedList], + request: compute.AggregatedListRoutersRequest, + response: compute.RouterAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListRoutersRequest): + The initial request object. + response (google.cloud.compute_v1.types.RouterAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListRoutersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RouterAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.RoutersScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.RoutersScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class GetNatMappingInfoPager: + """A pager for iterating through ``get_nat_mapping_info`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.VmEndpointNatMappingsList` object, and + provides an ``__iter__`` method to iterate through its + ``result`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``GetNatMappingInfo`` requests and continue to iterate + through the ``result`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.VmEndpointNatMappingsList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.VmEndpointNatMappingsList], + request: compute.GetNatMappingInfoRoutersRequest, + response: compute.VmEndpointNatMappingsList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.GetNatMappingInfoRoutersRequest): + The initial request object. + response (google.cloud.compute_v1.types.VmEndpointNatMappingsList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.GetNatMappingInfoRoutersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.VmEndpointNatMappingsList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.VmEndpointNatMappings]: + for page in self.pages: + yield from page.result + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RouterList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RouterList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RouterList], + request: compute.ListRoutersRequest, + response: compute.RouterList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRoutersRequest): + The initial request object. + response (google.cloud.compute_v1.types.RouterList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRoutersRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RouterList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Router]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/transports/rest.py index 562e4018c724..16e5806e40d0 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/routers/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -322,10 +322,10 @@ def get_nat_mapping_info( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -448,7 +448,9 @@ def insert( # Jsonify the request body body = compute.Router.to_json( - request.router_resource, including_default_value_fields=False + request.router_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -471,7 +473,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -511,10 +513,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -585,7 +587,9 @@ def patch( # Jsonify the request body body = compute.Router.to_json( - request.router_resource, including_default_value_fields=False + request.router_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -611,7 +615,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -644,7 +648,9 @@ def preview( # Jsonify the request body body = compute.Router.to_json( - request.router_resource, including_default_value_fields=False + request.router_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -668,7 +674,7 @@ def preview( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -728,7 +734,9 @@ def update( # Jsonify the request body body = compute.Router.to_json( - request.router_resource, including_default_value_fields=False + request.router_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -754,7 +762,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/client.py index 1e933c4be702..fa662b7a5b29 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.routes import pagers from google.cloud.compute_v1.types import compute from .transports.base import RoutesTransport, DEFAULT_CLIENT_INFO @@ -605,7 +606,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.RouteList: + ) -> pagers.ListPager: r"""Retrieves the list of Route resources available to the specified project. @@ -626,8 +627,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.RouteList: + google.cloud.compute_v1.services.routes.pagers.ListPager: Contains a list of Route resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -659,6 +664,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/pagers.py new file mode 100644 index 000000000000..4975a53326fa --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.RouteList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.RouteList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.RouteList], + request: compute.ListRoutesRequest, + response: compute.RouteList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListRoutesRequest): + The initial request object. + response (google.cloud.compute_v1.types.RouteList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListRoutesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.RouteList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Route]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/transports/rest.py index 36fec67817c7..e2d2e6332f08 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/routes/transports/rest.py @@ -278,7 +278,9 @@ def insert( # Jsonify the request body body = compute.Route.to_json( - request.route_resource, including_default_value_fields=False + request.route_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -301,7 +303,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -341,10 +343,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/client.py index 69706ee7491d..17356c0e91e0 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.security_policies import pagers from google.cloud.compute_v1.types import compute from .transports.base import SecurityPoliciesTransport, DEFAULT_CLIENT_INFO @@ -802,7 +803,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.SecurityPolicyList: + ) -> pagers.ListPager: r"""List all the policies that have been configured for the specified project. @@ -824,7 +825,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.SecurityPolicyList: + google.cloud.compute_v1.services.security_policies.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -857,6 +861,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/pagers.py new file mode 100644 index 000000000000..2d5191d8fade --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.SecurityPolicyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.SecurityPolicyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.SecurityPolicyList], + request: compute.ListSecurityPoliciesRequest, + response: compute.SecurityPolicyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListSecurityPoliciesRequest): + The initial request object. + response (google.cloud.compute_v1.types.SecurityPolicyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListSecurityPoliciesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.SecurityPolicyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.SecurityPolicy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/transports/rest.py index 91ac99c25bc9..fc361f3aacff 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/security_policies/transports/rest.py @@ -146,7 +146,9 @@ def add_rule( # Jsonify the request body body = compute.SecurityPolicyRule.to_json( - request.security_policy_rule_resource, including_default_value_fields=False + request.security_policy_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -169,7 +171,7 @@ def add_rule( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -426,7 +428,9 @@ def insert( # Jsonify the request body body = compute.SecurityPolicy.to_json( - request.security_policy_resource, including_default_value_fields=False + request.security_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -449,7 +453,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -490,10 +494,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -546,10 +550,10 @@ def list_preconfigured_expression_sets( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -623,7 +627,9 @@ def patch( # Jsonify the request body body = compute.SecurityPolicy.to_json( - request.security_policy_resource, including_default_value_fields=False + request.security_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -648,7 +654,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -709,7 +715,9 @@ def patch_rule( # Jsonify the request body body = compute.SecurityPolicyRule.to_json( - request.security_policy_rule_resource, including_default_value_fields=False + request.security_policy_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -734,7 +742,7 @@ def patch_rule( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/client.py index c3b881626ce0..215b9002aba6 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.snapshots import pagers from google.cloud.compute_v1.types import compute from .transports.base import SnapshotsTransport, DEFAULT_CLIENT_INFO @@ -644,7 +645,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.SnapshotList: + ) -> pagers.ListPager: r"""Retrieves the list of Snapshot resources contained within the specified project. @@ -665,9 +666,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.SnapshotList: + google.cloud.compute_v1.services.snapshots.pagers.ListPager: Contains a list of Snapshot resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -700,6 +704,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/pagers.py new file mode 100644 index 000000000000..6a26b39f988d --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.SnapshotList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.SnapshotList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.SnapshotList], + request: compute.ListSnapshotsRequest, + response: compute.SnapshotList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListSnapshotsRequest): + The initial request object. + response (google.cloud.compute_v1.types.SnapshotList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListSnapshotsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.SnapshotList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Snapshot]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/transports/rest.py index 114fcea01088..e8778613516c 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/snapshots/transports/rest.py @@ -361,10 +361,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -460,6 +460,7 @@ def set_iam_policy( body = compute.GlobalSetPolicyRequest.to_json( request.global_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -480,7 +481,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -543,6 +544,7 @@ def set_labels( body = compute.GlobalSetLabelsRequest.to_json( request.global_set_labels_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -563,7 +565,7 @@ def set_labels( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -598,6 +600,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -618,7 +621,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/client.py index e8949ff22395..b65c8d47e1b1 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.ssl_certificates import pagers from google.cloud.compute_v1.types import compute from .transports.base import SslCertificatesTransport, DEFAULT_CLIENT_INFO @@ -332,7 +333,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.SslCertificateAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of all SslCertificate resources, regional and global, available to the specified project. @@ -356,7 +357,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.SslCertificateAggregatedList: + google.cloud.compute_v1.services.ssl_certificates.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -389,6 +393,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -701,7 +711,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.SslCertificateList: + ) -> pagers.ListPager: r"""Retrieves the list of SslCertificate resources available to the specified project. @@ -723,9 +733,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.SslCertificateList: + google.cloud.compute_v1.services.ssl_certificates.pagers.ListPager: Contains a list of SslCertificate resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -758,6 +771,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/pagers.py new file mode 100644 index 000000000000..7aab7e108f5b --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.SslCertificateAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.SslCertificateAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.SslCertificateAggregatedList], + request: compute.AggregatedListSslCertificatesRequest, + response: compute.SslCertificateAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListSslCertificatesRequest): + The initial request object. + response (google.cloud.compute_v1.types.SslCertificateAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListSslCertificatesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.SslCertificateAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.SslCertificatesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.SslCertificatesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.SslCertificateList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.SslCertificateList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.SslCertificateList], + request: compute.ListSslCertificatesRequest, + response: compute.SslCertificateList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListSslCertificatesRequest): + The initial request object. + response (google.cloud.compute_v1.types.SslCertificateList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListSslCertificatesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.SslCertificateList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.SslCertificate]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/transports/rest.py index f1ef029a4442..c5278af9c295 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_certificates/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -360,7 +360,9 @@ def insert( # Jsonify the request body body = compute.SslCertificate.to_json( - request.ssl_certificate_resource, including_default_value_fields=False + request.ssl_certificate_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -383,7 +385,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -426,10 +428,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/client.py index 1c5796a2c5c8..8baa448c4732 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.ssl_policies import pagers from google.cloud.compute_v1.types import compute from .transports.base import SslPoliciesTransport, DEFAULT_CLIENT_INFO @@ -616,7 +617,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.SslPoliciesList: + ) -> pagers.ListPager: r"""Lists all the SSL policies that have been configured for the specified project. @@ -638,7 +639,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.SslPoliciesList: + google.cloud.compute_v1.services.ssl_policies.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -671,6 +675,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/pagers.py new file mode 100644 index 000000000000..ac8def99ec0d --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.SslPoliciesList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.SslPoliciesList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.SslPoliciesList], + request: compute.ListSslPoliciesRequest, + response: compute.SslPoliciesList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListSslPoliciesRequest): + The initial request object. + response (google.cloud.compute_v1.types.SslPoliciesList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListSslPoliciesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.SslPoliciesList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.SslPolicy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/transports/rest.py index 293bf196591f..8a86a8290170 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/ssl_policies/transports/rest.py @@ -281,7 +281,9 @@ def insert( # Jsonify the request body body = compute.SslPolicy.to_json( - request.ssl_policy_resource, including_default_value_fields=False + request.ssl_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -304,7 +306,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -345,10 +347,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -400,10 +402,10 @@ def list_available_features( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -477,7 +479,9 @@ def patch( # Jsonify the request body body = compute.SslPolicy.to_json( - request.ssl_policy_resource, including_default_value_fields=False + request.ssl_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -500,7 +504,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/client.py index 32a5a2fcc472..de8b447e75cd 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.subnetworks import pagers from google.cloud.compute_v1.types import compute from .transports.base import SubnetworksTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.SubnetworkAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of subnetworks. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.SubnetworkAggregatedList: + google.cloud.compute_v1.services.subnetworks.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -973,7 +983,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.SubnetworkList: + ) -> pagers.ListPager: r"""Retrieves a list of subnetworks available to the specified project. @@ -1002,9 +1012,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.SubnetworkList: + google.cloud.compute_v1.services.subnetworks.pagers.ListPager: Contains a list of Subnetwork resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1039,6 +1052,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1050,7 +1069,7 @@ def list_usable( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.UsableSubnetworksAggregatedList: + ) -> pagers.ListUsablePager: r"""Retrieves an aggregated list of all usable subnetworks in the project. @@ -1072,7 +1091,10 @@ def list_usable( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.UsableSubnetworksAggregatedList: + google.cloud.compute_v1.services.subnetworks.pagers.ListUsablePager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1105,6 +1127,12 @@ def list_usable( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListUsablePager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/pagers.py new file mode 100644 index 000000000000..294a77a9edc4 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/pagers.py @@ -0,0 +1,218 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.SubnetworkAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.SubnetworkAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.SubnetworkAggregatedList], + request: compute.AggregatedListSubnetworksRequest, + response: compute.SubnetworkAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListSubnetworksRequest): + The initial request object. + response (google.cloud.compute_v1.types.SubnetworkAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListSubnetworksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.SubnetworkAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.SubnetworksScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.SubnetworksScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.SubnetworkList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.SubnetworkList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.SubnetworkList], + request: compute.ListSubnetworksRequest, + response: compute.SubnetworkList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListSubnetworksRequest): + The initial request object. + response (google.cloud.compute_v1.types.SubnetworkList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListSubnetworksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.SubnetworkList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Subnetwork]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListUsablePager: + """A pager for iterating through ``list_usable`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.UsableSubnetworksAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``ListUsable`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.UsableSubnetworksAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.UsableSubnetworksAggregatedList], + request: compute.ListUsableSubnetworksRequest, + response: compute.UsableSubnetworksAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListUsableSubnetworksRequest): + The initial request object. + response (google.cloud.compute_v1.types.UsableSubnetworksAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListUsableSubnetworksRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.UsableSubnetworksAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.UsableSubnetwork]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/transports/rest.py index 0e7688ff6a07..4310a2c62745 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/subnetworks/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -286,6 +286,7 @@ def expand_ip_cidr_range( body = compute.SubnetworksExpandIpCidrRangeRequest.to_json( request.subnetworks_expand_ip_cidr_range_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -311,7 +312,7 @@ def expand_ip_cidr_range( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -535,7 +536,9 @@ def insert( # Jsonify the request body body = compute.Subnetwork.to_json( - request.subnetwork_resource, including_default_value_fields=False + request.subnetwork_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -558,7 +561,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -601,10 +604,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -656,10 +659,10 @@ def list_usable( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -731,7 +734,9 @@ def patch( # Jsonify the request body body = compute.Subnetwork.to_json( - request.subnetwork_resource, including_default_value_fields=False + request.subnetwork_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -758,7 +763,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -843,6 +848,7 @@ def set_iam_policy( body = compute.RegionSetPolicyRequest.to_json( request.region_set_policy_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -866,7 +872,7 @@ def set_iam_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -930,6 +936,7 @@ def set_private_ip_google_access( body = compute.SubnetworksSetPrivateIpGoogleAccessRequest.to_json( request.subnetworks_set_private_ip_google_access_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -955,7 +962,7 @@ def set_private_ip_google_access( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -990,6 +997,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -1013,7 +1021,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/client.py index 8df01dbf53c0..c13568178350 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.target_grpc_proxies import pagers from google.cloud.compute_v1.types import compute from .transports.base import TargetGrpcProxiesTransport, DEFAULT_CLIENT_INFO @@ -620,7 +621,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetGrpcProxyList: + ) -> pagers.ListPager: r"""Lists the TargetGrpcProxies for a project in the given scope. @@ -642,7 +643,10 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetGrpcProxyList: + google.cloud.compute_v1.services.target_grpc_proxies.pagers.ListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -675,6 +679,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/pagers.py new file mode 100644 index 000000000000..4602e3f62500 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetGrpcProxyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetGrpcProxyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetGrpcProxyList], + request: compute.ListTargetGrpcProxiesRequest, + response: compute.TargetGrpcProxyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListTargetGrpcProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetGrpcProxyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListTargetGrpcProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetGrpcProxyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetGrpcProxy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/transports/rest.py index 533fdd5e90fe..29f236e8d4fb 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_grpc_proxies/transports/rest.py @@ -286,7 +286,9 @@ def insert( # Jsonify the request body body = compute.TargetGrpcProxy.to_json( - request.target_grpc_proxy_resource, including_default_value_fields=False + request.target_grpc_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -309,7 +311,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -350,10 +352,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -425,7 +427,9 @@ def patch( # Jsonify the request body body = compute.TargetGrpcProxy.to_json( - request.target_grpc_proxy_resource, including_default_value_fields=False + request.target_grpc_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -450,7 +454,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/client.py index 7b3807bbfc4d..5065177ec40a 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.target_http_proxies import pagers from google.cloud.compute_v1.types import compute from .transports.base import TargetHttpProxiesTransport, DEFAULT_CLIENT_INFO @@ -334,7 +335,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetHttpProxyAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of all TargetHttpProxy resources, regional and global, available to the specified project. @@ -358,7 +359,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetHttpProxyAggregatedList: + google.cloud.compute_v1.services.target_http_proxies.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -391,6 +395,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -701,7 +711,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetHttpProxyList: + ) -> pagers.ListPager: r"""Retrieves the list of TargetHttpProxy resources available to the specified project. @@ -723,8 +733,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetHttpProxyList: + google.cloud.compute_v1.services.target_http_proxies.pagers.ListPager: A list of TargetHttpProxy resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -756,6 +770,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/pagers.py new file mode 100644 index 000000000000..8d5848858ba5 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetHttpProxyAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetHttpProxyAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetHttpProxyAggregatedList], + request: compute.AggregatedListTargetHttpProxiesRequest, + response: compute.TargetHttpProxyAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListTargetHttpProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetHttpProxyAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListTargetHttpProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetHttpProxyAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.TargetHttpProxiesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.TargetHttpProxiesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetHttpProxyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetHttpProxyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetHttpProxyList], + request: compute.ListTargetHttpProxiesRequest, + response: compute.TargetHttpProxyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListTargetHttpProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetHttpProxyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListTargetHttpProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetHttpProxyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetHttpProxy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/transports/rest.py index 7d7461bc61e4..5b9cd80ccb66 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_http_proxies/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -357,7 +357,9 @@ def insert( # Jsonify the request body body = compute.TargetHttpProxy.to_json( - request.target_http_proxy_resource, including_default_value_fields=False + request.target_http_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -380,7 +382,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -421,10 +423,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -496,7 +498,9 @@ def patch( # Jsonify the request body body = compute.TargetHttpProxy.to_json( - request.target_http_proxy_resource, including_default_value_fields=False + request.target_http_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -521,7 +525,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -582,7 +586,9 @@ def set_url_map( # Jsonify the request body body = compute.UrlMapReference.to_json( - request.url_map_reference_resource, including_default_value_fields=False + request.url_map_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -607,7 +613,7 @@ def set_url_map( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/client.py index a21532cc7882..254494a86604 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.target_https_proxies import pagers from google.cloud.compute_v1.types import compute from .transports.base import TargetHttpsProxiesTransport, DEFAULT_CLIENT_INFO @@ -334,7 +335,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetHttpsProxyAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of all TargetHttpsProxy resources, regional and global, available to the specified project. @@ -358,7 +359,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetHttpsProxyAggregatedList: + google.cloud.compute_v1.services.target_https_proxies.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -391,6 +395,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -701,7 +711,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetHttpsProxyList: + ) -> pagers.ListPager: r"""Retrieves the list of TargetHttpsProxy resources available to the specified project. @@ -723,9 +733,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetHttpsProxyList: + google.cloud.compute_v1.services.target_https_proxies.pagers.ListPager: Contains a list of TargetHttpsProxy resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -758,6 +771,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/pagers.py new file mode 100644 index 000000000000..d6817b0e5cd0 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetHttpsProxyAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetHttpsProxyAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetHttpsProxyAggregatedList], + request: compute.AggregatedListTargetHttpsProxiesRequest, + response: compute.TargetHttpsProxyAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListTargetHttpsProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetHttpsProxyAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListTargetHttpsProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetHttpsProxyAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.TargetHttpsProxiesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.TargetHttpsProxiesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetHttpsProxyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetHttpsProxyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetHttpsProxyList], + request: compute.ListTargetHttpsProxiesRequest, + response: compute.TargetHttpsProxyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListTargetHttpsProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetHttpsProxyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListTargetHttpsProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetHttpsProxyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetHttpsProxy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/transports/rest.py index 5a6dcfdcc92b..13e0bedf350d 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_https_proxies/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -356,7 +356,9 @@ def insert( # Jsonify the request body body = compute.TargetHttpsProxy.to_json( - request.target_https_proxy_resource, including_default_value_fields=False + request.target_https_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -379,7 +381,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -422,10 +424,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -499,6 +501,7 @@ def set_quic_override( body = compute.TargetHttpsProxiesSetQuicOverrideRequest.to_json( request.target_https_proxies_set_quic_override_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -523,7 +526,7 @@ def set_quic_override( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -586,6 +589,7 @@ def set_ssl_certificates( body = compute.TargetHttpsProxiesSetSslCertificatesRequest.to_json( request.target_https_proxies_set_ssl_certificates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -610,7 +614,7 @@ def set_ssl_certificates( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -671,7 +675,9 @@ def set_ssl_policy( # Jsonify the request body body = compute.SslPolicyReference.to_json( - request.ssl_policy_reference_resource, including_default_value_fields=False + request.ssl_policy_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -696,7 +702,7 @@ def set_ssl_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -757,7 +763,9 @@ def set_url_map( # Jsonify the request body body = compute.UrlMapReference.to_json( - request.url_map_reference_resource, including_default_value_fields=False + request.url_map_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -782,7 +790,7 @@ def set_url_map( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/client.py index 859f77117bd8..65f231da255e 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.target_instances import pagers from google.cloud.compute_v1.types import compute from .transports.base import TargetInstancesTransport, DEFAULT_CLIENT_INFO @@ -332,7 +333,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetInstanceAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of target instances. Args: @@ -353,7 +354,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetInstanceAggregatedList: + google.cloud.compute_v1.services.target_instances.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -386,6 +390,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -712,7 +722,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetInstanceList: + ) -> pagers.ListPager: r"""Retrieves a list of TargetInstance resources available to the specified project and zone. @@ -741,9 +751,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetInstanceList: + google.cloud.compute_v1.services.target_instances.pagers.ListPager: Contains a list of TargetInstance resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -778,6 +791,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/pagers.py new file mode 100644 index 000000000000..f31990bbdb5b --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetInstanceAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetInstanceAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetInstanceAggregatedList], + request: compute.AggregatedListTargetInstancesRequest, + response: compute.TargetInstanceAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListTargetInstancesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetInstanceAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListTargetInstancesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetInstanceAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.TargetInstancesScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.TargetInstancesScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetInstanceList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetInstanceList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetInstanceList], + request: compute.ListTargetInstancesRequest, + response: compute.TargetInstanceList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListTargetInstancesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetInstanceList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListTargetInstancesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetInstanceList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetInstance]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/transports/rest.py index 72d7f8158680..965535ef99f5 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_instances/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -344,7 +344,9 @@ def insert( # Jsonify the request body body = compute.TargetInstance.to_json( - request.target_instance_resource, including_default_value_fields=False + request.target_instance_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -367,7 +369,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -410,10 +412,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/client.py index 827af5780c5e..a98fe47d61ac 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.target_pools import pagers from google.cloud.compute_v1.types import compute from .transports.base import TargetPoolsTransport, DEFAULT_CLIENT_INFO @@ -579,7 +580,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetPoolAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of target pools. Args: @@ -600,7 +601,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetPoolAggregatedList: + google.cloud.compute_v1.services.target_pools.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -633,6 +637,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -1054,7 +1064,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetPoolList: + ) -> pagers.ListPager: r"""Retrieves a list of target pools available to the specified project and region. @@ -1083,9 +1093,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetPoolList: + google.cloud.compute_v1.services.target_pools.pagers.ListPager: Contains a list of TargetPool resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -1120,6 +1133,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/pagers.py new file mode 100644 index 000000000000..30ade23203cc --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetPoolAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetPoolAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetPoolAggregatedList], + request: compute.AggregatedListTargetPoolsRequest, + response: compute.TargetPoolAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListTargetPoolsRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetPoolAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListTargetPoolsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetPoolAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.TargetPoolsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.TargetPoolsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetPoolList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetPoolList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetPoolList], + request: compute.ListTargetPoolsRequest, + response: compute.TargetPoolList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListTargetPoolsRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetPoolList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListTargetPoolsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetPoolList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetPool]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/transports/rest.py index bd49a3e28c68..557e16548cc6 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_pools/transports/rest.py @@ -148,6 +148,7 @@ def add_health_check( body = compute.TargetPoolsAddHealthCheckRequest.to_json( request.target_pools_add_health_check_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -173,7 +174,7 @@ def add_health_check( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -236,6 +237,7 @@ def add_instance( body = compute.TargetPoolsAddInstanceRequest.to_json( request.target_pools_add_instance_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -261,7 +263,7 @@ def add_instance( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -302,11 +304,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -492,7 +494,9 @@ def get_health( # Jsonify the request body body = compute.InstanceReference.to_json( - request.instance_reference_resource, including_default_value_fields=False + request.instance_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -516,7 +520,7 @@ def get_health( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -577,7 +581,9 @@ def insert( # Jsonify the request body body = compute.TargetPool.to_json( - request.target_pool_resource, including_default_value_fields=False + request.target_pool_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -600,7 +606,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -643,10 +649,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -720,6 +726,7 @@ def remove_health_check( body = compute.TargetPoolsRemoveHealthCheckRequest.to_json( request.target_pools_remove_health_check_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -745,7 +752,7 @@ def remove_health_check( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -808,6 +815,7 @@ def remove_instance( body = compute.TargetPoolsRemoveInstanceRequest.to_json( request.target_pools_remove_instance_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -833,7 +841,7 @@ def remove_instance( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -894,7 +902,9 @@ def set_backup( # Jsonify the request body body = compute.TargetReference.to_json( - request.target_reference_resource, including_default_value_fields=False + request.target_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -921,7 +931,7 @@ def set_backup( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/client.py index a39378b1b903..bac26316e7e9 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.target_ssl_proxies import pagers from google.cloud.compute_v1.types import compute from .transports.base import TargetSslProxiesTransport, DEFAULT_CLIENT_INFO @@ -616,7 +617,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetSslProxyList: + ) -> pagers.ListPager: r"""Retrieves the list of TargetSslProxy resources available to the specified project. @@ -638,9 +639,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetSslProxyList: + google.cloud.compute_v1.services.target_ssl_proxies.pagers.ListPager: Contains a list of TargetSslProxy resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -673,6 +677,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/pagers.py new file mode 100644 index 000000000000..d995c620feb5 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetSslProxyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetSslProxyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetSslProxyList], + request: compute.ListTargetSslProxiesRequest, + response: compute.TargetSslProxyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListTargetSslProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetSslProxyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListTargetSslProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetSslProxyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetSslProxy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/transports/rest.py index acb233a868ee..8e0e23db317b 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_ssl_proxies/transports/rest.py @@ -286,7 +286,9 @@ def insert( # Jsonify the request body body = compute.TargetSslProxy.to_json( - request.target_ssl_proxy_resource, including_default_value_fields=False + request.target_ssl_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -309,7 +311,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -352,10 +354,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -429,6 +431,7 @@ def set_backend_service( body = compute.TargetSslProxiesSetBackendServiceRequest.to_json( request.target_ssl_proxies_set_backend_service_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -453,7 +456,7 @@ def set_backend_service( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -516,6 +519,7 @@ def set_proxy_header( body = compute.TargetSslProxiesSetProxyHeaderRequest.to_json( request.target_ssl_proxies_set_proxy_header_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -540,7 +544,7 @@ def set_proxy_header( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -603,6 +607,7 @@ def set_ssl_certificates( body = compute.TargetSslProxiesSetSslCertificatesRequest.to_json( request.target_ssl_proxies_set_ssl_certificates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -627,7 +632,7 @@ def set_ssl_certificates( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -688,7 +693,9 @@ def set_ssl_policy( # Jsonify the request body body = compute.SslPolicyReference.to_json( - request.ssl_policy_reference_resource, including_default_value_fields=False + request.ssl_policy_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -713,7 +720,7 @@ def set_ssl_policy( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/client.py index 0b0b76ca5ad8..9afbacfd9b0e 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.target_tcp_proxies import pagers from google.cloud.compute_v1.types import compute from .transports.base import TargetTcpProxiesTransport, DEFAULT_CLIENT_INFO @@ -616,7 +617,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetTcpProxyList: + ) -> pagers.ListPager: r"""Retrieves the list of TargetTcpProxy resources available to the specified project. @@ -638,9 +639,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetTcpProxyList: + google.cloud.compute_v1.services.target_tcp_proxies.pagers.ListPager: Contains a list of TargetTcpProxy resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -673,6 +677,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/pagers.py new file mode 100644 index 000000000000..b9fcb6753c6f --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetTcpProxyList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetTcpProxyList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetTcpProxyList], + request: compute.ListTargetTcpProxiesRequest, + response: compute.TargetTcpProxyList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListTargetTcpProxiesRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetTcpProxyList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListTargetTcpProxiesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetTcpProxyList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetTcpProxy]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/transports/rest.py index 7e25e2da9643..51ebec033d7e 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_tcp_proxies/transports/rest.py @@ -286,7 +286,9 @@ def insert( # Jsonify the request body body = compute.TargetTcpProxy.to_json( - request.target_tcp_proxy_resource, including_default_value_fields=False + request.target_tcp_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -309,7 +311,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -352,10 +354,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -429,6 +431,7 @@ def set_backend_service( body = compute.TargetTcpProxiesSetBackendServiceRequest.to_json( request.target_tcp_proxies_set_backend_service_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -453,7 +456,7 @@ def set_backend_service( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -516,6 +519,7 @@ def set_proxy_header( body = compute.TargetTcpProxiesSetProxyHeaderRequest.to_json( request.target_tcp_proxies_set_proxy_header_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -540,7 +544,7 @@ def set_proxy_header( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/client.py index dd2bb56f45bb..d12a7a27b1a7 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.target_vpn_gateways import pagers from google.cloud.compute_v1.types import compute from .transports.base import TargetVpnGatewaysTransport, DEFAULT_CLIENT_INFO @@ -334,7 +335,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetVpnGatewayAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of target VPN gateways. Args: @@ -355,7 +356,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetVpnGatewayAggregatedList: + google.cloud.compute_v1.services.target_vpn_gateways.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -388,6 +392,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -706,7 +716,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.TargetVpnGatewayList: + ) -> pagers.ListPager: r"""Retrieves a list of target VPN gateways available to the specified project and region. @@ -733,9 +743,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.TargetVpnGatewayList: + google.cloud.compute_v1.services.target_vpn_gateways.pagers.ListPager: Contains a list of TargetVpnGateway resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -770,6 +783,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/pagers.py new file mode 100644 index 000000000000..e01973f79eec --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetVpnGatewayAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetVpnGatewayAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetVpnGatewayAggregatedList], + request: compute.AggregatedListTargetVpnGatewaysRequest, + response: compute.TargetVpnGatewayAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListTargetVpnGatewaysRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetVpnGatewayAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListTargetVpnGatewaysRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetVpnGatewayAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.TargetVpnGatewaysScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.TargetVpnGatewaysScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.TargetVpnGatewayList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.TargetVpnGatewayList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.TargetVpnGatewayList], + request: compute.ListTargetVpnGatewaysRequest, + response: compute.TargetVpnGatewayList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListTargetVpnGatewaysRequest): + The initial request object. + response (google.cloud.compute_v1.types.TargetVpnGatewayList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListTargetVpnGatewaysRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.TargetVpnGatewayList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.TargetVpnGateway]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/transports/rest.py index 376ce132ef87..89f504e61086 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/target_vpn_gateways/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -342,7 +342,9 @@ def insert( # Jsonify the request body body = compute.TargetVpnGateway.to_json( - request.target_vpn_gateway_resource, including_default_value_fields=False + request.target_vpn_gateway_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -365,7 +367,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -408,10 +410,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/client.py index 13b87d6db182..c42f3ccd0c51 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.url_maps import pagers from google.cloud.compute_v1.types import compute from .transports.base import UrlMapsTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.UrlMapsAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves the list of all UrlMap resources, regional and global, available to the specified project. @@ -354,7 +355,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.UrlMapsAggregatedList: + google.cloud.compute_v1.services.url_maps.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -387,6 +391,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -817,7 +827,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.UrlMapList: + ) -> pagers.ListPager: r"""Retrieves the list of UrlMap resources available to the specified project. @@ -838,8 +848,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.UrlMapList: + google.cloud.compute_v1.services.url_maps.pagers.ListPager: Contains a list of UrlMap resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -871,6 +885,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/pagers.py new file mode 100644 index 000000000000..1ceea14927ec --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.UrlMapsAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.UrlMapsAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.UrlMapsAggregatedList], + request: compute.AggregatedListUrlMapsRequest, + response: compute.UrlMapsAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListUrlMapsRequest): + The initial request object. + response (google.cloud.compute_v1.types.UrlMapsAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListUrlMapsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.UrlMapsAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.UrlMapsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.UrlMapsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.UrlMapList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.UrlMapList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.UrlMapList], + request: compute.ListUrlMapsRequest, + response: compute.UrlMapList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListUrlMapsRequest): + The initial request object. + response (google.cloud.compute_v1.types.UrlMapList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListUrlMapsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.UrlMapList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.UrlMap]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/transports/rest.py index 49c94e80213e..49143112f5f5 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/url_maps/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -359,7 +359,9 @@ def insert( # Jsonify the request body body = compute.UrlMap.to_json( - request.url_map_resource, including_default_value_fields=False + request.url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -382,7 +384,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -445,6 +447,7 @@ def invalidate_cache( body = compute.CacheInvalidationRule.to_json( request.cache_invalidation_rule_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -467,7 +470,7 @@ def invalidate_cache( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -507,10 +510,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -581,7 +584,9 @@ def patch( # Jsonify the request body body = compute.UrlMap.to_json( - request.url_map_resource, including_default_value_fields=False + request.url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -604,7 +609,7 @@ def patch( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.patch(url, json=body,) + response = self._session.patch(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -664,7 +669,9 @@ def update( # Jsonify the request body body = compute.UrlMap.to_json( - request.url_map_resource, including_default_value_fields=False + request.url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -687,7 +694,7 @@ def update( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.put(url, json=body,) + response = self._session.put(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -722,6 +729,7 @@ def validate( body = compute.UrlMapsValidateRequest.to_json( request.url_maps_validate_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -742,7 +750,7 @@ def validate( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/client.py index 07c4596ed9d4..84d7fcfa40b5 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.vpn_gateways import pagers from google.cloud.compute_v1.types import compute from .transports.base import VpnGatewaysTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.VpnGatewayAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of VPN gateways. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.VpnGatewayAggregatedList: + google.cloud.compute_v1.services.vpn_gateways.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -780,7 +790,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.VpnGatewayList: + ) -> pagers.ListPager: r"""Retrieves a list of VPN gateways available to the specified project and region. @@ -807,9 +817,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.VpnGatewayList: + google.cloud.compute_v1.services.vpn_gateways.pagers.ListPager: Contains a list of VpnGateway resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -844,6 +857,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/pagers.py new file mode 100644 index 000000000000..8e414a067caa --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.VpnGatewayAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.VpnGatewayAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.VpnGatewayAggregatedList], + request: compute.AggregatedListVpnGatewaysRequest, + response: compute.VpnGatewayAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListVpnGatewaysRequest): + The initial request object. + response (google.cloud.compute_v1.types.VpnGatewayAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListVpnGatewaysRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.VpnGatewayAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.VpnGatewaysScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.VpnGatewaysScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.VpnGatewayList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.VpnGatewayList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.VpnGatewayList], + request: compute.ListVpnGatewaysRequest, + response: compute.VpnGatewayList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListVpnGatewaysRequest): + The initial request object. + response (google.cloud.compute_v1.types.VpnGatewayList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListVpnGatewaysRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.VpnGatewayList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.VpnGateway]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/transports/rest.py index 6874c6e976d8..73327dfe4311 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_gateways/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -397,7 +397,9 @@ def insert( # Jsonify the request body body = compute.VpnGateway.to_json( - request.vpn_gateway_resource, including_default_value_fields=False + request.vpn_gateway_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -420,7 +422,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -463,10 +465,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -540,6 +542,7 @@ def set_labels( body = compute.RegionSetLabelsRequest.to_json( request.region_set_labels_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -565,7 +568,7 @@ def set_labels( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -600,6 +603,7 @@ def test_iam_permissions( body = compute.TestPermissionsRequest.to_json( request.test_permissions_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -623,7 +627,7 @@ def test_iam_permissions( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/client.py index bff9918efae8..af6863525d3c 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.vpn_tunnels import pagers from google.cloud.compute_v1.types import compute from .transports.base import VpnTunnelsTransport, DEFAULT_CLIENT_INFO @@ -330,7 +331,7 @@ def aggregated_list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.VpnTunnelAggregatedList: + ) -> pagers.AggregatedListPager: r"""Retrieves an aggregated list of VPN tunnels. Args: @@ -351,7 +352,10 @@ def aggregated_list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.VpnTunnelAggregatedList: + google.cloud.compute_v1.services.vpn_tunnels.pagers.AggregatedListPager: + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -384,6 +388,12 @@ def aggregated_list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.AggregatedListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response @@ -699,7 +709,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.VpnTunnelList: + ) -> pagers.ListPager: r"""Retrieves a list of VpnTunnel resources contained in the specified project and region. @@ -725,9 +735,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.VpnTunnelList: + google.cloud.compute_v1.services.vpn_tunnels.pagers.ListPager: Contains a list of VpnTunnel resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -762,6 +775,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/pagers.py new file mode 100644 index 000000000000..13780f8d66be --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/pagers.py @@ -0,0 +1,156 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class AggregatedListPager: + """A pager for iterating through ``aggregated_list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.VpnTunnelAggregatedList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``AggregatedList`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.VpnTunnelAggregatedList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.VpnTunnelAggregatedList], + request: compute.AggregatedListVpnTunnelsRequest, + response: compute.VpnTunnelAggregatedList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.AggregatedListVpnTunnelsRequest): + The initial request object. + response (google.cloud.compute_v1.types.VpnTunnelAggregatedList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.AggregatedListVpnTunnelsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.VpnTunnelAggregatedList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[Tuple[str, compute.VpnTunnelsScopedList]]: + for page in self.pages: + yield from page.items.items() + + def get(self, key: str) -> Optional[compute.VpnTunnelsScopedList]: + return self._response.items.get(key) + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.VpnTunnelList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.VpnTunnelList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.VpnTunnelList], + request: compute.ListVpnTunnelsRequest, + response: compute.VpnTunnelList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListVpnTunnelsRequest): + The initial request object. + response (google.cloud.compute_v1.types.VpnTunnelList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListVpnTunnelsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.VpnTunnelList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.VpnTunnel]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/transports/rest.py index eb35ce1a3c9c..b68a8729aab2 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/vpn_tunnels/transports/rest.py @@ -126,11 +126,11 @@ def aggregated_list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, - "maxResults": request.max_results, "includeAllScopes": request.include_all_scopes, + "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values @@ -339,7 +339,9 @@ def insert( # Jsonify the request body body = compute.VpnTunnel.to_json( - request.vpn_tunnel_resource, including_default_value_fields=False + request.vpn_tunnel_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) # TODO(yon-mg): need to handle grpc transcoding and parse url correctly @@ -362,7 +364,7 @@ def insert( url += "?{}".format("&".join(query_params)).replace(" ", "+") # Send the request - response = self._session.post(url, json=body,) + response = self._session.post(url, data=body,) # Raise requests.exceptions.HTTPError if the status code is >= 400 response.raise_for_status() @@ -405,10 +407,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/client.py index 8c09f48fdf42..38e20d47485e 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.zone_operations import pagers from google.cloud.compute_v1.types import compute from .transports.base import ZoneOperationsTransport, DEFAULT_CLIENT_INFO @@ -529,7 +530,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.OperationList: + ) -> pagers.ListPager: r"""Retrieves a list of Operation resources contained within the specified zone. @@ -556,9 +557,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.OperationList: + google.cloud.compute_v1.services.zone_operations.pagers.ListPager: Contains a list of Operation resources. + Iterating over this object will yield + results and resolve additional pages + automatically. """ # Create or coerce a protobuf request object. @@ -593,6 +597,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/pagers.py new file mode 100644 index 000000000000..62db29079872 --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.OperationList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.OperationList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.OperationList], + request: compute.ListZoneOperationsRequest, + response: compute.OperationList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListZoneOperationsRequest): + The initial request object. + response (google.cloud.compute_v1.types.OperationList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListZoneOperationsRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.OperationList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Operation]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/transports/rest.py index b8de03e07917..444772246c38 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/zone_operations/transports/rest.py @@ -263,10 +263,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/client.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/client.py index fbced4c24b23..fec06f719212 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/client.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/client.py @@ -32,6 +32,7 @@ from google.auth.exceptions import MutualTLSChannelError # type: ignore from google.oauth2 import service_account # type: ignore +from google.cloud.compute_v1.services.zones import pagers from google.cloud.compute_v1.types import compute from .transports.base import ZonesTransport, DEFAULT_CLIENT_INFO @@ -410,7 +411,7 @@ def list( retry: retries.Retry = gapic_v1.method.DEFAULT, timeout: float = None, metadata: Sequence[Tuple[str, str]] = (), - ) -> compute.ZoneList: + ) -> pagers.ListPager: r"""Retrieves the list of Zone resources available to the specified project. @@ -431,8 +432,12 @@ def list( sent along with the request as metadata. Returns: - google.cloud.compute_v1.types.ZoneList: + google.cloud.compute_v1.services.zones.pagers.ListPager: Contains a list of zone resources. + Iterating over this object will yield + results and resolve additional pages + automatically. + """ # Create or coerce a protobuf request object. # Sanity check: If we got a request object, we should *not* have @@ -464,6 +469,12 @@ def list( # Send the request. response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) + # This method is paged; wrap the response in a pager, which provides + # an `__iter__` convenience method. + response = pagers.ListPager( + method=rpc, request=request, response=response, metadata=metadata, + ) + # Done; return the response. return response diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/pagers.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/pagers.py new file mode 100644 index 000000000000..8ed009ba3d6a --- /dev/null +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/pagers.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- + +# Copyright 2020 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. +# + +from typing import ( + Any, + AsyncIterable, + Awaitable, + Callable, + Iterable, + Sequence, + Tuple, + Optional, +) + +from google.cloud.compute_v1.types import compute + + +class ListPager: + """A pager for iterating through ``list`` requests. + + This class thinly wraps an initial + :class:`google.cloud.compute_v1.types.ZoneList` object, and + provides an ``__iter__`` method to iterate through its + ``items`` field. + + If there are more pages, the ``__iter__`` method will make additional + ``List`` requests and continue to iterate + through the ``items`` field on the + corresponding responses. + + All the usual :class:`google.cloud.compute_v1.types.ZoneList` + attributes are available on the pager. If multiple requests are made, only + the most recent response is retained, and thus used for attribute lookup. + """ + + def __init__( + self, + method: Callable[..., compute.ZoneList], + request: compute.ListZonesRequest, + response: compute.ZoneList, + *, + metadata: Sequence[Tuple[str, str]] = () + ): + """Instantiate the pager. + + Args: + method (Callable): The method that was originally called, and + which instantiated this pager. + request (google.cloud.compute_v1.types.ListZonesRequest): + The initial request object. + response (google.cloud.compute_v1.types.ZoneList): + The initial response object. + metadata (Sequence[Tuple[str, str]]): Strings which should be + sent along with the request as metadata. + """ + self._method = method + self._request = compute.ListZonesRequest(request) + self._response = response + self._metadata = metadata + + def __getattr__(self, name: str) -> Any: + return getattr(self._response, name) + + @property + def pages(self) -> Iterable[compute.ZoneList]: + yield self._response + while self._response.next_page_token: + self._request.page_token = self._response.next_page_token + self._response = self._method(self._request, metadata=self._metadata) + yield self._response + + def __iter__(self) -> Iterable[compute.Zone]: + for page in self.pages: + yield from page.items + + def __repr__(self) -> str: + return "{0}<{1!r}>".format(self.__class__.__name__, self._response) diff --git a/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/transports/rest.py b/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/transports/rest.py index 64f76b799964..a7ed4ab98932 100644 --- a/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/transports/rest.py +++ b/packages/google-cloud-compute/google/cloud/compute_v1/services/zones/transports/rest.py @@ -180,10 +180,10 @@ def list( # not required for GCE query_params = { "filter": request.filter, - "pageToken": request.page_token, - "returnPartialSuccess": request.return_partial_success, "maxResults": request.max_results, "orderBy": request.order_by, + "pageToken": request.page_token, + "returnPartialSuccess": request.return_partial_success, } # TODO(yon-mg): further discussion needed whether 'python truthiness' is appropriate here # discards default values diff --git a/packages/google-cloud-compute/noxfile.py b/packages/google-cloud-compute/noxfile.py index ff946ee14bf6..fb09e54a93f2 100644 --- a/packages/google-cloud-compute/noxfile.py +++ b/packages/google-cloud-compute/noxfile.py @@ -138,11 +138,6 @@ def system(session): ) session.install("-e", ".") - # If mtls is enabled via environment variable - # GOOGLE_API_USE_CLIENT_CERTIFICATE, then pyopenssl is needed. Here we - # install it regardless. - session.install("pyopenssl") - # Run py.test against the system tests. if system_test_exists: session.run("py.test", "--quiet", system_test_path, *session.posargs) diff --git a/packages/google-cloud-compute/scripts/fixup_compute_v1_keywords.py b/packages/google-cloud-compute/scripts/fixup_compute_v1_keywords.py index 98b137933a9a..c15bf5d9ccb4 100644 --- a/packages/google-cloud-compute/scripts/fixup_compute_v1_keywords.py +++ b/packages/google-cloud-compute/scripts/fixup_compute_v1_keywords.py @@ -50,32 +50,32 @@ class computeCallTransformer(cst.CSTTransformer): 'add_peering': ('network', 'networks_add_peering_request_resource', 'project', 'request_id', ), 'add_resource_policies': ('disk', 'disks_add_resource_policies_request_resource', 'project', 'zone', 'request_id', ), 'add_rule': ('project', 'security_policy', 'security_policy_rule_resource', ), - 'add_signed_url_key': ('backend_bucket', 'project', 'signed_url_key_resource', 'request_id', ), + 'add_signed_url_key': ('backend_service', 'project', 'signed_url_key_resource', 'request_id', ), 'aggregated_list': ('project', 'filter', 'include_all_scopes', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), 'apply_updates_to_instances': ('instance_group_manager', 'instance_group_managers_apply_updates_request_resource', 'project', 'zone', ), 'attach_disk': ('attached_disk_resource', 'instance', 'project', 'zone', 'force_attach', 'request_id', ), - 'attach_network_endpoints': ('global_network_endpoint_groups_attach_endpoints_request_resource', 'network_endpoint_group', 'project', 'request_id', ), + 'attach_network_endpoints': ('network_endpoint_group', 'network_endpoint_groups_attach_endpoints_request_resource', 'project', 'zone', 'request_id', ), 'create_instances': ('instance_group_manager', 'instance_group_managers_create_instances_request_resource', 'project', 'zone', 'request_id', ), 'create_snapshot': ('disk', 'project', 'snapshot_resource', 'zone', 'guest_flush', 'request_id', ), - 'delete': ('address', 'project', 'region', 'request_id', ), + 'delete': ('disk', 'project', 'zone', 'request_id', ), 'delete_access_config': ('access_config', 'instance', 'network_interface', 'project', 'zone', 'request_id', ), 'delete_instances': ('instance_group_manager', 'instance_group_managers_delete_instances_request_resource', 'project', 'zone', 'request_id', ), 'delete_nodes': ('node_group', 'node_groups_delete_nodes_request_resource', 'project', 'zone', 'request_id', ), 'delete_per_instance_configs': ('instance_group_manager', 'instance_group_managers_delete_per_instance_configs_req_resource', 'project', 'zone', ), - 'delete_signed_url_key': ('backend_bucket', 'key_name', 'project', 'request_id', ), + 'delete_signed_url_key': ('backend_service', 'key_name', 'project', 'request_id', ), 'deprecate': ('deprecation_status_resource', 'image', 'project', 'request_id', ), 'detach_disk': ('device_name', 'instance', 'project', 'zone', 'request_id', ), - 'detach_network_endpoints': ('global_network_endpoint_groups_detach_endpoints_request_resource', 'network_endpoint_group', 'project', 'request_id', ), + 'detach_network_endpoints': ('network_endpoint_group', 'network_endpoint_groups_detach_endpoints_request_resource', 'project', 'zone', 'request_id', ), 'disable_xpn_host': ('project', 'request_id', ), 'disable_xpn_resource': ('project', 'projects_disable_xpn_resource_request_resource', 'request_id', ), 'enable_xpn_host': ('project', 'request_id', ), 'enable_xpn_resource': ('project', 'projects_enable_xpn_resource_request_resource', 'request_id', ), 'expand_ip_cidr_range': ('project', 'region', 'subnetwork', 'subnetworks_expand_ip_cidr_range_request_resource', 'request_id', ), - 'get': ('accelerator_type', 'project', 'zone', ), + 'get': ('disk', 'project', 'zone', ), 'get_diagnostics': ('interconnect', 'project', ), 'get_from_family': ('family', 'project', ), 'get_guest_attributes': ('instance', 'project', 'zone', 'query_path', 'variable_key', ), - 'get_health': ('backend_service', 'project', 'resource_group_reference_resource', ), + 'get_health': ('backend_service', 'project', 'region', 'resource_group_reference_resource', ), 'get_iam_policy': ('project', 'resource', 'zone', 'options_requested_policy_version', ), 'get_nat_mapping_info': ('project', 'region', 'router', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), 'get_router_status': ('project', 'region', 'router', ), @@ -86,14 +86,14 @@ class computeCallTransformer(cst.CSTTransformer): 'get_status': ('project', 'region', 'vpn_gateway', ), 'get_xpn_host': ('project', ), 'get_xpn_resources': ('project', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), - 'insert': ('address_resource', 'project', 'region', 'request_id', ), + 'insert': ('disk_resource', 'project', 'zone', 'request_id', 'source_image', ), 'invalidate_cache': ('cache_invalidation_rule_resource', 'project', 'url_map', 'request_id', ), 'list': ('project', 'zone', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), 'list_available_features': ('project', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), 'list_errors': ('instance_group_manager', 'project', 'zone', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), 'list_instances': ('instance_group', 'instance_groups_list_instances_request_resource', 'project', 'zone', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), 'list_managed_instances': ('instance_group_manager', 'project', 'zone', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), - 'list_network_endpoints': ('network_endpoint_group', 'project', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), + 'list_network_endpoints': ('network_endpoint_group', 'network_endpoint_groups_list_endpoints_request_resource', 'project', 'zone', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), 'list_nodes': ('node_group', 'project', 'zone', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), 'list_peering_routes': ('network', 'project', 'direction', 'filter', 'max_results', 'order_by', 'page_token', 'peering_name', 'region', 'return_partial_success', ), 'list_per_instance_configs': ('instance_group_manager', 'project', 'zone', 'filter', 'max_results', 'order_by', 'page_token', 'return_partial_success', ), @@ -138,12 +138,12 @@ class computeCallTransformer(cst.CSTTransformer): 'set_security_policy': ('backend_service', 'project', 'security_policy_reference_resource', 'request_id', ), 'set_service_account': ('instance', 'instances_set_service_account_request_resource', 'project', 'zone', 'request_id', ), 'set_shielded_instance_integrity_policy': ('instance', 'project', 'shielded_instance_integrity_policy_resource', 'zone', 'request_id', ), - 'set_ssl_certificates': ('project', 'region', 'region_target_https_proxies_set_ssl_certificates_request_resource', 'target_https_proxy', 'request_id', ), + 'set_ssl_certificates': ('project', 'target_https_proxies_set_ssl_certificates_request_resource', 'target_https_proxy', 'request_id', ), 'set_ssl_policy': ('project', 'ssl_policy_reference_resource', 'target_https_proxy', 'request_id', ), 'set_tags': ('instance', 'project', 'tags_resource', 'zone', 'request_id', ), - 'set_target': ('forwarding_rule', 'project', 'region', 'target_reference_resource', 'request_id', ), + 'set_target': ('forwarding_rule', 'project', 'target_reference_resource', 'request_id', ), 'set_target_pools': ('instance_group_manager', 'instance_group_managers_set_target_pools_request_resource', 'project', 'zone', 'request_id', ), - 'set_url_map': ('project', 'region', 'target_http_proxy', 'url_map_reference_resource', 'request_id', ), + 'set_url_map': ('project', 'target_https_proxy', 'url_map_reference_resource', 'request_id', ), 'set_usage_export_bucket': ('project', 'usage_export_location_resource', 'request_id', ), 'simulate_maintenance_event': ('instance', 'project', 'zone', ), 'start': ('instance', 'project', 'zone', 'request_id', ), @@ -158,8 +158,8 @@ class computeCallTransformer(cst.CSTTransformer): 'update_peering': ('network', 'networks_update_peering_request_resource', 'project', 'request_id', ), 'update_per_instance_configs': ('instance_group_manager', 'instance_group_managers_update_per_instance_configs_req_resource', 'project', 'zone', 'request_id', ), 'update_shielded_instance_config': ('instance', 'project', 'shielded_instance_config_resource', 'zone', 'request_id', ), - 'validate': ('project', 'region', 'region_url_maps_validate_request_resource', 'url_map', ), - 'wait': ('operation', 'project', ), + 'validate': ('project', 'url_map', 'url_maps_validate_request_resource', ), + 'wait': ('operation', 'project', 'region', ), } diff --git a/packages/google-cloud-compute/synth.metadata b/packages/google-cloud-compute/synth.metadata index 0b3c56b957f1..100147148703 100644 --- a/packages/google-cloud-compute/synth.metadata +++ b/packages/google-cloud-compute/synth.metadata @@ -4,7 +4,14 @@ "git": { "name": ".", "remote": "https://github.com/googleapis/python-compute.git", - "sha": "d26bed251d6285dc779dc8892204930f86c37ec2" + "sha": "16e7294cd536af9a8bf8e4e99219a883339aa955" + } + }, + { + "git": { + "name": "googleapis-discovery", + "remote": "https://github.com/googleapis/googleapis-discovery.git", + "sha": "8bde5a64f149bd8f6a3b71efa20d232cb07a55de" } }, { @@ -32,5 +39,667 @@ "generator": "bazel" } } + ], + "generatedFiles": [ + ".coveragerc", + ".flake8", + ".github/CONTRIBUTING.md", + ".github/ISSUE_TEMPLATE/bug_report.md", + ".github/ISSUE_TEMPLATE/feature_request.md", + ".github/ISSUE_TEMPLATE/support_request.md", + ".github/PULL_REQUEST_TEMPLATE.md", + ".github/release-please.yml", + ".github/snippet-bot.yml", + ".gitignore", + ".kokoro/build.sh", + ".kokoro/continuous/common.cfg", + ".kokoro/continuous/continuous.cfg", + ".kokoro/docker/docs/Dockerfile", + ".kokoro/docker/docs/fetch_gpg_keys.sh", + ".kokoro/docs/common.cfg", + ".kokoro/docs/docs-presubmit.cfg", + ".kokoro/docs/docs.cfg", + ".kokoro/populate-secrets.sh", + ".kokoro/presubmit/common.cfg", + ".kokoro/presubmit/presubmit.cfg", + ".kokoro/publish-docs.sh", + ".kokoro/release.sh", + ".kokoro/release/common.cfg", + ".kokoro/release/release.cfg", + ".kokoro/samples/lint/common.cfg", + ".kokoro/samples/lint/continuous.cfg", + ".kokoro/samples/lint/periodic.cfg", + ".kokoro/samples/lint/presubmit.cfg", + ".kokoro/samples/python3.6/common.cfg", + ".kokoro/samples/python3.6/continuous.cfg", + ".kokoro/samples/python3.6/periodic.cfg", + ".kokoro/samples/python3.6/presubmit.cfg", + ".kokoro/samples/python3.7/common.cfg", + ".kokoro/samples/python3.7/continuous.cfg", + ".kokoro/samples/python3.7/periodic.cfg", + ".kokoro/samples/python3.7/presubmit.cfg", + ".kokoro/samples/python3.8/common.cfg", + ".kokoro/samples/python3.8/continuous.cfg", + ".kokoro/samples/python3.8/periodic.cfg", + ".kokoro/samples/python3.8/presubmit.cfg", + ".kokoro/test-samples.sh", + ".kokoro/trampoline.sh", + ".kokoro/trampoline_v2.sh", + ".pre-commit-config.yaml", + ".trampolinerc", + "CODE_OF_CONDUCT.md", + "CONTRIBUTING.rst", + "LICENSE", + "MANIFEST.in", + "docs/_static/custom.css", + "docs/_templates/layout.html", + "docs/compute_v1/accelerator_types.rst", + "docs/compute_v1/addresses.rst", + "docs/compute_v1/autoscalers.rst", + "docs/compute_v1/backend_buckets.rst", + "docs/compute_v1/backend_services.rst", + "docs/compute_v1/disk_types.rst", + "docs/compute_v1/disks.rst", + "docs/compute_v1/external_vpn_gateways.rst", + "docs/compute_v1/firewalls.rst", + "docs/compute_v1/forwarding_rules.rst", + "docs/compute_v1/global_addresses.rst", + "docs/compute_v1/global_forwarding_rules.rst", + "docs/compute_v1/global_network_endpoint_groups.rst", + "docs/compute_v1/global_operations.rst", + "docs/compute_v1/global_organization_operations.rst", + "docs/compute_v1/health_checks.rst", + "docs/compute_v1/images.rst", + "docs/compute_v1/instance_group_managers.rst", + "docs/compute_v1/instance_groups.rst", + "docs/compute_v1/instance_templates.rst", + "docs/compute_v1/instances.rst", + "docs/compute_v1/interconnect_attachments.rst", + "docs/compute_v1/interconnect_locations.rst", + "docs/compute_v1/interconnects.rst", + "docs/compute_v1/license_codes.rst", + "docs/compute_v1/licenses.rst", + "docs/compute_v1/machine_types.rst", + "docs/compute_v1/network_endpoint_groups.rst", + "docs/compute_v1/networks.rst", + "docs/compute_v1/node_groups.rst", + "docs/compute_v1/node_templates.rst", + "docs/compute_v1/node_types.rst", + "docs/compute_v1/packet_mirrorings.rst", + "docs/compute_v1/projects.rst", + "docs/compute_v1/region_autoscalers.rst", + "docs/compute_v1/region_backend_services.rst", + "docs/compute_v1/region_commitments.rst", + "docs/compute_v1/region_disk_types.rst", + "docs/compute_v1/region_disks.rst", + "docs/compute_v1/region_health_check_services.rst", + "docs/compute_v1/region_health_checks.rst", + "docs/compute_v1/region_instance_group_managers.rst", + "docs/compute_v1/region_instance_groups.rst", + "docs/compute_v1/region_network_endpoint_groups.rst", + "docs/compute_v1/region_notification_endpoints.rst", + "docs/compute_v1/region_operations.rst", + "docs/compute_v1/region_ssl_certificates.rst", + "docs/compute_v1/region_target_http_proxies.rst", + "docs/compute_v1/region_target_https_proxies.rst", + "docs/compute_v1/region_url_maps.rst", + "docs/compute_v1/regions.rst", + "docs/compute_v1/reservations.rst", + "docs/compute_v1/resource_policies.rst", + "docs/compute_v1/routers.rst", + "docs/compute_v1/routes.rst", + "docs/compute_v1/security_policies.rst", + "docs/compute_v1/services.rst", + "docs/compute_v1/snapshots.rst", + "docs/compute_v1/ssl_certificates.rst", + "docs/compute_v1/ssl_policies.rst", + "docs/compute_v1/subnetworks.rst", + "docs/compute_v1/target_grpc_proxies.rst", + "docs/compute_v1/target_http_proxies.rst", + "docs/compute_v1/target_https_proxies.rst", + "docs/compute_v1/target_instances.rst", + "docs/compute_v1/target_pools.rst", + "docs/compute_v1/target_ssl_proxies.rst", + "docs/compute_v1/target_tcp_proxies.rst", + "docs/compute_v1/target_vpn_gateways.rst", + "docs/compute_v1/types.rst", + "docs/compute_v1/url_maps.rst", + "docs/compute_v1/vpn_gateways.rst", + "docs/compute_v1/vpn_tunnels.rst", + "docs/compute_v1/zone_operations.rst", + "docs/compute_v1/zones.rst", + "docs/conf.py", + "docs/multiprocessing.rst", + "google/cloud/compute/__init__.py", + "google/cloud/compute/py.typed", + "google/cloud/compute_v1/__init__.py", + "google/cloud/compute_v1/py.typed", + "google/cloud/compute_v1/services/__init__.py", + "google/cloud/compute_v1/services/accelerator_types/__init__.py", + "google/cloud/compute_v1/services/accelerator_types/client.py", + "google/cloud/compute_v1/services/accelerator_types/pagers.py", + "google/cloud/compute_v1/services/accelerator_types/transports/__init__.py", + "google/cloud/compute_v1/services/accelerator_types/transports/base.py", + "google/cloud/compute_v1/services/accelerator_types/transports/rest.py", + "google/cloud/compute_v1/services/addresses/__init__.py", + "google/cloud/compute_v1/services/addresses/client.py", + "google/cloud/compute_v1/services/addresses/pagers.py", + "google/cloud/compute_v1/services/addresses/transports/__init__.py", + "google/cloud/compute_v1/services/addresses/transports/base.py", + "google/cloud/compute_v1/services/addresses/transports/rest.py", + "google/cloud/compute_v1/services/autoscalers/__init__.py", + "google/cloud/compute_v1/services/autoscalers/client.py", + "google/cloud/compute_v1/services/autoscalers/pagers.py", + "google/cloud/compute_v1/services/autoscalers/transports/__init__.py", + "google/cloud/compute_v1/services/autoscalers/transports/base.py", + "google/cloud/compute_v1/services/autoscalers/transports/rest.py", + "google/cloud/compute_v1/services/backend_buckets/__init__.py", + "google/cloud/compute_v1/services/backend_buckets/client.py", + "google/cloud/compute_v1/services/backend_buckets/pagers.py", + "google/cloud/compute_v1/services/backend_buckets/transports/__init__.py", + "google/cloud/compute_v1/services/backend_buckets/transports/base.py", + "google/cloud/compute_v1/services/backend_buckets/transports/rest.py", + "google/cloud/compute_v1/services/backend_services/__init__.py", + "google/cloud/compute_v1/services/backend_services/client.py", + "google/cloud/compute_v1/services/backend_services/pagers.py", + "google/cloud/compute_v1/services/backend_services/transports/__init__.py", + "google/cloud/compute_v1/services/backend_services/transports/base.py", + "google/cloud/compute_v1/services/backend_services/transports/rest.py", + "google/cloud/compute_v1/services/disk_types/__init__.py", + "google/cloud/compute_v1/services/disk_types/client.py", + "google/cloud/compute_v1/services/disk_types/pagers.py", + "google/cloud/compute_v1/services/disk_types/transports/__init__.py", + "google/cloud/compute_v1/services/disk_types/transports/base.py", + "google/cloud/compute_v1/services/disk_types/transports/rest.py", + "google/cloud/compute_v1/services/disks/__init__.py", + "google/cloud/compute_v1/services/disks/client.py", + "google/cloud/compute_v1/services/disks/pagers.py", + "google/cloud/compute_v1/services/disks/transports/__init__.py", + "google/cloud/compute_v1/services/disks/transports/base.py", + "google/cloud/compute_v1/services/disks/transports/rest.py", + "google/cloud/compute_v1/services/external_vpn_gateways/__init__.py", + "google/cloud/compute_v1/services/external_vpn_gateways/client.py", + "google/cloud/compute_v1/services/external_vpn_gateways/pagers.py", + "google/cloud/compute_v1/services/external_vpn_gateways/transports/__init__.py", + "google/cloud/compute_v1/services/external_vpn_gateways/transports/base.py", + "google/cloud/compute_v1/services/external_vpn_gateways/transports/rest.py", + "google/cloud/compute_v1/services/firewalls/__init__.py", + "google/cloud/compute_v1/services/firewalls/client.py", + "google/cloud/compute_v1/services/firewalls/pagers.py", + "google/cloud/compute_v1/services/firewalls/transports/__init__.py", + "google/cloud/compute_v1/services/firewalls/transports/base.py", + "google/cloud/compute_v1/services/firewalls/transports/rest.py", + "google/cloud/compute_v1/services/forwarding_rules/__init__.py", + "google/cloud/compute_v1/services/forwarding_rules/client.py", + "google/cloud/compute_v1/services/forwarding_rules/pagers.py", + "google/cloud/compute_v1/services/forwarding_rules/transports/__init__.py", + "google/cloud/compute_v1/services/forwarding_rules/transports/base.py", + "google/cloud/compute_v1/services/forwarding_rules/transports/rest.py", + "google/cloud/compute_v1/services/global_addresses/__init__.py", + "google/cloud/compute_v1/services/global_addresses/client.py", + "google/cloud/compute_v1/services/global_addresses/pagers.py", + "google/cloud/compute_v1/services/global_addresses/transports/__init__.py", + "google/cloud/compute_v1/services/global_addresses/transports/base.py", + "google/cloud/compute_v1/services/global_addresses/transports/rest.py", + "google/cloud/compute_v1/services/global_forwarding_rules/__init__.py", + "google/cloud/compute_v1/services/global_forwarding_rules/client.py", + "google/cloud/compute_v1/services/global_forwarding_rules/pagers.py", + "google/cloud/compute_v1/services/global_forwarding_rules/transports/__init__.py", + "google/cloud/compute_v1/services/global_forwarding_rules/transports/base.py", + "google/cloud/compute_v1/services/global_forwarding_rules/transports/rest.py", + "google/cloud/compute_v1/services/global_network_endpoint_groups/__init__.py", + "google/cloud/compute_v1/services/global_network_endpoint_groups/client.py", + "google/cloud/compute_v1/services/global_network_endpoint_groups/pagers.py", + "google/cloud/compute_v1/services/global_network_endpoint_groups/transports/__init__.py", + "google/cloud/compute_v1/services/global_network_endpoint_groups/transports/base.py", + "google/cloud/compute_v1/services/global_network_endpoint_groups/transports/rest.py", + "google/cloud/compute_v1/services/global_operations/__init__.py", + "google/cloud/compute_v1/services/global_operations/client.py", + "google/cloud/compute_v1/services/global_operations/pagers.py", + "google/cloud/compute_v1/services/global_operations/transports/__init__.py", + "google/cloud/compute_v1/services/global_operations/transports/base.py", + "google/cloud/compute_v1/services/global_operations/transports/rest.py", + "google/cloud/compute_v1/services/global_organization_operations/__init__.py", + "google/cloud/compute_v1/services/global_organization_operations/client.py", + "google/cloud/compute_v1/services/global_organization_operations/pagers.py", + "google/cloud/compute_v1/services/global_organization_operations/transports/__init__.py", + "google/cloud/compute_v1/services/global_organization_operations/transports/base.py", + "google/cloud/compute_v1/services/global_organization_operations/transports/rest.py", + "google/cloud/compute_v1/services/health_checks/__init__.py", + "google/cloud/compute_v1/services/health_checks/client.py", + "google/cloud/compute_v1/services/health_checks/pagers.py", + "google/cloud/compute_v1/services/health_checks/transports/__init__.py", + "google/cloud/compute_v1/services/health_checks/transports/base.py", + "google/cloud/compute_v1/services/health_checks/transports/rest.py", + "google/cloud/compute_v1/services/images/__init__.py", + "google/cloud/compute_v1/services/images/client.py", + "google/cloud/compute_v1/services/images/pagers.py", + "google/cloud/compute_v1/services/images/transports/__init__.py", + "google/cloud/compute_v1/services/images/transports/base.py", + "google/cloud/compute_v1/services/images/transports/rest.py", + "google/cloud/compute_v1/services/instance_group_managers/__init__.py", + "google/cloud/compute_v1/services/instance_group_managers/client.py", + "google/cloud/compute_v1/services/instance_group_managers/pagers.py", + "google/cloud/compute_v1/services/instance_group_managers/transports/__init__.py", + "google/cloud/compute_v1/services/instance_group_managers/transports/base.py", + "google/cloud/compute_v1/services/instance_group_managers/transports/rest.py", + "google/cloud/compute_v1/services/instance_groups/__init__.py", + "google/cloud/compute_v1/services/instance_groups/client.py", + "google/cloud/compute_v1/services/instance_groups/pagers.py", + "google/cloud/compute_v1/services/instance_groups/transports/__init__.py", + "google/cloud/compute_v1/services/instance_groups/transports/base.py", + "google/cloud/compute_v1/services/instance_groups/transports/rest.py", + "google/cloud/compute_v1/services/instance_templates/__init__.py", + "google/cloud/compute_v1/services/instance_templates/client.py", + "google/cloud/compute_v1/services/instance_templates/pagers.py", + "google/cloud/compute_v1/services/instance_templates/transports/__init__.py", + "google/cloud/compute_v1/services/instance_templates/transports/base.py", + "google/cloud/compute_v1/services/instance_templates/transports/rest.py", + "google/cloud/compute_v1/services/instances/__init__.py", + "google/cloud/compute_v1/services/instances/client.py", + "google/cloud/compute_v1/services/instances/pagers.py", + "google/cloud/compute_v1/services/instances/transports/__init__.py", + "google/cloud/compute_v1/services/instances/transports/base.py", + "google/cloud/compute_v1/services/instances/transports/rest.py", + "google/cloud/compute_v1/services/interconnect_attachments/__init__.py", + "google/cloud/compute_v1/services/interconnect_attachments/client.py", + "google/cloud/compute_v1/services/interconnect_attachments/pagers.py", + "google/cloud/compute_v1/services/interconnect_attachments/transports/__init__.py", + "google/cloud/compute_v1/services/interconnect_attachments/transports/base.py", + "google/cloud/compute_v1/services/interconnect_attachments/transports/rest.py", + "google/cloud/compute_v1/services/interconnect_locations/__init__.py", + "google/cloud/compute_v1/services/interconnect_locations/client.py", + "google/cloud/compute_v1/services/interconnect_locations/pagers.py", + "google/cloud/compute_v1/services/interconnect_locations/transports/__init__.py", + "google/cloud/compute_v1/services/interconnect_locations/transports/base.py", + "google/cloud/compute_v1/services/interconnect_locations/transports/rest.py", + "google/cloud/compute_v1/services/interconnects/__init__.py", + "google/cloud/compute_v1/services/interconnects/client.py", + "google/cloud/compute_v1/services/interconnects/pagers.py", + "google/cloud/compute_v1/services/interconnects/transports/__init__.py", + "google/cloud/compute_v1/services/interconnects/transports/base.py", + "google/cloud/compute_v1/services/interconnects/transports/rest.py", + "google/cloud/compute_v1/services/license_codes/__init__.py", + "google/cloud/compute_v1/services/license_codes/client.py", + "google/cloud/compute_v1/services/license_codes/transports/__init__.py", + "google/cloud/compute_v1/services/license_codes/transports/base.py", + "google/cloud/compute_v1/services/license_codes/transports/rest.py", + "google/cloud/compute_v1/services/licenses/__init__.py", + "google/cloud/compute_v1/services/licenses/client.py", + "google/cloud/compute_v1/services/licenses/pagers.py", + "google/cloud/compute_v1/services/licenses/transports/__init__.py", + "google/cloud/compute_v1/services/licenses/transports/base.py", + "google/cloud/compute_v1/services/licenses/transports/rest.py", + "google/cloud/compute_v1/services/machine_types/__init__.py", + "google/cloud/compute_v1/services/machine_types/client.py", + "google/cloud/compute_v1/services/machine_types/pagers.py", + "google/cloud/compute_v1/services/machine_types/transports/__init__.py", + "google/cloud/compute_v1/services/machine_types/transports/base.py", + "google/cloud/compute_v1/services/machine_types/transports/rest.py", + "google/cloud/compute_v1/services/network_endpoint_groups/__init__.py", + "google/cloud/compute_v1/services/network_endpoint_groups/client.py", + "google/cloud/compute_v1/services/network_endpoint_groups/pagers.py", + "google/cloud/compute_v1/services/network_endpoint_groups/transports/__init__.py", + "google/cloud/compute_v1/services/network_endpoint_groups/transports/base.py", + "google/cloud/compute_v1/services/network_endpoint_groups/transports/rest.py", + "google/cloud/compute_v1/services/networks/__init__.py", + "google/cloud/compute_v1/services/networks/client.py", + "google/cloud/compute_v1/services/networks/pagers.py", + "google/cloud/compute_v1/services/networks/transports/__init__.py", + "google/cloud/compute_v1/services/networks/transports/base.py", + "google/cloud/compute_v1/services/networks/transports/rest.py", + "google/cloud/compute_v1/services/node_groups/__init__.py", + "google/cloud/compute_v1/services/node_groups/client.py", + "google/cloud/compute_v1/services/node_groups/pagers.py", + "google/cloud/compute_v1/services/node_groups/transports/__init__.py", + "google/cloud/compute_v1/services/node_groups/transports/base.py", + "google/cloud/compute_v1/services/node_groups/transports/rest.py", + "google/cloud/compute_v1/services/node_templates/__init__.py", + "google/cloud/compute_v1/services/node_templates/client.py", + "google/cloud/compute_v1/services/node_templates/pagers.py", + "google/cloud/compute_v1/services/node_templates/transports/__init__.py", + "google/cloud/compute_v1/services/node_templates/transports/base.py", + "google/cloud/compute_v1/services/node_templates/transports/rest.py", + "google/cloud/compute_v1/services/node_types/__init__.py", + "google/cloud/compute_v1/services/node_types/client.py", + "google/cloud/compute_v1/services/node_types/pagers.py", + "google/cloud/compute_v1/services/node_types/transports/__init__.py", + "google/cloud/compute_v1/services/node_types/transports/base.py", + "google/cloud/compute_v1/services/node_types/transports/rest.py", + "google/cloud/compute_v1/services/packet_mirrorings/__init__.py", + "google/cloud/compute_v1/services/packet_mirrorings/client.py", + "google/cloud/compute_v1/services/packet_mirrorings/pagers.py", + "google/cloud/compute_v1/services/packet_mirrorings/transports/__init__.py", + "google/cloud/compute_v1/services/packet_mirrorings/transports/base.py", + "google/cloud/compute_v1/services/packet_mirrorings/transports/rest.py", + "google/cloud/compute_v1/services/projects/__init__.py", + "google/cloud/compute_v1/services/projects/client.py", + "google/cloud/compute_v1/services/projects/pagers.py", + "google/cloud/compute_v1/services/projects/transports/__init__.py", + "google/cloud/compute_v1/services/projects/transports/base.py", + "google/cloud/compute_v1/services/projects/transports/rest.py", + "google/cloud/compute_v1/services/region_autoscalers/__init__.py", + "google/cloud/compute_v1/services/region_autoscalers/client.py", + "google/cloud/compute_v1/services/region_autoscalers/pagers.py", + "google/cloud/compute_v1/services/region_autoscalers/transports/__init__.py", + "google/cloud/compute_v1/services/region_autoscalers/transports/base.py", + "google/cloud/compute_v1/services/region_autoscalers/transports/rest.py", + "google/cloud/compute_v1/services/region_backend_services/__init__.py", + "google/cloud/compute_v1/services/region_backend_services/client.py", + "google/cloud/compute_v1/services/region_backend_services/pagers.py", + "google/cloud/compute_v1/services/region_backend_services/transports/__init__.py", + "google/cloud/compute_v1/services/region_backend_services/transports/base.py", + "google/cloud/compute_v1/services/region_backend_services/transports/rest.py", + "google/cloud/compute_v1/services/region_commitments/__init__.py", + "google/cloud/compute_v1/services/region_commitments/client.py", + "google/cloud/compute_v1/services/region_commitments/pagers.py", + "google/cloud/compute_v1/services/region_commitments/transports/__init__.py", + "google/cloud/compute_v1/services/region_commitments/transports/base.py", + "google/cloud/compute_v1/services/region_commitments/transports/rest.py", + "google/cloud/compute_v1/services/region_disk_types/__init__.py", + "google/cloud/compute_v1/services/region_disk_types/client.py", + "google/cloud/compute_v1/services/region_disk_types/pagers.py", + "google/cloud/compute_v1/services/region_disk_types/transports/__init__.py", + "google/cloud/compute_v1/services/region_disk_types/transports/base.py", + "google/cloud/compute_v1/services/region_disk_types/transports/rest.py", + "google/cloud/compute_v1/services/region_disks/__init__.py", + "google/cloud/compute_v1/services/region_disks/client.py", + "google/cloud/compute_v1/services/region_disks/pagers.py", + "google/cloud/compute_v1/services/region_disks/transports/__init__.py", + "google/cloud/compute_v1/services/region_disks/transports/base.py", + "google/cloud/compute_v1/services/region_disks/transports/rest.py", + "google/cloud/compute_v1/services/region_health_check_services/__init__.py", + "google/cloud/compute_v1/services/region_health_check_services/client.py", + "google/cloud/compute_v1/services/region_health_check_services/pagers.py", + "google/cloud/compute_v1/services/region_health_check_services/transports/__init__.py", + "google/cloud/compute_v1/services/region_health_check_services/transports/base.py", + "google/cloud/compute_v1/services/region_health_check_services/transports/rest.py", + "google/cloud/compute_v1/services/region_health_checks/__init__.py", + "google/cloud/compute_v1/services/region_health_checks/client.py", + "google/cloud/compute_v1/services/region_health_checks/pagers.py", + "google/cloud/compute_v1/services/region_health_checks/transports/__init__.py", + "google/cloud/compute_v1/services/region_health_checks/transports/base.py", + "google/cloud/compute_v1/services/region_health_checks/transports/rest.py", + "google/cloud/compute_v1/services/region_instance_group_managers/__init__.py", + "google/cloud/compute_v1/services/region_instance_group_managers/client.py", + "google/cloud/compute_v1/services/region_instance_group_managers/pagers.py", + "google/cloud/compute_v1/services/region_instance_group_managers/transports/__init__.py", + "google/cloud/compute_v1/services/region_instance_group_managers/transports/base.py", + "google/cloud/compute_v1/services/region_instance_group_managers/transports/rest.py", + "google/cloud/compute_v1/services/region_instance_groups/__init__.py", + "google/cloud/compute_v1/services/region_instance_groups/client.py", + "google/cloud/compute_v1/services/region_instance_groups/pagers.py", + "google/cloud/compute_v1/services/region_instance_groups/transports/__init__.py", + "google/cloud/compute_v1/services/region_instance_groups/transports/base.py", + "google/cloud/compute_v1/services/region_instance_groups/transports/rest.py", + "google/cloud/compute_v1/services/region_network_endpoint_groups/__init__.py", + "google/cloud/compute_v1/services/region_network_endpoint_groups/client.py", + "google/cloud/compute_v1/services/region_network_endpoint_groups/pagers.py", + "google/cloud/compute_v1/services/region_network_endpoint_groups/transports/__init__.py", + "google/cloud/compute_v1/services/region_network_endpoint_groups/transports/base.py", + "google/cloud/compute_v1/services/region_network_endpoint_groups/transports/rest.py", + "google/cloud/compute_v1/services/region_notification_endpoints/__init__.py", + "google/cloud/compute_v1/services/region_notification_endpoints/client.py", + "google/cloud/compute_v1/services/region_notification_endpoints/pagers.py", + "google/cloud/compute_v1/services/region_notification_endpoints/transports/__init__.py", + "google/cloud/compute_v1/services/region_notification_endpoints/transports/base.py", + "google/cloud/compute_v1/services/region_notification_endpoints/transports/rest.py", + "google/cloud/compute_v1/services/region_operations/__init__.py", + "google/cloud/compute_v1/services/region_operations/client.py", + "google/cloud/compute_v1/services/region_operations/pagers.py", + "google/cloud/compute_v1/services/region_operations/transports/__init__.py", + "google/cloud/compute_v1/services/region_operations/transports/base.py", + "google/cloud/compute_v1/services/region_operations/transports/rest.py", + "google/cloud/compute_v1/services/region_ssl_certificates/__init__.py", + "google/cloud/compute_v1/services/region_ssl_certificates/client.py", + "google/cloud/compute_v1/services/region_ssl_certificates/pagers.py", + "google/cloud/compute_v1/services/region_ssl_certificates/transports/__init__.py", + "google/cloud/compute_v1/services/region_ssl_certificates/transports/base.py", + "google/cloud/compute_v1/services/region_ssl_certificates/transports/rest.py", + "google/cloud/compute_v1/services/region_target_http_proxies/__init__.py", + "google/cloud/compute_v1/services/region_target_http_proxies/client.py", + "google/cloud/compute_v1/services/region_target_http_proxies/pagers.py", + "google/cloud/compute_v1/services/region_target_http_proxies/transports/__init__.py", + "google/cloud/compute_v1/services/region_target_http_proxies/transports/base.py", + "google/cloud/compute_v1/services/region_target_http_proxies/transports/rest.py", + "google/cloud/compute_v1/services/region_target_https_proxies/__init__.py", + "google/cloud/compute_v1/services/region_target_https_proxies/client.py", + "google/cloud/compute_v1/services/region_target_https_proxies/pagers.py", + "google/cloud/compute_v1/services/region_target_https_proxies/transports/__init__.py", + "google/cloud/compute_v1/services/region_target_https_proxies/transports/base.py", + "google/cloud/compute_v1/services/region_target_https_proxies/transports/rest.py", + "google/cloud/compute_v1/services/region_url_maps/__init__.py", + "google/cloud/compute_v1/services/region_url_maps/client.py", + "google/cloud/compute_v1/services/region_url_maps/pagers.py", + "google/cloud/compute_v1/services/region_url_maps/transports/__init__.py", + "google/cloud/compute_v1/services/region_url_maps/transports/base.py", + "google/cloud/compute_v1/services/region_url_maps/transports/rest.py", + "google/cloud/compute_v1/services/regions/__init__.py", + "google/cloud/compute_v1/services/regions/client.py", + "google/cloud/compute_v1/services/regions/pagers.py", + "google/cloud/compute_v1/services/regions/transports/__init__.py", + "google/cloud/compute_v1/services/regions/transports/base.py", + "google/cloud/compute_v1/services/regions/transports/rest.py", + "google/cloud/compute_v1/services/reservations/__init__.py", + "google/cloud/compute_v1/services/reservations/client.py", + "google/cloud/compute_v1/services/reservations/pagers.py", + "google/cloud/compute_v1/services/reservations/transports/__init__.py", + "google/cloud/compute_v1/services/reservations/transports/base.py", + "google/cloud/compute_v1/services/reservations/transports/rest.py", + "google/cloud/compute_v1/services/resource_policies/__init__.py", + "google/cloud/compute_v1/services/resource_policies/client.py", + "google/cloud/compute_v1/services/resource_policies/pagers.py", + "google/cloud/compute_v1/services/resource_policies/transports/__init__.py", + "google/cloud/compute_v1/services/resource_policies/transports/base.py", + "google/cloud/compute_v1/services/resource_policies/transports/rest.py", + "google/cloud/compute_v1/services/routers/__init__.py", + "google/cloud/compute_v1/services/routers/client.py", + "google/cloud/compute_v1/services/routers/pagers.py", + "google/cloud/compute_v1/services/routers/transports/__init__.py", + "google/cloud/compute_v1/services/routers/transports/base.py", + "google/cloud/compute_v1/services/routers/transports/rest.py", + "google/cloud/compute_v1/services/routes/__init__.py", + "google/cloud/compute_v1/services/routes/client.py", + "google/cloud/compute_v1/services/routes/pagers.py", + "google/cloud/compute_v1/services/routes/transports/__init__.py", + "google/cloud/compute_v1/services/routes/transports/base.py", + "google/cloud/compute_v1/services/routes/transports/rest.py", + "google/cloud/compute_v1/services/security_policies/__init__.py", + "google/cloud/compute_v1/services/security_policies/client.py", + "google/cloud/compute_v1/services/security_policies/pagers.py", + "google/cloud/compute_v1/services/security_policies/transports/__init__.py", + "google/cloud/compute_v1/services/security_policies/transports/base.py", + "google/cloud/compute_v1/services/security_policies/transports/rest.py", + "google/cloud/compute_v1/services/snapshots/__init__.py", + "google/cloud/compute_v1/services/snapshots/client.py", + "google/cloud/compute_v1/services/snapshots/pagers.py", + "google/cloud/compute_v1/services/snapshots/transports/__init__.py", + "google/cloud/compute_v1/services/snapshots/transports/base.py", + "google/cloud/compute_v1/services/snapshots/transports/rest.py", + "google/cloud/compute_v1/services/ssl_certificates/__init__.py", + "google/cloud/compute_v1/services/ssl_certificates/client.py", + "google/cloud/compute_v1/services/ssl_certificates/pagers.py", + "google/cloud/compute_v1/services/ssl_certificates/transports/__init__.py", + "google/cloud/compute_v1/services/ssl_certificates/transports/base.py", + "google/cloud/compute_v1/services/ssl_certificates/transports/rest.py", + "google/cloud/compute_v1/services/ssl_policies/__init__.py", + "google/cloud/compute_v1/services/ssl_policies/client.py", + "google/cloud/compute_v1/services/ssl_policies/pagers.py", + "google/cloud/compute_v1/services/ssl_policies/transports/__init__.py", + "google/cloud/compute_v1/services/ssl_policies/transports/base.py", + "google/cloud/compute_v1/services/ssl_policies/transports/rest.py", + "google/cloud/compute_v1/services/subnetworks/__init__.py", + "google/cloud/compute_v1/services/subnetworks/client.py", + "google/cloud/compute_v1/services/subnetworks/pagers.py", + "google/cloud/compute_v1/services/subnetworks/transports/__init__.py", + "google/cloud/compute_v1/services/subnetworks/transports/base.py", + "google/cloud/compute_v1/services/subnetworks/transports/rest.py", + "google/cloud/compute_v1/services/target_grpc_proxies/__init__.py", + "google/cloud/compute_v1/services/target_grpc_proxies/client.py", + "google/cloud/compute_v1/services/target_grpc_proxies/pagers.py", + "google/cloud/compute_v1/services/target_grpc_proxies/transports/__init__.py", + "google/cloud/compute_v1/services/target_grpc_proxies/transports/base.py", + "google/cloud/compute_v1/services/target_grpc_proxies/transports/rest.py", + "google/cloud/compute_v1/services/target_http_proxies/__init__.py", + "google/cloud/compute_v1/services/target_http_proxies/client.py", + "google/cloud/compute_v1/services/target_http_proxies/pagers.py", + "google/cloud/compute_v1/services/target_http_proxies/transports/__init__.py", + "google/cloud/compute_v1/services/target_http_proxies/transports/base.py", + "google/cloud/compute_v1/services/target_http_proxies/transports/rest.py", + "google/cloud/compute_v1/services/target_https_proxies/__init__.py", + "google/cloud/compute_v1/services/target_https_proxies/client.py", + "google/cloud/compute_v1/services/target_https_proxies/pagers.py", + "google/cloud/compute_v1/services/target_https_proxies/transports/__init__.py", + "google/cloud/compute_v1/services/target_https_proxies/transports/base.py", + "google/cloud/compute_v1/services/target_https_proxies/transports/rest.py", + "google/cloud/compute_v1/services/target_instances/__init__.py", + "google/cloud/compute_v1/services/target_instances/client.py", + "google/cloud/compute_v1/services/target_instances/pagers.py", + "google/cloud/compute_v1/services/target_instances/transports/__init__.py", + "google/cloud/compute_v1/services/target_instances/transports/base.py", + "google/cloud/compute_v1/services/target_instances/transports/rest.py", + "google/cloud/compute_v1/services/target_pools/__init__.py", + "google/cloud/compute_v1/services/target_pools/client.py", + "google/cloud/compute_v1/services/target_pools/pagers.py", + "google/cloud/compute_v1/services/target_pools/transports/__init__.py", + "google/cloud/compute_v1/services/target_pools/transports/base.py", + "google/cloud/compute_v1/services/target_pools/transports/rest.py", + "google/cloud/compute_v1/services/target_ssl_proxies/__init__.py", + "google/cloud/compute_v1/services/target_ssl_proxies/client.py", + "google/cloud/compute_v1/services/target_ssl_proxies/pagers.py", + "google/cloud/compute_v1/services/target_ssl_proxies/transports/__init__.py", + "google/cloud/compute_v1/services/target_ssl_proxies/transports/base.py", + "google/cloud/compute_v1/services/target_ssl_proxies/transports/rest.py", + "google/cloud/compute_v1/services/target_tcp_proxies/__init__.py", + "google/cloud/compute_v1/services/target_tcp_proxies/client.py", + "google/cloud/compute_v1/services/target_tcp_proxies/pagers.py", + "google/cloud/compute_v1/services/target_tcp_proxies/transports/__init__.py", + "google/cloud/compute_v1/services/target_tcp_proxies/transports/base.py", + "google/cloud/compute_v1/services/target_tcp_proxies/transports/rest.py", + "google/cloud/compute_v1/services/target_vpn_gateways/__init__.py", + "google/cloud/compute_v1/services/target_vpn_gateways/client.py", + "google/cloud/compute_v1/services/target_vpn_gateways/pagers.py", + "google/cloud/compute_v1/services/target_vpn_gateways/transports/__init__.py", + "google/cloud/compute_v1/services/target_vpn_gateways/transports/base.py", + "google/cloud/compute_v1/services/target_vpn_gateways/transports/rest.py", + "google/cloud/compute_v1/services/url_maps/__init__.py", + "google/cloud/compute_v1/services/url_maps/client.py", + "google/cloud/compute_v1/services/url_maps/pagers.py", + "google/cloud/compute_v1/services/url_maps/transports/__init__.py", + "google/cloud/compute_v1/services/url_maps/transports/base.py", + "google/cloud/compute_v1/services/url_maps/transports/rest.py", + "google/cloud/compute_v1/services/vpn_gateways/__init__.py", + "google/cloud/compute_v1/services/vpn_gateways/client.py", + "google/cloud/compute_v1/services/vpn_gateways/pagers.py", + "google/cloud/compute_v1/services/vpn_gateways/transports/__init__.py", + "google/cloud/compute_v1/services/vpn_gateways/transports/base.py", + "google/cloud/compute_v1/services/vpn_gateways/transports/rest.py", + "google/cloud/compute_v1/services/vpn_tunnels/__init__.py", + "google/cloud/compute_v1/services/vpn_tunnels/client.py", + "google/cloud/compute_v1/services/vpn_tunnels/pagers.py", + "google/cloud/compute_v1/services/vpn_tunnels/transports/__init__.py", + "google/cloud/compute_v1/services/vpn_tunnels/transports/base.py", + "google/cloud/compute_v1/services/vpn_tunnels/transports/rest.py", + "google/cloud/compute_v1/services/zone_operations/__init__.py", + "google/cloud/compute_v1/services/zone_operations/client.py", + "google/cloud/compute_v1/services/zone_operations/pagers.py", + "google/cloud/compute_v1/services/zone_operations/transports/__init__.py", + "google/cloud/compute_v1/services/zone_operations/transports/base.py", + "google/cloud/compute_v1/services/zone_operations/transports/rest.py", + "google/cloud/compute_v1/services/zones/__init__.py", + "google/cloud/compute_v1/services/zones/client.py", + "google/cloud/compute_v1/services/zones/pagers.py", + "google/cloud/compute_v1/services/zones/transports/__init__.py", + "google/cloud/compute_v1/services/zones/transports/base.py", + "google/cloud/compute_v1/services/zones/transports/rest.py", + "google/cloud/compute_v1/types/__init__.py", + "google/cloud/compute_v1/types/compute.py", + "mypy.ini", + "noxfile.py", + "renovate.json", + "scripts/decrypt-secrets.sh", + "scripts/fixup_compute_v1_keywords.py", + "scripts/readme-gen/readme_gen.py", + "scripts/readme-gen/templates/README.tmpl.rst", + "scripts/readme-gen/templates/auth.tmpl.rst", + "scripts/readme-gen/templates/auth_api_key.tmpl.rst", + "scripts/readme-gen/templates/install_deps.tmpl.rst", + "scripts/readme-gen/templates/install_portaudio.tmpl.rst", + "setup.cfg", + "testing/.gitignore", + "tests/unit/gapic/compute_v1/__init__.py", + "tests/unit/gapic/compute_v1/test_accelerator_types.py", + "tests/unit/gapic/compute_v1/test_addresses.py", + "tests/unit/gapic/compute_v1/test_autoscalers.py", + "tests/unit/gapic/compute_v1/test_backend_buckets.py", + "tests/unit/gapic/compute_v1/test_backend_services.py", + "tests/unit/gapic/compute_v1/test_disk_types.py", + "tests/unit/gapic/compute_v1/test_disks.py", + "tests/unit/gapic/compute_v1/test_external_vpn_gateways.py", + "tests/unit/gapic/compute_v1/test_firewalls.py", + "tests/unit/gapic/compute_v1/test_forwarding_rules.py", + "tests/unit/gapic/compute_v1/test_global_addresses.py", + "tests/unit/gapic/compute_v1/test_global_forwarding_rules.py", + "tests/unit/gapic/compute_v1/test_global_network_endpoint_groups.py", + "tests/unit/gapic/compute_v1/test_global_operations.py", + "tests/unit/gapic/compute_v1/test_global_organization_operations.py", + "tests/unit/gapic/compute_v1/test_health_checks.py", + "tests/unit/gapic/compute_v1/test_images.py", + "tests/unit/gapic/compute_v1/test_instance_group_managers.py", + "tests/unit/gapic/compute_v1/test_instance_groups.py", + "tests/unit/gapic/compute_v1/test_instance_templates.py", + "tests/unit/gapic/compute_v1/test_instances.py", + "tests/unit/gapic/compute_v1/test_interconnect_attachments.py", + "tests/unit/gapic/compute_v1/test_interconnect_locations.py", + "tests/unit/gapic/compute_v1/test_interconnects.py", + "tests/unit/gapic/compute_v1/test_license_codes.py", + "tests/unit/gapic/compute_v1/test_licenses.py", + "tests/unit/gapic/compute_v1/test_machine_types.py", + "tests/unit/gapic/compute_v1/test_network_endpoint_groups.py", + "tests/unit/gapic/compute_v1/test_networks.py", + "tests/unit/gapic/compute_v1/test_node_groups.py", + "tests/unit/gapic/compute_v1/test_node_templates.py", + "tests/unit/gapic/compute_v1/test_node_types.py", + "tests/unit/gapic/compute_v1/test_packet_mirrorings.py", + "tests/unit/gapic/compute_v1/test_projects.py", + "tests/unit/gapic/compute_v1/test_region_autoscalers.py", + "tests/unit/gapic/compute_v1/test_region_backend_services.py", + "tests/unit/gapic/compute_v1/test_region_commitments.py", + "tests/unit/gapic/compute_v1/test_region_disk_types.py", + "tests/unit/gapic/compute_v1/test_region_disks.py", + "tests/unit/gapic/compute_v1/test_region_health_check_services.py", + "tests/unit/gapic/compute_v1/test_region_health_checks.py", + "tests/unit/gapic/compute_v1/test_region_instance_group_managers.py", + "tests/unit/gapic/compute_v1/test_region_instance_groups.py", + "tests/unit/gapic/compute_v1/test_region_network_endpoint_groups.py", + "tests/unit/gapic/compute_v1/test_region_notification_endpoints.py", + "tests/unit/gapic/compute_v1/test_region_operations.py", + "tests/unit/gapic/compute_v1/test_region_ssl_certificates.py", + "tests/unit/gapic/compute_v1/test_region_target_http_proxies.py", + "tests/unit/gapic/compute_v1/test_region_target_https_proxies.py", + "tests/unit/gapic/compute_v1/test_region_url_maps.py", + "tests/unit/gapic/compute_v1/test_regions.py", + "tests/unit/gapic/compute_v1/test_reservations.py", + "tests/unit/gapic/compute_v1/test_resource_policies.py", + "tests/unit/gapic/compute_v1/test_routers.py", + "tests/unit/gapic/compute_v1/test_routes.py", + "tests/unit/gapic/compute_v1/test_security_policies.py", + "tests/unit/gapic/compute_v1/test_snapshots.py", + "tests/unit/gapic/compute_v1/test_ssl_certificates.py", + "tests/unit/gapic/compute_v1/test_ssl_policies.py", + "tests/unit/gapic/compute_v1/test_subnetworks.py", + "tests/unit/gapic/compute_v1/test_target_grpc_proxies.py", + "tests/unit/gapic/compute_v1/test_target_http_proxies.py", + "tests/unit/gapic/compute_v1/test_target_https_proxies.py", + "tests/unit/gapic/compute_v1/test_target_instances.py", + "tests/unit/gapic/compute_v1/test_target_pools.py", + "tests/unit/gapic/compute_v1/test_target_ssl_proxies.py", + "tests/unit/gapic/compute_v1/test_target_tcp_proxies.py", + "tests/unit/gapic/compute_v1/test_target_vpn_gateways.py", + "tests/unit/gapic/compute_v1/test_url_maps.py", + "tests/unit/gapic/compute_v1/test_vpn_gateways.py", + "tests/unit/gapic/compute_v1/test_vpn_tunnels.py", + "tests/unit/gapic/compute_v1/test_zone_operations.py", + "tests/unit/gapic/compute_v1/test_zones.py" ] } \ No newline at end of file diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/__init__.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/__init__.py index 8b137891791f..42ffdf2bc43d 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/__init__.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/__init__.py @@ -1 +1,16 @@ +# -*- coding: utf-8 -*- +# Copyright 2020 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. +# diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_accelerator_types.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_accelerator_types.py index 07f310f2ad14..fc731eff731e 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_accelerator_types.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_accelerator_types.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.accelerator_types import AcceleratorTypesClient +from google.cloud.compute_v1.services.accelerator_types import pagers from google.cloud.compute_v1.services.accelerator_types import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -423,11 +424,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.AcceleratorTypeAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.AcceleratorTypesScopedList( @@ -470,7 +469,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -486,6 +485,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = AcceleratorTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.AcceleratorTypeAggregatedList( + items={ + "a": compute.AcceleratorTypesScopedList(), + "b": compute.AcceleratorTypesScopedList(), + "c": compute.AcceleratorTypesScopedList(), + }, + next_page_token="abc", + ), + compute.AcceleratorTypeAggregatedList(items={}, next_page_token="def",), + compute.AcceleratorTypeAggregatedList( + items={"g": compute.AcceleratorTypesScopedList(),}, + next_page_token="ghi", + ), + compute.AcceleratorTypeAggregatedList( + items={ + "h": compute.AcceleratorTypesScopedList(), + "i": compute.AcceleratorTypesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.AcceleratorTypeAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.AcceleratorTypesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.AcceleratorTypesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.AcceleratorTypesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_get_rest( transport: str = "rest", request_type=compute.GetAcceleratorTypeRequest ): @@ -565,7 +633,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -621,11 +689,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.AcceleratorTypeList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.AcceleratorType(creation_timestamp="creation_timestamp_value") @@ -665,7 +731,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -685,6 +751,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = AcceleratorTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.AcceleratorTypeList( + items=[ + compute.AcceleratorType(), + compute.AcceleratorType(), + compute.AcceleratorType(), + ], + next_page_token="abc", + ), + compute.AcceleratorTypeList(items=[], next_page_token="def",), + compute.AcceleratorTypeList( + items=[compute.AcceleratorType(),], next_page_token="ghi", + ), + compute.AcceleratorTypeList( + items=[compute.AcceleratorType(), compute.AcceleratorType(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.AcceleratorTypeList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.AcceleratorType) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.AcceleratorTypesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_addresses.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_addresses.py index 75889c99e7d2..3e2cf661108b 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_addresses.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_addresses.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.addresses import AddressesClient +from google.cloud.compute_v1.services.addresses import pagers from google.cloud.compute_v1.services.addresses import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -397,11 +398,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.AddressAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.AddressesScopedList( @@ -442,7 +441,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -458,6 +457,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = AddressesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.AddressAggregatedList( + items={ + "a": compute.AddressesScopedList(), + "b": compute.AddressesScopedList(), + "c": compute.AddressesScopedList(), + }, + next_page_token="abc", + ), + compute.AddressAggregatedList(items={}, next_page_token="def",), + compute.AddressAggregatedList( + items={"g": compute.AddressesScopedList(),}, next_page_token="ghi", + ), + compute.AddressAggregatedList( + items={ + "h": compute.AddressesScopedList(), + "i": compute.AddressesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.AddressAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.AddressesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.AddressesScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.AddressesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteAddressRequest ): @@ -565,7 +627,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -679,7 +741,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -813,14 +875,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.Address.to_json( - address_resource, including_default_value_fields=False + address_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -867,11 +931,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListAddressesRe response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.AddressList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.Address(address="address_value")] assert response.kind == "kind_value" @@ -909,7 +971,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -929,6 +991,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = AddressesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.AddressList( + items=[compute.Address(), compute.Address(), compute.Address(),], + next_page_token="abc", + ), + compute.AddressList(items=[], next_page_token="def",), + compute.AddressList(items=[compute.Address(),], next_page_token="ghi",), + compute.AddressList(items=[compute.Address(), compute.Address(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.AddressList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Address) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.AddressesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_autoscalers.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_autoscalers.py index 158c896c88b9..b636e53ebfd9 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_autoscalers.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_autoscalers.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.autoscalers import AutoscalersClient +from google.cloud.compute_v1.services.autoscalers import pagers from google.cloud.compute_v1.services.autoscalers import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -407,11 +408,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.AutoscalerAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.AutoscalersScopedList( @@ -458,7 +457,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -474,6 +473,72 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = AutoscalersClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.AutoscalerAggregatedList( + items={ + "a": compute.AutoscalersScopedList(), + "b": compute.AutoscalersScopedList(), + "c": compute.AutoscalersScopedList(), + }, + next_page_token="abc", + ), + compute.AutoscalerAggregatedList(items={}, next_page_token="def",), + compute.AutoscalerAggregatedList( + items={"g": compute.AutoscalersScopedList(),}, next_page_token="ghi", + ), + compute.AutoscalerAggregatedList( + items={ + "h": compute.AutoscalersScopedList(), + "i": compute.AutoscalersScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.AutoscalerAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.AutoscalersScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.AutoscalersScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.AutoscalersScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteAutoscalerRequest ): @@ -581,7 +646,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -691,7 +756,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -827,14 +892,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.Autoscaler.to_json( - autoscaler_resource, including_default_value_fields=False + autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -891,11 +958,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.AutoscalerList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Autoscaler( @@ -937,7 +1002,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -957,6 +1022,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = AutoscalersClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.AutoscalerList( + items=[ + compute.Autoscaler(), + compute.Autoscaler(), + compute.Autoscaler(), + ], + next_page_token="abc", + ), + compute.AutoscalerList(items=[], next_page_token="def",), + compute.AutoscalerList( + items=[compute.Autoscaler(),], next_page_token="ghi", + ), + compute.AutoscalerList( + items=[compute.Autoscaler(), compute.Autoscaler(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.AutoscalerList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Autoscaler) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchAutoscalerRequest ): @@ -1070,14 +1186,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.Autoscaler.to_json( - autoscaler_resource, including_default_value_fields=False + autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1210,14 +1328,16 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.Autoscaler.to_json( - autoscaler_resource, including_default_value_fields=False + autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_backend_buckets.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_backend_buckets.py index c155de331ad2..35e3019a095d 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_backend_buckets.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_backend_buckets.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.backend_buckets import BackendBucketsClient +from google.cloud.compute_v1.services.backend_buckets import pagers from google.cloud.compute_v1.services.backend_buckets import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -483,14 +484,16 @@ def test_add_signed_url_key_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "backend_bucket_value" in http_call[1] + str(body) assert compute.SignedUrlKey.to_json( - signed_url_key_resource, including_default_value_fields=False + signed_url_key_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -615,7 +618,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -744,7 +747,7 @@ def test_delete_signed_url_key_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -851,7 +854,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -980,12 +983,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.BackendBucket.to_json( - backend_bucket_resource, including_default_value_fields=False + backend_bucket_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1035,11 +1040,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.BackendBucketList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.BackendBucket(bucket_name="bucket_name_value")] assert response.kind == "kind_value" @@ -1075,7 +1078,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1091,6 +1094,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = BackendBucketsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.BackendBucketList( + items=[ + compute.BackendBucket(), + compute.BackendBucket(), + compute.BackendBucket(), + ], + next_page_token="abc", + ), + compute.BackendBucketList(items=[], next_page_token="def",), + compute.BackendBucketList( + items=[compute.BackendBucket(),], next_page_token="ghi", + ), + compute.BackendBucketList( + items=[compute.BackendBucket(), compute.BackendBucket(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.BackendBucketList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.BackendBucket) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchBackendBucketRequest ): @@ -1202,14 +1256,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "backend_bucket_value" in http_call[1] + str(body) assert compute.BackendBucket.to_json( - backend_bucket_resource, including_default_value_fields=False + backend_bucket_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1340,14 +1396,16 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "backend_bucket_value" in http_call[1] + str(body) assert compute.BackendBucket.to_json( - backend_bucket_resource, including_default_value_fields=False + backend_bucket_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_backend_services.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_backend_services.py index 7f1a4f545149..5e180766b02c 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_backend_services.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_backend_services.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.backend_services import BackendServicesClient +from google.cloud.compute_v1.services.backend_services import pagers from google.cloud.compute_v1.services.backend_services import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -493,14 +494,16 @@ def test_add_signed_url_key_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "backend_service_value" in http_call[1] + str(body) assert compute.SignedUrlKey.to_json( - signed_url_key_resource, including_default_value_fields=False + signed_url_key_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -556,11 +559,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.BackendServiceAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.BackendServicesScopedList( @@ -601,7 +602,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -617,6 +618,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = BackendServicesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.BackendServiceAggregatedList( + items={ + "a": compute.BackendServicesScopedList(), + "b": compute.BackendServicesScopedList(), + "c": compute.BackendServicesScopedList(), + }, + next_page_token="abc", + ), + compute.BackendServiceAggregatedList(items={}, next_page_token="def",), + compute.BackendServiceAggregatedList( + items={"g": compute.BackendServicesScopedList(),}, + next_page_token="ghi", + ), + compute.BackendServiceAggregatedList( + items={ + "h": compute.BackendServicesScopedList(), + "i": compute.BackendServicesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.BackendServiceAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.BackendServicesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.BackendServicesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.BackendServicesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteBackendServiceRequest ): @@ -724,7 +794,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -854,7 +924,7 @@ def test_delete_signed_url_key_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1037,7 +1107,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1132,14 +1202,16 @@ def test_get_health_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "backend_service_value" in http_call[1] + str(body) assert compute.ResourceGroupReference.to_json( - resource_group_reference_resource, including_default_value_fields=False + resource_group_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1268,12 +1340,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.BackendService.to_json( - backend_service_resource, including_default_value_fields=False + backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1323,11 +1397,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.BackendServiceList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.BackendService(affinity_cookie_ttl_sec=2432)] assert response.kind == "kind_value" @@ -1363,7 +1435,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1379,6 +1451,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = BackendServicesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.BackendServiceList( + items=[ + compute.BackendService(), + compute.BackendService(), + compute.BackendService(), + ], + next_page_token="abc", + ), + compute.BackendServiceList(items=[], next_page_token="def",), + compute.BackendServiceList( + items=[compute.BackendService(),], next_page_token="ghi", + ), + compute.BackendServiceList( + items=[compute.BackendService(), compute.BackendService(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.BackendServiceList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.BackendService) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchBackendServiceRequest ): @@ -1490,14 +1613,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "backend_service_value" in http_call[1] + str(body) assert compute.BackendService.to_json( - backend_service_resource, including_default_value_fields=False + backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1630,14 +1755,16 @@ def test_set_security_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "backend_service_value" in http_call[1] + str(body) assert compute.SecurityPolicyReference.to_json( - security_policy_reference_resource, including_default_value_fields=False + security_policy_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1768,14 +1895,16 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "backend_service_value" in http_call[1] + str(body) assert compute.BackendService.to_json( - backend_service_resource, including_default_value_fields=False + backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_disk_types.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_disk_types.py index 397382ab1fd0..e46c41bc79f8 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_disk_types.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_disk_types.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.disk_types import DiskTypesClient +from google.cloud.compute_v1.services.disk_types import pagers from google.cloud.compute_v1.services.disk_types import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -401,11 +402,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.DiskTypeAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.DiskTypesScopedList( @@ -446,7 +445,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -462,6 +461,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = DiskTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.DiskTypeAggregatedList( + items={ + "a": compute.DiskTypesScopedList(), + "b": compute.DiskTypesScopedList(), + "c": compute.DiskTypesScopedList(), + }, + next_page_token="abc", + ), + compute.DiskTypeAggregatedList(items={}, next_page_token="def",), + compute.DiskTypeAggregatedList( + items={"g": compute.DiskTypesScopedList(),}, next_page_token="ghi", + ), + compute.DiskTypeAggregatedList( + items={ + "h": compute.DiskTypesScopedList(), + "i": compute.DiskTypesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.DiskTypeAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.DiskTypesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.DiskTypesScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.DiskTypesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_get_rest(transport: str = "rest", request_type=compute.GetDiskTypeRequest): client = DiskTypesClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -541,7 +603,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -593,11 +655,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListDiskTypesRe response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.DiskTypeList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.DiskType(creation_timestamp="creation_timestamp_value") @@ -637,7 +697,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -655,6 +715,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = DiskTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.DiskTypeList( + items=[compute.DiskType(), compute.DiskType(), compute.DiskType(),], + next_page_token="abc", + ), + compute.DiskTypeList(items=[], next_page_token="def",), + compute.DiskTypeList(items=[compute.DiskType(),], next_page_token="ghi",), + compute.DiskTypeList(items=[compute.DiskType(), compute.DiskType(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.DiskTypeList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.DiskType) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.DiskTypesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_disks.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_disks.py index 06a64dd7f14c..304dc6c8be36 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_disks.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_disks.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.disks import DisksClient +from google.cloud.compute_v1.services.disks import pagers from google.cloud.compute_v1.services.disks import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -474,7 +475,7 @@ def test_add_resource_policies_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -485,6 +486,7 @@ def test_add_resource_policies_rest_flattened(): assert compute.DisksAddResourcePoliciesRequest.to_json( disks_add_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -541,11 +543,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.DiskAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.DisksScopedList( @@ -586,7 +586,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -602,6 +602,66 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = DisksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.DiskAggregatedList( + items={ + "a": compute.DisksScopedList(), + "b": compute.DisksScopedList(), + "c": compute.DisksScopedList(), + }, + next_page_token="abc", + ), + compute.DiskAggregatedList(items={}, next_page_token="def",), + compute.DiskAggregatedList( + items={"g": compute.DisksScopedList(),}, next_page_token="ghi", + ), + compute.DiskAggregatedList( + items={"h": compute.DisksScopedList(), "i": compute.DisksScopedList(),}, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.DiskAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.DisksScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.DisksScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.DisksScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_create_snapshot_rest( transport: str = "rest", request_type=compute.CreateSnapshotDiskRequest ): @@ -714,7 +774,7 @@ def test_create_snapshot_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -723,7 +783,9 @@ def test_create_snapshot_rest_flattened(): assert "disk_value" in http_call[1] + str(body) assert compute.Snapshot.to_json( - snapshot_resource, including_default_value_fields=False + snapshot_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -847,7 +909,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1011,7 +1073,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1120,7 +1182,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1250,14 +1312,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.Disk.to_json( - disk_resource, including_default_value_fields=False + disk_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1304,11 +1368,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListDisksReques response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.DiskList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Disk(creation_timestamp="creation_timestamp_value") @@ -1348,7 +1410,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1366,6 +1428,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = DisksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.DiskList( + items=[compute.Disk(), compute.Disk(), compute.Disk(),], + next_page_token="abc", + ), + compute.DiskList(items=[], next_page_token="def",), + compute.DiskList(items=[compute.Disk(),], next_page_token="ghi",), + compute.DiskList(items=[compute.Disk(), compute.Disk(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.DiskList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Disk) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_remove_resource_policies_rest( transport: str = "rest", request_type=compute.RemoveResourcePoliciesDiskRequest ): @@ -1480,7 +1585,7 @@ def test_remove_resource_policies_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1491,6 +1596,7 @@ def test_remove_resource_policies_rest_flattened(): assert compute.DisksRemoveResourcePoliciesRequest.to_json( disks_remove_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1623,7 +1729,7 @@ def test_resize_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1632,7 +1738,9 @@ def test_resize_rest_flattened(): assert "disk_value" in http_call[1] + str(body) assert compute.DisksResizeRequest.to_json( - disks_resize_request_resource, including_default_value_fields=False + disks_resize_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1746,7 +1854,7 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1755,7 +1863,9 @@ def test_set_iam_policy_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.ZoneSetPolicyRequest.to_json( - zone_set_policy_request_resource, including_default_value_fields=False + zone_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1890,7 +2000,7 @@ def test_set_labels_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1899,7 +2009,9 @@ def test_set_labels_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.ZoneSetLabelsRequest.to_json( - zone_set_labels_request_resource, including_default_value_fields=False + zone_set_labels_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1988,7 +2100,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1997,7 +2109,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_external_vpn_gateways.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_external_vpn_gateways.py index 00c0a9e8c436..212b049e251f 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_external_vpn_gateways.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_external_vpn_gateways.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.external_vpn_gateways import ( ExternalVpnGatewaysClient, ) +from google.cloud.compute_v1.services.external_vpn_gateways import pagers from google.cloud.compute_v1.services.external_vpn_gateways import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -492,7 +493,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -594,7 +595,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -726,12 +727,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.ExternalVpnGateway.to_json( - external_vpn_gateway_resource, including_default_value_fields=False + external_vpn_gateway_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -786,11 +789,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ExternalVpnGatewayList) + assert isinstance(response, pagers.ListPager) assert response.etag == "etag_value" assert response.id == "id_value" assert response.items == [ @@ -829,7 +830,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -845,6 +846,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = ExternalVpnGatewaysClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ExternalVpnGatewayList( + items=[ + compute.ExternalVpnGateway(), + compute.ExternalVpnGateway(), + compute.ExternalVpnGateway(), + ], + next_page_token="abc", + ), + compute.ExternalVpnGatewayList(items=[], next_page_token="def",), + compute.ExternalVpnGatewayList( + items=[compute.ExternalVpnGateway(),], next_page_token="ghi", + ), + compute.ExternalVpnGatewayList( + items=[compute.ExternalVpnGateway(), compute.ExternalVpnGateway(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ExternalVpnGatewayList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.ExternalVpnGateway) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_labels_rest( transport: str = "rest", request_type=compute.SetLabelsExternalVpnGatewayRequest ): @@ -958,14 +1010,16 @@ def test_set_labels_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.GlobalSetLabelsRequest.to_json( - global_set_labels_request_resource, including_default_value_fields=False + global_set_labels_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1053,14 +1107,16 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_firewalls.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_firewalls.py index 793441292a64..636f12c6b526 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_firewalls.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_firewalls.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.firewalls import FirewallsClient +from google.cloud.compute_v1.services.firewalls import pagers from google.cloud.compute_v1.services.firewalls import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -468,7 +469,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -584,7 +585,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -715,12 +716,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.Firewall.to_json( - firewall_resource, including_default_value_fields=False + firewall_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -772,11 +775,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListFirewallsRe response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.FirewallList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Firewall(allowed=[compute.Allowed(i_p_protocol="i_p_protocol_value")]) @@ -814,7 +815,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -830,6 +831,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = FirewallsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.FirewallList( + items=[compute.Firewall(), compute.Firewall(), compute.Firewall(),], + next_page_token="abc", + ), + compute.FirewallList(items=[], next_page_token="def",), + compute.FirewallList(items=[compute.Firewall(),], next_page_token="ghi",), + compute.FirewallList(items=[compute.Firewall(), compute.Firewall(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.FirewallList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Firewall) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest(transport: str = "rest", request_type=compute.PatchFirewallRequest): client = FirewallsClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -941,14 +985,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "firewall_value" in http_call[1] + str(body) assert compute.Firewall.to_json( - firewall_resource, including_default_value_fields=False + firewall_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1081,14 +1127,16 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "firewall_value" in http_call[1] + str(body) assert compute.Firewall.to_json( - firewall_resource, including_default_value_fields=False + firewall_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_forwarding_rules.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_forwarding_rules.py index 0557c6222a4e..af98e35bad43 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_forwarding_rules.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_forwarding_rules.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.forwarding_rules import ForwardingRulesClient +from google.cloud.compute_v1.services.forwarding_rules import pagers from google.cloud.compute_v1.services.forwarding_rules import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -418,11 +419,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ForwardingRuleAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.ForwardingRulesScopedList( @@ -463,7 +462,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -479,6 +478,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = ForwardingRulesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ForwardingRuleAggregatedList( + items={ + "a": compute.ForwardingRulesScopedList(), + "b": compute.ForwardingRulesScopedList(), + "c": compute.ForwardingRulesScopedList(), + }, + next_page_token="abc", + ), + compute.ForwardingRuleAggregatedList(items={}, next_page_token="def",), + compute.ForwardingRuleAggregatedList( + items={"g": compute.ForwardingRulesScopedList(),}, + next_page_token="ghi", + ), + compute.ForwardingRuleAggregatedList( + items={ + "h": compute.ForwardingRulesScopedList(), + "i": compute.ForwardingRulesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.ForwardingRuleAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.ForwardingRulesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.ForwardingRulesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.ForwardingRulesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteForwardingRuleRequest ): @@ -588,7 +656,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -736,7 +804,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -870,14 +938,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.ForwardingRule.to_json( - forwarding_rule_resource, including_default_value_fields=False + forwarding_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -926,11 +996,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ForwardingRuleList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.ForwardingRule(all_ports=True)] assert response.kind == "kind_value" @@ -968,7 +1036,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -988,6 +1056,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = ForwardingRulesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ForwardingRuleList( + items=[ + compute.ForwardingRule(), + compute.ForwardingRule(), + compute.ForwardingRule(), + ], + next_page_token="abc", + ), + compute.ForwardingRuleList(items=[], next_page_token="def",), + compute.ForwardingRuleList( + items=[compute.ForwardingRule(),], next_page_token="ghi", + ), + compute.ForwardingRuleList( + items=[compute.ForwardingRule(), compute.ForwardingRule(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ForwardingRuleList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.ForwardingRule) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchForwardingRuleRequest ): @@ -1100,7 +1219,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1109,7 +1228,9 @@ def test_patch_rest_flattened(): assert "forwarding_rule_value" in http_call[1] + str(body) assert compute.ForwardingRule.to_json( - forwarding_rule_resource, including_default_value_fields=False + forwarding_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1240,7 +1361,7 @@ def test_set_target_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1249,7 +1370,9 @@ def test_set_target_rest_flattened(): assert "forwarding_rule_value" in http_call[1] + str(body) assert compute.TargetReference.to_json( - target_reference_resource, including_default_value_fields=False + target_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_addresses.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_addresses.py index 6da05fe3d642..90a94e2558ae 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_addresses.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_addresses.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.global_addresses import GlobalAddressesClient +from google.cloud.compute_v1.services.global_addresses import pagers from google.cloud.compute_v1.services.global_addresses import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -489,7 +490,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -602,7 +603,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -731,12 +732,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.Address.to_json( - address_resource, including_default_value_fields=False + address_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -784,11 +787,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.AddressList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.Address(address="address_value")] assert response.kind == "kind_value" @@ -824,7 +825,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -840,6 +841,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = GlobalAddressesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.AddressList( + items=[compute.Address(), compute.Address(), compute.Address(),], + next_page_token="abc", + ), + compute.AddressList(items=[], next_page_token="def",), + compute.AddressList(items=[compute.Address(),], next_page_token="ghi",), + compute.AddressList(items=[compute.Address(), compute.Address(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.AddressList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Address) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.GlobalAddressesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_forwarding_rules.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_forwarding_rules.py index 467b567056e5..562428ea7be2 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_forwarding_rules.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_forwarding_rules.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.global_forwarding_rules import ( GlobalForwardingRulesClient, ) +from google.cloud.compute_v1.services.global_forwarding_rules import pagers from google.cloud.compute_v1.services.global_forwarding_rules import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -512,7 +513,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -659,7 +660,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -792,12 +793,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.ForwardingRule.to_json( - forwarding_rule_resource, including_default_value_fields=False + forwarding_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -847,11 +850,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ForwardingRuleList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.ForwardingRule(all_ports=True)] assert response.kind == "kind_value" @@ -889,7 +890,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -907,6 +908,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = GlobalForwardingRulesClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ForwardingRuleList( + items=[ + compute.ForwardingRule(), + compute.ForwardingRule(), + compute.ForwardingRule(), + ], + next_page_token="abc", + ), + compute.ForwardingRuleList(items=[], next_page_token="def",), + compute.ForwardingRuleList( + items=[compute.ForwardingRule(),], next_page_token="ghi", + ), + compute.ForwardingRuleList( + items=[compute.ForwardingRule(), compute.ForwardingRule(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ForwardingRuleList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.ForwardingRule) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchGlobalForwardingRuleRequest ): @@ -1020,14 +1074,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "forwarding_rule_value" in http_call[1] + str(body) assert compute.ForwardingRule.to_json( - forwarding_rule_resource, including_default_value_fields=False + forwarding_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1160,14 +1216,16 @@ def test_set_target_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "forwarding_rule_value" in http_call[1] + str(body) assert compute.TargetReference.to_json( - target_reference_resource, including_default_value_fields=False + target_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_network_endpoint_groups.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_network_endpoint_groups.py index b96c951727e7..dac41f62bf3b 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_network_endpoint_groups.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_network_endpoint_groups.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.global_network_endpoint_groups import ( GlobalNetworkEndpointGroupsClient, ) +from google.cloud.compute_v1.services.global_network_endpoint_groups import pagers from google.cloud.compute_v1.services.global_network_endpoint_groups import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -527,7 +528,7 @@ def test_attach_network_endpoints_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -536,6 +537,7 @@ def test_attach_network_endpoints_rest_flattened(): assert compute.GlobalNetworkEndpointGroupsAttachEndpointsRequest.to_json( global_network_endpoint_groups_attach_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -670,7 +672,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -810,7 +812,7 @@ def test_detach_network_endpoints_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -819,6 +821,7 @@ def test_detach_network_endpoints_rest_flattened(): assert compute.GlobalNetworkEndpointGroupsDetachEndpointsRequest.to_json( global_network_endpoint_groups_detach_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -949,7 +952,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1086,12 +1089,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.NetworkEndpointGroup.to_json( - network_endpoint_group_resource, including_default_value_fields=False + network_endpoint_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1145,11 +1150,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NetworkEndpointGroupList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.NetworkEndpointGroup(annotations={"key_value": "value_value"}) @@ -1189,7 +1192,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1207,6 +1210,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = GlobalNetworkEndpointGroupsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NetworkEndpointGroupList( + items=[ + compute.NetworkEndpointGroup(), + compute.NetworkEndpointGroup(), + compute.NetworkEndpointGroup(), + ], + next_page_token="abc", + ), + compute.NetworkEndpointGroupList(items=[], next_page_token="def",), + compute.NetworkEndpointGroupList( + items=[compute.NetworkEndpointGroup(),], next_page_token="ghi", + ), + compute.NetworkEndpointGroupList( + items=[compute.NetworkEndpointGroup(), compute.NetworkEndpointGroup(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NetworkEndpointGroupList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.NetworkEndpointGroup) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_network_endpoints_rest( transport: str = "rest", request_type=compute.ListNetworkEndpointsGlobalNetworkEndpointGroupsRequest, @@ -1250,11 +1306,9 @@ def test_list_network_endpoints_rest( response = client.list_network_endpoints(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NetworkEndpointGroupsListNetworkEndpoints) + assert isinstance(response, pagers.ListNetworkEndpointsPager) assert response.id == "id_value" assert response.items == [ compute.NetworkEndpointWithHealthStatus( @@ -1306,7 +1360,7 @@ def test_list_network_endpoints_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1328,6 +1382,70 @@ def test_list_network_endpoints_rest_flattened_error(): ) +def test_list_network_endpoints_pager(): + client = GlobalNetworkEndpointGroupsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NetworkEndpointGroupsListNetworkEndpoints( + items=[ + compute.NetworkEndpointWithHealthStatus(), + compute.NetworkEndpointWithHealthStatus(), + compute.NetworkEndpointWithHealthStatus(), + ], + next_page_token="abc", + ), + compute.NetworkEndpointGroupsListNetworkEndpoints( + items=[], next_page_token="def", + ), + compute.NetworkEndpointGroupsListNetworkEndpoints( + items=[compute.NetworkEndpointWithHealthStatus(),], + next_page_token="ghi", + ), + compute.NetworkEndpointGroupsListNetworkEndpoints( + items=[ + compute.NetworkEndpointWithHealthStatus(), + compute.NetworkEndpointWithHealthStatus(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.NetworkEndpointGroupsListNetworkEndpoints.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 + + metadata = () + pager = client.list_network_endpoints(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all( + isinstance(i, compute.NetworkEndpointWithHealthStatus) for i in results + ) + + pages = list(client.list_network_endpoints(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.GlobalNetworkEndpointGroupsRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_operations.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_operations.py index 1ae21f7c6381..6678196c0d19 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_operations.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_operations.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.global_operations import GlobalOperationsClient +from google.cloud.compute_v1.services.global_operations import pagers from google.cloud.compute_v1.services.global_operations import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -423,11 +424,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.OperationAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.OperationsScopedList( @@ -470,7 +469,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -486,6 +485,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = GlobalOperationsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.OperationAggregatedList( + items={ + "a": compute.OperationsScopedList(), + "b": compute.OperationsScopedList(), + "c": compute.OperationsScopedList(), + }, + next_page_token="abc", + ), + compute.OperationAggregatedList(items={}, next_page_token="def",), + compute.OperationAggregatedList( + items={"g": compute.OperationsScopedList(),}, next_page_token="ghi", + ), + compute.OperationAggregatedList( + items={ + "h": compute.OperationsScopedList(), + "i": compute.OperationsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.OperationAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.OperationsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.OperationsScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.OperationsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteGlobalOperationRequest ): @@ -544,7 +606,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -671,7 +733,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -722,11 +784,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.OperationList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Operation(client_operation_id="client_operation_id_value") @@ -764,7 +824,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -780,6 +840,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = GlobalOperationsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.OperationList( + items=[compute.Operation(), compute.Operation(), compute.Operation(),], + next_page_token="abc", + ), + compute.OperationList(items=[], next_page_token="def",), + compute.OperationList(items=[compute.Operation(),], next_page_token="ghi",), + compute.OperationList(items=[compute.Operation(), compute.Operation(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.OperationList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Operation) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_wait_rest( transport: str = "rest", request_type=compute.WaitGlobalOperationRequest ): @@ -887,7 +990,7 @@ def test_wait_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_organization_operations.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_organization_operations.py index 23ec886fd13f..8527031d659f 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_organization_operations.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_global_organization_operations.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.global_organization_operations import ( GlobalOrganizationOperationsClient, ) +from google.cloud.compute_v1.services.global_organization_operations import pagers from google.cloud.compute_v1.services.global_organization_operations import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -472,7 +473,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "operation_value" in http_call[1] + str(body) @@ -598,7 +599,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "operation_value" in http_call[1] + str(body) @@ -649,11 +650,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.OperationList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Operation(client_operation_id="client_operation_id_value") @@ -693,7 +692,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") def test_list_rest_flattened_error(): @@ -707,6 +706,51 @@ def test_list_rest_flattened_error(): client.list(compute.ListGlobalOrganizationOperationsRequest(),) +def test_list_pager(): + client = GlobalOrganizationOperationsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.OperationList( + items=[compute.Operation(), compute.Operation(), compute.Operation(),], + next_page_token="abc", + ), + compute.OperationList(items=[], next_page_token="def",), + compute.OperationList(items=[compute.Operation(),], next_page_token="ghi",), + compute.OperationList(items=[compute.Operation(), compute.Operation(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.OperationList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Operation) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.GlobalOrganizationOperationsRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_health_checks.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_health_checks.py index 71f812603f03..112b5f21412d 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_health_checks.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_health_checks.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.health_checks import HealthChecksClient +from google.cloud.compute_v1.services.health_checks import pagers from google.cloud.compute_v1.services.health_checks import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -401,11 +402,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.HealthChecksAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.HealthChecksScopedList( @@ -446,7 +445,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -462,6 +461,74 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = HealthChecksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.HealthChecksAggregatedList( + items={ + "a": compute.HealthChecksScopedList(), + "b": compute.HealthChecksScopedList(), + "c": compute.HealthChecksScopedList(), + }, + next_page_token="abc", + ), + compute.HealthChecksAggregatedList(items={}, next_page_token="def",), + compute.HealthChecksAggregatedList( + items={"g": compute.HealthChecksScopedList(),}, next_page_token="ghi", + ), + compute.HealthChecksAggregatedList( + items={ + "h": compute.HealthChecksScopedList(), + "i": compute.HealthChecksScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.HealthChecksAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.HealthChecksScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.HealthChecksScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.HealthChecksScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteHealthCheckRequest ): @@ -569,7 +636,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -688,7 +755,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -817,12 +884,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.HealthCheck.to_json( - health_check_resource, including_default_value_fields=False + health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -870,11 +939,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.HealthCheckList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.HealthCheck(check_interval_sec=1884)] assert response.kind == "kind_value" @@ -910,7 +977,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -926,6 +993,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = HealthChecksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.HealthCheckList( + items=[ + compute.HealthCheck(), + compute.HealthCheck(), + compute.HealthCheck(), + ], + next_page_token="abc", + ), + compute.HealthCheckList(items=[], next_page_token="def",), + compute.HealthCheckList( + items=[compute.HealthCheck(),], next_page_token="ghi", + ), + compute.HealthCheckList( + items=[compute.HealthCheck(), compute.HealthCheck(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.HealthCheckList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.HealthCheck) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchHealthCheckRequest ): @@ -1037,14 +1155,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "health_check_value" in http_call[1] + str(body) assert compute.HealthCheck.to_json( - health_check_resource, including_default_value_fields=False + health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1173,14 +1293,16 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "health_check_value" in http_call[1] + str(body) assert compute.HealthCheck.to_json( - health_check_resource, including_default_value_fields=False + health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_images.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_images.py index 1cb7e1cff13b..86b5bd7111e4 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_images.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_images.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.images import ImagesClient +from google.cloud.compute_v1.services.images import pagers from google.cloud.compute_v1.services.images import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -465,7 +466,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -594,14 +595,16 @@ def test_deprecate_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "image_value" in http_call[1] + str(body) assert compute.DeprecationStatus.to_json( - deprecation_status_resource, including_default_value_fields=False + deprecation_status_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -768,7 +771,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -935,7 +938,7 @@ def test_get_from_family_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1041,7 +1044,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1168,12 +1171,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.Image.to_json( - image_resource, including_default_value_fields=False + image_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1219,11 +1224,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListImagesReque response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ImageList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Image(archive_size_bytes="archive_size_bytes_value") @@ -1261,7 +1264,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1277,6 +1280,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = ImagesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ImageList( + items=[compute.Image(), compute.Image(), compute.Image(),], + next_page_token="abc", + ), + compute.ImageList(items=[], next_page_token="def",), + compute.ImageList(items=[compute.Image(),], next_page_token="ghi",), + compute.ImageList(items=[compute.Image(), compute.Image(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ImageList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Image) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest(transport: str = "rest", request_type=compute.PatchImageRequest): client = ImagesClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -1384,14 +1430,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "image_value" in http_call[1] + str(body) assert compute.Image.to_json( - image_resource, including_default_value_fields=False + image_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1501,14 +1549,16 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.GlobalSetPolicyRequest.to_json( - global_set_policy_request_resource, including_default_value_fields=False + global_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1641,14 +1691,16 @@ def test_set_labels_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.GlobalSetLabelsRequest.to_json( - global_set_labels_request_resource, including_default_value_fields=False + global_set_labels_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1735,14 +1787,16 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_group_managers.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_group_managers.py index 66aff7d4f9f7..49c9ae908693 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_group_managers.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_group_managers.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.instance_group_managers import ( InstanceGroupManagersClient, ) +from google.cloud.compute_v1.services.instance_group_managers import pagers from google.cloud.compute_v1.services.instance_group_managers import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -520,7 +521,7 @@ def test_abandon_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -531,6 +532,7 @@ def test_abandon_instances_rest_flattened(): assert compute.InstanceGroupManagersAbandonInstancesRequest.to_json( instance_group_managers_abandon_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -600,11 +602,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceGroupManagerAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.InstanceGroupManagersScopedList( @@ -657,7 +657,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -676,6 +676,79 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = InstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceGroupManagerAggregatedList( + items={ + "a": compute.InstanceGroupManagersScopedList(), + "b": compute.InstanceGroupManagersScopedList(), + "c": compute.InstanceGroupManagersScopedList(), + }, + next_page_token="abc", + ), + compute.InstanceGroupManagerAggregatedList( + items={}, next_page_token="def", + ), + compute.InstanceGroupManagerAggregatedList( + items={"g": compute.InstanceGroupManagersScopedList(),}, + next_page_token="ghi", + ), + compute.InstanceGroupManagerAggregatedList( + items={ + "h": compute.InstanceGroupManagersScopedList(), + "i": compute.InstanceGroupManagersScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.InstanceGroupManagerAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.InstanceGroupManagersScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.InstanceGroupManagersScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.InstanceGroupManagersScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_apply_updates_to_instances_rest( transport: str = "rest", request_type=compute.ApplyUpdatesToInstancesInstanceGroupManagerRequest, @@ -793,7 +866,7 @@ def test_apply_updates_to_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -804,6 +877,7 @@ def test_apply_updates_to_instances_rest_flattened(): assert compute.InstanceGroupManagersApplyUpdatesRequest.to_json( instance_group_managers_apply_updates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -943,7 +1017,7 @@ def test_create_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -954,6 +1028,7 @@ def test_create_instances_rest_flattened(): assert compute.InstanceGroupManagersCreateInstancesRequest.to_json( instance_group_managers_create_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1087,7 +1162,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1229,7 +1304,7 @@ def test_delete_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1240,6 +1315,7 @@ def test_delete_instances_rest_flattened(): assert compute.InstanceGroupManagersDeleteInstancesRequest.to_json( instance_group_managers_delete_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1379,7 +1455,7 @@ def test_delete_per_instance_configs_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1390,6 +1466,7 @@ def test_delete_per_instance_configs_rest_flattened(): assert compute.InstanceGroupManagersDeletePerInstanceConfigsReq.to_json( instance_group_managers_delete_per_instance_configs_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1559,7 +1636,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1703,14 +1780,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.InstanceGroupManager.to_json( - instance_group_manager_resource, including_default_value_fields=False + instance_group_manager_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1775,11 +1854,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceGroupManagerList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.InstanceGroupManager( @@ -1827,7 +1904,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1849,6 +1926,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = InstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceGroupManagerList( + items=[ + compute.InstanceGroupManager(), + compute.InstanceGroupManager(), + compute.InstanceGroupManager(), + ], + next_page_token="abc", + ), + compute.InstanceGroupManagerList(items=[], next_page_token="def",), + compute.InstanceGroupManagerList( + items=[compute.InstanceGroupManager(),], next_page_token="ghi", + ), + compute.InstanceGroupManagerList( + items=[compute.InstanceGroupManager(), compute.InstanceGroupManager(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.InstanceGroupManagerList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceGroupManager) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_errors_rest( transport: str = "rest", request_type=compute.ListErrorsInstanceGroupManagersRequest ): @@ -1884,11 +2014,9 @@ def test_list_errors_rest( response = client.list_errors(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceGroupManagersListErrorsResponse) + assert isinstance(response, pagers.ListErrorsPager) assert response.items == [ compute.InstanceManagedByIgmError( error=compute.InstanceManagedByIgmErrorManagedInstanceError( @@ -1934,7 +2062,7 @@ def test_list_errors_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1959,6 +2087,66 @@ def test_list_errors_rest_flattened_error(): ) +def test_list_errors_pager(): + client = InstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceGroupManagersListErrorsResponse( + items=[ + compute.InstanceManagedByIgmError(), + compute.InstanceManagedByIgmError(), + compute.InstanceManagedByIgmError(), + ], + next_page_token="abc", + ), + compute.InstanceGroupManagersListErrorsResponse( + items=[], next_page_token="def", + ), + compute.InstanceGroupManagersListErrorsResponse( + items=[compute.InstanceManagedByIgmError(),], next_page_token="ghi", + ), + compute.InstanceGroupManagersListErrorsResponse( + items=[ + compute.InstanceManagedByIgmError(), + compute.InstanceManagedByIgmError(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.InstanceGroupManagersListErrorsResponse.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 + + metadata = () + pager = client.list_errors(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceManagedByIgmError) for i in results) + + pages = list(client.list_errors(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_managed_instances_rest( transport: str = "rest", request_type=compute.ListManagedInstancesInstanceGroupManagersRequest, @@ -1993,13 +2181,9 @@ def test_list_managed_instances_rest( response = client.list_managed_instances(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance( - response, compute.InstanceGroupManagersListManagedInstancesResponse - ) + assert isinstance(response, pagers.ListManagedInstancesPager) assert response.managed_instances == [ compute.ManagedInstance( current_action=compute.ManagedInstance.CurrentAction.ABANDONING @@ -2043,7 +2227,7 @@ def test_list_managed_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2068,6 +2252,67 @@ def test_list_managed_instances_rest_flattened_error(): ) +def test_list_managed_instances_pager(): + client = InstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceGroupManagersListManagedInstancesResponse( + managed_instances=[ + compute.ManagedInstance(), + compute.ManagedInstance(), + compute.ManagedInstance(), + ], + next_page_token="abc", + ), + compute.InstanceGroupManagersListManagedInstancesResponse( + managed_instances=[], next_page_token="def", + ), + compute.InstanceGroupManagersListManagedInstancesResponse( + managed_instances=[compute.ManagedInstance(),], next_page_token="ghi", + ), + compute.InstanceGroupManagersListManagedInstancesResponse( + managed_instances=[ + compute.ManagedInstance(), + compute.ManagedInstance(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.InstanceGroupManagersListManagedInstancesResponse.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 + + metadata = () + pager = client.list_managed_instances(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.ManagedInstance) for i in results) + + pages = list(client.list_managed_instances(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_per_instance_configs_rest( transport: str = "rest", request_type=compute.ListPerInstanceConfigsInstanceGroupManagersRequest, @@ -2099,11 +2344,9 @@ def test_list_per_instance_configs_rest( response = client.list_per_instance_configs(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceGroupManagersListPerInstanceConfigsResp) + assert isinstance(response, pagers.ListPerInstanceConfigsPager) assert response.items == [ compute.PerInstanceConfig(fingerprint="fingerprint_value") ] @@ -2146,7 +2389,7 @@ def test_list_per_instance_configs_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2171,6 +2414,64 @@ def test_list_per_instance_configs_rest_flattened_error(): ) +def test_list_per_instance_configs_pager(): + client = InstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceGroupManagersListPerInstanceConfigsResp( + items=[ + compute.PerInstanceConfig(), + compute.PerInstanceConfig(), + compute.PerInstanceConfig(), + ], + next_page_token="abc", + ), + compute.InstanceGroupManagersListPerInstanceConfigsResp( + items=[], next_page_token="def", + ), + compute.InstanceGroupManagersListPerInstanceConfigsResp( + items=[compute.PerInstanceConfig(),], next_page_token="ghi", + ), + compute.InstanceGroupManagersListPerInstanceConfigsResp( + items=[compute.PerInstanceConfig(), compute.PerInstanceConfig(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.InstanceGroupManagersListPerInstanceConfigsResp.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 + + metadata = () + pager = client.list_per_instance_configs(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.PerInstanceConfig) for i in results) + + pages = list(client.list_per_instance_configs(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchInstanceGroupManagerRequest ): @@ -2291,7 +2592,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2300,7 +2601,9 @@ def test_patch_rest_flattened(): assert "instance_group_manager_value" in http_call[1] + str(body) assert compute.InstanceGroupManager.to_json( - instance_group_manager_resource, including_default_value_fields=False + instance_group_manager_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2446,7 +2749,7 @@ def test_patch_per_instance_configs_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2457,6 +2760,7 @@ def test_patch_per_instance_configs_rest_flattened(): assert compute.InstanceGroupManagersPatchPerInstanceConfigsReq.to_json( instance_group_managers_patch_per_instance_configs_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2598,7 +2902,7 @@ def test_recreate_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2609,6 +2913,7 @@ def test_recreate_instances_rest_flattened(): assert compute.InstanceGroupManagersRecreateInstancesRequest.to_json( instance_group_managers_recreate_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2743,7 +3048,7 @@ def test_resize_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2888,7 +3193,7 @@ def test_set_instance_template_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2899,6 +3204,7 @@ def test_set_instance_template_rest_flattened(): assert compute.InstanceGroupManagersSetInstanceTemplateRequest.to_json( instance_group_managers_set_instance_template_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3038,7 +3344,7 @@ def test_set_target_pools_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3049,6 +3355,7 @@ def test_set_target_pools_rest_flattened(): assert compute.InstanceGroupManagersSetTargetPoolsRequest.to_json( instance_group_managers_set_target_pools_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3190,7 +3497,7 @@ def test_update_per_instance_configs_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3201,6 +3508,7 @@ def test_update_per_instance_configs_rest_flattened(): assert compute.InstanceGroupManagersUpdatePerInstanceConfigsReq.to_json( instance_group_managers_update_per_instance_configs_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_groups.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_groups.py index 1fa8f241d460..e0d328caf004 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_groups.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_groups.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.instance_groups import InstanceGroupsClient +from google.cloud.compute_v1.services.instance_groups import pagers from google.cloud.compute_v1.services.instance_groups import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -486,7 +487,7 @@ def test_add_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -497,6 +498,7 @@ def test_add_instances_rest_flattened(): assert compute.InstanceGroupsAddInstancesRequest.to_json( instance_groups_add_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -557,11 +559,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceGroupAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.InstanceGroupsScopedList( @@ -604,7 +604,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -620,6 +620,74 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = InstanceGroupsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceGroupAggregatedList( + items={ + "a": compute.InstanceGroupsScopedList(), + "b": compute.InstanceGroupsScopedList(), + "c": compute.InstanceGroupsScopedList(), + }, + next_page_token="abc", + ), + compute.InstanceGroupAggregatedList(items={}, next_page_token="def",), + compute.InstanceGroupAggregatedList( + items={"g": compute.InstanceGroupsScopedList(),}, next_page_token="ghi", + ), + compute.InstanceGroupAggregatedList( + items={ + "h": compute.InstanceGroupsScopedList(), + "i": compute.InstanceGroupsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.InstanceGroupAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.InstanceGroupsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.InstanceGroupsScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.InstanceGroupsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteInstanceGroupRequest ): @@ -729,7 +797,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -839,7 +907,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -975,14 +1043,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.InstanceGroup.to_json( - instance_group_resource, including_default_value_fields=False + instance_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1035,11 +1105,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceGroupList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.InstanceGroup(creation_timestamp="creation_timestamp_value") @@ -1079,7 +1147,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1099,6 +1167,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = InstanceGroupsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceGroupList( + items=[ + compute.InstanceGroup(), + compute.InstanceGroup(), + compute.InstanceGroup(), + ], + next_page_token="abc", + ), + compute.InstanceGroupList(items=[], next_page_token="def",), + compute.InstanceGroupList( + items=[compute.InstanceGroup(),], next_page_token="ghi", + ), + compute.InstanceGroupList( + items=[compute.InstanceGroup(), compute.InstanceGroup(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.InstanceGroupList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceGroup) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_instances_rest( transport: str = "rest", request_type=compute.ListInstancesInstanceGroupsRequest ): @@ -1130,11 +1249,9 @@ def test_list_instances_rest( response = client.list_instances(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceGroupsListInstances) + assert isinstance(response, pagers.ListInstancesPager) assert response.id == "id_value" assert response.items == [compute.InstanceWithNamedPorts(instance="instance_value")] assert response.kind == "kind_value" @@ -1179,7 +1296,7 @@ def test_list_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1190,6 +1307,7 @@ def test_list_instances_rest_flattened(): assert compute.InstanceGroupsListInstancesRequest.to_json( instance_groups_list_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1210,6 +1328,62 @@ def test_list_instances_rest_flattened_error(): ) +def test_list_instances_pager(): + client = InstanceGroupsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceGroupsListInstances( + items=[ + compute.InstanceWithNamedPorts(), + compute.InstanceWithNamedPorts(), + compute.InstanceWithNamedPorts(), + ], + next_page_token="abc", + ), + compute.InstanceGroupsListInstances(items=[], next_page_token="def",), + compute.InstanceGroupsListInstances( + items=[compute.InstanceWithNamedPorts(),], next_page_token="ghi", + ), + compute.InstanceGroupsListInstances( + items=[ + compute.InstanceWithNamedPorts(), + compute.InstanceWithNamedPorts(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.InstanceGroupsListInstances.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 + + metadata = () + pager = client.list_instances(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceWithNamedPorts) for i in results) + + pages = list(client.list_instances(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_remove_instances_rest( transport: str = "rest", request_type=compute.RemoveInstancesInstanceGroupRequest ): @@ -1324,7 +1498,7 @@ def test_remove_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1335,6 +1509,7 @@ def test_remove_instances_rest_flattened(): assert compute.InstanceGroupsRemoveInstancesRequest.to_json( instance_groups_remove_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1469,7 +1644,7 @@ def test_set_named_ports_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1480,6 +1655,7 @@ def test_set_named_ports_rest_flattened(): assert compute.InstanceGroupsSetNamedPortsRequest.to_json( instance_groups_set_named_ports_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_templates.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_templates.py index 9eb835b6a127..35fde8f9fab9 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_templates.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instance_templates.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.instance_templates import InstanceTemplatesClient +from google.cloud.compute_v1.services.instance_templates import pagers from google.cloud.compute_v1.services.instance_templates import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -490,7 +491,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -591,7 +592,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -697,7 +698,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -829,12 +830,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.InstanceTemplate.to_json( - instance_template_resource, including_default_value_fields=False + instance_template_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -886,11 +889,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceTemplateList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.InstanceTemplate(creation_timestamp="creation_timestamp_value") @@ -928,7 +929,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -944,6 +945,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = InstanceTemplatesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceTemplateList( + items=[ + compute.InstanceTemplate(), + compute.InstanceTemplate(), + compute.InstanceTemplate(), + ], + next_page_token="abc", + ), + compute.InstanceTemplateList(items=[], next_page_token="def",), + compute.InstanceTemplateList( + items=[compute.InstanceTemplate(),], next_page_token="ghi", + ), + compute.InstanceTemplateList( + items=[compute.InstanceTemplate(), compute.InstanceTemplate(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.InstanceTemplateList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceTemplate) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_iam_policy_rest( transport: str = "rest", request_type=compute.SetIamPolicyInstanceTemplateRequest ): @@ -1036,14 +1088,16 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.GlobalSetPolicyRequest.to_json( - global_set_policy_request_resource, including_default_value_fields=False + global_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1131,14 +1185,16 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instances.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instances.py index b526d4041c53..7e2d9d0be827 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instances.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_instances.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.instances import InstancesClient +from google.cloud.compute_v1.services.instances import pagers from google.cloud.compute_v1.services.instances import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -474,7 +475,7 @@ def test_add_access_config_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -485,7 +486,9 @@ def test_add_access_config_rest_flattened(): assert "network_interface_value" in http_call[1] + str(body) assert compute.AccessConfig.to_json( - access_config_resource, including_default_value_fields=False + access_config_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -619,7 +622,7 @@ def test_add_resource_policies_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -630,6 +633,7 @@ def test_add_resource_policies_rest_flattened(): assert compute.InstancesAddResourcePoliciesRequest.to_json( instances_add_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -686,11 +690,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.InstancesScopedList( @@ -731,7 +733,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -747,6 +749,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = InstancesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceAggregatedList( + items={ + "a": compute.InstancesScopedList(), + "b": compute.InstancesScopedList(), + "c": compute.InstancesScopedList(), + }, + next_page_token="abc", + ), + compute.InstanceAggregatedList(items={}, next_page_token="def",), + compute.InstanceAggregatedList( + items={"g": compute.InstancesScopedList(),}, next_page_token="ghi", + ), + compute.InstanceAggregatedList( + items={ + "h": compute.InstancesScopedList(), + "i": compute.InstancesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.InstanceAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.InstancesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.InstancesScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.InstancesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_attach_disk_rest( transport: str = "rest", request_type=compute.AttachDiskInstanceRequest ): @@ -859,7 +924,7 @@ def test_attach_disk_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -868,7 +933,9 @@ def test_attach_disk_rest_flattened(): assert "instance_value" in http_call[1] + str(body) assert compute.AttachedDisk.to_json( - attached_disk_resource, including_default_value_fields=False + attached_disk_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -994,7 +1061,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1128,7 +1195,7 @@ def test_delete_access_config_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1267,7 +1334,7 @@ def test_detach_disk_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1455,7 +1522,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1553,7 +1620,7 @@ def test_get_guest_attributes_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1662,7 +1729,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1745,7 +1812,7 @@ def test_get_screenshot_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1837,7 +1904,7 @@ def test_get_serial_port_output_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1932,7 +1999,7 @@ def test_get_shielded_instance_identity_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2066,14 +2133,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.Instance.to_json( - instance_resource, including_default_value_fields=False + instance_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2120,11 +2189,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListInstancesRe response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.Instance(can_ip_forward=True)] assert response.kind == "kind_value" @@ -2162,7 +2229,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2180,6 +2247,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = InstancesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceList( + items=[compute.Instance(), compute.Instance(), compute.Instance(),], + next_page_token="abc", + ), + compute.InstanceList(items=[], next_page_token="def",), + compute.InstanceList(items=[compute.Instance(),], next_page_token="ghi",), + compute.InstanceList(items=[compute.Instance(), compute.Instance(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.InstanceList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Instance) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_referrers_rest( transport: str = "rest", request_type=compute.ListReferrersInstancesRequest ): @@ -2211,11 +2321,9 @@ def test_list_referrers_rest( response = client.list_referrers(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InstanceListReferrers) + assert isinstance(response, pagers.ListReferrersPager) assert response.id == "id_value" assert response.items == [compute.Reference(kind="kind_value")] assert response.kind == "kind_value" @@ -2253,7 +2361,7 @@ def test_list_referrers_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2276,6 +2384,53 @@ def test_list_referrers_rest_flattened_error(): ) +def test_list_referrers_pager(): + client = InstancesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InstanceListReferrers( + items=[compute.Reference(), compute.Reference(), compute.Reference(),], + next_page_token="abc", + ), + compute.InstanceListReferrers(items=[], next_page_token="def",), + compute.InstanceListReferrers( + items=[compute.Reference(),], next_page_token="ghi", + ), + compute.InstanceListReferrers( + items=[compute.Reference(), compute.Reference(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.InstanceListReferrers.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 + + metadata = () + pager = client.list_referrers(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Reference) for i in results) + + pages = list(client.list_referrers(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_remove_resource_policies_rest( transport: str = "rest", request_type=compute.RemoveResourcePoliciesInstanceRequest ): @@ -2390,7 +2545,7 @@ def test_remove_resource_policies_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2401,6 +2556,7 @@ def test_remove_resource_policies_rest_flattened(): assert compute.InstancesRemoveResourcePoliciesRequest.to_json( instances_remove_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2526,7 +2682,7 @@ def test_reset_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2656,7 +2812,7 @@ def test_set_deletion_protection_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2790,7 +2946,7 @@ def test_set_disk_auto_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2912,7 +3068,7 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2921,7 +3077,9 @@ def test_set_iam_policy_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.ZoneSetPolicyRequest.to_json( - zone_set_policy_request_resource, including_default_value_fields=False + zone_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3056,7 +3214,7 @@ def test_set_labels_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3065,7 +3223,9 @@ def test_set_labels_rest_flattened(): assert "instance_value" in http_call[1] + str(body) assert compute.InstancesSetLabelsRequest.to_json( - instances_set_labels_request_resource, including_default_value_fields=False + instances_set_labels_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3200,7 +3360,7 @@ def test_set_machine_resources_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3211,6 +3371,7 @@ def test_set_machine_resources_rest_flattened(): assert compute.InstancesSetMachineResourcesRequest.to_json( instances_set_machine_resources_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3345,7 +3506,7 @@ def test_set_machine_type_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3356,6 +3517,7 @@ def test_set_machine_type_rest_flattened(): assert compute.InstancesSetMachineTypeRequest.to_json( instances_set_machine_type_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3488,7 +3650,7 @@ def test_set_metadata_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3497,7 +3659,9 @@ def test_set_metadata_rest_flattened(): assert "instance_value" in http_call[1] + str(body) assert compute.Metadata.to_json( - metadata_resource, including_default_value_fields=False + metadata_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3630,7 +3794,7 @@ def test_set_min_cpu_platform_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3641,6 +3805,7 @@ def test_set_min_cpu_platform_rest_flattened(): assert compute.InstancesSetMinCpuPlatformRequest.to_json( instances_set_min_cpu_platform_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3773,7 +3938,7 @@ def test_set_scheduling_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3782,7 +3947,9 @@ def test_set_scheduling_rest_flattened(): assert "instance_value" in http_call[1] + str(body) assert compute.Scheduling.to_json( - scheduling_resource, including_default_value_fields=False + scheduling_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3915,7 +4082,7 @@ def test_set_service_account_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3926,6 +4093,7 @@ def test_set_service_account_rest_flattened(): assert compute.InstancesSetServiceAccountRequest.to_json( instances_set_service_account_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -4061,7 +4229,7 @@ def test_set_shielded_instance_integrity_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -4072,6 +4240,7 @@ def test_set_shielded_instance_integrity_policy_rest_flattened(): assert compute.ShieldedInstanceIntegrityPolicy.to_json( shielded_instance_integrity_policy_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -4204,7 +4373,7 @@ def test_set_tags_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -4213,7 +4382,9 @@ def test_set_tags_rest_flattened(): assert "instance_value" in http_call[1] + str(body) assert compute.Tags.to_json( - tags_resource, including_default_value_fields=False + tags_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -4340,7 +4511,7 @@ def test_simulate_maintenance_event_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -4468,7 +4639,7 @@ def test_start_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -4611,7 +4782,7 @@ def test_start_with_encryption_key_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -4622,6 +4793,7 @@ def test_start_with_encryption_key_rest_flattened(): assert compute.InstancesStartWithEncryptionKeyRequest.to_json( instances_start_with_encryption_key_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -4753,7 +4925,7 @@ def test_stop_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -4844,7 +5016,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -4853,7 +5025,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -4986,7 +5160,7 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -4995,7 +5169,9 @@ def test_update_rest_flattened(): assert "instance_value" in http_call[1] + str(body) assert compute.Instance.to_json( - instance_resource, including_default_value_fields=False + instance_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -5127,7 +5303,7 @@ def test_update_access_config_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -5138,7 +5314,9 @@ def test_update_access_config_rest_flattened(): assert "network_interface_value" in http_call[1] + str(body) assert compute.AccessConfig.to_json( - access_config_resource, including_default_value_fields=False + access_config_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -5270,7 +5448,7 @@ def test_update_display_device_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -5279,7 +5457,9 @@ def test_update_display_device_rest_flattened(): assert "instance_value" in http_call[1] + str(body) assert compute.DisplayDevice.to_json( - display_device_resource, including_default_value_fields=False + display_device_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -5413,7 +5593,7 @@ def test_update_network_interface_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -5424,7 +5604,9 @@ def test_update_network_interface_rest_flattened(): assert "network_interface_value" in http_call[1] + str(body) assert compute.NetworkInterface.to_json( - network_interface_resource, including_default_value_fields=False + network_interface_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -5561,7 +5743,7 @@ def test_update_shielded_instance_config_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -5570,7 +5752,9 @@ def test_update_shielded_instance_config_rest_flattened(): assert "instance_value" in http_call[1] + str(body) assert compute.ShieldedInstanceConfig.to_json( - shielded_instance_config_resource, including_default_value_fields=False + shielded_instance_config_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnect_attachments.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnect_attachments.py index 2c2b7127ef5f..14ee4caaa7e2 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnect_attachments.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnect_attachments.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.interconnect_attachments import ( InterconnectAttachmentsClient, ) +from google.cloud.compute_v1.services.interconnect_attachments import pagers from google.cloud.compute_v1.services.interconnect_attachments import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -444,11 +445,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InterconnectAttachmentAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.InterconnectAttachmentsScopedList( @@ -495,7 +494,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -514,6 +513,79 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = InterconnectAttachmentsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InterconnectAttachmentAggregatedList( + items={ + "a": compute.InterconnectAttachmentsScopedList(), + "b": compute.InterconnectAttachmentsScopedList(), + "c": compute.InterconnectAttachmentsScopedList(), + }, + next_page_token="abc", + ), + compute.InterconnectAttachmentAggregatedList( + items={}, next_page_token="def", + ), + compute.InterconnectAttachmentAggregatedList( + items={"g": compute.InterconnectAttachmentsScopedList(),}, + next_page_token="ghi", + ), + compute.InterconnectAttachmentAggregatedList( + items={ + "h": compute.InterconnectAttachmentsScopedList(), + "i": compute.InterconnectAttachmentsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.InterconnectAttachmentAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.InterconnectAttachmentsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.InterconnectAttachmentsScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.InterconnectAttachmentsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteInterconnectAttachmentRequest ): @@ -625,7 +697,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -781,7 +853,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -921,14 +993,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.InterconnectAttachment.to_json( - interconnect_attachment_resource, including_default_value_fields=False + interconnect_attachment_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -981,11 +1055,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InterconnectAttachmentList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.InterconnectAttachment(admin_enabled=True)] assert response.kind == "kind_value" @@ -1025,7 +1097,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1047,6 +1119,64 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = InterconnectAttachmentsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InterconnectAttachmentList( + items=[ + compute.InterconnectAttachment(), + compute.InterconnectAttachment(), + compute.InterconnectAttachment(), + ], + next_page_token="abc", + ), + compute.InterconnectAttachmentList(items=[], next_page_token="def",), + compute.InterconnectAttachmentList( + items=[compute.InterconnectAttachment(),], next_page_token="ghi", + ), + compute.InterconnectAttachmentList( + items=[ + compute.InterconnectAttachment(), + compute.InterconnectAttachment(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.InterconnectAttachmentList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InterconnectAttachment) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchInterconnectAttachmentRequest ): @@ -1163,7 +1293,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1172,7 +1302,9 @@ def test_patch_rest_flattened(): assert "interconnect_attachment_value" in http_call[1] + str(body) assert compute.InterconnectAttachment.to_json( - interconnect_attachment_resource, including_default_value_fields=False + interconnect_attachment_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnect_locations.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnect_locations.py index 9b8adc54e0f2..a8129229d963 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnect_locations.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnect_locations.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.interconnect_locations import ( InterconnectLocationsClient, ) +from google.cloud.compute_v1.services.interconnect_locations import pagers from google.cloud.compute_v1.services.interconnect_locations import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -503,7 +504,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -556,11 +557,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InterconnectLocationList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.InterconnectLocation(address="address_value")] assert response.kind == "kind_value" @@ -598,7 +597,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -616,6 +615,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = InterconnectLocationsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InterconnectLocationList( + items=[ + compute.InterconnectLocation(), + compute.InterconnectLocation(), + compute.InterconnectLocation(), + ], + next_page_token="abc", + ), + compute.InterconnectLocationList(items=[], next_page_token="def",), + compute.InterconnectLocationList( + items=[compute.InterconnectLocation(),], next_page_token="ghi", + ), + compute.InterconnectLocationList( + items=[compute.InterconnectLocation(), compute.InterconnectLocation(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.InterconnectLocationList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InterconnectLocation) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.InterconnectLocationsRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnects.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnects.py index 1836834e6d77..f0859ddb8670 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnects.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_interconnects.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.interconnects import InterconnectsClient +from google.cloud.compute_v1.services.interconnects import pagers from google.cloud.compute_v1.services.interconnects import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -479,7 +480,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -619,7 +620,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -714,7 +715,7 @@ def test_get_diagnostics_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -843,12 +844,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.Interconnect.to_json( - interconnect_resource, including_default_value_fields=False + interconnect_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -896,11 +899,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.InterconnectList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.Interconnect(admin_enabled=True)] assert response.kind == "kind_value" @@ -936,7 +937,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -952,6 +953,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = InterconnectsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.InterconnectList( + items=[ + compute.Interconnect(), + compute.Interconnect(), + compute.Interconnect(), + ], + next_page_token="abc", + ), + compute.InterconnectList(items=[], next_page_token="def",), + compute.InterconnectList( + items=[compute.Interconnect(),], next_page_token="ghi", + ), + compute.InterconnectList( + items=[compute.Interconnect(), compute.Interconnect(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.InterconnectList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Interconnect) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchInterconnectRequest ): @@ -1063,14 +1115,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "interconnect_value" in http_call[1] + str(body) assert compute.Interconnect.to_json( - interconnect_resource, including_default_value_fields=False + interconnect_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_license_codes.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_license_codes.py index 28484d2ca5d1..f1c8749c8a8a 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_license_codes.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_license_codes.py @@ -445,7 +445,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -532,14 +532,16 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_licenses.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_licenses.py index 47de92e388c5..794e8d7dd81b 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_licenses.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_licenses.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.licenses import LicensesClient +from google.cloud.compute_v1.services.licenses import pagers from google.cloud.compute_v1.services.licenses import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -468,7 +469,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -571,7 +572,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -677,7 +678,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -806,12 +807,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.License.to_json( - license_resource, including_default_value_fields=False + license_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -856,11 +859,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListLicensesReq response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.LicensesListResponse) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.License(charges_use_fee=True)] assert response.next_page_token == "next_page_token_value" @@ -895,7 +896,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -911,6 +912,53 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = LicensesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.LicensesListResponse( + items=[compute.License(), compute.License(), compute.License(),], + next_page_token="abc", + ), + compute.LicensesListResponse(items=[], next_page_token="def",), + compute.LicensesListResponse( + items=[compute.License(),], next_page_token="ghi", + ), + compute.LicensesListResponse( + items=[compute.License(), compute.License(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.LicensesListResponse.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.License) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_iam_policy_rest( transport: str = "rest", request_type=compute.SetIamPolicyLicenseRequest ): @@ -1003,14 +1051,16 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.GlobalSetPolicyRequest.to_json( - global_set_policy_request_resource, including_default_value_fields=False + global_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1097,14 +1147,16 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_machine_types.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_machine_types.py index d36d0b547515..b89798a9a655 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_machine_types.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_machine_types.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.machine_types import MachineTypesClient +from google.cloud.compute_v1.services.machine_types import pagers from google.cloud.compute_v1.services.machine_types import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -407,11 +408,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.MachineTypeAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.MachineTypesScopedList( @@ -456,7 +455,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -472,6 +471,72 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = MachineTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.MachineTypeAggregatedList( + items={ + "a": compute.MachineTypesScopedList(), + "b": compute.MachineTypesScopedList(), + "c": compute.MachineTypesScopedList(), + }, + next_page_token="abc", + ), + compute.MachineTypeAggregatedList(items={}, next_page_token="def",), + compute.MachineTypeAggregatedList( + items={"g": compute.MachineTypesScopedList(),}, next_page_token="ghi", + ), + compute.MachineTypeAggregatedList( + items={ + "h": compute.MachineTypesScopedList(), + "i": compute.MachineTypesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.MachineTypeAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.MachineTypesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.MachineTypesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.MachineTypesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_get_rest(transport: str = "rest", request_type=compute.GetMachineTypeRequest): client = MachineTypesClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -567,7 +632,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -625,11 +690,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.MachineTypeList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.MachineType( @@ -671,7 +734,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -691,6 +754,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = MachineTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.MachineTypeList( + items=[ + compute.MachineType(), + compute.MachineType(), + compute.MachineType(), + ], + next_page_token="abc", + ), + compute.MachineTypeList(items=[], next_page_token="def",), + compute.MachineTypeList( + items=[compute.MachineType(),], next_page_token="ghi", + ), + compute.MachineTypeList( + items=[compute.MachineType(), compute.MachineType(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.MachineTypeList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.MachineType) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.MachineTypesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_network_endpoint_groups.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_network_endpoint_groups.py index 432924e8e31c..e9016843ee3e 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_network_endpoint_groups.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_network_endpoint_groups.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.network_endpoint_groups import ( NetworkEndpointGroupsClient, ) +from google.cloud.compute_v1.services.network_endpoint_groups import pagers from google.cloud.compute_v1.services.network_endpoint_groups import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -446,11 +447,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NetworkEndpointGroupAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.NetworkEndpointGroupsScopedList( @@ -497,7 +496,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -516,6 +515,79 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = NetworkEndpointGroupsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NetworkEndpointGroupAggregatedList( + items={ + "a": compute.NetworkEndpointGroupsScopedList(), + "b": compute.NetworkEndpointGroupsScopedList(), + "c": compute.NetworkEndpointGroupsScopedList(), + }, + next_page_token="abc", + ), + compute.NetworkEndpointGroupAggregatedList( + items={}, next_page_token="def", + ), + compute.NetworkEndpointGroupAggregatedList( + items={"g": compute.NetworkEndpointGroupsScopedList(),}, + next_page_token="ghi", + ), + compute.NetworkEndpointGroupAggregatedList( + items={ + "h": compute.NetworkEndpointGroupsScopedList(), + "i": compute.NetworkEndpointGroupsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.NetworkEndpointGroupAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.NetworkEndpointGroupsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.NetworkEndpointGroupsScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.NetworkEndpointGroupsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_attach_network_endpoints_rest( transport: str = "rest", request_type=compute.AttachNetworkEndpointsNetworkEndpointGroupRequest, @@ -635,7 +707,7 @@ def test_attach_network_endpoints_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -646,6 +718,7 @@ def test_attach_network_endpoints_rest_flattened(): assert compute.NetworkEndpointGroupsAttachEndpointsRequest.to_json( network_endpoint_groups_attach_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -781,7 +854,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -925,7 +998,7 @@ def test_detach_network_endpoints_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -936,6 +1009,7 @@ def test_detach_network_endpoints_rest_flattened(): assert compute.NetworkEndpointGroupsDetachEndpointsRequest.to_json( network_endpoint_groups_detach_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1068,7 +1142,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1208,14 +1282,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.NetworkEndpointGroup.to_json( - network_endpoint_group_resource, including_default_value_fields=False + network_endpoint_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1270,11 +1346,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NetworkEndpointGroupList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.NetworkEndpointGroup(annotations={"key_value": "value_value"}) @@ -1316,7 +1390,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1338,6 +1412,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = NetworkEndpointGroupsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NetworkEndpointGroupList( + items=[ + compute.NetworkEndpointGroup(), + compute.NetworkEndpointGroup(), + compute.NetworkEndpointGroup(), + ], + next_page_token="abc", + ), + compute.NetworkEndpointGroupList(items=[], next_page_token="def",), + compute.NetworkEndpointGroupList( + items=[compute.NetworkEndpointGroup(),], next_page_token="ghi", + ), + compute.NetworkEndpointGroupList( + items=[compute.NetworkEndpointGroup(), compute.NetworkEndpointGroup(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NetworkEndpointGroupList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.NetworkEndpointGroup) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_network_endpoints_rest( transport: str = "rest", request_type=compute.ListNetworkEndpointsNetworkEndpointGroupsRequest, @@ -1381,11 +1508,9 @@ def test_list_network_endpoints_rest( response = client.list_network_endpoints(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NetworkEndpointGroupsListNetworkEndpoints) + assert isinstance(response, pagers.ListNetworkEndpointsPager) assert response.id == "id_value" assert response.items == [ compute.NetworkEndpointWithHealthStatus( @@ -1443,7 +1568,7 @@ def test_list_network_endpoints_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1454,6 +1579,7 @@ def test_list_network_endpoints_rest_flattened(): assert compute.NetworkEndpointGroupsListEndpointsRequest.to_json( network_endpoint_groups_list_endpoints_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1476,6 +1602,70 @@ def test_list_network_endpoints_rest_flattened_error(): ) +def test_list_network_endpoints_pager(): + client = NetworkEndpointGroupsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NetworkEndpointGroupsListNetworkEndpoints( + items=[ + compute.NetworkEndpointWithHealthStatus(), + compute.NetworkEndpointWithHealthStatus(), + compute.NetworkEndpointWithHealthStatus(), + ], + next_page_token="abc", + ), + compute.NetworkEndpointGroupsListNetworkEndpoints( + items=[], next_page_token="def", + ), + compute.NetworkEndpointGroupsListNetworkEndpoints( + items=[compute.NetworkEndpointWithHealthStatus(),], + next_page_token="ghi", + ), + compute.NetworkEndpointGroupsListNetworkEndpoints( + items=[ + compute.NetworkEndpointWithHealthStatus(), + compute.NetworkEndpointWithHealthStatus(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.NetworkEndpointGroupsListNetworkEndpoints.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 + + metadata = () + pager = client.list_network_endpoints(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all( + isinstance(i, compute.NetworkEndpointWithHealthStatus) for i in results + ) + + pages = list(client.list_network_endpoints(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_test_iam_permissions_rest( transport: str = "rest", request_type=compute.TestIamPermissionsNetworkEndpointGroupRequest, @@ -1547,7 +1737,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1556,7 +1746,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_networks.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_networks.py index 38ee8d502297..a2b70a6e2831 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_networks.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_networks.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.networks import NetworksClient +from google.cloud.compute_v1.services.networks import pagers from google.cloud.compute_v1.services.networks import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -474,14 +475,16 @@ def test_add_peering_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "network_value" in http_call[1] + str(body) assert compute.NetworksAddPeeringRequest.to_json( - networks_add_peering_request_resource, including_default_value_fields=False + networks_add_peering_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -608,7 +611,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -716,7 +719,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -845,12 +848,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.Network.to_json( - network_resource, including_default_value_fields=False + network_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -896,11 +901,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListNetworksReq response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NetworkList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.Network(auto_create_subnetworks=True)] assert response.kind == "kind_value" @@ -936,7 +939,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -952,6 +955,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = NetworksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NetworkList( + items=[compute.Network(), compute.Network(), compute.Network(),], + next_page_token="abc", + ), + compute.NetworkList(items=[], next_page_token="def",), + compute.NetworkList(items=[compute.Network(),], next_page_token="ghi",), + compute.NetworkList(items=[compute.Network(), compute.Network(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NetworkList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Network) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_peering_routes_rest( transport: str = "rest", request_type=compute.ListPeeringRoutesNetworksRequest ): @@ -983,11 +1029,9 @@ def test_list_peering_routes_rest( response = client.list_peering_routes(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ExchangedPeeringRoutesList) + assert isinstance(response, pagers.ListPeeringRoutesPager) assert response.id == "id_value" assert response.items == [ compute.ExchangedPeeringRoute(dest_range="dest_range_value") @@ -1027,7 +1071,7 @@ def test_list_peering_routes_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1047,6 +1091,62 @@ def test_list_peering_routes_rest_flattened_error(): ) +def test_list_peering_routes_pager(): + client = NetworksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ExchangedPeeringRoutesList( + items=[ + compute.ExchangedPeeringRoute(), + compute.ExchangedPeeringRoute(), + compute.ExchangedPeeringRoute(), + ], + next_page_token="abc", + ), + compute.ExchangedPeeringRoutesList(items=[], next_page_token="def",), + compute.ExchangedPeeringRoutesList( + items=[compute.ExchangedPeeringRoute(),], next_page_token="ghi", + ), + compute.ExchangedPeeringRoutesList( + items=[ + compute.ExchangedPeeringRoute(), + compute.ExchangedPeeringRoute(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.ExchangedPeeringRoutesList.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 + + metadata = () + pager = client.list_peering_routes(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.ExchangedPeeringRoute) for i in results) + + pages = list(client.list_peering_routes(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest(transport: str = "rest", request_type=compute.PatchNetworkRequest): client = NetworksClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -1156,14 +1256,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "network_value" in http_call[1] + str(body) assert compute.Network.to_json( - network_resource, including_default_value_fields=False + network_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1294,7 +1396,7 @@ def test_remove_peering_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1303,6 +1405,7 @@ def test_remove_peering_rest_flattened(): assert compute.NetworksRemovePeeringRequest.to_json( networks_remove_peering_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1429,7 +1532,7 @@ def test_switch_to_custom_mode_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1562,7 +1665,7 @@ def test_update_peering_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1571,6 +1674,7 @@ def test_update_peering_rest_flattened(): assert compute.NetworksUpdatePeeringRequest.to_json( networks_update_peering_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_groups.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_groups.py index 3102dc9ae394..0b02a199b2ed 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_groups.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_groups.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.node_groups import NodeGroupsClient +from google.cloud.compute_v1.services.node_groups import pagers from google.cloud.compute_v1.services.node_groups import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -479,7 +480,7 @@ def test_add_nodes_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -488,7 +489,9 @@ def test_add_nodes_rest_flattened(): assert "node_group_value" in http_call[1] + str(body) assert compute.NodeGroupsAddNodesRequest.to_json( - node_groups_add_nodes_request_resource, including_default_value_fields=False + node_groups_add_nodes_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -551,11 +554,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NodeGroupAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.NodeGroupsScopedList( @@ -600,7 +601,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -616,6 +617,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = NodeGroupsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NodeGroupAggregatedList( + items={ + "a": compute.NodeGroupsScopedList(), + "b": compute.NodeGroupsScopedList(), + "c": compute.NodeGroupsScopedList(), + }, + next_page_token="abc", + ), + compute.NodeGroupAggregatedList(items={}, next_page_token="def",), + compute.NodeGroupAggregatedList( + items={"g": compute.NodeGroupsScopedList(),}, next_page_token="ghi", + ), + compute.NodeGroupAggregatedList( + items={ + "h": compute.NodeGroupsScopedList(), + "i": compute.NodeGroupsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NodeGroupAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.NodeGroupsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.NodeGroupsScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.NodeGroupsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteNodeGroupRequest ): @@ -723,7 +787,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -860,7 +924,7 @@ def test_delete_nodes_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -871,6 +935,7 @@ def test_delete_nodes_rest_flattened(): assert compute.NodeGroupsDeleteNodesRequest.to_json( node_groups_delete_nodes_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -982,7 +1047,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1091,7 +1156,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1228,7 +1293,7 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1237,7 +1302,9 @@ def test_insert_rest_flattened(): assert str(1911) in http_call[1] + str(body) assert compute.NodeGroup.to_json( - node_group_resource, including_default_value_fields=False + node_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1291,11 +1358,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListNodeGroupsR response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NodeGroupList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.NodeGroup( @@ -1337,7 +1402,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1355,6 +1420,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = NodeGroupsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NodeGroupList( + items=[compute.NodeGroup(), compute.NodeGroup(), compute.NodeGroup(),], + next_page_token="abc", + ), + compute.NodeGroupList(items=[], next_page_token="def",), + compute.NodeGroupList(items=[compute.NodeGroup(),], next_page_token="ghi",), + compute.NodeGroupList(items=[compute.NodeGroup(), compute.NodeGroup(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NodeGroupList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.NodeGroup) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_nodes_rest( transport: str = "rest", request_type=compute.ListNodesNodeGroupsRequest ): @@ -1390,11 +1498,9 @@ def test_list_nodes_rest( response = client.list_nodes(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NodeGroupsListNodes) + assert isinstance(response, pagers.ListNodesPager) assert response.id == "id_value" assert response.items == [ compute.NodeGroupNode( @@ -1436,7 +1542,7 @@ def test_list_nodes_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1459,6 +1565,57 @@ def test_list_nodes_rest_flattened_error(): ) +def test_list_nodes_pager(): + client = NodeGroupsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NodeGroupsListNodes( + items=[ + compute.NodeGroupNode(), + compute.NodeGroupNode(), + compute.NodeGroupNode(), + ], + next_page_token="abc", + ), + compute.NodeGroupsListNodes(items=[], next_page_token="def",), + compute.NodeGroupsListNodes( + items=[compute.NodeGroupNode(),], next_page_token="ghi", + ), + compute.NodeGroupsListNodes( + items=[compute.NodeGroupNode(), compute.NodeGroupNode(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NodeGroupsListNodes.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 + + metadata = () + pager = client.list_nodes(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.NodeGroupNode) for i in results) + + pages = list(client.list_nodes(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchNodeGroupRequest ): @@ -1573,7 +1730,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1582,7 +1739,9 @@ def test_patch_rest_flattened(): assert "node_group_value" in http_call[1] + str(body) assert compute.NodeGroup.to_json( - node_group_resource, including_default_value_fields=False + node_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1696,7 +1855,7 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1705,7 +1864,9 @@ def test_set_iam_policy_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.ZoneSetPolicyRequest.to_json( - zone_set_policy_request_resource, including_default_value_fields=False + zone_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1840,7 +2001,7 @@ def test_set_node_template_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1851,6 +2012,7 @@ def test_set_node_template_rest_flattened(): assert compute.NodeGroupsSetNodeTemplateRequest.to_json( node_groups_set_node_template_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1939,7 +2101,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1948,7 +2110,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_templates.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_templates.py index d61646d4bffc..fc07d6298ee1 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_templates.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_templates.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.node_templates import NodeTemplatesClient +from google.cloud.compute_v1.services.node_templates import pagers from google.cloud.compute_v1.services.node_templates import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -414,11 +415,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NodeTemplateAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.NodeTemplatesScopedList( @@ -463,7 +462,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -479,6 +478,74 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = NodeTemplatesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NodeTemplateAggregatedList( + items={ + "a": compute.NodeTemplatesScopedList(), + "b": compute.NodeTemplatesScopedList(), + "c": compute.NodeTemplatesScopedList(), + }, + next_page_token="abc", + ), + compute.NodeTemplateAggregatedList(items={}, next_page_token="def",), + compute.NodeTemplateAggregatedList( + items={"g": compute.NodeTemplatesScopedList(),}, next_page_token="ghi", + ), + compute.NodeTemplateAggregatedList( + items={ + "h": compute.NodeTemplatesScopedList(), + "i": compute.NodeTemplatesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.NodeTemplateAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.NodeTemplatesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.NodeTemplatesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.NodeTemplatesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteNodeTemplateRequest ): @@ -588,7 +655,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -713,7 +780,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -822,7 +889,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -958,14 +1025,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.NodeTemplate.to_json( - node_template_resource, including_default_value_fields=False + node_template_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1020,11 +1089,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NodeTemplateList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.NodeTemplate( @@ -1066,7 +1133,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1086,6 +1153,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = NodeTemplatesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NodeTemplateList( + items=[ + compute.NodeTemplate(), + compute.NodeTemplate(), + compute.NodeTemplate(), + ], + next_page_token="abc", + ), + compute.NodeTemplateList(items=[], next_page_token="def",), + compute.NodeTemplateList( + items=[compute.NodeTemplate(),], next_page_token="ghi", + ), + compute.NodeTemplateList( + items=[compute.NodeTemplate(), compute.NodeTemplate(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NodeTemplateList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.NodeTemplate) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_iam_policy_rest( transport: str = "rest", request_type=compute.SetIamPolicyNodeTemplateRequest ): @@ -1179,7 +1297,7 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1188,7 +1306,9 @@ def test_set_iam_policy_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.RegionSetPolicyRequest.to_json( - region_set_policy_request_resource, including_default_value_fields=False + region_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1277,7 +1397,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1286,7 +1406,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_types.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_types.py index b8bb4c9d887d..63eb4e27ca4d 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_types.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_node_types.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.node_types import NodeTypesClient +from google.cloud.compute_v1.services.node_types import pagers from google.cloud.compute_v1.services.node_types import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -399,11 +400,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NodeTypeAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.NodeTypesScopedList( @@ -444,7 +443,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -460,6 +459,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = NodeTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NodeTypeAggregatedList( + items={ + "a": compute.NodeTypesScopedList(), + "b": compute.NodeTypesScopedList(), + "c": compute.NodeTypesScopedList(), + }, + next_page_token="abc", + ), + compute.NodeTypeAggregatedList(items={}, next_page_token="def",), + compute.NodeTypeAggregatedList( + items={"g": compute.NodeTypesScopedList(),}, next_page_token="ghi", + ), + compute.NodeTypeAggregatedList( + items={ + "h": compute.NodeTypesScopedList(), + "i": compute.NodeTypesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NodeTypeAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.NodeTypesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.NodeTypesScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.NodeTypesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_get_rest(transport: str = "rest", request_type=compute.GetNodeTypeRequest): client = NodeTypesClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -541,7 +603,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -593,11 +655,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListNodeTypesRe response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NodeTypeList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.NodeType(cpu_platform="cpu_platform_value")] assert response.kind == "kind_value" @@ -635,7 +695,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -653,6 +713,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = NodeTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NodeTypeList( + items=[compute.NodeType(), compute.NodeType(), compute.NodeType(),], + next_page_token="abc", + ), + compute.NodeTypeList(items=[], next_page_token="def",), + compute.NodeTypeList(items=[compute.NodeType(),], next_page_token="ghi",), + compute.NodeTypeList(items=[compute.NodeType(), compute.NodeType(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NodeTypeList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.NodeType) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.NodeTypesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_packet_mirrorings.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_packet_mirrorings.py index 29518be95fdf..2c0379dac5aa 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_packet_mirrorings.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_packet_mirrorings.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.packet_mirrorings import PacketMirroringsClient +from google.cloud.compute_v1.services.packet_mirrorings import pagers from google.cloud.compute_v1.services.packet_mirrorings import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -425,11 +426,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.PacketMirroringAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.PacketMirroringsScopedList( @@ -476,7 +475,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -492,6 +491,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = PacketMirroringsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.PacketMirroringAggregatedList( + items={ + "a": compute.PacketMirroringsScopedList(), + "b": compute.PacketMirroringsScopedList(), + "c": compute.PacketMirroringsScopedList(), + }, + next_page_token="abc", + ), + compute.PacketMirroringAggregatedList(items={}, next_page_token="def",), + compute.PacketMirroringAggregatedList( + items={"g": compute.PacketMirroringsScopedList(),}, + next_page_token="ghi", + ), + compute.PacketMirroringAggregatedList( + items={ + "h": compute.PacketMirroringsScopedList(), + "i": compute.PacketMirroringsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.PacketMirroringAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.PacketMirroringsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.PacketMirroringsScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.PacketMirroringsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeletePacketMirroringRequest ): @@ -601,7 +669,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -733,7 +801,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -871,14 +939,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.PacketMirroring.to_json( - packet_mirroring_resource, including_default_value_fields=False + packet_mirroring_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -937,11 +1007,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.PacketMirroringList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.PacketMirroring( @@ -985,7 +1053,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1005,6 +1073,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = PacketMirroringsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.PacketMirroringList( + items=[ + compute.PacketMirroring(), + compute.PacketMirroring(), + compute.PacketMirroring(), + ], + next_page_token="abc", + ), + compute.PacketMirroringList(items=[], next_page_token="def",), + compute.PacketMirroringList( + items=[compute.PacketMirroring(),], next_page_token="ghi", + ), + compute.PacketMirroringList( + items=[compute.PacketMirroring(), compute.PacketMirroring(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.PacketMirroringList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.PacketMirroring) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchPacketMirroringRequest ): @@ -1121,7 +1240,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1130,7 +1249,9 @@ def test_patch_rest_flattened(): assert "packet_mirroring_value" in http_call[1] + str(body) assert compute.PacketMirroring.to_json( - packet_mirroring_resource, including_default_value_fields=False + packet_mirroring_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1222,7 +1343,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1231,7 +1352,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_projects.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_projects.py index 69ed1c89d5fa..53972df759d6 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_projects.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_projects.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.projects import ProjectsClient +from google.cloud.compute_v1.services.projects import pagers from google.cloud.compute_v1.services.projects import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -466,7 +467,7 @@ def test_disable_xpn_host_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -594,13 +595,14 @@ def test_disable_xpn_resource_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.ProjectsDisableXpnResourceRequest.to_json( projects_disable_xpn_resource_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -724,7 +726,7 @@ def test_enable_xpn_host_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -852,13 +854,14 @@ def test_enable_xpn_resource_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.ProjectsEnableXpnResourceRequest.to_json( projects_enable_xpn_resource_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -964,7 +967,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1069,7 +1072,7 @@ def test_get_xpn_host_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1113,11 +1116,9 @@ def test_get_xpn_resources_rest( response = client.get_xpn_resources(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ProjectsGetXpnResources) + assert isinstance(response, pagers.GetXpnResourcesPager) assert response.kind == "kind_value" assert response.next_page_token == "next_page_token_value" assert response.resources == [compute.XpnResourceId(id="id_value")] @@ -1150,7 +1151,7 @@ def test_get_xpn_resources_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1166,6 +1167,57 @@ def test_get_xpn_resources_rest_flattened_error(): ) +def test_get_xpn_resources_pager(): + client = ProjectsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ProjectsGetXpnResources( + resources=[ + compute.XpnResourceId(), + compute.XpnResourceId(), + compute.XpnResourceId(), + ], + next_page_token="abc", + ), + compute.ProjectsGetXpnResources(resources=[], next_page_token="def",), + compute.ProjectsGetXpnResources( + resources=[compute.XpnResourceId(),], next_page_token="ghi", + ), + compute.ProjectsGetXpnResources( + resources=[compute.XpnResourceId(), compute.XpnResourceId(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ProjectsGetXpnResources.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 + + metadata = () + pager = client.get_xpn_resources(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.XpnResourceId) for i in results) + + pages = list(client.get_xpn_resources(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_xpn_hosts_rest( transport: str = "rest", request_type=compute.ListXpnHostsProjectsRequest ): @@ -1203,11 +1255,9 @@ def test_list_xpn_hosts_rest( response = client.list_xpn_hosts(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.XpnHostList) + assert isinstance(response, pagers.ListXpnHostsPager) assert response.id == "id_value" assert response.items == [ compute.Project( @@ -1254,13 +1304,14 @@ def test_list_xpn_hosts_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.ProjectsListXpnHostsRequest.to_json( projects_list_xpn_hosts_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1279,6 +1330,49 @@ def test_list_xpn_hosts_rest_flattened_error(): ) +def test_list_xpn_hosts_pager(): + client = ProjectsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.XpnHostList( + items=[compute.Project(), compute.Project(), compute.Project(),], + next_page_token="abc", + ), + compute.XpnHostList(items=[], next_page_token="def",), + compute.XpnHostList(items=[compute.Project(),], next_page_token="ghi",), + compute.XpnHostList(items=[compute.Project(), compute.Project(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.XpnHostList.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 + + metadata = () + pager = client.list_xpn_hosts(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Project) for i in results) + + pages = list(client.list_xpn_hosts(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_move_disk_rest( transport: str = "rest", request_type=compute.MoveDiskProjectRequest ): @@ -1391,12 +1485,14 @@ def test_move_disk_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.DiskMoveRequest.to_json( - disk_move_request_resource, including_default_value_fields=False + disk_move_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1527,12 +1623,14 @@ def test_move_instance_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.InstanceMoveRequest.to_json( - instance_move_request_resource, including_default_value_fields=False + instance_move_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1661,12 +1759,14 @@ def test_set_common_instance_metadata_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.Metadata.to_json( - metadata_resource, including_default_value_fields=False + metadata_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1795,13 +1895,14 @@ def test_set_default_network_tier_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.ProjectsSetDefaultNetworkTierRequest.to_json( projects_set_default_network_tier_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1932,12 +2033,14 @@ def test_set_usage_export_bucket_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.UsageExportLocation.to_json( - usage_export_location_resource, including_default_value_fields=False + usage_export_location_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_autoscalers.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_autoscalers.py index 9be54ad1e148..a13fddcd12d6 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_autoscalers.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_autoscalers.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.region_autoscalers import RegionAutoscalersClient +from google.cloud.compute_v1.services.region_autoscalers import pagers from google.cloud.compute_v1.services.region_autoscalers import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -492,7 +493,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -606,7 +607,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -742,14 +743,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.Autoscaler.to_json( - autoscaler_resource, including_default_value_fields=False + autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -806,11 +809,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RegionAutoscalerList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Autoscaler( @@ -852,7 +853,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -872,6 +873,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionAutoscalersClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionAutoscalerList( + items=[ + compute.Autoscaler(), + compute.Autoscaler(), + compute.Autoscaler(), + ], + next_page_token="abc", + ), + compute.RegionAutoscalerList(items=[], next_page_token="def",), + compute.RegionAutoscalerList( + items=[compute.Autoscaler(),], next_page_token="ghi", + ), + compute.RegionAutoscalerList( + items=[compute.Autoscaler(), compute.Autoscaler(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.RegionAutoscalerList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Autoscaler) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchRegionAutoscalerRequest ): @@ -985,14 +1037,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.Autoscaler.to_json( - autoscaler_resource, including_default_value_fields=False + autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1125,14 +1179,16 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.Autoscaler.to_json( - autoscaler_resource, including_default_value_fields=False + autoscaler_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_backend_services.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_backend_services.py index d03683d102de..1fb40fe1ab99 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_backend_services.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_backend_services.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_backend_services import ( RegionBackendServicesClient, ) +from google.cloud.compute_v1.services.region_backend_services import pagers from google.cloud.compute_v1.services.region_backend_services import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -514,7 +515,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -703,7 +704,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -806,7 +807,7 @@ def test_get_health_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -815,7 +816,9 @@ def test_get_health_rest_flattened(): assert "backend_service_value" in http_call[1] + str(body) assert compute.ResourceGroupReference.to_json( - resource_group_reference_resource, including_default_value_fields=False + resource_group_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -951,14 +954,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.BackendService.to_json( - backend_service_resource, including_default_value_fields=False + backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1011,11 +1016,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.BackendServiceList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.BackendService(affinity_cookie_ttl_sec=2432)] assert response.kind == "kind_value" @@ -1055,7 +1058,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1077,6 +1080,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionBackendServicesClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.BackendServiceList( + items=[ + compute.BackendService(), + compute.BackendService(), + compute.BackendService(), + ], + next_page_token="abc", + ), + compute.BackendServiceList(items=[], next_page_token="def",), + compute.BackendServiceList( + items=[compute.BackendService(),], next_page_token="ghi", + ), + compute.BackendServiceList( + items=[compute.BackendService(), compute.BackendService(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.BackendServiceList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.BackendService) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchRegionBackendServiceRequest ): @@ -1191,7 +1247,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1200,7 +1256,9 @@ def test_patch_rest_flattened(): assert "backend_service_value" in http_call[1] + str(body) assert compute.BackendService.to_json( - backend_service_resource, including_default_value_fields=False + backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1337,7 +1395,7 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1346,7 +1404,9 @@ def test_update_rest_flattened(): assert "backend_service_value" in http_call[1] + str(body) assert compute.BackendService.to_json( - backend_service_resource, including_default_value_fields=False + backend_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_commitments.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_commitments.py index b8f92545da25..7bb9d516f43f 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_commitments.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_commitments.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.region_commitments import RegionCommitmentsClient +from google.cloud.compute_v1.services.region_commitments import pagers from google.cloud.compute_v1.services.region_commitments import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -423,11 +424,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.CommitmentAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.CommitmentsScopedList( @@ -472,7 +471,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -488,6 +487,72 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = RegionCommitmentsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.CommitmentAggregatedList( + items={ + "a": compute.CommitmentsScopedList(), + "b": compute.CommitmentsScopedList(), + "c": compute.CommitmentsScopedList(), + }, + next_page_token="abc", + ), + compute.CommitmentAggregatedList(items={}, next_page_token="def",), + compute.CommitmentAggregatedList( + items={"g": compute.CommitmentsScopedList(),}, next_page_token="ghi", + ), + compute.CommitmentAggregatedList( + items={ + "h": compute.CommitmentsScopedList(), + "i": compute.CommitmentsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.CommitmentAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.CommitmentsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.CommitmentsScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.CommitmentsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_get_rest( transport: str = "rest", request_type=compute.GetRegionCommitmentRequest ): @@ -587,7 +652,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -723,14 +788,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.Commitment.to_json( - commitment_resource, including_default_value_fields=False + commitment_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -785,11 +852,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.CommitmentList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Commitment(category=compute.Commitment.Category.CATEGORY_UNSPECIFIED) @@ -829,7 +894,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -849,6 +914,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionCommitmentsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.CommitmentList( + items=[ + compute.Commitment(), + compute.Commitment(), + compute.Commitment(), + ], + next_page_token="abc", + ), + compute.CommitmentList(items=[], next_page_token="def",), + compute.CommitmentList( + items=[compute.Commitment(),], next_page_token="ghi", + ), + compute.CommitmentList( + items=[compute.Commitment(), compute.Commitment(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.CommitmentList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Commitment) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.RegionCommitmentsRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_disk_types.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_disk_types.py index 209c7acc4deb..b43e39df9813 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_disk_types.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_disk_types.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.region_disk_types import RegionDiskTypesClient +from google.cloud.compute_v1.services.region_disk_types import pagers from google.cloud.compute_v1.services.region_disk_types import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -463,7 +464,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -517,11 +518,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RegionDiskTypeList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.DiskType(creation_timestamp="creation_timestamp_value") @@ -561,7 +560,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -581,6 +580,53 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionDiskTypesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionDiskTypeList( + items=[compute.DiskType(), compute.DiskType(), compute.DiskType(),], + next_page_token="abc", + ), + compute.RegionDiskTypeList(items=[], next_page_token="def",), + compute.RegionDiskTypeList( + items=[compute.DiskType(),], next_page_token="ghi", + ), + compute.RegionDiskTypeList( + items=[compute.DiskType(), compute.DiskType(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.RegionDiskTypeList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.DiskType) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.RegionDiskTypesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_disks.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_disks.py index d6637cd4510d..3dac15c2468b 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_disks.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_disks.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.region_disks import RegionDisksClient +from google.cloud.compute_v1.services.region_disks import pagers from google.cloud.compute_v1.services.region_disks import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -479,7 +480,7 @@ def test_add_resource_policies_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -490,6 +491,7 @@ def test_add_resource_policies_rest_flattened(): assert compute.RegionDisksAddResourcePoliciesRequest.to_json( region_disks_add_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -622,7 +624,7 @@ def test_create_snapshot_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -631,7 +633,9 @@ def test_create_snapshot_rest_flattened(): assert "disk_value" in http_call[1] + str(body) assert compute.Snapshot.to_json( - snapshot_resource, including_default_value_fields=False + snapshot_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -757,7 +761,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -921,7 +925,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1030,7 +1034,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1162,14 +1166,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.Disk.to_json( - disk_resource, including_default_value_fields=False + disk_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1218,11 +1224,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.DiskList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Disk(creation_timestamp="creation_timestamp_value") @@ -1262,7 +1266,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1282,6 +1286,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionDisksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.DiskList( + items=[compute.Disk(), compute.Disk(), compute.Disk(),], + next_page_token="abc", + ), + compute.DiskList(items=[], next_page_token="def",), + compute.DiskList(items=[compute.Disk(),], next_page_token="ghi",), + compute.DiskList(items=[compute.Disk(), compute.Disk(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.DiskList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Disk) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_remove_resource_policies_rest( transport: str = "rest", request_type=compute.RemoveResourcePoliciesRegionDiskRequest, @@ -1397,7 +1444,7 @@ def test_remove_resource_policies_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1408,6 +1455,7 @@ def test_remove_resource_policies_rest_flattened(): assert compute.RegionDisksRemoveResourcePoliciesRequest.to_json( region_disks_remove_resource_policies_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1542,7 +1590,7 @@ def test_resize_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1551,7 +1599,9 @@ def test_resize_rest_flattened(): assert "disk_value" in http_call[1] + str(body) assert compute.RegionDisksResizeRequest.to_json( - region_disks_resize_request_resource, including_default_value_fields=False + region_disks_resize_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1665,7 +1715,7 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1674,7 +1724,9 @@ def test_set_iam_policy_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.RegionSetPolicyRequest.to_json( - region_set_policy_request_resource, including_default_value_fields=False + region_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1809,7 +1861,7 @@ def test_set_labels_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1818,7 +1870,9 @@ def test_set_labels_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.RegionSetLabelsRequest.to_json( - region_set_labels_request_resource, including_default_value_fields=False + region_set_labels_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1907,7 +1961,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1916,7 +1970,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_health_check_services.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_health_check_services.py index 7e84f76dae8c..320685c273c6 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_health_check_services.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_health_check_services.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_health_check_services import ( RegionHealthCheckServicesClient, ) +from google.cloud.compute_v1.services.region_health_check_services import pagers from google.cloud.compute_v1.services.region_health_check_services import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -520,7 +521,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -635,7 +636,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -775,14 +776,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.HealthCheckService.to_json( - health_check_service_resource, including_default_value_fields=False + health_check_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -839,11 +842,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.HealthCheckServicesList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.HealthCheckService(creation_timestamp="creation_timestamp_value") @@ -885,7 +886,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -907,6 +908,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionHealthCheckServicesClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.HealthCheckServicesList( + items=[ + compute.HealthCheckService(), + compute.HealthCheckService(), + compute.HealthCheckService(), + ], + next_page_token="abc", + ), + compute.HealthCheckServicesList(items=[], next_page_token="def",), + compute.HealthCheckServicesList( + items=[compute.HealthCheckService(),], next_page_token="ghi", + ), + compute.HealthCheckServicesList( + items=[compute.HealthCheckService(), compute.HealthCheckService(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.HealthCheckServicesList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.HealthCheckService) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchRegionHealthCheckServiceRequest ): @@ -1023,7 +1077,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1032,7 +1086,9 @@ def test_patch_rest_flattened(): assert "health_check_service_value" in http_call[1] + str(body) assert compute.HealthCheckService.to_json( - health_check_service_resource, including_default_value_fields=False + health_check_service_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_health_checks.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_health_checks.py index 3a0ce9e34b7a..dbe1162c1de1 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_health_checks.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_health_checks.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_health_checks import ( RegionHealthChecksClient, ) +from google.cloud.compute_v1.services.region_health_checks import pagers from google.cloud.compute_v1.services.region_health_checks import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -494,7 +495,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -620,7 +621,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -754,14 +755,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.HealthCheck.to_json( - health_check_resource, including_default_value_fields=False + health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -810,11 +813,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.HealthCheckList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.HealthCheck(check_interval_sec=1884)] assert response.kind == "kind_value" @@ -852,7 +853,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -872,6 +873,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionHealthChecksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.HealthCheckList( + items=[ + compute.HealthCheck(), + compute.HealthCheck(), + compute.HealthCheck(), + ], + next_page_token="abc", + ), + compute.HealthCheckList(items=[], next_page_token="def",), + compute.HealthCheckList( + items=[compute.HealthCheck(),], next_page_token="ghi", + ), + compute.HealthCheckList( + items=[compute.HealthCheck(), compute.HealthCheck(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.HealthCheckList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.HealthCheck) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchRegionHealthCheckRequest ): @@ -984,7 +1036,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -993,7 +1045,9 @@ def test_patch_rest_flattened(): assert "health_check_value" in http_call[1] + str(body) assert compute.HealthCheck.to_json( - health_check_resource, including_default_value_fields=False + health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1124,7 +1178,7 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1133,7 +1187,9 @@ def test_update_rest_flattened(): assert "health_check_value" in http_call[1] + str(body) assert compute.HealthCheck.to_json( - health_check_resource, including_default_value_fields=False + health_check_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_instance_group_managers.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_instance_group_managers.py index 4c24ad0f888a..abf5c43bdb37 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_instance_group_managers.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_instance_group_managers.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_instance_group_managers import ( RegionInstanceGroupManagersClient, ) +from google.cloud.compute_v1.services.region_instance_group_managers import pagers from google.cloud.compute_v1.services.region_instance_group_managers import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -526,7 +527,7 @@ def test_abandon_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -537,6 +538,7 @@ def test_abandon_instances_rest_flattened(): assert compute.RegionInstanceGroupManagersAbandonInstancesRequest.to_json( region_instance_group_managers_abandon_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -676,7 +678,7 @@ def test_apply_updates_to_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -687,6 +689,7 @@ def test_apply_updates_to_instances_rest_flattened(): assert compute.RegionInstanceGroupManagersApplyUpdatesRequest.to_json( region_instance_group_managers_apply_updates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -826,7 +829,7 @@ def test_create_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -837,6 +840,7 @@ def test_create_instances_rest_flattened(): assert compute.RegionInstanceGroupManagersCreateInstancesRequest.to_json( region_instance_group_managers_create_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -971,7 +975,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1113,7 +1117,7 @@ def test_delete_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1124,6 +1128,7 @@ def test_delete_instances_rest_flattened(): assert compute.RegionInstanceGroupManagersDeleteInstancesRequest.to_json( region_instance_group_managers_delete_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1263,7 +1268,7 @@ def test_delete_per_instance_configs_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1274,6 +1279,7 @@ def test_delete_per_instance_configs_rest_flattened(): assert compute.RegionInstanceGroupManagerDeleteInstanceConfigReq.to_json( region_instance_group_manager_delete_instance_config_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1443,7 +1449,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1588,14 +1594,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.InstanceGroupManager.to_json( - instance_group_manager_resource, including_default_value_fields=False + instance_group_manager_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1660,11 +1668,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RegionInstanceGroupManagerList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.InstanceGroupManager( @@ -1712,7 +1718,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1734,6 +1740,61 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionInstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionInstanceGroupManagerList( + items=[ + compute.InstanceGroupManager(), + compute.InstanceGroupManager(), + compute.InstanceGroupManager(), + ], + next_page_token="abc", + ), + compute.RegionInstanceGroupManagerList(items=[], next_page_token="def",), + compute.RegionInstanceGroupManagerList( + items=[compute.InstanceGroupManager(),], next_page_token="ghi", + ), + compute.RegionInstanceGroupManagerList( + items=[compute.InstanceGroupManager(), compute.InstanceGroupManager(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.RegionInstanceGroupManagerList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceGroupManager) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_errors_rest( transport: str = "rest", request_type=compute.ListErrorsRegionInstanceGroupManagersRequest, @@ -1770,11 +1831,9 @@ def test_list_errors_rest( response = client.list_errors(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RegionInstanceGroupManagersListErrorsResponse) + assert isinstance(response, pagers.ListErrorsPager) assert response.items == [ compute.InstanceManagedByIgmError( error=compute.InstanceManagedByIgmErrorManagedInstanceError( @@ -1820,7 +1879,7 @@ def test_list_errors_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1845,6 +1904,67 @@ def test_list_errors_rest_flattened_error(): ) +def test_list_errors_pager(): + client = RegionInstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionInstanceGroupManagersListErrorsResponse( + items=[ + compute.InstanceManagedByIgmError(), + compute.InstanceManagedByIgmError(), + compute.InstanceManagedByIgmError(), + ], + next_page_token="abc", + ), + compute.RegionInstanceGroupManagersListErrorsResponse( + items=[], next_page_token="def", + ), + compute.RegionInstanceGroupManagersListErrorsResponse( + items=[compute.InstanceManagedByIgmError(),], next_page_token="ghi", + ), + compute.RegionInstanceGroupManagersListErrorsResponse( + items=[ + compute.InstanceManagedByIgmError(), + compute.InstanceManagedByIgmError(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.RegionInstanceGroupManagersListErrorsResponse.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 + + metadata = () + pager = client.list_errors(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceManagedByIgmError) for i in results) + + pages = list(client.list_errors(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_managed_instances_rest( transport: str = "rest", request_type=compute.ListManagedInstancesRegionInstanceGroupManagersRequest, @@ -1879,13 +1999,9 @@ def test_list_managed_instances_rest( response = client.list_managed_instances(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance( - response, compute.RegionInstanceGroupManagersListInstancesResponse - ) + assert isinstance(response, pagers.ListManagedInstancesPager) assert response.managed_instances == [ compute.ManagedInstance( current_action=compute.ManagedInstance.CurrentAction.ABANDONING @@ -1929,7 +2045,7 @@ def test_list_managed_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1954,6 +2070,67 @@ def test_list_managed_instances_rest_flattened_error(): ) +def test_list_managed_instances_pager(): + client = RegionInstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionInstanceGroupManagersListInstancesResponse( + managed_instances=[ + compute.ManagedInstance(), + compute.ManagedInstance(), + compute.ManagedInstance(), + ], + next_page_token="abc", + ), + compute.RegionInstanceGroupManagersListInstancesResponse( + managed_instances=[], next_page_token="def", + ), + compute.RegionInstanceGroupManagersListInstancesResponse( + managed_instances=[compute.ManagedInstance(),], next_page_token="ghi", + ), + compute.RegionInstanceGroupManagersListInstancesResponse( + managed_instances=[ + compute.ManagedInstance(), + compute.ManagedInstance(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.RegionInstanceGroupManagersListInstancesResponse.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 + + metadata = () + pager = client.list_managed_instances(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.ManagedInstance) for i in results) + + pages = list(client.list_managed_instances(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_per_instance_configs_rest( transport: str = "rest", request_type=compute.ListPerInstanceConfigsRegionInstanceGroupManagersRequest, @@ -1985,13 +2162,9 @@ def test_list_per_instance_configs_rest( response = client.list_per_instance_configs(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance( - response, compute.RegionInstanceGroupManagersListInstanceConfigsResp - ) + assert isinstance(response, pagers.ListPerInstanceConfigsPager) assert response.items == [ compute.PerInstanceConfig(fingerprint="fingerprint_value") ] @@ -2034,7 +2207,7 @@ def test_list_per_instance_configs_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2059,6 +2232,64 @@ def test_list_per_instance_configs_rest_flattened_error(): ) +def test_list_per_instance_configs_pager(): + client = RegionInstanceGroupManagersClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionInstanceGroupManagersListInstanceConfigsResp( + items=[ + compute.PerInstanceConfig(), + compute.PerInstanceConfig(), + compute.PerInstanceConfig(), + ], + next_page_token="abc", + ), + compute.RegionInstanceGroupManagersListInstanceConfigsResp( + items=[], next_page_token="def", + ), + compute.RegionInstanceGroupManagersListInstanceConfigsResp( + items=[compute.PerInstanceConfig(),], next_page_token="ghi", + ), + compute.RegionInstanceGroupManagersListInstanceConfigsResp( + items=[compute.PerInstanceConfig(), compute.PerInstanceConfig(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.RegionInstanceGroupManagersListInstanceConfigsResp.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 + + metadata = () + pager = client.list_per_instance_configs(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.PerInstanceConfig) for i in results) + + pages = list(client.list_per_instance_configs(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchRegionInstanceGroupManagerRequest ): @@ -2179,7 +2410,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2188,7 +2419,9 @@ def test_patch_rest_flattened(): assert "instance_group_manager_value" in http_call[1] + str(body) assert compute.InstanceGroupManager.to_json( - instance_group_manager_resource, including_default_value_fields=False + instance_group_manager_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2334,7 +2567,7 @@ def test_patch_per_instance_configs_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2345,6 +2578,7 @@ def test_patch_per_instance_configs_rest_flattened(): assert compute.RegionInstanceGroupManagerPatchInstanceConfigReq.to_json( region_instance_group_manager_patch_instance_config_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2486,7 +2720,7 @@ def test_recreate_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2497,6 +2731,7 @@ def test_recreate_instances_rest_flattened(): assert compute.RegionInstanceGroupManagersRecreateRequest.to_json( region_instance_group_managers_recreate_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2632,7 +2867,7 @@ def test_resize_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2777,7 +3012,7 @@ def test_set_instance_template_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2788,6 +3023,7 @@ def test_set_instance_template_rest_flattened(): assert compute.RegionInstanceGroupManagersSetTemplateRequest.to_json( region_instance_group_managers_set_template_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -2927,7 +3163,7 @@ def test_set_target_pools_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -2938,6 +3174,7 @@ def test_set_target_pools_rest_flattened(): assert compute.RegionInstanceGroupManagersSetTargetPoolsRequest.to_json( region_instance_group_managers_set_target_pools_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -3079,7 +3316,7 @@ def test_update_per_instance_configs_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -3090,6 +3327,7 @@ def test_update_per_instance_configs_rest_flattened(): assert compute.RegionInstanceGroupManagerUpdateInstanceConfigReq.to_json( region_instance_group_manager_update_instance_config_req_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_instance_groups.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_instance_groups.py index a027673d534d..099eeeced09d 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_instance_groups.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_instance_groups.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_instance_groups import ( RegionInstanceGroupsClient, ) +from google.cloud.compute_v1.services.region_instance_groups import pagers from google.cloud.compute_v1.services.region_instance_groups import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -490,7 +491,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -546,11 +547,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RegionInstanceGroupList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.InstanceGroup(creation_timestamp="creation_timestamp_value") @@ -590,7 +589,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -610,6 +609,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionInstanceGroupsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionInstanceGroupList( + items=[ + compute.InstanceGroup(), + compute.InstanceGroup(), + compute.InstanceGroup(), + ], + next_page_token="abc", + ), + compute.RegionInstanceGroupList(items=[], next_page_token="def",), + compute.RegionInstanceGroupList( + items=[compute.InstanceGroup(),], next_page_token="ghi", + ), + compute.RegionInstanceGroupList( + items=[compute.InstanceGroup(), compute.InstanceGroup(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.RegionInstanceGroupList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceGroup) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_instances_rest( transport: str = "rest", request_type=compute.ListInstancesRegionInstanceGroupsRequest, @@ -644,11 +694,9 @@ def test_list_instances_rest( response = client.list_instances(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RegionInstanceGroupsListInstances) + assert isinstance(response, pagers.ListInstancesPager) assert response.id == "id_value" assert response.items == [compute.InstanceWithNamedPorts(instance="instance_value")] assert response.kind == "kind_value" @@ -695,7 +743,7 @@ def test_list_instances_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -706,6 +754,7 @@ def test_list_instances_rest_flattened(): assert compute.RegionInstanceGroupsListInstancesRequest.to_json( region_instance_groups_list_instances_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -726,6 +775,62 @@ def test_list_instances_rest_flattened_error(): ) +def test_list_instances_pager(): + client = RegionInstanceGroupsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionInstanceGroupsListInstances( + items=[ + compute.InstanceWithNamedPorts(), + compute.InstanceWithNamedPorts(), + compute.InstanceWithNamedPorts(), + ], + next_page_token="abc", + ), + compute.RegionInstanceGroupsListInstances(items=[], next_page_token="def",), + compute.RegionInstanceGroupsListInstances( + items=[compute.InstanceWithNamedPorts(),], next_page_token="ghi", + ), + compute.RegionInstanceGroupsListInstances( + items=[ + compute.InstanceWithNamedPorts(), + compute.InstanceWithNamedPorts(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.RegionInstanceGroupsListInstances.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 + + metadata = () + pager = client.list_instances(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.InstanceWithNamedPorts) for i in results) + + pages = list(client.list_instances(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_named_ports_rest( transport: str = "rest", request_type=compute.SetNamedPortsRegionInstanceGroupRequest, @@ -841,7 +946,7 @@ def test_set_named_ports_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -852,6 +957,7 @@ def test_set_named_ports_rest_flattened(): assert compute.RegionInstanceGroupsSetNamedPortsRequest.to_json( region_instance_groups_set_named_ports_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_network_endpoint_groups.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_network_endpoint_groups.py index 98cf4a8cf5d0..4b095591e072 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_network_endpoint_groups.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_network_endpoint_groups.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_network_endpoint_groups import ( RegionNetworkEndpointGroupsClient, ) +from google.cloud.compute_v1.services.region_network_endpoint_groups import pagers from google.cloud.compute_v1.services.region_network_endpoint_groups import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -521,7 +522,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -654,7 +655,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -795,14 +796,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.NetworkEndpointGroup.to_json( - network_endpoint_group_resource, including_default_value_fields=False + network_endpoint_group_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -857,11 +860,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NetworkEndpointGroupList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.NetworkEndpointGroup(annotations={"key_value": "value_value"}) @@ -903,7 +904,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -925,6 +926,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionNetworkEndpointGroupsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NetworkEndpointGroupList( + items=[ + compute.NetworkEndpointGroup(), + compute.NetworkEndpointGroup(), + compute.NetworkEndpointGroup(), + ], + next_page_token="abc", + ), + compute.NetworkEndpointGroupList(items=[], next_page_token="def",), + compute.NetworkEndpointGroupList( + items=[compute.NetworkEndpointGroup(),], next_page_token="ghi", + ), + compute.NetworkEndpointGroupList( + items=[compute.NetworkEndpointGroup(), compute.NetworkEndpointGroup(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NetworkEndpointGroupList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.NetworkEndpointGroup) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.RegionNetworkEndpointGroupsRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_notification_endpoints.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_notification_endpoints.py index 22bd4a9979f8..940d8730e024 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_notification_endpoints.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_notification_endpoints.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_notification_endpoints import ( RegionNotificationEndpointsClient, ) +from google.cloud.compute_v1.services.region_notification_endpoints import pagers from google.cloud.compute_v1.services.region_notification_endpoints import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -521,7 +522,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -629,7 +630,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -770,14 +771,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.NotificationEndpoint.to_json( - notification_endpoint_resource, including_default_value_fields=False + notification_endpoint_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -834,11 +837,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.NotificationEndpointList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.NotificationEndpoint(creation_timestamp="creation_timestamp_value") @@ -880,7 +881,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -902,6 +903,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionNotificationEndpointsClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.NotificationEndpointList( + items=[ + compute.NotificationEndpoint(), + compute.NotificationEndpoint(), + compute.NotificationEndpoint(), + ], + next_page_token="abc", + ), + compute.NotificationEndpointList(items=[], next_page_token="def",), + compute.NotificationEndpointList( + items=[compute.NotificationEndpoint(),], next_page_token="ghi", + ), + compute.NotificationEndpointList( + items=[compute.NotificationEndpoint(), compute.NotificationEndpoint(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.NotificationEndpointList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.NotificationEndpoint) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.RegionNotificationEndpointsRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_operations.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_operations.py index 9c5e42b94b09..5ce7bdf38dc2 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_operations.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_operations.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.region_operations import RegionOperationsClient +from google.cloud.compute_v1.services.region_operations import pagers from google.cloud.compute_v1.services.region_operations import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -441,7 +442,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -571,7 +572,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -625,11 +626,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.OperationList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Operation(client_operation_id="client_operation_id_value") @@ -669,7 +668,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -689,6 +688,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionOperationsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.OperationList( + items=[compute.Operation(), compute.Operation(), compute.Operation(),], + next_page_token="abc", + ), + compute.OperationList(items=[], next_page_token="def",), + compute.OperationList(items=[compute.Operation(),], next_page_token="ghi",), + compute.OperationList(items=[compute.Operation(), compute.Operation(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.OperationList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Operation) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_wait_rest( transport: str = "rest", request_type=compute.WaitRegionOperationRequest ): @@ -796,7 +838,7 @@ def test_wait_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_ssl_certificates.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_ssl_certificates.py index d6cbdc5f4bac..0deb0805d11d 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_ssl_certificates.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_ssl_certificates.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_ssl_certificates import ( RegionSslCertificatesClient, ) +from google.cloud.compute_v1.services.region_ssl_certificates import pagers from google.cloud.compute_v1.services.region_ssl_certificates import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -514,7 +515,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -638,7 +639,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -778,14 +779,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.SslCertificate.to_json( - ssl_certificate_resource, including_default_value_fields=False + ssl_certificate_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -838,11 +841,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.SslCertificateList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.SslCertificate(certificate="certificate_value")] assert response.kind == "kind_value" @@ -882,7 +883,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -904,6 +905,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionSslCertificatesClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.SslCertificateList( + items=[ + compute.SslCertificate(), + compute.SslCertificate(), + compute.SslCertificate(), + ], + next_page_token="abc", + ), + compute.SslCertificateList(items=[], next_page_token="def",), + compute.SslCertificateList( + items=[compute.SslCertificate(),], next_page_token="ghi", + ), + compute.SslCertificateList( + items=[compute.SslCertificate(), compute.SslCertificate(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.SslCertificateList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.SslCertificate) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.RegionSslCertificatesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_target_http_proxies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_target_http_proxies.py index 76c24db17a90..d2235532d2ef 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_target_http_proxies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_target_http_proxies.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_target_http_proxies import ( RegionTargetHttpProxiesClient, ) +from google.cloud.compute_v1.services.region_target_http_proxies import pagers from google.cloud.compute_v1.services.region_target_http_proxies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -514,7 +515,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -623,7 +624,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -763,14 +764,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.TargetHttpProxy.to_json( - target_http_proxy_resource, including_default_value_fields=False + target_http_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -825,11 +828,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetHttpProxyList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetHttpProxy(creation_timestamp="creation_timestamp_value") @@ -871,7 +872,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -893,6 +894,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionTargetHttpProxiesClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetHttpProxyList( + items=[ + compute.TargetHttpProxy(), + compute.TargetHttpProxy(), + compute.TargetHttpProxy(), + ], + next_page_token="abc", + ), + compute.TargetHttpProxyList(items=[], next_page_token="def",), + compute.TargetHttpProxyList( + items=[compute.TargetHttpProxy(),], next_page_token="ghi", + ), + compute.TargetHttpProxyList( + items=[compute.TargetHttpProxy(), compute.TargetHttpProxy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetHttpProxyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetHttpProxy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_url_map_rest( transport: str = "rest", request_type=compute.SetUrlMapRegionTargetHttpProxyRequest ): @@ -1007,7 +1061,7 @@ def test_set_url_map_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1016,7 +1070,9 @@ def test_set_url_map_rest_flattened(): assert "target_http_proxy_value" in http_call[1] + str(body) assert compute.UrlMapReference.to_json( - url_map_reference_resource, including_default_value_fields=False + url_map_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_target_https_proxies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_target_https_proxies.py index aaa5bf7b105e..df3cab9b9ecd 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_target_https_proxies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_target_https_proxies.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.region_target_https_proxies import ( RegionTargetHttpsProxiesClient, ) +from google.cloud.compute_v1.services.region_target_https_proxies import pagers from google.cloud.compute_v1.services.region_target_https_proxies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -518,7 +519,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -635,7 +636,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -775,14 +776,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.TargetHttpsProxy.to_json( - target_https_proxy_resource, including_default_value_fields=False + target_https_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -839,11 +842,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetHttpsProxyList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetHttpsProxy(authorization_policy="authorization_policy_value") @@ -885,7 +886,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -907,6 +908,59 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionTargetHttpsProxiesClient( + credentials=credentials.AnonymousCredentials(), + ) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetHttpsProxyList( + items=[ + compute.TargetHttpsProxy(), + compute.TargetHttpsProxy(), + compute.TargetHttpsProxy(), + ], + next_page_token="abc", + ), + compute.TargetHttpsProxyList(items=[], next_page_token="def",), + compute.TargetHttpsProxyList( + items=[compute.TargetHttpsProxy(),], next_page_token="ghi", + ), + compute.TargetHttpsProxyList( + items=[compute.TargetHttpsProxy(), compute.TargetHttpsProxy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetHttpsProxyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetHttpsProxy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_ssl_certificates_rest( transport: str = "rest", request_type=compute.SetSslCertificatesRegionTargetHttpsProxyRequest, @@ -1024,7 +1078,7 @@ def test_set_ssl_certificates_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1035,6 +1089,7 @@ def test_set_ssl_certificates_rest_flattened(): assert compute.RegionTargetHttpsProxiesSetSslCertificatesRequest.to_json( region_target_https_proxies_set_ssl_certificates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1171,7 +1226,7 @@ def test_set_url_map_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1180,7 +1235,9 @@ def test_set_url_map_rest_flattened(): assert "target_https_proxy_value" in http_call[1] + str(body) assert compute.UrlMapReference.to_json( - url_map_reference_resource, including_default_value_fields=False + url_map_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_url_maps.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_url_maps.py index 7616cd3f0e55..41132a284587 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_url_maps.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_region_url_maps.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.region_url_maps import RegionUrlMapsClient +from google.cloud.compute_v1.services.region_url_maps import pagers from google.cloud.compute_v1.services.region_url_maps import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -479,7 +480,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -617,7 +618,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -751,14 +752,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.UrlMap.to_json( - url_map_resource, including_default_value_fields=False + url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -809,11 +812,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.UrlMapList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.UrlMap(creation_timestamp="creation_timestamp_value") @@ -853,7 +854,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -873,6 +874,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionUrlMapsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.UrlMapList( + items=[compute.UrlMap(), compute.UrlMap(), compute.UrlMap(),], + next_page_token="abc", + ), + compute.UrlMapList(items=[], next_page_token="def",), + compute.UrlMapList(items=[compute.UrlMap(),], next_page_token="ghi",), + compute.UrlMapList(items=[compute.UrlMap(), compute.UrlMap(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.UrlMapList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.UrlMap) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchRegionUrlMapRequest ): @@ -985,7 +1029,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -994,7 +1038,9 @@ def test_patch_rest_flattened(): assert "url_map_value" in http_call[1] + str(body) assert compute.UrlMap.to_json( - url_map_resource, including_default_value_fields=False + url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1127,7 +1173,7 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1136,7 +1182,9 @@ def test_update_rest_flattened(): assert "url_map_value" in http_call[1] + str(body) assert compute.UrlMap.to_json( - url_map_resource, including_default_value_fields=False + url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1227,7 +1275,7 @@ def test_validate_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1238,6 +1286,7 @@ def test_validate_rest_flattened(): assert compute.RegionUrlMapsValidateRequest.to_json( region_url_maps_validate_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_regions.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_regions.py index 4956dee38d28..35598ef81d94 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_regions.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_regions.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.regions import RegionsClient +from google.cloud.compute_v1.services.regions import pagers from google.cloud.compute_v1.services.regions import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -437,7 +438,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -484,11 +485,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListRegionsRequ response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RegionList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Region(creation_timestamp="creation_timestamp_value") @@ -526,7 +525,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -542,6 +541,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RegionsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RegionList( + items=[compute.Region(), compute.Region(), compute.Region(),], + next_page_token="abc", + ), + compute.RegionList(items=[], next_page_token="def",), + compute.RegionList(items=[compute.Region(),], next_page_token="ghi",), + compute.RegionList(items=[compute.Region(), compute.Region(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.RegionList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Region) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.RegionsRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_reservations.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_reservations.py index 1e7c60bbaf3e..7779697b620f 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_reservations.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_reservations.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.reservations import ReservationsClient +from google.cloud.compute_v1.services.reservations import pagers from google.cloud.compute_v1.services.reservations import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -401,11 +402,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ReservationAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.ReservationsScopedList( @@ -446,7 +445,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -462,6 +461,72 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = ReservationsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ReservationAggregatedList( + items={ + "a": compute.ReservationsScopedList(), + "b": compute.ReservationsScopedList(), + "c": compute.ReservationsScopedList(), + }, + next_page_token="abc", + ), + compute.ReservationAggregatedList(items={}, next_page_token="def",), + compute.ReservationAggregatedList( + items={"g": compute.ReservationsScopedList(),}, next_page_token="ghi", + ), + compute.ReservationAggregatedList( + items={ + "h": compute.ReservationsScopedList(), + "i": compute.ReservationsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ReservationAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.ReservationsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.ReservationsScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.ReservationsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteReservationRequest ): @@ -569,7 +634,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -676,7 +741,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -785,7 +850,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -919,14 +984,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.Reservation.to_json( - reservation_resource, including_default_value_fields=False + reservation_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -975,11 +1042,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ReservationList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.Reservation(commitment="commitment_value")] assert response.kind == "kind_value" @@ -1017,7 +1082,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1037,6 +1102,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = ReservationsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ReservationList( + items=[ + compute.Reservation(), + compute.Reservation(), + compute.Reservation(), + ], + next_page_token="abc", + ), + compute.ReservationList(items=[], next_page_token="def",), + compute.ReservationList( + items=[compute.Reservation(),], next_page_token="ghi", + ), + compute.ReservationList( + items=[compute.Reservation(), compute.Reservation(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ReservationList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Reservation) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_resize_rest( transport: str = "rest", request_type=compute.ResizeReservationRequest ): @@ -1151,7 +1267,7 @@ def test_resize_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1160,7 +1276,9 @@ def test_resize_rest_flattened(): assert "reservation_value" in http_call[1] + str(body) assert compute.ReservationsResizeRequest.to_json( - reservations_resize_request_resource, including_default_value_fields=False + reservations_resize_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1274,7 +1392,7 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1283,7 +1401,9 @@ def test_set_iam_policy_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.ZoneSetPolicyRequest.to_json( - zone_set_policy_request_resource, including_default_value_fields=False + zone_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1372,7 +1492,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1381,7 +1501,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_resource_policies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_resource_policies.py index aec50c31f464..7bc23a452c0b 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_resource_policies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_resource_policies.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.resource_policies import ResourcePoliciesClient +from google.cloud.compute_v1.services.resource_policies import pagers from google.cloud.compute_v1.services.resource_policies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -424,11 +425,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ResourcePolicyAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.etag == "etag_value" assert response.id == "id_value" assert response.items == { @@ -472,7 +471,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -488,6 +487,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = ResourcePoliciesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ResourcePolicyAggregatedList( + items={ + "a": compute.ResourcePoliciesScopedList(), + "b": compute.ResourcePoliciesScopedList(), + "c": compute.ResourcePoliciesScopedList(), + }, + next_page_token="abc", + ), + compute.ResourcePolicyAggregatedList(items={}, next_page_token="def",), + compute.ResourcePolicyAggregatedList( + items={"g": compute.ResourcePoliciesScopedList(),}, + next_page_token="ghi", + ), + compute.ResourcePolicyAggregatedList( + items={ + "h": compute.ResourcePoliciesScopedList(), + "i": compute.ResourcePoliciesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.ResourcePolicyAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.ResourcePoliciesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.ResourcePoliciesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.ResourcePoliciesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteResourcePolicyRequest ): @@ -597,7 +665,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -717,7 +785,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -826,7 +894,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -962,14 +1030,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.ResourcePolicy.to_json( - resource_policy_resource, including_default_value_fields=False + resource_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1023,11 +1093,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ResourcePolicyList) + assert isinstance(response, pagers.ListPager) assert response.etag == "etag_value" assert response.id == "id_value" assert response.items == [ @@ -1068,7 +1136,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1088,6 +1156,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = ResourcePoliciesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ResourcePolicyList( + items=[ + compute.ResourcePolicy(), + compute.ResourcePolicy(), + compute.ResourcePolicy(), + ], + next_page_token="abc", + ), + compute.ResourcePolicyList(items=[], next_page_token="def",), + compute.ResourcePolicyList( + items=[compute.ResourcePolicy(),], next_page_token="ghi", + ), + compute.ResourcePolicyList( + items=[compute.ResourcePolicy(), compute.ResourcePolicy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ResourcePolicyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.ResourcePolicy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_iam_policy_rest( transport: str = "rest", request_type=compute.SetIamPolicyResourcePolicyRequest ): @@ -1181,7 +1300,7 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1190,7 +1309,9 @@ def test_set_iam_policy_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.RegionSetPolicyRequest.to_json( - region_set_policy_request_resource, including_default_value_fields=False + region_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1280,7 +1401,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1289,7 +1410,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_routers.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_routers.py index 342383acf762..00d4348eff08 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_routers.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_routers.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.routers import RoutersClient +from google.cloud.compute_v1.services.routers import pagers from google.cloud.compute_v1.services.routers import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -402,11 +403,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RouterAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.RoutersScopedList( @@ -453,7 +452,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -469,6 +468,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = RoutersClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RouterAggregatedList( + items={ + "a": compute.RoutersScopedList(), + "b": compute.RoutersScopedList(), + "c": compute.RoutersScopedList(), + }, + next_page_token="abc", + ), + compute.RouterAggregatedList(items={}, next_page_token="def",), + compute.RouterAggregatedList( + items={"g": compute.RoutersScopedList(),}, next_page_token="ghi", + ), + compute.RouterAggregatedList( + items={ + "h": compute.RoutersScopedList(), + "i": compute.RoutersScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.RouterAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.RoutersScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.RoutersScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.RoutersScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest(transport: str = "rest", request_type=compute.DeleteRouterRequest): client = RoutersClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -574,7 +636,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -688,7 +750,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -742,11 +804,9 @@ def test_get_nat_mapping_info_rest( response = client.get_nat_mapping_info(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.VmEndpointNatMappingsList) + assert isinstance(response, pagers.GetNatMappingInfoPager) assert response.id == "id_value" assert response.kind == "kind_value" assert response.next_page_token == "next_page_token_value" @@ -786,7 +846,7 @@ def test_get_nat_mapping_info_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -809,6 +869,60 @@ def test_get_nat_mapping_info_rest_flattened_error(): ) +def test_get_nat_mapping_info_pager(): + client = RoutersClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.VmEndpointNatMappingsList( + result=[ + compute.VmEndpointNatMappings(), + compute.VmEndpointNatMappings(), + compute.VmEndpointNatMappings(), + ], + next_page_token="abc", + ), + compute.VmEndpointNatMappingsList(result=[], next_page_token="def",), + compute.VmEndpointNatMappingsList( + result=[compute.VmEndpointNatMappings(),], next_page_token="ghi", + ), + compute.VmEndpointNatMappingsList( + result=[ + compute.VmEndpointNatMappings(), + compute.VmEndpointNatMappings(), + ], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.VmEndpointNatMappingsList.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 + + metadata = () + pager = client.get_nat_mapping_info(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.VmEndpointNatMappings) for i in results) + + pages = list(client.get_nat_mapping_info(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_get_router_status_rest( transport: str = "rest", request_type=compute.GetRouterStatusRouterRequest ): @@ -878,7 +992,7 @@ def test_get_router_status_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1012,14 +1126,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.Router.to_json( - router_resource, including_default_value_fields=False + router_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1076,11 +1192,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListRoutersRequ response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RouterList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Router( @@ -1122,7 +1236,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1142,6 +1256,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RoutersClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RouterList( + items=[compute.Router(), compute.Router(), compute.Router(),], + next_page_token="abc", + ), + compute.RouterList(items=[], next_page_token="def",), + compute.RouterList(items=[compute.Router(),], next_page_token="ghi",), + compute.RouterList(items=[compute.Router(), compute.Router(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.RouterList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Router) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest(transport: str = "rest", request_type=compute.PatchRouterRequest): client = RoutersClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -1254,7 +1411,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1263,7 +1420,9 @@ def test_patch_rest_flattened(): assert "router_value" in http_call[1] + str(body) assert compute.Router.to_json( - router_resource, including_default_value_fields=False + router_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1360,7 +1519,7 @@ def test_preview_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1369,7 +1528,9 @@ def test_preview_rest_flattened(): assert "router_value" in http_call[1] + str(body) assert compute.Router.to_json( - router_resource, including_default_value_fields=False + router_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1504,7 +1665,7 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1513,7 +1674,9 @@ def test_update_rest_flattened(): assert "router_value" in http_call[1] + str(body) assert compute.Router.to_json( - router_resource, including_default_value_fields=False + router_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_routes.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_routes.py index 12957b5a0e67..d079d9e0baf0 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_routes.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_routes.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.routes import RoutesClient +from google.cloud.compute_v1.services.routes import pagers from google.cloud.compute_v1.services.routes import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -465,7 +466,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -578,7 +579,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -703,12 +704,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.Route.to_json( - route_resource, including_default_value_fields=False + route_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -754,11 +757,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListRoutesReque response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.RouteList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Route(creation_timestamp="creation_timestamp_value") @@ -796,7 +797,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -812,6 +813,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = RoutesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.RouteList( + items=[compute.Route(), compute.Route(), compute.Route(),], + next_page_token="abc", + ), + compute.RouteList(items=[], next_page_token="def",), + compute.RouteList(items=[compute.Route(),], next_page_token="ghi",), + compute.RouteList(items=[compute.Route(), compute.Route(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.RouteList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Route) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.RoutesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_security_policies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_security_policies.py index fb4429aad678..3d1f7b03a8a2 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_security_policies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_security_policies.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.security_policies import SecurityPoliciesClient +from google.cloud.compute_v1.services.security_policies import pagers from google.cloud.compute_v1.services.security_policies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -496,14 +497,16 @@ def test_add_rule_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "security_policy_value" in http_call[1] + str(body) assert compute.SecurityPolicyRule.to_json( - security_policy_rule_resource, including_default_value_fields=False + security_policy_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -630,7 +633,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -725,7 +728,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -825,7 +828,7 @@ def test_get_rule_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -956,12 +959,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.SecurityPolicy.to_json( - security_policy_resource, including_default_value_fields=False + security_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1012,11 +1017,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.SecurityPolicyList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.SecurityPolicy(creation_timestamp="creation_timestamp_value") @@ -1053,7 +1056,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1069,6 +1072,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = SecurityPoliciesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.SecurityPolicyList( + items=[ + compute.SecurityPolicy(), + compute.SecurityPolicy(), + compute.SecurityPolicy(), + ], + next_page_token="abc", + ), + compute.SecurityPolicyList(items=[], next_page_token="def",), + compute.SecurityPolicyList( + items=[compute.SecurityPolicy(),], next_page_token="ghi", + ), + compute.SecurityPolicyList( + items=[compute.SecurityPolicy(), compute.SecurityPolicy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.SecurityPolicyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.SecurityPolicy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_preconfigured_expression_sets_rest( transport: str = "rest", request_type=compute.ListPreconfiguredExpressionSetsSecurityPoliciesRequest, @@ -1145,7 +1199,7 @@ def test_list_preconfigured_expression_sets_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1275,14 +1329,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "security_policy_value" in http_call[1] + str(body) assert compute.SecurityPolicy.to_json( - security_policy_resource, including_default_value_fields=False + security_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1415,14 +1471,16 @@ def test_patch_rule_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "security_policy_value" in http_call[1] + str(body) assert compute.SecurityPolicyRule.to_json( - security_policy_rule_resource, including_default_value_fields=False + security_policy_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1549,7 +1607,7 @@ def test_remove_rule_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_snapshots.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_snapshots.py index 07e6e8fb3b53..92cee9796b04 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_snapshots.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_snapshots.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.snapshots import SnapshotsClient +from google.cloud.compute_v1.services.snapshots import pagers from google.cloud.compute_v1.services.snapshots import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -468,7 +469,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -598,7 +599,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -704,7 +705,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -753,11 +754,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListSnapshotsRe response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.SnapshotList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.Snapshot(auto_created=True)] assert response.kind == "kind_value" @@ -793,7 +792,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -809,6 +808,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = SnapshotsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.SnapshotList( + items=[compute.Snapshot(), compute.Snapshot(), compute.Snapshot(),], + next_page_token="abc", + ), + compute.SnapshotList(items=[], next_page_token="def",), + compute.SnapshotList(items=[compute.Snapshot(),], next_page_token="ghi",), + compute.SnapshotList(items=[compute.Snapshot(), compute.Snapshot(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.SnapshotList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Snapshot) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_iam_policy_rest( transport: str = "rest", request_type=compute.SetIamPolicySnapshotRequest ): @@ -901,14 +943,16 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.GlobalSetPolicyRequest.to_json( - global_set_policy_request_resource, including_default_value_fields=False + global_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1041,14 +1085,16 @@ def test_set_labels_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.GlobalSetLabelsRequest.to_json( - global_set_labels_request_resource, including_default_value_fields=False + global_set_labels_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1135,14 +1181,16 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_ssl_certificates.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_ssl_certificates.py index bd54dbb28556..cf106e007241 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_ssl_certificates.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_ssl_certificates.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.ssl_certificates import SslCertificatesClient +from google.cloud.compute_v1.services.ssl_certificates import pagers from google.cloud.compute_v1.services.ssl_certificates import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -420,11 +421,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.SslCertificateAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.SslCertificatesScopedList( @@ -465,7 +464,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -481,6 +480,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = SslCertificatesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.SslCertificateAggregatedList( + items={ + "a": compute.SslCertificatesScopedList(), + "b": compute.SslCertificatesScopedList(), + "c": compute.SslCertificatesScopedList(), + }, + next_page_token="abc", + ), + compute.SslCertificateAggregatedList(items={}, next_page_token="def",), + compute.SslCertificateAggregatedList( + items={"g": compute.SslCertificatesScopedList(),}, + next_page_token="ghi", + ), + compute.SslCertificateAggregatedList( + items={ + "h": compute.SslCertificatesScopedList(), + "i": compute.SslCertificatesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.SslCertificateAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.SslCertificatesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.SslCertificatesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.SslCertificatesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteSslCertificateRequest ): @@ -588,7 +656,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -703,7 +771,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -834,12 +902,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.SslCertificate.to_json( - ssl_certificate_resource, including_default_value_fields=False + ssl_certificate_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -889,11 +959,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.SslCertificateList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.SslCertificate(certificate="certificate_value")] assert response.kind == "kind_value" @@ -929,7 +997,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -945,6 +1013,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = SslCertificatesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.SslCertificateList( + items=[ + compute.SslCertificate(), + compute.SslCertificate(), + compute.SslCertificate(), + ], + next_page_token="abc", + ), + compute.SslCertificateList(items=[], next_page_token="def",), + compute.SslCertificateList( + items=[compute.SslCertificate(),], next_page_token="ghi", + ), + compute.SslCertificateList( + items=[compute.SslCertificate(), compute.SslCertificate(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.SslCertificateList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.SslCertificate) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.SslCertificatesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_ssl_policies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_ssl_policies.py index f311c4ea9a16..83e39611cc05 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_ssl_policies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_ssl_policies.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.ssl_policies import SslPoliciesClient +from google.cloud.compute_v1.services.ssl_policies import pagers from google.cloud.compute_v1.services.ssl_policies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -472,7 +473,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -575,7 +576,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -706,12 +707,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.SslPolicy.to_json( - ssl_policy_resource, including_default_value_fields=False + ssl_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -761,11 +764,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.SslPoliciesList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.SslPolicy(creation_timestamp="creation_timestamp_value") @@ -803,7 +804,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -819,6 +820,51 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = SslPoliciesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.SslPoliciesList( + items=[compute.SslPolicy(), compute.SslPolicy(), compute.SslPolicy(),], + next_page_token="abc", + ), + compute.SslPoliciesList(items=[], next_page_token="def",), + compute.SslPoliciesList( + items=[compute.SslPolicy(),], next_page_token="ghi", + ), + compute.SslPoliciesList(items=[compute.SslPolicy(), compute.SslPolicy(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.SslPoliciesList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.SslPolicy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_available_features_rest( transport: str = "rest", request_type=compute.ListAvailableFeaturesSslPoliciesRequest, @@ -883,7 +929,7 @@ def test_list_available_features_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1012,14 +1058,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "ssl_policy_value" in http_call[1] + str(body) assert compute.SslPolicy.to_json( - ssl_policy_resource, including_default_value_fields=False + ssl_policy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_subnetworks.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_subnetworks.py index 26006d5758db..128c6d849fbf 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_subnetworks.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_subnetworks.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.subnetworks import SubnetworksClient +from google.cloud.compute_v1.services.subnetworks import pagers from google.cloud.compute_v1.services.subnetworks import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -405,11 +406,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.SubnetworkAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.SubnetworksScopedList( @@ -452,7 +451,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -468,6 +467,72 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = SubnetworksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.SubnetworkAggregatedList( + items={ + "a": compute.SubnetworksScopedList(), + "b": compute.SubnetworksScopedList(), + "c": compute.SubnetworksScopedList(), + }, + next_page_token="abc", + ), + compute.SubnetworkAggregatedList(items={}, next_page_token="def",), + compute.SubnetworkAggregatedList( + items={"g": compute.SubnetworksScopedList(),}, next_page_token="ghi", + ), + compute.SubnetworkAggregatedList( + items={ + "h": compute.SubnetworksScopedList(), + "i": compute.SubnetworksScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.SubnetworkAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.SubnetworksScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.SubnetworksScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.SubnetworksScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteSubnetworkRequest ): @@ -577,7 +642,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -714,7 +779,7 @@ def test_expand_ip_cidr_range_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -725,6 +790,7 @@ def test_expand_ip_cidr_range_rest_flattened(): assert compute.SubnetworksExpandIpCidrRangeRequest.to_json( subnetworks_expand_ip_cidr_range_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -857,7 +923,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -966,7 +1032,7 @@ def test_get_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1102,14 +1168,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.Subnetwork.to_json( - subnetwork_resource, including_default_value_fields=False + subnetwork_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1160,11 +1228,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.SubnetworkList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Subnetwork(creation_timestamp="creation_timestamp_value") @@ -1204,7 +1270,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1224,6 +1290,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = SubnetworksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.SubnetworkList( + items=[ + compute.Subnetwork(), + compute.Subnetwork(), + compute.Subnetwork(), + ], + next_page_token="abc", + ), + compute.SubnetworkList(items=[], next_page_token="def",), + compute.SubnetworkList( + items=[compute.Subnetwork(),], next_page_token="ghi", + ), + compute.SubnetworkList( + items=[compute.Subnetwork(), compute.Subnetwork(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.SubnetworkList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Subnetwork) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_list_usable_rest( transport: str = "rest", request_type=compute.ListUsableSubnetworksRequest ): @@ -1257,11 +1374,9 @@ def test_list_usable_rest( response = client.list_usable(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.UsableSubnetworksAggregatedList) + assert isinstance(response, pagers.ListUsablePager) assert response.id == "id_value" assert response.items == [ compute.UsableSubnetwork(ip_cidr_range="ip_cidr_range_value") @@ -1301,7 +1416,7 @@ def test_list_usable_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1317,6 +1432,59 @@ def test_list_usable_rest_flattened_error(): ) +def test_list_usable_pager(): + client = SubnetworksClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.UsableSubnetworksAggregatedList( + items=[ + compute.UsableSubnetwork(), + compute.UsableSubnetwork(), + compute.UsableSubnetwork(), + ], + next_page_token="abc", + ), + compute.UsableSubnetworksAggregatedList(items=[], next_page_token="def",), + compute.UsableSubnetworksAggregatedList( + items=[compute.UsableSubnetwork(),], next_page_token="ghi", + ), + compute.UsableSubnetworksAggregatedList( + items=[compute.UsableSubnetwork(), compute.UsableSubnetwork(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.UsableSubnetworksAggregatedList.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 + + metadata = () + pager = client.list_usable(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.UsableSubnetwork) for i in results) + + pages = list(client.list_usable(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchSubnetworkRequest ): @@ -1431,7 +1599,7 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1440,7 +1608,9 @@ def test_patch_rest_flattened(): assert "subnetwork_value" in http_call[1] + str(body) assert compute.Subnetwork.to_json( - subnetwork_resource, including_default_value_fields=False + subnetwork_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1554,7 +1724,7 @@ def test_set_iam_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1563,7 +1733,9 @@ def test_set_iam_policy_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.RegionSetPolicyRequest.to_json( - region_set_policy_request_resource, including_default_value_fields=False + region_set_policy_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1699,7 +1871,7 @@ def test_set_private_ip_google_access_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1710,6 +1882,7 @@ def test_set_private_ip_google_access_rest_flattened(): assert compute.SubnetworksSetPrivateIpGoogleAccessRequest.to_json( subnetworks_set_private_ip_google_access_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1798,7 +1971,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1807,7 +1980,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_grpc_proxies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_grpc_proxies.py index 9a80358f1c4d..4b5bac2df056 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_grpc_proxies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_grpc_proxies.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.target_grpc_proxies import TargetGrpcProxiesClient +from google.cloud.compute_v1.services.target_grpc_proxies import pagers from google.cloud.compute_v1.services.target_grpc_proxies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -490,7 +491,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -590,7 +591,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -722,12 +723,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.TargetGrpcProxy.to_json( - target_grpc_proxy_resource, including_default_value_fields=False + target_grpc_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -779,11 +782,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetGrpcProxyList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetGrpcProxy(creation_timestamp="creation_timestamp_value") @@ -821,7 +822,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -837,6 +838,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = TargetGrpcProxiesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetGrpcProxyList( + items=[ + compute.TargetGrpcProxy(), + compute.TargetGrpcProxy(), + compute.TargetGrpcProxy(), + ], + next_page_token="abc", + ), + compute.TargetGrpcProxyList(items=[], next_page_token="def",), + compute.TargetGrpcProxyList( + items=[compute.TargetGrpcProxy(),], next_page_token="ghi", + ), + compute.TargetGrpcProxyList( + items=[compute.TargetGrpcProxy(), compute.TargetGrpcProxy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetGrpcProxyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetGrpcProxy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchTargetGrpcProxyRequest ): @@ -950,14 +1002,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "target_grpc_proxy_value" in http_call[1] + str(body) assert compute.TargetGrpcProxy.to_json( - target_grpc_proxy_resource, including_default_value_fields=False + target_grpc_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_http_proxies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_http_proxies.py index 06e894a248db..683d2de43fd5 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_http_proxies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_http_proxies.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.target_http_proxies import TargetHttpProxiesClient +from google.cloud.compute_v1.services.target_http_proxies import pagers from google.cloud.compute_v1.services.target_http_proxies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -422,11 +423,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetHttpProxyAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.TargetHttpProxiesScopedList( @@ -468,7 +467,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -484,6 +483,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = TargetHttpProxiesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetHttpProxyAggregatedList( + items={ + "a": compute.TargetHttpProxiesScopedList(), + "b": compute.TargetHttpProxiesScopedList(), + "c": compute.TargetHttpProxiesScopedList(), + }, + next_page_token="abc", + ), + compute.TargetHttpProxyAggregatedList(items={}, next_page_token="def",), + compute.TargetHttpProxyAggregatedList( + items={"g": compute.TargetHttpProxiesScopedList(),}, + next_page_token="ghi", + ), + compute.TargetHttpProxyAggregatedList( + items={ + "h": compute.TargetHttpProxiesScopedList(), + "i": compute.TargetHttpProxiesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.TargetHttpProxyAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.TargetHttpProxiesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.TargetHttpProxiesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.TargetHttpProxiesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteTargetHttpProxyRequest ): @@ -591,7 +659,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -691,7 +759,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -823,12 +891,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.TargetHttpProxy.to_json( - target_http_proxy_resource, including_default_value_fields=False + target_http_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -880,11 +950,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetHttpProxyList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetHttpProxy(creation_timestamp="creation_timestamp_value") @@ -922,7 +990,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -938,6 +1006,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = TargetHttpProxiesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetHttpProxyList( + items=[ + compute.TargetHttpProxy(), + compute.TargetHttpProxy(), + compute.TargetHttpProxy(), + ], + next_page_token="abc", + ), + compute.TargetHttpProxyList(items=[], next_page_token="def",), + compute.TargetHttpProxyList( + items=[compute.TargetHttpProxy(),], next_page_token="ghi", + ), + compute.TargetHttpProxyList( + items=[compute.TargetHttpProxy(), compute.TargetHttpProxy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetHttpProxyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetHttpProxy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest( transport: str = "rest", request_type=compute.PatchTargetHttpProxyRequest ): @@ -1051,14 +1170,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "target_http_proxy_value" in http_call[1] + str(body) assert compute.TargetHttpProxy.to_json( - target_http_proxy_resource, including_default_value_fields=False + target_http_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1189,14 +1310,16 @@ def test_set_url_map_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "target_http_proxy_value" in http_call[1] + str(body) assert compute.UrlMapReference.to_json( - url_map_reference_resource, including_default_value_fields=False + url_map_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_https_proxies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_https_proxies.py index 6bc2387d7728..743a4c848f0b 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_https_proxies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_https_proxies.py @@ -37,6 +37,7 @@ from google.cloud.compute_v1.services.target_https_proxies import ( TargetHttpsProxiesClient, ) +from google.cloud.compute_v1.services.target_https_proxies import pagers from google.cloud.compute_v1.services.target_https_proxies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -426,11 +427,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetHttpsProxyAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.TargetHttpsProxiesScopedList( @@ -475,7 +474,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -491,6 +490,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = TargetHttpsProxiesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetHttpsProxyAggregatedList( + items={ + "a": compute.TargetHttpsProxiesScopedList(), + "b": compute.TargetHttpsProxiesScopedList(), + "c": compute.TargetHttpsProxiesScopedList(), + }, + next_page_token="abc", + ), + compute.TargetHttpsProxyAggregatedList(items={}, next_page_token="def",), + compute.TargetHttpsProxyAggregatedList( + items={"g": compute.TargetHttpsProxiesScopedList(),}, + next_page_token="ghi", + ), + compute.TargetHttpsProxyAggregatedList( + items={ + "h": compute.TargetHttpsProxiesScopedList(), + "i": compute.TargetHttpsProxiesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.TargetHttpsProxyAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.TargetHttpsProxiesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.TargetHttpsProxiesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.TargetHttpsProxiesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteTargetHttpsProxyRequest ): @@ -598,7 +666,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -706,7 +774,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -838,12 +906,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.TargetHttpsProxy.to_json( - target_https_proxy_resource, including_default_value_fields=False + target_https_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -897,11 +967,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetHttpsProxyList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetHttpsProxy(authorization_policy="authorization_policy_value") @@ -939,7 +1007,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -955,6 +1023,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = TargetHttpsProxiesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetHttpsProxyList( + items=[ + compute.TargetHttpsProxy(), + compute.TargetHttpsProxy(), + compute.TargetHttpsProxy(), + ], + next_page_token="abc", + ), + compute.TargetHttpsProxyList(items=[], next_page_token="def",), + compute.TargetHttpsProxyList( + items=[compute.TargetHttpsProxy(),], next_page_token="ghi", + ), + compute.TargetHttpsProxyList( + items=[compute.TargetHttpsProxy(), compute.TargetHttpsProxy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetHttpsProxyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetHttpsProxy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_quic_override_rest( transport: str = "rest", request_type=compute.SetQuicOverrideTargetHttpsProxyRequest ): @@ -1068,7 +1187,7 @@ def test_set_quic_override_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1077,6 +1196,7 @@ def test_set_quic_override_rest_flattened(): assert compute.TargetHttpsProxiesSetQuicOverrideRequest.to_json( target_https_proxies_set_quic_override_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1210,7 +1330,7 @@ def test_set_ssl_certificates_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1219,6 +1339,7 @@ def test_set_ssl_certificates_rest_flattened(): assert compute.TargetHttpsProxiesSetSslCertificatesRequest.to_json( target_https_proxies_set_ssl_certificates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1351,14 +1472,16 @@ def test_set_ssl_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "target_https_proxy_value" in http_call[1] + str(body) assert compute.SslPolicyReference.to_json( - ssl_policy_reference_resource, including_default_value_fields=False + ssl_policy_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1489,14 +1612,16 @@ def test_set_url_map_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "target_https_proxy_value" in http_call[1] + str(body) assert compute.UrlMapReference.to_json( - url_map_reference_resource, including_default_value_fields=False + url_map_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_instances.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_instances.py index 198607c97c29..90696b2415f1 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_instances.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_instances.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.target_instances import TargetInstancesClient +from google.cloud.compute_v1.services.target_instances import pagers from google.cloud.compute_v1.services.target_instances import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -422,11 +423,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetInstanceAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.TargetInstancesScopedList( @@ -469,7 +468,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -485,6 +484,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = TargetInstancesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetInstanceAggregatedList( + items={ + "a": compute.TargetInstancesScopedList(), + "b": compute.TargetInstancesScopedList(), + "c": compute.TargetInstancesScopedList(), + }, + next_page_token="abc", + ), + compute.TargetInstanceAggregatedList(items={}, next_page_token="def",), + compute.TargetInstanceAggregatedList( + items={"g": compute.TargetInstancesScopedList(),}, + next_page_token="ghi", + ), + compute.TargetInstanceAggregatedList( + items={ + "h": compute.TargetInstancesScopedList(), + "i": compute.TargetInstancesScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.TargetInstanceAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.TargetInstancesScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.TargetInstancesScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.TargetInstancesScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteTargetInstanceRequest ): @@ -594,7 +662,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -696,7 +764,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -832,14 +900,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "zone_value" in http_call[1] + str(body) assert compute.TargetInstance.to_json( - target_instance_resource, including_default_value_fields=False + target_instance_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -892,11 +962,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetInstanceList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetInstance(creation_timestamp="creation_timestamp_value") @@ -936,7 +1004,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -956,6 +1024,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = TargetInstancesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetInstanceList( + items=[ + compute.TargetInstance(), + compute.TargetInstance(), + compute.TargetInstance(), + ], + next_page_token="abc", + ), + compute.TargetInstanceList(items=[], next_page_token="def",), + compute.TargetInstanceList( + items=[compute.TargetInstance(),], next_page_token="ghi", + ), + compute.TargetInstanceList( + items=[compute.TargetInstance(), compute.TargetInstance(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetInstanceList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetInstance) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.TargetInstancesRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_pools.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_pools.py index 0567f9963210..d7cfacad7aee 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_pools.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_pools.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.target_pools import TargetPoolsClient +from google.cloud.compute_v1.services.target_pools import pagers from google.cloud.compute_v1.services.target_pools import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -481,7 +482,7 @@ def test_add_health_check_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -492,6 +493,7 @@ def test_add_health_check_rest_flattened(): assert compute.TargetPoolsAddHealthCheckRequest.to_json( target_pools_add_health_check_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -628,7 +630,7 @@ def test_add_instance_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -639,6 +641,7 @@ def test_add_instance_rest_flattened(): assert compute.TargetPoolsAddInstanceRequest.to_json( target_pools_add_instance_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -695,11 +698,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetPoolAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.TargetPoolsScopedList( @@ -740,7 +741,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -756,6 +757,72 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = TargetPoolsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetPoolAggregatedList( + items={ + "a": compute.TargetPoolsScopedList(), + "b": compute.TargetPoolsScopedList(), + "c": compute.TargetPoolsScopedList(), + }, + next_page_token="abc", + ), + compute.TargetPoolAggregatedList(items={}, next_page_token="def",), + compute.TargetPoolAggregatedList( + items={"g": compute.TargetPoolsScopedList(),}, next_page_token="ghi", + ), + compute.TargetPoolAggregatedList( + items={ + "h": compute.TargetPoolsScopedList(), + "i": compute.TargetPoolsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetPoolAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.TargetPoolsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.TargetPoolsScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.TargetPoolsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteTargetPoolRequest ): @@ -865,7 +932,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -971,7 +1038,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1068,7 +1135,7 @@ def test_get_health_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1077,7 +1144,9 @@ def test_get_health_rest_flattened(): assert "target_pool_value" in http_call[1] + str(body) assert compute.InstanceReference.to_json( - instance_reference_resource, including_default_value_fields=False + instance_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1209,14 +1278,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.TargetPool.to_json( - target_pool_resource, including_default_value_fields=False + target_pool_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1265,11 +1336,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetPoolList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [compute.TargetPool(backup_pool="backup_pool_value")] assert response.kind == "kind_value" @@ -1307,7 +1376,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1327,6 +1396,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = TargetPoolsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetPoolList( + items=[ + compute.TargetPool(), + compute.TargetPool(), + compute.TargetPool(), + ], + next_page_token="abc", + ), + compute.TargetPoolList(items=[], next_page_token="def",), + compute.TargetPoolList( + items=[compute.TargetPool(),], next_page_token="ghi", + ), + compute.TargetPoolList( + items=[compute.TargetPool(), compute.TargetPool(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetPoolList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetPool) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_remove_health_check_rest( transport: str = "rest", request_type=compute.RemoveHealthCheckTargetPoolRequest ): @@ -1443,7 +1563,7 @@ def test_remove_health_check_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1454,6 +1574,7 @@ def test_remove_health_check_rest_flattened(): assert compute.TargetPoolsRemoveHealthCheckRequest.to_json( target_pools_remove_health_check_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1590,7 +1711,7 @@ def test_remove_instance_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1601,6 +1722,7 @@ def test_remove_instance_rest_flattened(): assert compute.TargetPoolsRemoveInstanceRequest.to_json( target_pools_remove_instance_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1733,7 +1855,7 @@ def test_set_backup_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1742,7 +1864,9 @@ def test_set_backup_rest_flattened(): assert "target_pool_value" in http_call[1] + str(body) assert compute.TargetReference.to_json( - target_reference_resource, including_default_value_fields=False + target_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_ssl_proxies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_ssl_proxies.py index 315086003f8d..716a6404516d 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_ssl_proxies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_ssl_proxies.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.target_ssl_proxies import TargetSslProxiesClient +from google.cloud.compute_v1.services.target_ssl_proxies import pagers from google.cloud.compute_v1.services.target_ssl_proxies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -490,7 +491,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -589,7 +590,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -721,12 +722,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.TargetSslProxy.to_json( - target_ssl_proxy_resource, including_default_value_fields=False + target_ssl_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -778,11 +781,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetSslProxyList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetSslProxy(creation_timestamp="creation_timestamp_value") @@ -820,7 +821,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -836,6 +837,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = TargetSslProxiesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetSslProxyList( + items=[ + compute.TargetSslProxy(), + compute.TargetSslProxy(), + compute.TargetSslProxy(), + ], + next_page_token="abc", + ), + compute.TargetSslProxyList(items=[], next_page_token="def",), + compute.TargetSslProxyList( + items=[compute.TargetSslProxy(),], next_page_token="ghi", + ), + compute.TargetSslProxyList( + items=[compute.TargetSslProxy(), compute.TargetSslProxy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetSslProxyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetSslProxy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_backend_service_rest( transport: str = "rest", request_type=compute.SetBackendServiceTargetSslProxyRequest ): @@ -949,7 +1001,7 @@ def test_set_backend_service_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -958,6 +1010,7 @@ def test_set_backend_service_rest_flattened(): assert compute.TargetSslProxiesSetBackendServiceRequest.to_json( target_ssl_proxies_set_backend_service_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1090,7 +1143,7 @@ def test_set_proxy_header_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1099,6 +1152,7 @@ def test_set_proxy_header_rest_flattened(): assert compute.TargetSslProxiesSetProxyHeaderRequest.to_json( target_ssl_proxies_set_proxy_header_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1232,7 +1286,7 @@ def test_set_ssl_certificates_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1241,6 +1295,7 @@ def test_set_ssl_certificates_rest_flattened(): assert compute.TargetSslProxiesSetSslCertificatesRequest.to_json( target_ssl_proxies_set_ssl_certificates_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1373,14 +1428,16 @@ def test_set_ssl_policy_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "target_ssl_proxy_value" in http_call[1] + str(body) assert compute.SslPolicyReference.to_json( - ssl_policy_reference_resource, including_default_value_fields=False + ssl_policy_reference_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_tcp_proxies.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_tcp_proxies.py index a464a9dc17a5..e06fba947571 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_tcp_proxies.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_tcp_proxies.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.target_tcp_proxies import TargetTcpProxiesClient +from google.cloud.compute_v1.services.target_tcp_proxies import pagers from google.cloud.compute_v1.services.target_tcp_proxies import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -490,7 +491,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -585,7 +586,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -717,12 +718,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.TargetTcpProxy.to_json( - target_tcp_proxy_resource, including_default_value_fields=False + target_tcp_proxy_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -774,11 +777,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetTcpProxyList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetTcpProxy(creation_timestamp="creation_timestamp_value") @@ -816,7 +817,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -832,6 +833,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = TargetTcpProxiesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetTcpProxyList( + items=[ + compute.TargetTcpProxy(), + compute.TargetTcpProxy(), + compute.TargetTcpProxy(), + ], + next_page_token="abc", + ), + compute.TargetTcpProxyList(items=[], next_page_token="def",), + compute.TargetTcpProxyList( + items=[compute.TargetTcpProxy(),], next_page_token="ghi", + ), + compute.TargetTcpProxyList( + items=[compute.TargetTcpProxy(), compute.TargetTcpProxy(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetTcpProxyList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetTcpProxy) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_backend_service_rest( transport: str = "rest", request_type=compute.SetBackendServiceTargetTcpProxyRequest ): @@ -945,7 +997,7 @@ def test_set_backend_service_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -954,6 +1006,7 @@ def test_set_backend_service_rest_flattened(): assert compute.TargetTcpProxiesSetBackendServiceRequest.to_json( target_tcp_proxies_set_backend_service_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1086,7 +1139,7 @@ def test_set_proxy_header_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1095,6 +1148,7 @@ def test_set_proxy_header_rest_flattened(): assert compute.TargetTcpProxiesSetProxyHeaderRequest.to_json( target_tcp_proxies_set_proxy_header_request_resource, including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_vpn_gateways.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_vpn_gateways.py index cd595155fff4..8e696dc655bc 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_vpn_gateways.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_target_vpn_gateways.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.target_vpn_gateways import TargetVpnGatewaysClient +from google.cloud.compute_v1.services.target_vpn_gateways import pagers from google.cloud.compute_v1.services.target_vpn_gateways import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -423,11 +424,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetVpnGatewayAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.TargetVpnGatewaysScopedList( @@ -470,7 +469,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -486,6 +485,75 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = TargetVpnGatewaysClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetVpnGatewayAggregatedList( + items={ + "a": compute.TargetVpnGatewaysScopedList(), + "b": compute.TargetVpnGatewaysScopedList(), + "c": compute.TargetVpnGatewaysScopedList(), + }, + next_page_token="abc", + ), + compute.TargetVpnGatewayAggregatedList(items={}, next_page_token="def",), + compute.TargetVpnGatewayAggregatedList( + items={"g": compute.TargetVpnGatewaysScopedList(),}, + next_page_token="ghi", + ), + compute.TargetVpnGatewayAggregatedList( + items={ + "h": compute.TargetVpnGatewaysScopedList(), + "i": compute.TargetVpnGatewaysScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple( + compute.TargetVpnGatewayAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.TargetVpnGatewaysScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.TargetVpnGatewaysScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.TargetVpnGatewaysScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteTargetVpnGatewayRequest ): @@ -595,7 +663,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -701,7 +769,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -837,14 +905,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.TargetVpnGateway.to_json( - target_vpn_gateway_resource, including_default_value_fields=False + target_vpn_gateway_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -897,11 +967,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.TargetVpnGatewayList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.TargetVpnGateway(creation_timestamp="creation_timestamp_value") @@ -941,7 +1009,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -961,6 +1029,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = TargetVpnGatewaysClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.TargetVpnGatewayList( + items=[ + compute.TargetVpnGateway(), + compute.TargetVpnGateway(), + compute.TargetVpnGateway(), + ], + next_page_token="abc", + ), + compute.TargetVpnGatewayList(items=[], next_page_token="def",), + compute.TargetVpnGatewayList( + items=[compute.TargetVpnGateway(),], next_page_token="ghi", + ), + compute.TargetVpnGatewayList( + items=[compute.TargetVpnGateway(), compute.TargetVpnGateway(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.TargetVpnGatewayList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.TargetVpnGateway) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.TargetVpnGatewaysRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_url_maps.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_url_maps.py index 658fcffc590e..6a1d79757932 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_url_maps.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_url_maps.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.url_maps import UrlMapsClient +from google.cloud.compute_v1.services.url_maps import pagers from google.cloud.compute_v1.services.url_maps import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -398,11 +399,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.UrlMapsAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.UrlMapsScopedList( @@ -443,7 +442,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -459,6 +458,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = UrlMapsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.UrlMapsAggregatedList( + items={ + "a": compute.UrlMapsScopedList(), + "b": compute.UrlMapsScopedList(), + "c": compute.UrlMapsScopedList(), + }, + next_page_token="abc", + ), + compute.UrlMapsAggregatedList(items={}, next_page_token="def",), + compute.UrlMapsAggregatedList( + items={"g": compute.UrlMapsScopedList(),}, next_page_token="ghi", + ), + compute.UrlMapsAggregatedList( + items={ + "h": compute.UrlMapsScopedList(), + "i": compute.UrlMapsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.UrlMapsAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.UrlMapsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.UrlMapsScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.UrlMapsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest(transport: str = "rest", request_type=compute.DeleteUrlMapRequest): client = UrlMapsClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -564,7 +626,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -699,7 +761,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -826,12 +888,14 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert compute.UrlMap.to_json( - url_map_resource, including_default_value_fields=False + url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -963,14 +1027,16 @@ def test_invalidate_cache_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "url_map_value" in http_call[1] + str(body) assert compute.CacheInvalidationRule.to_json( - cache_invalidation_rule_resource, including_default_value_fields=False + cache_invalidation_rule_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1019,11 +1085,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListUrlMapsRequ response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.UrlMapList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.UrlMap(creation_timestamp="creation_timestamp_value") @@ -1061,7 +1125,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1077,6 +1141,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = UrlMapsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.UrlMapList( + items=[compute.UrlMap(), compute.UrlMap(), compute.UrlMap(),], + next_page_token="abc", + ), + compute.UrlMapList(items=[], next_page_token="def",), + compute.UrlMapList(items=[compute.UrlMap(),], next_page_token="ghi",), + compute.UrlMapList(items=[compute.UrlMap(), compute.UrlMap(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.UrlMapList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.UrlMap) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_patch_rest(transport: str = "rest", request_type=compute.PatchUrlMapRequest): client = UrlMapsClient( credentials=credentials.AnonymousCredentials(), transport=transport, @@ -1186,14 +1293,16 @@ def test_patch_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "url_map_value" in http_call[1] + str(body) assert compute.UrlMap.to_json( - url_map_resource, including_default_value_fields=False + url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1322,14 +1431,16 @@ def test_update_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "url_map_value" in http_call[1] + str(body) assert compute.UrlMap.to_json( - url_map_resource, including_default_value_fields=False + url_map_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1418,14 +1529,16 @@ def test_validate_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "url_map_value" in http_call[1] + str(body) assert compute.UrlMapsValidateRequest.to_json( - url_maps_validate_request_resource, including_default_value_fields=False + url_maps_validate_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_vpn_gateways.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_vpn_gateways.py index 78abf7d460a7..cbd4ba4bf8b2 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_vpn_gateways.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_vpn_gateways.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.vpn_gateways import VpnGatewaysClient +from google.cloud.compute_v1.services.vpn_gateways import pagers from google.cloud.compute_v1.services.vpn_gateways import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -405,11 +406,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.VpnGatewayAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.VpnGatewaysScopedList( @@ -452,7 +451,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -468,6 +467,72 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = VpnGatewaysClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.VpnGatewayAggregatedList( + items={ + "a": compute.VpnGatewaysScopedList(), + "b": compute.VpnGatewaysScopedList(), + "c": compute.VpnGatewaysScopedList(), + }, + next_page_token="abc", + ), + compute.VpnGatewayAggregatedList(items={}, next_page_token="def",), + compute.VpnGatewayAggregatedList( + items={"g": compute.VpnGatewaysScopedList(),}, next_page_token="ghi", + ), + compute.VpnGatewayAggregatedList( + items={ + "h": compute.VpnGatewaysScopedList(), + "i": compute.VpnGatewaysScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.VpnGatewayAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.VpnGatewaysScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == ( + str, + compute.VpnGatewaysScopedList, + ) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.VpnGatewaysScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteVpnGatewayRequest ): @@ -577,7 +642,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -681,7 +746,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -779,7 +844,7 @@ def test_get_status_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -915,14 +980,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.VpnGateway.to_json( - vpn_gateway_resource, including_default_value_fields=False + vpn_gateway_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -973,11 +1040,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.VpnGatewayList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.VpnGateway(creation_timestamp="creation_timestamp_value") @@ -1017,7 +1082,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1037,6 +1102,57 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = VpnGatewaysClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.VpnGatewayList( + items=[ + compute.VpnGateway(), + compute.VpnGateway(), + compute.VpnGateway(), + ], + next_page_token="abc", + ), + compute.VpnGatewayList(items=[], next_page_token="def",), + compute.VpnGatewayList( + items=[compute.VpnGateway(),], next_page_token="ghi", + ), + compute.VpnGatewayList( + items=[compute.VpnGateway(), compute.VpnGateway(),], + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.VpnGatewayList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.VpnGateway) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_set_labels_rest( transport: str = "rest", request_type=compute.SetLabelsVpnGatewayRequest ): @@ -1151,7 +1267,7 @@ def test_set_labels_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1160,7 +1276,9 @@ def test_set_labels_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.RegionSetLabelsRequest.to_json( - region_set_labels_request_resource, including_default_value_fields=False + region_set_labels_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -1249,7 +1367,7 @@ def test_test_iam_permissions_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -1258,7 +1376,9 @@ def test_test_iam_permissions_rest_flattened(): assert "resource_value" in http_call[1] + str(body) assert compute.TestPermissionsRequest.to_json( - test_permissions_request_resource, including_default_value_fields=False + test_permissions_request_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_vpn_tunnels.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_vpn_tunnels.py index 46ab35d54fd5..b3d1b1068ce2 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_vpn_tunnels.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_vpn_tunnels.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.vpn_tunnels import VpnTunnelsClient +from google.cloud.compute_v1.services.vpn_tunnels import pagers from google.cloud.compute_v1.services.vpn_tunnels import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -403,11 +404,9 @@ def test_aggregated_list_rest( response = client.aggregated_list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.VpnTunnelAggregatedList) + assert isinstance(response, pagers.AggregatedListPager) assert response.id == "id_value" assert response.items == { "key_value": compute.VpnTunnelsScopedList( @@ -450,7 +449,7 @@ def test_aggregated_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -466,6 +465,69 @@ def test_aggregated_list_rest_flattened_error(): ) +def test_aggregated_list_pager(): + client = VpnTunnelsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.VpnTunnelAggregatedList( + items={ + "a": compute.VpnTunnelsScopedList(), + "b": compute.VpnTunnelsScopedList(), + "c": compute.VpnTunnelsScopedList(), + }, + next_page_token="abc", + ), + compute.VpnTunnelAggregatedList(items={}, next_page_token="def",), + compute.VpnTunnelAggregatedList( + items={"g": compute.VpnTunnelsScopedList(),}, next_page_token="ghi", + ), + compute.VpnTunnelAggregatedList( + items={ + "h": compute.VpnTunnelsScopedList(), + "i": compute.VpnTunnelsScopedList(), + }, + ), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.VpnTunnelAggregatedList.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 + + metadata = () + pager = client.aggregated_list(request={}) + + assert pager._metadata == metadata + + assert isinstance(pager.get("a"), compute.VpnTunnelsScopedList) + assert pager.get("h") is None + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, tuple) for i in results) + for result in results: + assert isinstance(result, tuple) + assert tuple(type(t) for t in result) == (str, compute.VpnTunnelsScopedList) + + assert pager.get("a") is None + assert isinstance(pager.get("h"), compute.VpnTunnelsScopedList) + + pages = list(client.aggregated_list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_delete_rest( transport: str = "rest", request_type=compute.DeleteVpnTunnelRequest ): @@ -575,7 +637,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -701,7 +763,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -837,14 +899,16 @@ def test_insert_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) assert "region_value" in http_call[1] + str(body) assert compute.VpnTunnel.to_json( - vpn_tunnel_resource, including_default_value_fields=False + vpn_tunnel_resource, + including_default_value_fields=False, + use_integers_for_enums=False, ) in http_call[1] + str(body) @@ -893,11 +957,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListVpnTunnelsR response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.VpnTunnelList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.VpnTunnel(creation_timestamp="creation_timestamp_value") @@ -937,7 +999,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -957,6 +1019,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = VpnTunnelsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.VpnTunnelList( + items=[compute.VpnTunnel(), compute.VpnTunnel(), compute.VpnTunnel(),], + next_page_token="abc", + ), + compute.VpnTunnelList(items=[], next_page_token="def",), + compute.VpnTunnelList(items=[compute.VpnTunnel(),], next_page_token="ghi",), + compute.VpnTunnelList(items=[compute.VpnTunnel(), compute.VpnTunnel(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.VpnTunnelList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.VpnTunnel) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.VpnTunnelsRestTransport( diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_zone_operations.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_zone_operations.py index f73f6bdad816..5fccf058ad33 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_zone_operations.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_zone_operations.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.zone_operations import ZoneOperationsClient +from google.cloud.compute_v1.services.zone_operations import pagers from google.cloud.compute_v1.services.zone_operations import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -430,7 +431,7 @@ def test_delete_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -560,7 +561,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -614,11 +615,9 @@ def test_list_rest( response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.OperationList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Operation(client_operation_id="client_operation_id_value") @@ -658,7 +657,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -678,6 +677,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = ZoneOperationsClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.OperationList( + items=[compute.Operation(), compute.Operation(), compute.Operation(),], + next_page_token="abc", + ), + compute.OperationList(items=[], next_page_token="def",), + compute.OperationList(items=[compute.Operation(),], next_page_token="ghi",), + compute.OperationList(items=[compute.Operation(), compute.Operation(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.OperationList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Operation) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_wait_rest( transport: str = "rest", request_type=compute.WaitZoneOperationRequest ): @@ -785,7 +827,7 @@ def test_wait_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) diff --git a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_zones.py b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_zones.py index aa82212d4eea..25d3ec804546 100644 --- a/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_zones.py +++ b/packages/google-cloud-compute/tests/unit/gapic/compute_v1/test_zones.py @@ -35,6 +35,7 @@ from google.auth import credentials from google.auth.exceptions import MutualTLSChannelError from google.cloud.compute_v1.services.zones import ZonesClient +from google.cloud.compute_v1.services.zones import pagers from google.cloud.compute_v1.services.zones import transports from google.cloud.compute_v1.types import compute from google.oauth2 import service_account @@ -437,7 +438,7 @@ def test_get_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -486,11 +487,9 @@ def test_list_rest(transport: str = "rest", request_type=compute.ListZonesReques response = client.list(request) - assert response.raw_page is response - # Establish that the response is the type that we expect. - assert isinstance(response, compute.ZoneList) + assert isinstance(response, pagers.ListPager) assert response.id == "id_value" assert response.items == [ compute.Zone(available_cpu_platforms=["available_cpu_platforms_value"]) @@ -528,7 +527,7 @@ def test_list_rest_flattened(): # request object values. assert len(req.mock_calls) == 1 _, http_call, http_params = req.mock_calls[0] - body = http_params.get("json") + body = http_params.get("data") assert "project_value" in http_call[1] + str(body) @@ -544,6 +543,49 @@ def test_list_rest_flattened_error(): ) +def test_list_pager(): + client = ZonesClient(credentials=credentials.AnonymousCredentials(),) + + # Mock the http request call within the method and fake a response. + with mock.patch.object(Session, "request") as req: + # Set the response as a series of pages + + response = ( + compute.ZoneList( + items=[compute.Zone(), compute.Zone(), compute.Zone(),], + next_page_token="abc", + ), + compute.ZoneList(items=[], next_page_token="def",), + compute.ZoneList(items=[compute.Zone(),], next_page_token="ghi",), + compute.ZoneList(items=[compute.Zone(), compute.Zone(),],), + ) + + # Two responses for two calls + response = response + response + + # Wrap the values into proper Response objs + response = tuple(compute.ZoneList.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 + + metadata = () + pager = client.list(request={}) + + assert pager._metadata == metadata + + results = list(pager) + assert len(results) == 6 + + assert all(isinstance(i, compute.Zone) for i in results) + + pages = list(client.list(request={}).pages) + for page_, token in zip(pages, ["abc", "def", "ghi", ""]): + assert page_.raw_page.next_page_token == token + + def test_credentials_transport_error(): # It is an error to provide credentials and a transport instance. transport = transports.ZonesRestTransport(