Skip to content

Commit

Permalink
Merge pull request #75 from release-engineering/fixbc
Browse files Browse the repository at this point in the history
Community: Set billing type for AMI and Snapshot
  • Loading branch information
JAVGan authored Nov 11, 2024
2 parents 575b667 + a4bb51a commit edcd740
Show file tree
Hide file tree
Showing 47 changed files with 608 additions and 370 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cloudimg>=1.14.0
cloudimg>=1.15.0
pubtools>=1.3.0
more_executors>=2.11.4
pushcollector>=1.3.0
Expand Down
10 changes: 10 additions & 0 deletions src/pubtools/_marketplacesvm/cloud_providers/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ def _upload(
self,
push_item: AmiPushItem,
custom_tags: Optional[Dict[str, str]] = None,
ami_tags: Optional[Dict[str, str]] = None,
snapshot_tags: Optional[Dict[str, str]] = None,
**kwargs,
) -> Tuple[AmiPushItem, Any]:
"""
Expand All @@ -358,6 +360,10 @@ def _upload(
The push item with the required data to upload the AMI image into AWS.
custom_tags (dict, optional)
Dictionary with keyword values to be added as custom tags.
snapshot_tags (dict, optional)
Dictionary with keyword values to be added as tags for the snapshot
ami_tags (dict, optional)
Dictionary with keyword values to be added as tags for the AMI
groups (list, optional)
List of groups to share the image with. Defaults to ``self.aws_groups``.
accounts (list, optional)
Expand Down Expand Up @@ -430,6 +436,10 @@ def _upload(
upload_metadata_kwargs.update({"boot_mode": push_item.boot_mode.value})
if push_item.billing_codes:
upload_metadata_kwargs.update({"billing_products": push_item.billing_codes.codes})
if ami_tags:
upload_metadata_kwargs.update({"ami_tags": ami_tags})
if snapshot_tags:
upload_metadata_kwargs.update({"snapshot_tags": snapshot_tags})

LOG.debug("%s", upload_metadata_kwargs)
metadata = AWSUploadMetadata(**upload_metadata_kwargs)
Expand Down
9 changes: 8 additions & 1 deletion src/pubtools/_marketplacesvm/tasks/community_push/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ def _upload(
accounts = kwargs.get("accounts") or kwargs.get("sharing_accounts")
snapshot_accounts = kwargs.get("snapshot_accounts")
ami_version_template = kwargs.get("ami_version_template")
ami_tags = kwargs.get("ami_tags")
snapshot_tags = kwargs.get("snapshot_tags")
log.info(
"Uploading %s to region %s (type: %s, ship: %s, account: %s) with sharing accounts: %s and snapshot accounts: %s", # noqa: E501
push_item.src,
Expand All @@ -379,6 +381,8 @@ def _upload(
pi, image = self.cloud_instance(marketplace).upload(
push_item,
custom_tags=custom_tags,
ami_tags=ami_tags,
snapshot_tags=snapshot_tags,
container=container,
accounts=accounts,
snapshot_accounts=snapshot_accounts,
Expand Down Expand Up @@ -409,6 +413,8 @@ def _upload(
pi, _ = self.cloud_instance(marketplace).upload(
push_item,
custom_tags=custom_tags,
ami_tags=ami_tags,
snapshot_tags=snapshot_tags,
container=container,
accounts=accounts,
snapshot_accounts=snapshot_accounts,
Expand Down Expand Up @@ -499,7 +505,8 @@ def _data_to_upload(

# Add the billing type on tags if it's set
if pi.type:
additional_args["custom_tags"] = {"billing_type": pi.type}
additional_args["ami_tags"] = {"billing_type": pi.type}
additional_args["snapshot_tags"] = {"billing_type": pi.type}

# Generate the push items to upload
params = {
Expand Down
8 changes: 7 additions & 1 deletion tests/cloud_providers/test_provider_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ def test_upload_custom_tags(
binfo = aws_push_item.build_info

custom_tags = {"custom_key": "custom_value"}
ami_tags = {"ami_key": "ami_value"}
snapshot_tags = {"snapshot_key": "snapshot_value"}

tags = {
"arch": aws_push_item.release.arch,
Expand Down Expand Up @@ -611,13 +613,17 @@ def test_upload_custom_tags(
"sriov_net_support": aws_push_item.sriov_net_support,
"ena_support": aws_push_item.ena_support,
"tags": tags,
"ami_tags": ami_tags,
"snapshot_tags": snapshot_tags,
}
meta_obj = MagicMock(**metadata)
mock_metadata.return_value = meta_obj

fake_aws_provider.upload_svc_partial.return_value.publish.return_value = FakeImageResp() # type: ignore [attr-defined] # noqa: E501

fake_aws_provider.upload(aws_push_item, custom_tags=custom_tags)
fake_aws_provider.upload(
aws_push_item, custom_tags=custom_tags, ami_tags=ami_tags, snapshot_tags=snapshot_tags
)

mock_metadata.assert_called_once_with(**metadata)
fake_aws_provider.upload_svc_partial.return_value.publish.assert_called_once_with(meta_obj) # type: ignore [attr-defined] # noqa: E501
Expand Down
8 changes: 6 additions & 2 deletions tests/community_push/test_community_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -871,15 +871,19 @@ def test_do_community_push_different_sharing_accounts(
[
mock.call(
mock.ANY,
custom_tags={'billing_type': 'access'},
custom_tags=None,
ami_tags={'billing_type': 'access'},
snapshot_tags={'billing_type': 'access'},
container='redhat-cloudimg-fake-destination',
accounts=['first_account', 'second_account'],
snapshot_accounts=None,
ami_version_template='{major}.{minor}.0',
),
mock.call(
mock.ANY,
custom_tags={'billing_type': 'access'},
custom_tags=None,
ami_tags={'billing_type': 'access'},
snapshot_tags={'billing_type': 'access'},
container='redhat-cloudimg-fake-destination2',
accounts=['third_account', 'fourth_account'],
snapshot_accounts=None,
Expand Down
Loading

0 comments on commit edcd740

Please sign in to comment.