create default networks in model policy instead of admin
diff --git a/planetstack/core/admin.py b/planetstack/core/admin.py
index 1ccc1d6..d2a9b53 100644
--- a/planetstack/core/admin.py
+++ b/planetstack/core/admin.py
@@ -799,28 +799,6 @@
inline.model.caller = request.user
yield inline.get_formset(request, obj)
- def save_model(self, request, obj, form, change):
- obj.save()
- # create default public slice networks
- public_net = Network(
- name = obj.name+'-public',
- template = NetworkTemplate.objects.get(name='Public dedicated IPv4'),
- owner = obj
- )
- public_net.save()
- public_slice_net = NetworkSlice(network=public_net, slice=obj)
- public_slice_net.save()
- # create default private slice networks
- private_net = Network(
- name = obj.name+'-private',
- template = NetworkTemplate.objects.get(name='Private'),
- owner = obj
- )
- private_net.save()
- private_slice_net = NetworkSlice(network=private_net, slice=obj)
- private_slice_net.save()
-
-
class SlicePrivilegeAdmin(PlanetStackBaseAdmin):
fieldsets = [
diff --git a/planetstack/model_policies/model_policy_Slice.py b/planetstack/model_policies/model_policy_Slice.py
index 6fd968c..493975f 100644
--- a/planetstack/model_policies/model_policy_Slice.py
+++ b/planetstack/model_policies/model_policy_Slice.py
@@ -1,6 +1,6 @@
def handle(slice):
- from core.models import SiteDeployments,SliceDeployments,Deployment
+ from core.models import SiteDeployments,SliceDeployments,Deployment,Network,NetworkSlice,NetworkTemplate
from collections import defaultdict
site_deployments = SiteDeployments.objects.all()
site_deploy_lookup = defaultdict(list)
@@ -22,3 +22,46 @@
sd = SliceDeployments(slice=slice, deployment=expected_deployment)
sd.save()
+ # make sure slice has at least 1 public and 1 private networkd
+ public_net = None
+ private_net = None
+ networks = Network.objects.filter(owner=slice)
+ for network in networks:
+ if network.template.name == 'Public dedicated IPv4':
+ public_net = network
+ elif network.template.name == 'Private':
+ private_net = network
+ if not public_net:
+ public_net = Network(
+ name = slice.name+'-public',
+ template = NetworkTemplate.objects.get(name='Public dedicated IPv4'),
+ owner = slice
+ )
+ public_net.save()
+
+ if not private_net:
+ private_net = Network(
+ name = slice.name+'-private',
+ template = NetworkTemplate.objects.get(name='Private'),
+ owner = slice
+ )
+ private_net.save()
+ # create slice networks
+ public_net_slice = None
+ private_net_slice = None
+ net_slices = NetworkSlice.objects.filter(slice=slice, network__in=[public_net,private_net])
+ for net_slice in net_slices:
+ if net_slice.network == public_net:
+ public_net_slice = net_slice
+ elif net_slice.network == private_net:
+ private_net_slice = net_slice
+ if not public_net_slice:
+ public_net_slice = NetworkSlice(slice=slice, network=public_net)
+ public_net_slice.save()
+ if not private_net_slice:
+ private_net_slice = NetworkSlice(slice=slice, network=private_net)
+ private_net_slice.save()
+
+
+
+