From e984af0ddc9fb1ac25cd3f68c4e1fa81f8a75ee7 Mon Sep 17 00:00:00 2001
From: Dominik Fleischmann <dominik.fleischmann@canonical.com>
Date: Fri, 29 May 2020 13:32:58 +0200
Subject: [PATCH] Remove service Actions and add Delete URL

This commit add the deleteurl action which is
written in python. The service action were
removed as they were not working properly.

Signed-off-by: Dominik Fleischmann <dominik.fleischmann@canonical.com>
---
 .../charms/squid/actions.yaml                 | 15 ++++++-----
 .../charms/squid/actions/restart              |  2 --
 .../charms/squid/actions/start                |  2 --
 .../charms/squid/actions/stop                 |  2 --
 .../charms/squid/config.yaml                  |  2 +-
 .../charms/squid/src/charm.py                 | 26 ++++++++++++++++++-
 magma/hackfest_squid_cnf/squid_vnf.yaml       | 15 +++--------
 7 files changed, 38 insertions(+), 26 deletions(-)
 delete mode 100755 magma/hackfest_squid_cnf/charms/squid/actions/restart
 delete mode 100755 magma/hackfest_squid_cnf/charms/squid/actions/start
 delete mode 100755 magma/hackfest_squid_cnf/charms/squid/actions/stop

diff --git a/magma/hackfest_squid_cnf/charms/squid/actions.yaml b/magma/hackfest_squid_cnf/charms/squid/actions.yaml
index 687a92f4..3e57d8e3 100644
--- a/magma/hackfest_squid_cnf/charms/squid/actions.yaml
+++ b/magma/hackfest_squid_cnf/charms/squid/actions.yaml
@@ -1,13 +1,14 @@
 addurl:
-    description: "Add squid config"
+    description: "Add allowed URL to squid config"
     params:
         url:
             description: "URL that will be allowed"
             type: string
             default: ""
-start:
-    description: "Start squid service"
-restart:
-    description: "Retart squid service"
-stop:
-    description: "Stop squid service"
+deleteurl:
+    description: "Delete allowed URL squid config"
+    params:
+        url:
+            description: "URL that will stop to be allowed"
+            type: string
+            default: ""
diff --git a/magma/hackfest_squid_cnf/charms/squid/actions/restart b/magma/hackfest_squid_cnf/charms/squid/actions/restart
deleted file mode 100755
index d6c3f2e3..00000000
--- a/magma/hackfest_squid_cnf/charms/squid/actions/restart
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/bash
-service squid restart
diff --git a/magma/hackfest_squid_cnf/charms/squid/actions/start b/magma/hackfest_squid_cnf/charms/squid/actions/start
deleted file mode 100755
index 1f62c7bd..00000000
--- a/magma/hackfest_squid_cnf/charms/squid/actions/start
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/bash
-service squid start
diff --git a/magma/hackfest_squid_cnf/charms/squid/actions/stop b/magma/hackfest_squid_cnf/charms/squid/actions/stop
deleted file mode 100755
index 536232dd..00000000
--- a/magma/hackfest_squid_cnf/charms/squid/actions/stop
+++ /dev/null
@@ -1,2 +0,0 @@
-#!/bin/bash
-service squid stop
diff --git a/magma/hackfest_squid_cnf/charms/squid/config.yaml b/magma/hackfest_squid_cnf/charms/squid/config.yaml
index 7efafff5..65c33a86 100644
--- a/magma/hackfest_squid_cnf/charms/squid/config.yaml
+++ b/magma/hackfest_squid_cnf/charms/squid/config.yaml
@@ -2,7 +2,7 @@ options:
     image:
         type: string
         description: 'Docker image for squid'
-        default: 'sameersbn/squid:latest'
+        default: 'domfleischmann/squid-python'
     port:
         type: int
         description: 'Port'
diff --git a/magma/hackfest_squid_cnf/charms/squid/src/charm.py b/magma/hackfest_squid_cnf/charms/squid/src/charm.py
index 67383ee8..f15c42e4 100755
--- a/magma/hackfest_squid_cnf/charms/squid/src/charm.py
+++ b/magma/hackfest_squid_cnf/charms/squid/src/charm.py
@@ -12,6 +12,7 @@ from ops.charm import CharmBase
 from ops.framework import StoredState
 from ops.main import main
 from ops.model import ActiveStatus, MaintenanceStatus
+import subprocess
 
 
 class SquidK8SCharm(CharmBase):
@@ -26,7 +27,7 @@ class SquidK8SCharm(CharmBase):
         self.framework.observe(self.on.install, self.on_install)
         self.framework.observe(self.on.start, self.on_start)
         self.framework.observe(self.on.config_changed, self.on_config_changed)
-#        self.framework.observe(self.on.addconfig_action, self)
+        self.framework.observe(self.on.deleteurl_action, self)
         # -- initialize states --
         self.state.set_default(installed=False)
         self.state.set_default(configured=False)
@@ -91,6 +92,29 @@ class SquidK8SCharm(CharmBase):
         self.state.started = True
         logging.info("Started")
 
+    def on_deleteurl_action(self, event):
+        """Handle the deleteurl action."""
+        url = event.params["url"]
+
+        line_to_delete = "acl allowedurls dstdomain .{}".format(url)
+        line_deleted = False
+
+        with open("/etc/squid/squid.conf", "r") as f:
+            lines = f.readlines()
+        with open("/etc/squid/squid.conf", "w") as f:
+            for line in lines:
+                if line_to_delete not in line:
+                    f.write(line)
+                else:
+                    line_deleted = True
+
+        if line_deleted:
+            event.set_results({"output": "URL deleted succesfully"})
+            subprocess.check_output("kill -HUP `cat /var/run/squid.pid`", shell=True)
+        else:
+            event.fail("No URL was deleted")
+
+
 if __name__ == "__main__":
     from ops.main import main
     main(SquidK8SCharm)
diff --git a/magma/hackfest_squid_cnf/squid_vnf.yaml b/magma/hackfest_squid_cnf/squid_vnf.yaml
index e77ebb3f..b15c49d1 100644
--- a/magma/hackfest_squid_cnf/squid_vnf.yaml
+++ b/magma/hackfest_squid_cnf/squid_vnf.yaml
@@ -11,22 +11,15 @@ vnfd-catalog:
             juju-bundle: bundle.yaml
             kdu-configuration:
                 config-primitive:
-                -   name: restart
-                    parameter:
-                    -   name: application-name
-                        data-type: STRING
-                        default-value: squid
-                -   name: start
+                -   name: addurl
                     parameter:
                     -   name: application-name
                         data-type: STRING
                         default-value: squid
-                -   name: stop
-                    parameter:
-                    -   name: application-name
+                    -   name: url
                         data-type: STRING
-                        default-value: squid
-                -   name: addurl
+                        default-value: ""
+                -   name: deleteurl
                     parameter:
                     -   name: application-name
                         data-type: STRING
-- 
GitLab