Implement ECR batch_delete_image (#2225)
This implements the endpoint in spulec #2224
This commit is contained in:
parent
8f53b16b9a
commit
664b27d8e7
4 changed files with 424 additions and 5 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
import hashlib
|
||||
import re
|
||||
from copy import copy
|
||||
from random import random
|
||||
|
||||
|
|
@ -119,6 +120,12 @@ class Image(BaseObject):
|
|||
def get_image_manifest(self):
|
||||
return self.image_manifest
|
||||
|
||||
def remove_tag(self, tag):
|
||||
if tag is not None and tag in self.image_tags:
|
||||
self.image_tags.remove(tag)
|
||||
if self.image_tags:
|
||||
self.image_tag = self.image_tags[-1]
|
||||
|
||||
def update_tag(self, tag):
|
||||
self.image_tag = tag
|
||||
if tag not in self.image_tags and tag is not None:
|
||||
|
|
@ -165,6 +172,13 @@ class Image(BaseObject):
|
|||
response_object['registryId'] = self.registry_id
|
||||
return {k: v for k, v in response_object.items() if v is not None and v != [None]}
|
||||
|
||||
@property
|
||||
def response_batch_delete_image(self):
|
||||
response_object = {}
|
||||
response_object['imageDigest'] = self.get_image_digest()
|
||||
response_object['imageTag'] = self.image_tag
|
||||
return {k: v for k, v in response_object.items() if v is not None and v != [None]}
|
||||
|
||||
|
||||
class ECRBackend(BaseBackend):
|
||||
|
||||
|
|
@ -310,6 +324,103 @@ class ECRBackend(BaseBackend):
|
|||
|
||||
return response
|
||||
|
||||
def batch_delete_image(self, repository_name, registry_id=None, image_ids=None):
|
||||
if repository_name in self.repositories:
|
||||
repository = self.repositories[repository_name]
|
||||
else:
|
||||
raise RepositoryNotFoundException(
|
||||
repository_name, registry_id or DEFAULT_REGISTRY_ID
|
||||
)
|
||||
|
||||
if not image_ids:
|
||||
raise ParamValidationError(
|
||||
msg='Missing required parameter in input: "imageIds"'
|
||||
)
|
||||
|
||||
response = {
|
||||
"imageIds": [],
|
||||
"failures": []
|
||||
}
|
||||
|
||||
for image_id in image_ids:
|
||||
image_found = False
|
||||
|
||||
# Is request missing both digest and tag?
|
||||
if "imageDigest" not in image_id and "imageTag" not in image_id:
|
||||
response["failures"].append(
|
||||
{
|
||||
"imageId": {},
|
||||
"failureCode": "MissingDigestAndTag",
|
||||
"failureReason": "Invalid request parameters: both tag and digest cannot be null",
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
# If we have a digest, is it valid?
|
||||
if "imageDigest" in image_id:
|
||||
pattern = re.compile("^[0-9a-zA-Z_+\.-]+:[0-9a-fA-F]{64}")
|
||||
if not pattern.match(image_id.get("imageDigest")):
|
||||
response["failures"].append(
|
||||
{
|
||||
"imageId": {
|
||||
"imageDigest": image_id.get("imageDigest", "null")
|
||||
},
|
||||
"failureCode": "InvalidImageDigest",
|
||||
"failureReason": "Invalid request parameters: image digest should satisfy the regex '[a-zA-Z0-9-_+.]+:[a-fA-F0-9]+'",
|
||||
}
|
||||
)
|
||||
continue
|
||||
|
||||
for num, image in enumerate(repository.images):
|
||||
|
||||
# Search by matching both digest and tag
|
||||
if "imageDigest" in image_id and "imageTag" in image_id:
|
||||
if (
|
||||
image_id["imageDigest"] == image.get_image_digest() and
|
||||
image_id["imageTag"] in image.image_tags
|
||||
):
|
||||
image_found = True
|
||||
for image_tag in reversed(image.image_tags):
|
||||
repository.images[num].image_tag = image_tag
|
||||
response["imageIds"].append(
|
||||
image.response_batch_delete_image
|
||||
)
|
||||
repository.images[num].remove_tag(image_tag)
|
||||
del repository.images[num]
|
||||
|
||||
# Search by matching digest
|
||||
elif "imageDigest" in image_id and image.get_image_digest() == image_id["imageDigest"]:
|
||||
image_found = True
|
||||
for image_tag in reversed(image.image_tags):
|
||||
repository.images[num].image_tag = image_tag
|
||||
response["imageIds"].append(image.response_batch_delete_image)
|
||||
repository.images[num].remove_tag(image_tag)
|
||||
del repository.images[num]
|
||||
|
||||
# Search by matching tag
|
||||
elif "imageTag" in image_id and image_id["imageTag"] in image.image_tags:
|
||||
image_found = True
|
||||
repository.images[num].image_tag = image_id["imageTag"]
|
||||
response["imageIds"].append(image.response_batch_delete_image)
|
||||
repository.images[num].remove_tag(image_id["imageTag"])
|
||||
|
||||
if not image_found:
|
||||
failure_response = {
|
||||
"imageId": {},
|
||||
"failureCode": "ImageNotFound",
|
||||
"failureReason": "Requested image not found",
|
||||
}
|
||||
|
||||
if "imageDigest" in image_id:
|
||||
failure_response["imageId"]["imageDigest"] = image_id.get("imageDigest", "null")
|
||||
|
||||
if "imageTag" in image_id:
|
||||
failure_response["imageId"]["imageTag"] = image_id.get("imageTag", "null")
|
||||
|
||||
response["failures"].append(failure_response)
|
||||
|
||||
return response
|
||||
|
||||
|
||||
ecr_backends = {}
|
||||
for region, ec2_backend in ec2_backends.items():
|
||||
|
|
|
|||
|
|
@ -84,9 +84,12 @@ class ECRResponse(BaseResponse):
|
|||
'ECR.batch_check_layer_availability is not yet implemented')
|
||||
|
||||
def batch_delete_image(self):
|
||||
if self.is_not_dryrun('BatchDeleteImage'):
|
||||
raise NotImplementedError(
|
||||
'ECR.batch_delete_image is not yet implemented')
|
||||
repository_str = self._get_param('repositoryName')
|
||||
registry_id = self._get_param('registryId')
|
||||
image_ids = self._get_param('imageIds')
|
||||
|
||||
response = self.ecr_backend.batch_delete_image(repository_str, registry_id, image_ids)
|
||||
return json.dumps(response)
|
||||
|
||||
def batch_get_image(self):
|
||||
repository_str = self._get_param('repositoryName')
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue