Revision b47f167a snf-cyclades-app/synnefo/api/management/commands/network-remove.py

b/snf-cyclades-app/synnefo/api/management/commands/network-remove.py
1
# Copyright 2011-2012 GRNET S.A. All rights reserved.
1
# Copyright 2011-2013 GRNET S.A. All rights reserved.
2 2
#
3 3
# Redistribution and use in source and binary forms, with or without
4 4
# modification, are permitted provided that the following conditions
......
29 29
#
30 30

  
31 31
from django.core.management.base import BaseCommand, CommandError
32
from synnefo.logic.backend import delete_network
32
from synnefo.logic import networks
33 33
from synnefo.management.common import get_network
34
from synnefo import quotas
35 34

  
36 35

  
37 36
class Command(BaseCommand):
38 37
    can_import_settings = True
39

  
40 38
    help = "Remove a network from the Database, and Ganeti"
41 39

  
42
    output_transaction = True  # The management command runs inside
43
                               # an SQL transaction
44

  
45 40
    def handle(self, *args, **options):
46 41
        if len(args) < 1:
47 42
            raise CommandError("Please provide a network ID")
48 43

  
49 44
        network = get_network(args[0])
50 45

  
51
        self.stdout.write('Trying to remove network: %s\n' % str(network))
52

  
53
        if network.machines.exists():
54
            raise CommandError('Can not delete: Network has connected VMs.')
55
        if network.floating_ips.filter(deleted=False).exists():
56
            raise CommandError("Can not delete: Network has reserved floating"
57
                               " IP addresses.")
58

  
59
        network.action = 'DESTROY'
60
        network.save()
61

  
62
        if network.userid:
63
            quotas.issue_and_accept_commission(network, delete=True)
64

  
65
        for bnet in network.backend_networks.exclude(operstate="DELETED"):
66
            delete_network(network, bnet.backend)
46
        self.stdout.write('Removing network: %s\n' % network.backend_id)
67 47

  
68
        self.stdout.write("Successfully submitted Ganeti jobs to"
69
                          " remove network %s" % network.backend_id)
48
        networks.delete(network)

Also available in: Unified diff