Revision 7fede91e snf-cyclades-app/synnefo/api/management/commands/network-create.py

b/snf-cyclades-app/synnefo/api/management/commands/network-create.py
35 35

  
36 36
from django.core.management.base import BaseCommand, CommandError
37 37

  
38
from synnefo.db.models import Network
38
from synnefo.db.models import Network, Backend
39 39
from synnefo.api.util import network_link_from_type
40 40
from synnefo.logic.backend import create_network
41 41

  
......
88 88
            dest='gateway6',
89 89
            default=None,
90 90
            help='IPv6 gateway of the network'),
91
        make_option('--backend-id',
92
            dest='backend_id',
93
            default=None,
94
            help='ID of the backend that the network will be created. Only for'
95
                 ' public networks')
91 96
        )
92 97

  
93 98
    def handle(self, *args, **options):
......
97 102
        name = options['name']
98 103
        subnet = options['subnet']
99 104
        typ = options['type']
105
        backend_id = options['backend_id']
106
        public = options['public']
100 107

  
101 108
        if not name:
102 109
            raise CommandError("Name is required")
103 110
        if not subnet:
104 111
            raise CommandError("Subnet is required")
112
        if public and not backend_id:
113
            raise CommandError("backend-id is required")
114
        if backend_id and not public:
115
            raise CommandError("Private networks must be created to"
116
                               " all backends")
117

  
118
        if backend_id:
119
            try:
120
                backend_id = int(backend_id)
121
                backend = Backend.objects.get(id=backend_id)
122
            except ValueError:
123
                raise CommandError("Invalid backend ID")
124
            except Backend.DoesNotExist:
125
                raise CommandError("Backend not found in DB")
105 126

  
106 127
        link = network_link_from_type(typ)
107 128

  
......
117 138
                gateway=gateway,
118 139
                dhcp=options['dhcp'],
119 140
                type=options['type'],
120
                public=options['public'],
141
                public=public,
121 142
                link=link,
122 143
                gateway6=gateway6,
123 144
                subnet6=subnet6,
124 145
                state='PENDING')
125 146

  
126
        # Create BackendNetwork entries for each Backend
127
        network.create_backend_network()
147
        if public:
148
            # Create BackendNetwork only to the specified Backend
149
            network.create_backend_network(backend)
150
            create_network(network, backends=[backend])
151
        else:
152
            # Create BackendNetwork entries for all Backends
153
            network.create_backend_network()
154
            create_network(network)
128 155

  
129
        # Create the network to the backends
130
        create_network(network)
131 156

  
132 157

  
133 158
def validate_network_info(options):

Also available in: Unified diff