Skip to content

Commit 385995b

Browse files
m-strzelczykdandhlee
authored andcommitted
docs(samples): Fixing an issue with listing all instances (#349)
1 parent 6fe10c0 commit 385995b

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

compute/compute/ingredients/instances/list_all.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

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

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

4344
agg_list = instance_client.aggregated_list(request=request)
4445

45-
all_instances = {}
46+
all_instances = defaultdict(list)
4647
print("Instances found:")
4748
# Despite using the `max_results` parameter, you don't need to handle the pagination
4849
# yourself. The returned `AggregatedListPager` object handles pagination
4950
# automatically, returning separated pages as you iterate over the results.
5051
for zone, response in agg_list:
5152
if response.instances:
52-
all_instances[zone] = response.instances
53+
all_instances[zone].extend(response.instances)
5354
print(f" {zone}:")
5455
for instance in response.instances:
5556
print(f" - {instance.name} ({instance.machine_type})")
5657
return all_instances
5758
# </INGREDIENT>
58-

compute/compute/snippets/instances/list_all.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
# [START compute_instances_list_all]
23+
from collections import defaultdict
2324
from typing import Dict, Iterable
2425

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

4647
agg_list = instance_client.aggregated_list(request=request)
4748

48-
all_instances = {}
49+
all_instances = defaultdict(list)
4950
print("Instances found:")
5051
# Despite using the `max_results` parameter, you don't need to handle the pagination
5152
# yourself. The returned `AggregatedListPager` object handles pagination
5253
# automatically, returning separated pages as you iterate over the results.
5354
for zone, response in agg_list:
5455
if response.instances:
55-
all_instances[zone] = response.instances
56+
all_instances[zone].extend(response.instances)
5657
print(f" {zone}:")
5758
for instance in response.instances:
5859
print(f" - {instance.name} ({instance.machine_type})")

0 commit comments

Comments
 (0)