Skip to content

Commit

Permalink
Condition on value not index when checking for lastModifiedTime. (#454)
Browse files Browse the repository at this point in the history
When lastModifiedTime is the first field in the list, the 'if' on it's index
would fail as a zero value is false. Use field value instead.

Co-authored-by: Jacob Ferriero <jferriero@google.com>
  • Loading branch information
bmenasha and Jacob Ferriero authored Apr 28, 2020
1 parent 6e6f84c commit 58034a6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/asset-inventory/asset_inventory/api_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ def _add_asset_export_fields(cls,
}]
if include_resource:
resource_schema = list(schema)
last_modified, _ = bigquery_schema.get_field_by_name(
_, last_modified = bigquery_schema.get_field_by_name(
resource_schema,
'lastModifiedTime')
if not last_modified:
Expand Down
30 changes: 30 additions & 0 deletions tools/asset-inventory/tests/test_api_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,36 @@ def test_for_for_asset_type(self):
# name, asset_type, timestamp, resource, iam_policy
self.assertEqual(len(schema), 5)

def test_resource_last_modified(self):
# Test that resource lastModifiedTime takes precedence.
APISchema._discovery_document_cache = {
'https://www.googleapis.com/discovery/v1/apis/compute/v1/rest': {
'id': 'compute.v1',
'schemas': {
'Machine': {
'properties': {
'lastModifiedTime': {
'type': 'string',
'description': 'Track time of last change.'
}}}}},
'https://content.googleapis.com/discovery/v1/apis': {
'items': [{
'name': 'compute',
'version': 'v1',
'discoveryRestUrl': 'https://www.googleapis.com/discovery/v1/apis/compute/v1/rest'}]}}
schema = APISchema.bigquery_schema_for_resource(
'google.compute.Machine',
'Machine',
'https://www.googleapis.com/discovery/v1/apis/compute/v1/rest',
True, True)
data_fields = self.get_schema_data_field(schema)
self.assertEqual(
[{'field_type': 'STRING',
'name': 'lastModifiedTime',
'description': 'Track time of last change.',
'mode': 'NULLABLE'}],
data_fields)

def test_self_recursive_properties(self):
discovery_doc = {
'id': 'recursive#api',
Expand Down

0 comments on commit 58034a6

Please sign in to comment.