Skip to content

Commit 97faa21

Browse files
author
guido tijskens
committed
LINUX-11499 oci-metadata --value-only returns None
1 parent c056682 commit 97faa21

File tree

3 files changed

+53
-39
lines changed

3 files changed

+53
-39
lines changed

buildrpm/oci-utils.spec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ rm -rf %{buildroot}
182182
/opt/oci-utils/tests/__init__*
183183

184184
%changelog
185+
* Tue Sep 07 2021 Guido Tijskens <guido.tijskens@oracle.com> -- 0.12.5.10
186+
- LINUX-11499: oci-metadata --value-only returning null
187+
185188
* Fri Aug 27 2021 Guido Tijskens <guido.tijskens@oracle.com> -- 0.12.5.9
186189
- LINUX-11457: public API oci_api missing get_object_storage_client
187190

lib/oci_utils/impl/oci-metadata-main.py

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -105,38 +105,31 @@ def parse_args():
105105
-------
106106
The argparse namespace.
107107
"""
108-
parser = argparse.ArgumentParser(description='Utility for displaying '
109-
'metadata for an instance '
110-
'running in '
111-
'the Oracle Cloud '
112-
'Infrastructure.',
108+
parser = argparse.ArgumentParser(description='Utility for displaying metadata for an instance running in '
109+
'the Oracle Cloud Infrastructure.',
113110
add_help=False)
114111
parser.add_argument('-h', '--human-readable', action='store_true',
115112
help='Display human readable output (default)')
116113
parser.add_argument('-j', '--json', action='store_true',
117114
help='Display json output')
118115
parser.add_argument('-g', '--get', metavar='KEY', dest='keys',
119116
action='append', type=str.lower,
120-
help='Display the value of a specific key. '
121-
'Key can be any single field-name in metadata json output,'
122-
'or a path like /instance/id, or /vnics/*/vnicid')
117+
help='Display the value of a specific key. Key can be any single field-name in metadata json '
118+
'output,or a path like /instance/id, or /vnics/*/vnicid')
123119
parser.add_argument('--value-only', action='store_true',
124120
help='Used with one -g option, return a list of values matching the key.')
125121

126122
parser.add_argument('--export', action='store_true',
127-
help='Used with the -g option, export the keys as '
128-
'environment variables.')
123+
help='Used with the -g option, export the keys as environment variables.')
129124
parser.add_argument('--trim', action='store_true',
130-
help='Used with the -g option, trim the key path to '
131-
'the last component.')
125+
help='Used with the -g option, trim the key path to the last component.')
132126
parser.add_argument('-u', '--update', nargs='+', metavar='KEY=VALUE ',
133127
dest='setkeys',
134128
action='append', type=str,
135-
help='Update the value for a specific key. KEY can '
136-
'be displayName or a key in the extended metadata.'
137-
' VALUE can be a string, JSON data or a pointer '
138-
'to a file containing JSON data.'
139-
' Note: do not put spaces around "=".'
129+
help='Update the value for a specific key. '
130+
'KEY can be displayName or a key in the extended metadata. '
131+
'VALUE can be a string, JSON data or a pointer to a file containing JSON data. '
132+
'Note: do not put spaces around "=".'
140133
)
141134
parser.add_argument('-i', '--instance-id', metavar='OCID',
142135
action='store', type=str,
@@ -367,6 +360,7 @@ def verify_setkeys(set_keys):
367360
----------
368361
set_keys: dict
369362
The list of key-value pairs
363+
370364
Returns
371365
-------
372366
bool
@@ -491,7 +485,7 @@ def get_trimed_key_values(keys, metadata):
491485
if v:
492486
exportKeys[ke].append(v)
493487
continue
494-
v = get_values(ke, metadata)
488+
v = get_values(ke, metadata)
495489
if isinstance(v, list):
496490
exportKeys[ke].extend(v)
497491
elif v is not None:
@@ -507,11 +501,13 @@ def remove_list_for_single_item_list(dic):
507501
one or less item, then the key
508502
will be equal to the item in the list.
509503
510-
Inputs:
511-
dic: a dictionary
504+
Parameters
505+
----------
506+
dic: a dictionary
512507
513-
Returns:
514-
dic: the changed dictionary
508+
Returns
509+
-------
510+
dic: the changed dictionary
515511
"""
516512
for k, v in dic.items():
517513
if isinstance(v, list):
@@ -523,14 +519,16 @@ def remove_list_for_single_item_list(dic):
523519

524520
def print_trimed_key_values(keys, metadata):
525521
"""
526-
print the trimmed key and its value.
522+
Print the trimmed key and its value.
527523
528-
Inputs:
529-
keys: a list of getting keys.
530-
metadata: a dict of matching values
524+
Parameters
525+
----------
526+
keys: a list of getting keys.
527+
metadata: a dict of matching values
531528
532-
Returns:
533-
None.
529+
Returns
530+
-------
531+
No return value.
534532
"""
535533

536534
kValues = get_trimed_key_values(keys, metadata)
@@ -539,33 +537,44 @@ def print_trimed_key_values(keys, metadata):
539537
for item in v:
540538
print(k + ": " + str(item))
541539
else:
542-
print(k + ": " + str(v))
540+
if k == 'region':
541+
region = oci_regions[v] if v in oci_regions else str(v)
542+
print(k + ": " + region)
543+
else:
544+
print(k + ": " + str(v))
543545

544546

545547
def print_value_only(keys, metadata):
546548
"""
547-
print the values only for the matching key.
549+
Print the values only for the matching key.
548550
549-
Inputs:
550-
keys: a list of getting keys.
551-
metadata: a dict of matching values
552551
553-
Returns:
554-
None.
552+
Parameters
553+
----------
554+
keys: a list of getting keys.
555+
metadata: a dict of matching values
556+
557+
Returns
558+
-------
559+
No return value.
555560
"""
556561

557562
kValues = get_trimed_key_values(keys, metadata)
558-
for _, v in kValues.items():
563+
for k, v in kValues.items():
559564
if isinstance(v, list):
560565
for item in v:
561566
print(str(item))
562567
else:
563-
print(str(v))
568+
if k == 'region':
569+
region = oci_regions[v] if v in oci_regions else str(v)
570+
print(region)
571+
else:
572+
print(str(v))
564573

565574

566575
def export_keys(keys, metadata):
567576
"""
568-
export the key and values in the metadata.
577+
Export the key and values in the metadata.
569578
570579
Parameters
571580
----------
@@ -705,6 +714,7 @@ def main():
705714
if args.trim:
706715
print_trimed_key_values(args.keys, metadata)
707716
return 0
717+
708718
if args.json:
709719
print(json.dumps(metadata, default=dumper, indent=2))
710720
return 0

lib/oci_utils/metadata.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,8 @@ def _filter(self, metadata, keys):
413413
return None
414414
return new_metadata
415415
if isinstance(metadata, tuple):
416-
filtered_tuple = [filter_results(x, keys) for x in metadata]
416+
# _GT_ filtered_tuple = [filter_results(x, keys) for x in metadata]
417+
filtered_tuple = [(x, keys) for x in metadata]
417418
for a in filtered_tuple:
418419
if a is not None:
419420
return tuple(filtered_tuple)

0 commit comments

Comments
 (0)