This is an automated email from the ASF dual-hosted git repository.

eamonford pushed a commit to branch config_map
in repository https://gitbox.apache.org/repos/asf/incubator-sdap-ingester.git

commit 5520d27582029858ddc3e38725616971fd1c04e9
Author: thomas loubrieu <[email protected]>
AuthorDate: Wed Jun 3 11:32:04 2020 -0700

    initiate a configMap object with unit test (with bug)
---
 requirements.txt                                   |  1 +
 sdap_ingest_manager/config/ConfigMap.py            | 70 ++++++++++++++++++++++
 .../GitIngestionOrderStore.py                      |  2 +
 .../ingestion_order_store/IngestionOrderStore.py   |  3 +
 tests/config/__init__.py                           |  0
 tests/config/test_ConfigMap.py                     | 32 ++++++++++
 6 files changed, 108 insertions(+)

diff --git a/requirements.txt b/requirements.txt
index f4e39d0..996a27e 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -6,3 +6,4 @@ Flask==1.1.2
 flask-restplus==0.13.0
 Werkzeug==0.16.0
 pika==1.1.0
+kubernetes==11.0
\ No newline at end of file
diff --git a/sdap_ingest_manager/config/ConfigMap.py 
b/sdap_ingest_manager/config/ConfigMap.py
new file mode 100644
index 0000000..c5eb244
--- /dev/null
+++ b/sdap_ingest_manager/config/ConfigMap.py
@@ -0,0 +1,70 @@
+import logging
+from kubernetes import client, config
+from kubernetes.client.rest import ApiException
+
+logging.basicConfig(level=logging.INFO)
+logger = logging.getLogger(__name__)
+
+class ConfigMap:
+    def __init__(self, configmap_name, namespace, ingestion_order_store, 
output_collection='collections.yml'):
+        self._ingestion_order_store = ingestion_order_store
+        self._namespace = namespace
+        self._configmap_name = configmap_name
+        self._output_collection = output_collection
+        config.load_kube_config()
+        configuration = client.Configuration()
+        self._api_instance = client.CoreV1Api(client.ApiClient(configuration))
+
+
+    def __del__(self):
+        pass
+
+    def _create_configmap_object(self):
+
+        metadata = client.V1ObjectMeta(
+            name=self._configmap_name ,
+            namespace=self._namespace,
+        )
+        
+        data = 
{self._output_collection:self._ingestion_order_store.get_content()}
+
+        configmap = client.V1ConfigMap(
+            api_version="v1",
+            kind="ConfigMap",
+            data=data,
+            metadata=metadata
+        )
+        return configmap
+
+    def _get_deployed_config(self):
+        try:
+            api_list_response = 
self._api_instance.list_namespaced_config_map(self._namespace)
+            config_keys = set()
+            for item in api_list_response.items:
+                config_keys = config_keys.union(item.data.keys())
+        except ApiException as e:
+            logger.error("Exception when calling Kubernetes CoreV1Api %s\n" % 
e)
+        finally:
+            return config_keys
+
+    def publish(self):
+        try:
+
+            if self._output_collection in self._get_deployed_config():
+                logger.info(f'replace configMap entry 
{self._output_collection}')
+                api_response = 
self._api_instance.replace_namespaced_config_map(
+                    name=self._output_collection,
+                    namespace=self._namespace,
+                    body=self._create_configmap_object()
+                )
+            else:
+                logger.info(f'create configMap entry 
{self._output_collection}')
+                api_response = self._api_instance.create_namespaced_config_map(
+                    namespace=self._namespace,
+                    body=self._create_configmap_object()
+                )
+            logger.info(api_response)
+
+        except ApiException as e:
+            logger.error("Exception when calling Kubernetes CoreV1Api %s\n" % 
e)
+
diff --git 
a/sdap_ingest_manager/ingestion_order_store/GitIngestionOrderStore.py 
b/sdap_ingest_manager/ingestion_order_store/GitIngestionOrderStore.py
index 349eeb2..1020679 100644
--- a/sdap_ingest_manager/ingestion_order_store/GitIngestionOrderStore.py
+++ b/sdap_ingest_manager/ingestion_order_store/GitIngestionOrderStore.py
@@ -2,6 +2,7 @@ import logging
 import os
 import sys
 
+
 from git import Repo, Remote
 
 from sdap_ingest_manager.ingestion_order_store.IngestionOrderStore import 
IngestionOrderStore
@@ -54,3 +55,4 @@ class GitIngestionOrderStore(IngestionOrderStore):
             self._repo.create_remote('origin', self._git_url)
         self._repo.git.fetch()
         self._repo.git.checkout(self._git_branch)
+
diff --git a/sdap_ingest_manager/ingestion_order_store/IngestionOrderStore.py 
b/sdap_ingest_manager/ingestion_order_store/IngestionOrderStore.py
index c791fe3..89af10d 100644
--- a/sdap_ingest_manager/ingestion_order_store/IngestionOrderStore.py
+++ b/sdap_ingest_manager/ingestion_order_store/IngestionOrderStore.py
@@ -31,3 +31,6 @@ class IngestionOrderStore:
 
         except FileNotFoundError:
             logger.error(f"no collection configuration found at 
{self._ingestion_orders}")
+               
+    def get_content(self):
+        return yaml.dump(self._ingestion_orders)
\ No newline at end of file
diff --git a/tests/config/__init__.py b/tests/config/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/config/test_ConfigMap.py b/tests/config/test_ConfigMap.py
new file mode 100644
index 0000000..a536202
--- /dev/null
+++ b/tests/config/test_ConfigMap.py
@@ -0,0 +1,32 @@
+import unittest
+import os
+
+from flask import Flask
+from flask_restplus import Api
+
+from sdap_ingest_manager.config.ConfigMap import ConfigMap
+from sdap_ingest_manager.ingestion_order_store.FileIngestionOrderStore import 
FileIngestionOrderStore
+from sdap_ingest_manager.ingestion_order_store.templates import Templates
+
+flask_app = Flask(__name__)
+app = Api(app=flask_app)
+templates = Templates(app)
+
+class ConfigMapTest(unittest.TestCase):
+    def test_createconfigmap(self):
+
+        test_ingestion_order_file = 
os.path.join(os.path.dirname(os.path.abspath(__file__)),
+                                                 '..',
+                                                 'resources',
+                                                 'data',
+                                                 'collections.yml')
+        file_ingestion_order_store = 
FileIngestionOrderStore(path=test_ingestion_order_file,
+                                                             
order_template=templates.order_template)
+        
+        config_map = ConfigMap('collections.yml', 'sdap', 
file_ingestion_order_store)
+        config_map.publish()
+
+
+
+if __name__ == '__main__':
+    unittest.main()

Reply via email to