File tree Expand file tree Collapse file tree 2 files changed +7
-6
lines changed
Expand file tree Collapse file tree 2 files changed +7
-6
lines changed Original file line number Diff line number Diff line change 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
1920from typing import Dict , Iterable
2021
2122from 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-
Original file line number Diff line number Diff line change 2020
2121
2222# [START compute_instances_list_all]
23+ from collections import defaultdict
2324from typing import Dict , Iterable
2425
2526from 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 } )" )
You can’t perform that action at this time.
0 commit comments