Skip to content

Commit

Permalink
Fix loading of commitments_schema. (GoogleCloudPlatform#455)
Browse files Browse the repository at this point in the history
Code expects a commitments_schema.schema field, so define it.
Other minor fixes:

- Add creator to the bigquery dataset so Terraform doesn't complain when
  it's added post creation.
- Remove build product files in the tests directory.
- Add .gitingore for terraform files.
- Cleanup Readme.md a bit.

Co-authored-by: Jacob Ferriero <jferriero@google.com>
  • Loading branch information
bmenasha and Jacob Ferriero authored Apr 28, 2020
1 parent ac98544 commit 6e6f84c
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 262 deletions.
431 changes: 203 additions & 228 deletions tools/cuds-prioritized-attribution/README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an 'AS IS' BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


[{"type":"STRING",
"name":"id",
"mode":"REQUIRED"},
{"type":"STRING",
"name":"folder_ids",
"mode":"NULLABLE"},
{"type":"STRING",
"name":"project_ids",
"mode":"NULLABLE"},
{"type":"STRING",
"name":"commitments_unit_type",
"mode":"REQUIRED"},
{"type":"STRING",
"name":"commitments_cud_type",
"mode":"REQUIRED"},
{"type":"NUMERIC",
"name":"commitments_amount",
"mode":"REQUIRED"},
{"type":"STRING",
"name":"commitments_region",
"mode":"REQUIRED"},
{"type":"DATE",
"name":"commit_start_date",
"mode":"REQUIRED"},
{"type":"DATE",
"name":"commit_end_date",
"mode":"REQUIRED"}
]
schema = [{'type': 'STRING',
'name': 'id',
'mode': 'REQUIRED'},
{'type': 'STRING',
'name': 'folder_ids',
'mode': 'NULLABLE'},
{'type': 'STRING',
'name': 'project_ids',
'mode': 'NULLABLE'},
{'type': 'STRING',
'name': 'commitments_unit_type',
'mode': 'REQUIRED'},
{'type': 'STRING',
'name': 'commitments_cud_type',
'mode': 'REQUIRED'},
{'type': 'NUMERIC',
'name': 'commitments_amount',
'mode': 'REQUIRED'},
{'type': 'STRING',
'name': 'commitments_region',
'mode': 'REQUIRED'},
{'type': 'DATE',
'name': 'commit_start_date',
'mode': 'REQUIRED'},
{'type': 'DATE',
'name': 'commit_end_date',
'mode': 'REQUIRED'}]
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import json
from typing import Dict
from typing import List
from google.cloud import bigquery
from google.cloud import storage
Expand Down Expand Up @@ -110,16 +111,16 @@ def local_to_gcs(bucket_name: str, object_name: str, input_file: str) -> None:
blob.upload_from_file(file_obj)


def convert_to_schema(commitment_schema: str) -> List[bigquery.SchemaField]:
def convert_to_schema(schema: List[Dict[str, str]) -> List[bigquery.SchemaField]:
"""Read the schema as a JSON and reformats as an array.
Args:
commitment_schema: String to convert to JSON which holds the schema
schema: list of dicts to convert to list of SchemaField
Returns:
List of bigquery.SchemaField objects holding the schema.
"""
input_fields = json.loads(commitment_schema)
input_fields = schema
schema = []
for input_field in input_fields:
schema.append(
Expand Down
4 changes: 4 additions & 0 deletions tools/cuds-prioritized-attribution/terraform/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.terraform
terraform.tfstate*
terraform.tfvars
plan.*
11 changes: 10 additions & 1 deletion tools/cuds-prioritized-attribution/terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,20 @@ resource "google_project_iam_binding" "composer_binding" {
role = "roles/composer.worker"
}

# Get the creator's user name.
data "google_client_openid_userinfo" "me" {
}


# Grant BigQuery Permissions for Service Account on Corrected Dataset
resource "google_bigquery_dataset" "corrected_dataset" {
dataset_id = var.corrected_dataset_id
location = var.billing_export_location

depends_on = [google_project_service.bqapi]
access {
role = "OWNER"
user_by_email = data.google_client_openid_userinfo.me.email
}
access {
role = "WRITER"
user_by_email = google_service_account.cud_service_account.email
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.

0 comments on commit 6e6f84c

Please sign in to comment.