From 8c0d7aaa58e3e3736ba6cc4e38232f75f34c715f Mon Sep 17 00:00:00 2001
From: Einar Forselv <eforselv@gmail.com>
Date: Fri, 29 Nov 2019 05:37:02 +0100
Subject: [PATCH] containers: get mysql creds

---
 restic_volume_backup/containers.py | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/restic_volume_backup/containers.py b/restic_volume_backup/containers.py
index 610bc76..658651f 100644
--- a/restic_volume_backup/containers.py
+++ b/restic_volume_backup/containers.py
@@ -12,7 +12,6 @@ class Container:
 
     def __init__(self, data: dict):
         self._data = data
-
         self._state = data.get('State')
         self._config = data.get('Config')
         self._mounts = [Mount(mnt, container=self) for mnt in data.get('Mounts')]
@@ -45,10 +44,16 @@ class Container:
         return self.get_config('Image')
 
     @property
-    def environment(self) -> dict:
-        """All configured env vars for the container"""
+    def environment(self) -> list:
+        """All configured env vars for the container as a list"""
         return self.get_config('Env', default=[])
 
+    def get_config_env(self, name) -> str:
+        """Get a config environment variable by name"""
+        # convert to dict and fetch env var by name
+        data = {i[0:i.find('=')]: i[i.find('=')+1:] for i in self.environment}
+        return data.get(name)
+
     @property
     def volumes(self) -> dict:
         """
@@ -160,6 +165,14 @@ class Container:
 
         return volumes
 
+    def get_mysql_credentials(self):
+        return {
+            'host': self.hostname,
+            'username': self.get_config_env('MYSQL_USER'),
+            'password': self.get_config_env('MYSQL_PASSWORD'),
+            'port': "3306",
+        }
+
     def _parse_pattern(self, value):
         if not value:
             return None