| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -15,7 +15,7 @@ | |
| import contextlib | ||
| import itertools | ||
| import logging | ||
| from collections import OrderedDict | ||
| from collections import OrderedDict, defaultdict | ||
| from datetime import datetime | ||
| from typing import Any, Callable, Dict, List, Literal, Optional, Sequence, Tuple, Union | ||
|
|
||
| Expand Down Expand Up | @@ -138,6 +138,38 @@ async def close(self): | |
| def async_supported(self) -> SupportedAsyncMethods: | ||
| return SupportedAsyncMethods(read=True, write=True) | ||
|
|
||
| @staticmethod | ||
| def _table_tags(online_config, table_instance) -> list[dict[str, str]]: | ||
| table_instance_tags = table_instance.tags or {} | ||
| online_tags = online_config.tags or {} | ||
|
|
||
| common_tags = [ | ||
| {"Key": key, "Value": table_instance_tags.get(key) or value} | ||
| for key, value in online_tags.items() | ||
| ] | ||
| table_tags = [ | ||
| {"Key": key, "Value": value} | ||
| for key, value in table_instance_tags.items() | ||
| if key not in online_tags | ||
| ] | ||
|
|
||
| return common_tags + table_tags | ||
|
|
||
| @staticmethod | ||
| def _update_tags(dynamodb_client, table_name: str, new_tags: list[dict[str, str]]): | ||
| table_arn = dynamodb_client.describe_table(TableName=table_name)["Table"][ | ||
| "TableArn" | ||
| ] | ||
| current_tags = dynamodb_client.list_tags_of_resource(ResourceArn=table_arn)[ | ||
| "Tags" | ||
| ] | ||
| if current_tags: | ||
| remove_keys = [tag["Key"] for tag in current_tags] | ||
| dynamodb_client.untag_resource(ResourceArn=table_arn, TagKeys=remove_keys) | ||
|
|
||
| if new_tags: | ||
| dynamodb_client.tag_resource(ResourceArn=table_arn, Tags=new_tags) | ||
|
|
||
| def update( | ||
| self, | ||
| config: RepoConfig, | ||
| Expand Down Expand Up | @@ -167,40 +199,43 @@ def update( | |
| online_config.endpoint_url, | ||
| online_config.session_based_auth, | ||
| ) | ||
| # Add Tags attribute to creation request only if configured to prevent | ||
| # TagResource permission issues, even with an empty Tags array. | ||
| kwargs = ( | ||
| { | ||
| "Tags": [ | ||
| {"Key": key, "Value": value} | ||
| for key, value in online_config.tags.items() | ||
| ] | ||
| } | ||
| if online_config.tags | ||
| else {} | ||
| ) | ||
|
|
||
| do_tag_updates = defaultdict(bool) | ||
| for table_instance in tables_to_keep: | ||
| # Add Tags attribute to creation request only if configured to prevent | ||
| # TagResource permission issues, even with an empty Tags array. | ||
| table_tags = self._table_tags(online_config, table_instance) | ||
| kwargs = {"Tags": table_tags} if table_tags else {} | ||
|
|
||
| table_name = _get_table_name(online_config, config, table_instance) | ||
| try: | ||
| dynamodb_resource.create_table( | ||
| TableName=_get_table_name(online_config, config, table_instance), | ||
| TableName=table_name, | ||
| KeySchema=[{"AttributeName": "entity_id", "KeyType": "HASH"}], | ||
| AttributeDefinitions=[ | ||
| {"AttributeName": "entity_id", "AttributeType": "S"} | ||
| ], | ||
| BillingMode="PAY_PER_REQUEST", | ||
| **kwargs, | ||
| ) | ||
|
|
||
| except ClientError as ce: | ||
| do_tag_updates[table_name] = True | ||
|
|
||
| # If the table creation fails with ResourceInUseException, | ||
| # it means the table already exists or is being created. | ||
| # Otherwise, re-raise the exception | ||
| if ce.response["Error"]["Code"] != "ResourceInUseException": | ||
| raise | ||
|
|
||
| for table_instance in tables_to_keep: | ||
| dynamodb_client.get_waiter("table_exists").wait( | ||
| TableName=_get_table_name(online_config, config, table_instance) | ||
| ) | ||
| table_name = _get_table_name(online_config, config, table_instance) | ||
| dynamodb_client.get_waiter("table_exists").wait(TableName=table_name) | ||
| # once table is confirmed to exist, update the tags. | ||
| # tags won't be updated in the create_table call if the table already exists | ||
| if do_tag_updates[table_name]: | ||
| tags = self._table_tags(online_config, table_instance) | ||
| self._update_tags(dynamodb_client, table_name, tags) | ||
|
Comment thread
Comment on lines
+236
to
+238
Copy link
Copy Markdown
Contributor
Author
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualityif the table already exists, then we should perform tag updates. otherwise we can skip that bc the tags would've been added in the create_table call
Sorry, something went wrong.
All reactions
|
||
|
|
||
| for table_to_delete in tables_to_delete: | ||
| _delete_table_idempotent( | ||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.
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.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualitythe table level tags override the global where applicable. eg
# in feature view py file we override for one particular instance tags={"team": "product-team"}Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.