-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
metadata service: upload metadata files to gcs #25115
Merged
alafanechere
merged 3 commits into
master
from
augustin/metadata-service/upload-metadata-files-to-gcs
Apr 12, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import click | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
import pathlib | ||
|
||
import click | ||
from metadata_service.gcs_upload import upload_metadata_to_gcs | ||
from metadata_service.validators.metadata_validator import validate_metadata_file | ||
from pydantic import ValidationError | ||
|
||
|
||
@click.group(help="Airbyte Metadata Service top-level command group.") | ||
|
@@ -12,7 +17,7 @@ def metadata_service(): | |
@metadata_service.command(help="Validate a given metadata YAML file.") | ||
@click.argument("file_path", type=click.Path(exists=True, path_type=pathlib.Path)) | ||
def validate(file_path: pathlib.Path): | ||
file_path = file_path if not file_path.is_dir() else file_path / "metadata.yml" | ||
file_path = file_path if not file_path.is_dir() else file_path / "metadata.yaml" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Maybe |
||
|
||
click.echo(f"Validating {file_path}...") | ||
|
||
|
@@ -23,3 +28,22 @@ def validate(file_path: pathlib.Path): | |
click.echo(f"{file_path} is not a valid ConnectorMetadataDefinitionV1 YAML file.") | ||
click.echo(str(error)) | ||
exit(1) | ||
|
||
|
||
@metadata_service.command(help="Upload a metadata YAML file to a GCS bucket.") | ||
@click.argument("metadata-file-path", type=click.Path(exists=True, path_type=pathlib.Path)) | ||
@click.argument("bucket-name", type=click.STRING) | ||
@click.option( | ||
"--service-account-file-path", "-sa", type=click.Path(exists=True, path_type=pathlib.Path), envvar="GOOGLE_APPLICATION_CREDENTIALS" | ||
) | ||
def upload(metadata_file_path: pathlib.Path, bucket_name: str, service_account_file_path: pathlib.Path): | ||
metadata_file_path = metadata_file_path if not metadata_file_path.is_dir() else metadata_file_path / "metadata.yaml" | ||
try: | ||
uploaded, blob_id = upload_metadata_to_gcs(bucket_name, metadata_file_path, service_account_file_path) | ||
except (ValidationError, FileNotFoundError) as e: | ||
click.secho(f"The metadata file could not be uploaded: {str(e)}", color="red") | ||
exit(1) | ||
if uploaded: | ||
click.secho(f"The metadata file {metadata_file_path} was uploaded to {blob_id}.", color="green") | ||
else: | ||
click.secho(f"The metadata file {metadata_file_path} was not uploaded.", color="yellow") |
41 changes: 41 additions & 0 deletions
41
airbyte-ci/connectors/metadata_service/lib/metadata_service/gcs_upload.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
# | ||
from pathlib import Path | ||
from typing import Tuple | ||
|
||
import yaml | ||
from google.cloud import storage | ||
from google.oauth2 import service_account | ||
from metadata_service.models.generated.ConnectorMetadataDefinitionV1 import ConnectorMetadataDefinitionV1 | ||
|
||
|
||
def upload_metadata_to_gcs(bucket_name: str, metadata_file_path: Path, service_account_file_path: Path) -> Tuple[bool, str]: | ||
"""Upload a metadata file to a GCS bucket. | ||
|
||
If the per 'version' key already exists it won't be overwritten. | ||
Also updates the 'latest' key on each new version. | ||
|
||
Args: | ||
bucket_name (str): Name of the GCS bucket to which the metadata file will be uploade. | ||
metadata_file_path (Path): Path to the metadata file. | ||
service_account_file_path (Path): Path to the JSON file with the service account allowed to read and write on the bucket. | ||
Returns: | ||
Tuple[bool, str]: Whether the metadata file was uploaded and its blob id. | ||
""" | ||
uploaded = False | ||
raw_metadata = yaml.safe_load(metadata_file_path.read_text()) | ||
metadata = ConnectorMetadataDefinitionV1.parse_obj(raw_metadata) | ||
|
||
credentials = service_account.Credentials.from_service_account_file(service_account_file_path) | ||
storage_client = storage.Client(credentials=credentials) | ||
bucket = storage_client.bucket(bucket_name) | ||
|
||
version_blob = bucket.blob(f"metadata/{metadata.data.dockerRepository}/{metadata.data.dockerImageTag}/metadata.yaml") | ||
latest_blob = bucket.blob(f"metadata/{metadata.data.dockerRepository}/latest/metadata.yaml") | ||
if not version_blob.exists(): | ||
version_blob.upload_from_filename(str(metadata_file_path)) | ||
uploaded = True | ||
if version_blob.etag != latest_blob.etag: | ||
latest_blob.upload_from_filename(str(metadata_file_path)) | ||
return uploaded, version_blob.id |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New guy question: What is the command that adds this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's the pre-commit hook that does it.
pre-commit install
and you should be good to go.