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

[COST-4716] Prevent removal of stale tags linked to a mapping. #4948

Merged
merged 11 commits into from
Mar 7, 2024

Conversation

myersCody
Copy link
Contributor

@myersCody myersCody commented Feb 28, 2024

Jira Ticket

COST-4716

Description

This change will prevent removing a stale enabled tag key if it is linked to a tag mapping.

Testing

testing instructions:

  1. Delete a key that you just mapped from the tag summary:
    DELETE FROM reporting_awstags_summary where key='com_redhat_rhel_usage';

  2. Check that the key would of been returned with the new sql structure:

WITH cte_tag_mapping AS(
    SELECT distinct
        array_agg(child_id) as children_uuids,
        array_agg(parent_id) as parent_uuids
    FROM reporting_tagmapping
)
SELECT * FROM reporting_enabledtagkeys etk
CROSS JOIN cte_tag_mapping tag_map
WHERE NOT EXISTS (
    SELECT 1
    FROM reporting_awstags_summary AS ts
    WHERE ts.key = etk.key
    AND etk.provider_type = 'AWS'
)
AND etk.enabled = true
AND etk.provider_type = 'AWS'
AND NOT etk.uuid = ANY(tag_map.children_uuids || tag_map.parent_uuids);
  1. Create a tag mapping with the key.
    url: http://localhost:8000/api/cost-management/v1/settings/tags/mappings/child/add/
    body:
{
    "parent": "f1df9dba-e8f8-43b6-8489-41c645474fbe",
    "children": ["1299f779-46eb-41da-b60e-c95e1946650b", "d1376436-2726-4dad-a3c3-e45a7b9e10df", "3c21b1fd-fb98-4dce-8999-a8be4996724b"]
}

You can find the uuids to use here:

http://localhost:8000/api/cost-management/v1/settings/tags/?limit=100
  1. Rerun the sql:
WITH cte_tag_mapping AS(
    SELECT distinct
        array_agg(child_id) as children_uuids,
        array_agg(parent_id) as parent_uuids
    FROM reporting_tagmapping
)
SELECT * FROM reporting_enabledtagkeys etk
CROSS JOIN cte_tag_mapping tag_map
WHERE NOT EXISTS (
    SELECT 1
    FROM reporting_awstags_summary AS ts
    WHERE ts.key = etk.key
    AND etk.provider_type = 'AWS'
)
AND etk.enabled = true
AND etk.provider_type = 'AWS'
AND NOT etk.uuid = ANY(tag_map.children_uuids || tag_map.parent_uuids);
 uuid | key | enabled | provider_type | children_uuids | parent_uuids
------+-----+---------+---------------+----------------+--------------
(0 rows)

See that the enabled tag would not be deleted.

  1. Delete another value from the tag summary tables for each provide.
    Then post to the following endpoint:
    http://127.0.0.1:5042/api/cost-management/v1/enabled_tags/
{
    "schema": "org1234567",
    "action": "remove_stale",
    "provider_type": "aws"
}

Note that the provider_type here is all lower case unlike our other endpoints. This will run the stale removal SQL for you. You should see the key not associated with a tag mapping disappear, but the one that is stay.

Notes

...

@myersCody myersCody marked this pull request as ready for review March 1, 2024 19:47
@myersCody myersCody requested review from a team as code owners March 1, 2024 19:47
@myersCody myersCody added the smoke-tests pr_check will build the image and run minimal required smokes label Mar 1, 2024
Copy link

codecov bot commented Mar 1, 2024

Codecov Report

Merging #4948 (9c038b1) into main (d3f546c) will not change coverage.
The diff coverage is n/a.

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #4948   +/-   ##
=====================================
  Coverage   94.0%   94.0%           
=====================================
  Files        375     375           
  Lines      31129   31129           
  Branches    3698    3698           
=====================================
  Hits       29262   29262           
  Misses      1193    1193           
  Partials     674     674           

Copy link
Contributor

@cgoodfred cgoodfred left a comment

Choose a reason for hiding this comment

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

LGTM, great stuff

@myersCody myersCody enabled auto-merge (squash) March 7, 2024 14:12
@myersCody
Copy link
Contributor Author

/retest

1 similar comment
@samdoran
Copy link
Contributor

samdoran commented Mar 7, 2024

/retest

@samdoran
Copy link
Contributor

samdoran commented Mar 7, 2024

Test failures are unrelated to changes in this PR.

@samdoran
Copy link
Contributor

samdoran commented Mar 7, 2024

/retest

@myersCody myersCody merged commit c58f3e0 into main Mar 7, 2024
11 checks passed
@myersCody myersCody deleted the COST-4716-prevent-stale-removal branch March 7, 2024 21:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
smoke-tests pr_check will build the image and run minimal required smokes smokes-required
Projects
None yet
3 participants