Skip to content
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

feat: Add a task for writing (edge) identity overrides #3127

Merged
merged 7 commits into from
Dec 10, 2023

Conversation

khvn26
Copy link
Member

@khvn26 khvn26 commented Dec 8, 2023

Closes #3106.

  • I have run pre-commit to check linting
  • I have added information to docs/ if required so people know about the feature!
  • I have filled in the "Changes" section below?
  • I have filled in the "How did you test this code" section below?
  • I have used a Conventional Commit title for this Pull Request

Changes

This PR adds the following:

  1. update_flagsmith_environments_v2_identity_overrides task responsible for parsing changes generated in EdgeIdentity.save() method and reflecting them in DynamoDB environments_v2_table.
  2. The new task is now invoked in EdgeIdentity.save() method, in case identity override changes are detected, along with the audit log entries generation task.
  3. EdgeIdentity.save() method is now invoked during the identity override trimming routine by the sync_identity_document_features task whenever features are deleted.
  4. All relevant code accommodated for environments_v2_table's newly string-typed environment_id column.

Various touch-ups and improvements:

  • Task processor now marshals datetime arguments.
  • DynamoEnvironmentV2Wrapper slightly reworked (see comment)

How did you test this code?

The code is covered by unit tests.
I've also tested this by pointing local Core API instance at environments_v2_table in staging.

Copy link

vercel bot commented Dec 8, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2023 9:55pm
flagsmith-frontend-preview ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2023 9:55pm
flagsmith-frontend-staging ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 9, 2023 9:55pm

@github-actions github-actions bot added the api Issue related to the REST API label Dec 8, 2023
@khvn26 khvn26 changed the title feat: Add new files and update existing files feat: Task for writing (edge) identity overrides Dec 8, 2023
Copy link
Contributor

github-actions bot commented Dec 8, 2023

Uffizzi Preview deployment-42464 was deleted.

@codecov-commenter
Copy link

codecov-commenter commented Dec 8, 2023

Codecov Report

Attention: 3 lines in your changes are missing coverage. Please review.

Comparison is base (0604ec1) 95.90% compared to head (5a7a551) 95.95%.
Report is 4 commits behind head on main.

Files Patch % Lines
api/edge_api/identities/utils.py 86.66% 2 Missing ⚠️
api/environments/dynamodb/utils.py 90.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3127      +/-   ##
==========================================
+ Coverage   95.90%   95.95%   +0.04%     
==========================================
  Files        1048     1053       +5     
  Lines       31384    31588     +204     
==========================================
+ Hits        30100    30311     +211     
+ Misses       1284     1277       -7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@khvn26 khvn26 changed the title feat: Task for writing (edge) identity overrides feat: Add a task for writing (edge) identity overrides Dec 8, 2023
api/conftest.py Outdated
Comment on lines 551 to 596
@pytest.fixture()
def aws_credentials():
"""Mocked AWS Credentials for moto."""
os.environ["AWS_ACCESS_KEY_ID"] = "testing"
os.environ["AWS_SECRET_ACCESS_KEY"] = "testing"
os.environ["AWS_SECURITY_TOKEN"] = "testing"
os.environ["AWS_SESSION_TOKEN"] = "testing"
os.environ["AWS_DEFAULT_REGION"] = "eu-west-2"


@pytest.fixture()
def dynamodb(aws_credentials):
# TODO: move all wrapper tests to using moto
with mock_dynamodb():
yield boto3.resource("dynamodb")


@pytest.fixture()
def flagsmith_environments_v2_table(dynamodb) -> Table:
return dynamodb.create_table(
TableName="flagsmith_environments_v2",
KeySchema=[
{
"AttributeName": "environment_id",
"KeyType": "HASH",
},
{
"AttributeName": "document_key",
"KeyType": "RANGE",
},
],
AttributeDefinitions=[
{"AttributeName": "environment_id", "AttributeType": "N"},
{"AttributeName": "document_key", "AttributeType": "S"},
],
BillingMode="PAY_PER_REQUEST",
)


@pytest.fixture
def dynamodb_wrapper_v2(
settings: SettingsWrapper,
flagsmith_environments_v2_table: Table,
) -> DynamoEnvironmentV2Wrapper:
settings.ENVIRONMENTS_V2_TABLE_NAME_DYNAMO = flagsmith_environments_v2_table.name
return DynamoEnvironmentV2Wrapper()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewelwell I've relocated these from your endpoint PR so they can be used by some other tests too.

key_expression_condition = Key(self.ENVIRONMENT_ID_ATTRIBUTE).eq(
environment_id
) & Key(self.DOCUMENT_KEY_ATTRIBUTE).begins_with(document_key_begins_with)
def get_table_name(self):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewelwell Various improvements here that will introduce conflicts in your PR:

  • Constants live in their own module now.
  • get_identity_overrides is now get_identity_overrides_by_feature_id to avoid dead code paths — to my best knowledge, only Edge API will need to fetch overrides by environment id alone.

Comment on lines +52 to +61
def map_environment_to_environment_v2_document(
environment: "Environment",
) -> Document:
return {
**map_environment_to_environment_document(environment),
"document_key": "META",
"environment_id": str(environment.id),
}


Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@matthewelwell This part is also from your PR, with slight changes.

) -> dict[str, typing.Any]:
return {
**feature_state.dict(),
"feature_state_value": feature_state.get_value(identity_id),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just making a note here as well that this is different to the previous behaviour - this used to be "value" I think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I can tell, this data is used in the generate_audit_log_records task (as well as the new task you created) but the value is not actually used so I think this is fine.

Copy link
Member Author

@khvn26 khvn26 Dec 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, "value" was not used. I made sure to modify the generate_audit_log_records tests accordingly.

Comment on lines +76 to +80
class IdentityOverrideV2(BaseModel):
environment_id: str
document_key: str
environment_api_key: str
feature_state: FeatureStateModel
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might want to integrate this with my code too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've done this, but I also had to add a new attribute: identifier: str to it as well. More detail in a comment on my PR here.

@khvn26 khvn26 added this pull request to the merge queue Dec 10, 2023
Merged via the queue into main with commit 2a9cd7c Dec 10, 2023
@khvn26 khvn26 deleted the feat/environments-v2-task branch December 10, 2023 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api Issue related to the REST API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Populate flagsmith_environments_v2 from flagsmith_identities
3 participants