Skip to content

Commit

Permalink
docs(samples): Fixing an issue with listing all instances (#349)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-strzelczyk authored and dandhlee committed Nov 16, 2022
1 parent 6fe10c0 commit 385995b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 4 additions & 4 deletions compute/compute/ingredients/instances/list_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
# folder for complete code samples that are ready to be used.
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
# flake8: noqa
from collections import defaultdict
from typing import Dict, Iterable

from google.cloud import compute_v1
Expand All @@ -42,17 +43,16 @@ def list_all_instances(

agg_list = instance_client.aggregated_list(request=request)

all_instances = {}
all_instances = defaultdict(list)
print("Instances found:")
# Despite using the `max_results` parameter, you don't need to handle the pagination
# yourself. The returned `AggregatedListPager` object handles pagination
# automatically, returning separated pages as you iterate over the results.
for zone, response in agg_list:
if response.instances:
all_instances[zone] = response.instances
all_instances[zone].extend(response.instances)
print(f" {zone}:")
for instance in response.instances:
print(f" - {instance.name} ({instance.machine_type})")
return all_instances
# </INGREDIENT>

5 changes: 3 additions & 2 deletions compute/compute/snippets/instances/list_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@


# [START compute_instances_list_all]
from collections import defaultdict
from typing import Dict, Iterable

from google.cloud import compute_v1
Expand All @@ -45,14 +46,14 @@ def list_all_instances(

agg_list = instance_client.aggregated_list(request=request)

all_instances = {}
all_instances = defaultdict(list)
print("Instances found:")
# Despite using the `max_results` parameter, you don't need to handle the pagination
# yourself. The returned `AggregatedListPager` object handles pagination
# automatically, returning separated pages as you iterate over the results.
for zone, response in agg_list:
if response.instances:
all_instances[zone] = response.instances
all_instances[zone].extend(response.instances)
print(f" {zone}:")
for instance in response.instances:
print(f" - {instance.name} ({instance.machine_type})")
Expand Down

0 comments on commit 385995b

Please sign in to comment.