Skip to content

Commit

Permalink
Adds restricting minor versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jajreidy committed Mar 22, 2023
1 parent 6a69e75 commit 4d0a799
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 39 deletions.
75 changes: 65 additions & 10 deletions cloudpub/aws/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from copy import deepcopy
from typing import Any, Dict, List

import dateutil.parser
from boto3.session import Session

from cloudpub.common import BaseService, PublishingMetadata
Expand Down Expand Up @@ -162,29 +163,34 @@ def get_product_version_details(self, entity_id: str, version_id: str) -> List[D

self._raise_error(NotFoundError, f"No such version with id \"{version_id}\"")

def get_product_version_ids(self, entity_id: str) -> List[Dict[str, Any]]:
def get_product_versions(self, entity_id: str) -> Dict[str, Any]:
"""
Get the ids of all the versions of a product. Product is searched by it's id.
Get the titles, ids, and date created of all the versions of a product.
Args:
entity_id (str)
The Id of the entity to edit
Returns:
List[Dict[str, Any]]: A list of version ids
Dict[str, Any]: A dictionary of versions
Raises:
NotFoundError when the product is not found.
"""
details = self.get_product_by_id(entity_id)
self._check_product_versions(details)

version_ids: List[Dict[str, Any]] = []
version_ids: Dict[str, Any] = {}

for v in details["Versions"]:
delivery_ids = []
delivery_options_list = []
for delivery_option in v["DeliveryOptions"]:
delivery_ids.append(delivery_option["Id"])
version = {v["VersionTitle"]: delivery_ids}
version_ids.append(version)
delivery_options_list.append(
{"id": delivery_option["Id"], "visibility": delivery_option["Visibility"]}
)
delivery_options = {
"delivery_options": delivery_options_list,
"created_date": v['CreationDate'],
}
version_ids[v['VersionTitle']] = delivery_options

return version_ids

Expand Down Expand Up @@ -301,7 +307,7 @@ def check_publish_status(self, change_set_id: str) -> str:

return rsp["Status"]

def wait_for_publish_task(
def wait_for_changeset(
self, change_set_id: str, attempts: int = 144, interval: int = 600
) -> None:
"""
Expand Down Expand Up @@ -357,6 +363,55 @@ def check_image_scan(self, ami_id: str) -> None:
"""
self._raise_error(NotImplementedError, "To be added at a future date")

def restrict_minor_versions(
self,
entity_id: str,
marketplace_entity_type: str,
restrict_version: str,
) -> None:
"""
Restrict the old minor versions of a release.
Args:
entity_id (str)
The entity id to modifiy.
marketplace_entity_type (str)
Product type of the AWS product
Example: AmiProduct
restrict_version (str)
The restrict version to look for.
example: 9.0
"""
versions = self.get_product_versions(entity_id)

# TODO: Version matching using regex

matching_version_list = [v for t, v in versions.items() if restrict_version in t]

if not matching_version_list:
return

newest_matching_version_created_date = max(
(x["created_date"] for x in matching_version_list),
key=lambda x: dateutil.parser.isoparse(x),
)

restrict_delivery_ids = []
for version in matching_version_list:
if newest_matching_version_created_date != version["created_date"]:
for del_opt in version["delivery_options"]:
# Usually there is only one delivery option
# but we'll iterate through to make sure nothing is missing
if del_opt["visibility"] == "Public":
restrict_delivery_ids.append(del_opt["id"])

if restrict_delivery_ids:
log.debug(f"Restricting these minor version(s) with id(s): {restrict_delivery_ids}")
change_id = self.set_restrict_versions(
entity_id, marketplace_entity_type, restrict_delivery_ids
)
self.wait_for_changeset(change_id)

def publish(self, metadata: AWSVersionMetadata) -> None:
"""
Add new version to an existing product.
Expand Down Expand Up @@ -409,4 +464,4 @@ def publish(self, metadata: AWSVersionMetadata) -> None:

log.debug(f"The response from publishing was: {rsp}")

self.wait_for_publish_task(rsp["ChangeSetId"])
self.wait_for_changeset(rsp["ChangeSetId"])
1 change: 1 addition & 0 deletions requirements-test.in
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ mypy
sphinx
sphinx-autodoc-typehints
types-requests
types-python-dateutil
36 changes: 35 additions & 1 deletion requirements-test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,16 @@ babel==2.11.0 \
--hash=sha256:1ad3eca1c885218f6dce2ab67291178944f810a10a9b5f3cb8382a5a232b64fe \
--hash=sha256:5ef4b3226b0180dedded4229651c8b0e1a3a6a2837d45a073272f313e4cf97f6
# via sphinx
boto3==1.26.80 \
--hash=sha256:704065abc8fdd2519491c11eeb65d69f21d0baf278b66b0b44090cc09c8c7bf8 \
--hash=sha256:7816da48b9626a482e955628ade527a4a84ff462043ac1a129011f7f66e8648c
# via cloudpub (setup.py)
botocore==1.29.80 \
--hash=sha256:19d4cadc79f75b31ffa78b5e750833d53e0edfe414d308b6788382ad32d23e12 \
--hash=sha256:9a301cb4de8ec6efe4e4b6ebb29b4301decd5b00c1a7f9187652002efb520ad0
# via
# boto3
# s3transfer
certifi==2022.12.7 \
--hash=sha256:35824b4c3a97115964b408844d64aa14db1cc518f6562e8d7261699d1350a9e3 \
--hash=sha256:4ad3232f5e926d6718ec31cfc1fcadfde020920e278684144551c91769c7bc18
Expand Down Expand Up @@ -118,6 +128,12 @@ jinja2==3.1.2 \
--hash=sha256:31351a702a408a9e7595a8fc6150fc3f43bb6bf7e319770cbc0db9df9437e852 \
--hash=sha256:6088930bfe239f0e6710546ab9c19c9ef35e29792895fed6e6e31a023a182a61
# via sphinx
jmespath==1.0.1 \
--hash=sha256:02e2e4cc71b5bcab88332eebf907519190dd9e6e82107fa7f83b1003a6252980 \
--hash=sha256:90261b206d6defd58fdd5e85f478bf633a2901798906be2ad389150c5c60edbe
# via
# boto3
# botocore
markupsafe==2.1.1 \
--hash=sha256:0212a68688482dc52b2d45013df70d169f542b7394fc744c02a57374a4207003 \
--hash=sha256:089cf3dbf0cd6c100f02945abeb18484bd1ee57a079aefd52cffd17fba910b88 \
Expand Down Expand Up @@ -220,6 +236,10 @@ pytest-cov==4.0.0 \
--hash=sha256:2feb1b751d66a8bd934e5edfa2e961d11309dc37b73b0eabe73b5945fee20f6b \
--hash=sha256:996b79efde6433cdbd0088872dbc5fb3ed7fe1578b68cdbba634f14bb8dd0470
# via -r requirements-test.in
python-dateutil==2.8.2 \
--hash=sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86 \
--hash=sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9
# via botocore
pytz==2022.7 \
--hash=sha256:7ccfae7b4b2c067464a6733c6261673fdb8fd1be905460396b97a073e9fa683a \
--hash=sha256:93007def75ae22f7cd991c84e02d434876818661f8df9ad5df9e950ff4e52cfd
Expand All @@ -231,6 +251,14 @@ requests==2.28.1 \
# cloudpub (setup.py)
# httmock
# sphinx
s3transfer==0.6.0 \
--hash=sha256:06176b74f3a15f61f1b4f25a1fc29a4429040b7647133a463da8fa5bd28d5ecd \
--hash=sha256:2ed07d3866f523cc561bf4a00fc5535827981b117dd7876f036b0c1aca42c947
# via boto3
six==1.16.0 \
--hash=sha256:1e61c37477a1626458e36f7b1d82aa5c9b094fa4802892072e49de9c60c4c926 \
--hash=sha256:8abb2f1d86890a2dfb989f9a77cfcfd3e47c2a354b01111771326f8aa26e0254
# via python-dateutil
snowballstemmer==2.2.0 \
--hash=sha256:09b16deb8547d3412ad7b590689584cd0fe25ec8db3be37788be3810cbf19cb1 \
--hash=sha256:c8e1716e83cc398ae16824e5572ae04e0d9fc2c6b985fb0f900f5f0c96ecba1a
Expand Down Expand Up @@ -306,6 +334,10 @@ typed-ast==1.5.4 \
--hash=sha256:ebd9d7f80ccf7a82ac5f88c521115cc55d84e35bf8b446fcd7836eb6b98929a3 \
--hash=sha256:ed855bbe3eb3715fca349c80174cfcfd699c2f9de574d40527b8429acae23a66
# via mypy
types-python-dateutil==2.8.19.10 \
--hash=sha256:c640f2eb71b4b94a9d3bfda4c04250d29a24e51b8bad6e12fddec0cf6e96f7a3 \
--hash=sha256:fbecd02c19cac383bf4a16248d45ffcff17c93a04c0794be5f95d42c6aa5de39
# via -r requirements-test.in
types-requests==2.28.11.7 \
--hash=sha256:0ae38633734990d019b80f5463dfa164ebd3581998ac8435f526da6fe4d598c3 \
--hash=sha256:b6a2fca8109f4fdba33052f11ed86102bddb2338519e1827387137fefc66a98b
Expand All @@ -323,7 +355,9 @@ typing-extensions==4.4.0 \
urllib3==1.26.14 \
--hash=sha256:076907bf8fd355cde77728471316625a4d2f7e713c125f51953bb5b3eecf4f72 \
--hash=sha256:75edcdc2f7d85b137124a6c3c9fc3933cdeaa12ecb9a6a959f22797a0feca7e1
# via requests
# via
# botocore
# requests
zipp==3.11.0 \
--hash=sha256:83a28fcb75844b5c0cdaf5aa4003c2d728c77e05f5aeabe8e95e56727005fbaa \
--hash=sha256:a7a22e05929290a67401440b39690ae6563279bced5f314609d9d03798f56766
Expand Down
Loading

0 comments on commit 4d0a799

Please sign in to comment.