Add three StorPool API access config options

Add the 'sp-api-http-host', 'sp-api-http-port', and 'sp-auth-token'
config options. The StorPool Cinder backend driver will read them from
the Cinder configuration if specified, so that there is no need for the
/etc/storpool.conf file to exist.

Change-Id: I04669408c789ad3a337edfbc52f54d0bd59238c1
diff --git a/config.yaml b/config.yaml
index 09a0cde..df9d2c3 100644
--- a/config.yaml
+++ b/config.yaml
@@ -48,3 +48,18 @@
       globally, as a default value for Cinder volumes. There is no
       default value for this option; the administrator needs to provide
       an explicit template name.
+  sp-api-http-host:
+    type: string
+    default:
+    description: |
+      The IP address on which the StorPool API is available.
+  sp-api-http-port:
+    type: int
+    default: 81
+    description: |
+      The port on which the StorPool API is available.
+  sp-auth-token:
+    type: string
+    default:
+    description: |
+      Authentication token for the StorPool API.
diff --git a/src/charm.py b/src/charm.py
index 02d1062..8b261c0 100755
--- a/src/charm.py
+++ b/src/charm.py
@@ -27,7 +27,13 @@
 class CinderCharmBase(CinderStoragePluginCharm):
 
     PACKAGES = ["cinder-common"]
-    MANDATORY_CONFIG = ["protocol", "storpool-template"]
+    MANDATORY_CONFIG = [
+        "protocol",
+        "storpool-template",
+        "sp-api-http-host",
+        "sp-api-http-port",
+        "sp-auth-token",
+    ]
     # Overriden from the parent. May be set depending on the charm's properties
     stateless = True
     active_active = True
@@ -53,6 +59,12 @@
         if config["protocol"] == "block":
             return "'protocol' value 'block' not yet supported"
 
+        if not (0 < config["sp-api-http-port"] < 65536):
+            return (
+                f"""'sp-api-http-port' ('{config["sp-api-http-port"]}')"""
+                "is not a valid port (0-65535)"
+            )
+
     def on_config(self, event):
         config = dict(self.framework.model.config)
         conf_error = self._check_for_config_errors(config)
@@ -77,6 +89,9 @@
             ("volume_driver", volume_driver),
             ("volume_backend_name", backend_name),
             ("storpool_template", config["storpool-template"]),
+            ("sp_api_http_host", config["sp-api-http-host"]),
+            ("sp_api_http_port", config["sp-api-http-port"]),
+            ("sp_auth_token", config["sp-auth-token"]),
         ]
 
         if config.get("use-multipath"):
diff --git a/unit_tests/test_cinder_storpool_charm.py b/unit_tests/test_cinder_storpool_charm.py
index ea4e306..18f09e2 100644
--- a/unit_tests/test_cinder_storpool_charm.py
+++ b/unit_tests/test_cinder_storpool_charm.py
@@ -26,7 +26,12 @@
         self.harness.set_leader(True)
         backend = self.harness.add_relation("storage-backend", "cinder")
         self.harness.update_config(
-            {"volume-backend-name": "test", "storpool-template": "test_template"}
+            {
+                "volume-backend-name": "test",
+                "storpool-template": "test_template",
+                "sp-api-http-host": "127.0.0.1",
+                "sp-auth-token": "1000000000000000000",
+            }
         )
         self.harness.add_relation_unit(backend, "cinder/0")
 
@@ -50,6 +55,18 @@
         self.harness.update_config({"storpool-template": "test_template"})
         self.assertTrue(isinstance(self.harness.model.unit.status, ActiveStatus))
 
+        self.harness.update_config(unset=["sp-api-http-host"])
+        self.assertTrue(isinstance(self.harness.model.unit.status, BlockedStatus))
+
+        self.harness.update_config({"sp-api-http-host": "127.0.0.1"})
+        self.assertTrue(isinstance(self.harness.model.unit.status, ActiveStatus))
+
+        self.harness.update_config(unset=["sp-auth-token"])
+        self.assertTrue(isinstance(self.harness.model.unit.status, BlockedStatus))
+
+        self.harness.update_config({"sp-auth-token": "1000000000000000000"})
+        self.assertTrue(isinstance(self.harness.model.unit.status, ActiveStatus))
+
         self.harness.update_config({"protocol": "block"})
         self.assertTrue(isinstance(self.harness.model.unit.status, BlockedStatus))
 
@@ -65,3 +82,4 @@
         # Cannot test for missing options with default values as it seems the
         # defaults are applied after update_config() below:
         # self.harness.update_config(unset=["protocol"])
+        # self.harness.update_config(unset=["sp_api_http_port"])