libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anthonys...@apache.org
Subject [37/50] libcloud git commit: Added basic ECR support
Date Wed, 20 Jan 2016 03:44:25 GMT
Added basic ECR support


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/6e4df6d3
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/6e4df6d3
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/6e4df6d3

Branch: refs/heads/trunk
Commit: 6e4df6d37b30652569e10b8361582bfabc96a15f
Parents: 5aa34e1
Author: anthony-shaw <anthony.p.shaw@gmail.com>
Authored: Sat Jan 9 21:58:19 2016 +1100
Committer: anthony-shaw <anthony.p.shaw@gmail.com>
Committed: Sat Jan 9 21:58:19 2016 +1100

----------------------------------------------------------------------
 libcloud/container/drivers/ecs.py   | 87 +++++++++++++++++++++++++++++---
 libcloud/test/container/test_ecs.py |  3 ++
 2 files changed, 83 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/6e4df6d3/libcloud/container/drivers/ecs.py
----------------------------------------------------------------------
diff --git a/libcloud/container/drivers/ecs.py b/libcloud/container/drivers/ecs.py
index 9de61df..7f6faf3 100644
--- a/libcloud/container/drivers/ecs.py
+++ b/libcloud/container/drivers/ecs.py
@@ -28,23 +28,36 @@ __all__ = [
 ]
 
 
-VERSION = '2014-11-13'
-HOST = 'ecs.%s.amazonaws.com'
+ECS_VERSION = '2014-11-13'
+ECR_VERSION = '2015-09-21'
+ECS_HOST = 'ecs.%s.amazonaws.com'
+ECR_HOST = 'ecr.%s.amazonaws.com'
 ROOT = '/'
-TARGET_BASE = 'AmazonEC2ContainerServiceV%s' % (VERSION.replace('-', ''))
+ECS_TARGET_BASE = 'AmazonEC2ContainerServiceV%s' % \
+                  (ECS_VERSION.replace('-', ''))
+ECR_TARGET_BASE = 'AmazonEC2ContainerRegistry_V%s' % \
+                  (ECR_VERSION.replace('-', ''))
 
 
 class ECSJsonConnection(SignedAWSConnection):
-    version = VERSION
-    host = HOST
+    version = ECS_VERSION
+    host = ECS_HOST
     responseCls = AWSJsonResponse
     service_name = 'ecs'
 
 
+class ECRJsonConnection(SignedAWSConnection):
+    version = ECR_VERSION
+    host = ECR_HOST
+    responseCls = AWSJsonResponse
+    service_name = 'ecr'
+
+
 class ElasticContainerDriver(ContainerDriver):
     name = 'Amazon Elastic Container Service'
     website = 'https://aws.amazon.com/ecs/details/'
     connectionCls = ECSJsonConnection
+    ecrConnectionClass = ECRJsonConnection
     supports_clusters = False
     status_map = {
         'RUNNING': ContainerState.RUNNING
@@ -54,11 +67,39 @@ class ElasticContainerDriver(ContainerDriver):
         super(ElasticContainerDriver, self).__init__(access_id, secret)
         self.region = region
         self.region_name = region
-        self.connection.host = HOST % (region)
+        self.connection.host = ECS_HOST % (region)
+
+        # Setup another connection class for ECR
+        conn_kwargs = self._ex_connection_class_kwargs()
+        self.ecr_connection = self.ecrConnectionClass(
+            access_id, secret, **conn_kwargs)
+        self.ecr_connection.host = ECR_HOST % (region)
+        self.ecr_connection.driver = self
+        self.ecr_connection.connect()
 
     def _ex_connection_class_kwargs(self):
         return {'signature_version': '4'}
 
+    def list_images(self, ex_repository_name=None):
+        """
+        List the images in an ECR repository
+
+        :param  ex_repository_name: The name of the repository to check
+            defaults to the default repository.
+        :type   ex_repository_name: ``str``
+
+        :return: a list of images
+        :rtype: ``list`` of :class:`libcloud.container.base.ContainerImage`
+        """
+        request = {'repositoryName': 'default'}
+        list_response = self.ecr_connection.request(
+            ROOT,
+            method='POST',
+            data=json.dumps(request),
+            headers=self._get_ecr_headers('ListImages')
+        ).object
+        return self._to_images(list_response)
+
     def list_clusters(self):
         """
         Get a list of potential locations to deploy clusters into
@@ -143,6 +184,8 @@ class ElasticContainerDriver(ContainerDriver):
             data=json.dumps(request),
             headers=self._get_headers('ListTasks')
         ).object
+        if len(list_response['taskArns']) == 0:
+            return []
         containers = self.ex_list_containers_for_task(
             list_response['taskArns'])
         return containers
@@ -441,10 +484,25 @@ class ElasticContainerDriver(ContainerDriver):
 
     def _get_headers(self, action):
         return {'x-amz-target': '%s.%s' %
-                (TARGET_BASE, action),
+                (ECS_TARGET_BASE, action),
                 'Content-Type': 'application/x-amz-json-1.1'
                 }
 
+    def _get_ecr_headers(self, action):
+        return {'x-amz-target': '%s.%s' %
+                (ECR_TARGET_BASE, action),
+                'Content-Type': 'application/x-amz-json-1.1'
+                }
+
+    def ex_docker_hub_image(self, name):
+        return ContainerImage(
+            id=None,
+            name=name,
+            path=None,
+            version=None,
+            driver=self.connection.driver
+        )
+
     def _to_clusters(self, data):
         clusters = []
         for cluster in data['clusters']:
@@ -483,3 +541,18 @@ class ElasticContainerDriver(ContainerDriver):
             },
             driver=self.connection.driver
         )
+
+    def _to_images(self, data):
+        images = []
+        for image in data['images']:
+            images.append(self._to_image(image))
+        return images
+
+    def _to_image(self, data):
+        return ContainerImage(
+            id=None,
+            name=data['name'],
+            path=None,
+            version=None,
+            driver=self.connection.driver
+        )

http://git-wip-us.apache.org/repos/asf/libcloud/blob/6e4df6d3/libcloud/test/container/test_ecs.py
----------------------------------------------------------------------
diff --git a/libcloud/test/container/test_ecs.py b/libcloud/test/container/test_ecs.py
index e2fdb3f..2a3a2c2 100644
--- a/libcloud/test/container/test_ecs.py
+++ b/libcloud/test/container/test_ecs.py
@@ -33,6 +33,9 @@ class ElasticContainerDriverTestCase(unittest.TestCase):
             ECSMockHttp, ECSMockHttp)
         ECSMockHttp.type = None
         ECSMockHttp.use_param = 'a'
+        ElasticContainerDriver.ecrConnectionClass.conn_classes = (
+            ECSMockHttp, ECSMockHttp)
+        
         self.driver = ElasticContainerDriver(*CONTAINER_PARAMS_ECS)
 
     def test_list_clusters(self):


Mime
View raw message