File tree 2 files changed +7
-6
lines changed
2 files changed +7
-6
lines changed Original file line number Diff line number Diff line change 12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
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
16
16
# folder for complete code samples that are ready to be used.
17
17
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
18
18
# flake8: noqa
19
+ from collections import defaultdict
19
20
from typing import Dict , Iterable
20
21
21
22
from google .cloud import compute_v1
@@ -42,17 +43,16 @@ def list_all_instances(
42
43
43
44
agg_list = instance_client .aggregated_list (request = request )
44
45
45
- all_instances = {}
46
+ all_instances = defaultdict ( list )
46
47
print ("Instances found:" )
47
48
# Despite using the `max_results` parameter, you don't need to handle the pagination
48
49
# yourself. The returned `AggregatedListPager` object handles pagination
49
50
# automatically, returning separated pages as you iterate over the results.
50
51
for zone , response in agg_list :
51
52
if response .instances :
52
- all_instances [zone ] = response .instances
53
+ all_instances [zone ]. extend ( response .instances )
53
54
print (f" { zone } :" )
54
55
for instance in response .instances :
55
56
print (f" - { instance .name } ({ instance .machine_type } )" )
56
57
return all_instances
57
58
# </INGREDIENT>
58
-
Original file line number Diff line number Diff line change 20
20
21
21
22
22
# [START compute_instances_list_all]
23
+ from collections import defaultdict
23
24
from typing import Dict , Iterable
24
25
25
26
from google .cloud import compute_v1
@@ -45,14 +46,14 @@ def list_all_instances(
45
46
46
47
agg_list = instance_client .aggregated_list (request = request )
47
48
48
- all_instances = {}
49
+ all_instances = defaultdict ( list )
49
50
print ("Instances found:" )
50
51
# Despite using the `max_results` parameter, you don't need to handle the pagination
51
52
# yourself. The returned `AggregatedListPager` object handles pagination
52
53
# automatically, returning separated pages as you iterate over the results.
53
54
for zone , response in agg_list :
54
55
if response .instances :
55
- all_instances [zone ] = response .instances
56
+ all_instances [zone ]. extend ( response .instances )
56
57
print (f" { zone } :" )
57
58
for instance in response .instances :
58
59
print (f" - { instance .name } ({ instance .machine_type } )" )
You can’t perform that action at this time.
0 commit comments