root / snf-cyclades-app / synnefo / api / management / commands / port-create.py @ 0069a20c
History | View | Annotate | Download (3.7 kB)
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
11 |
# 2. Redistributions in binary form must reproduce the above
|
12 |
# copyright notice, this list of conditions and the following
|
13 |
# disclaimer in the documentation and/or other materials
|
14 |
# provided with the distribution.
|
15 |
#
|
16 |
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
# POSSIBILITY OF SUCH DAMAGE.
|
28 |
#
|
29 |
# The views and conclusions contained in the software and
|
30 |
# documentation are those of the authors and should not be
|
31 |
# interpreted as representing official policies, either expressed
|
32 |
# or implied, of GRNET S.A.
|
33 |
|
34 |
from optparse import make_option |
35 |
|
36 |
from django.core.management.base import BaseCommand, CommandError |
37 |
from synnefo.management.common import get_backend, convert_api_faults |
38 |
from snf_django.management.utils import parse_bool |
39 |
|
40 |
from synnefo.db.models import Network, NetworkInterface, SecurityGroup |
41 |
from synnefo.api import util |
42 |
from synnefo.management.common import get_network, get_vm |
43 |
from synnefo.logic import ports |
44 |
|
45 |
|
46 |
class Command(BaseCommand): |
47 |
can_import_settings = True
|
48 |
output_transaction = True
|
49 |
|
50 |
help = "Create a new port"
|
51 |
|
52 |
option_list = BaseCommand.option_list + ( |
53 |
make_option( |
54 |
'--name',
|
55 |
dest='name',
|
56 |
default=None,
|
57 |
help="Name of port"),
|
58 |
make_option( |
59 |
'--network',
|
60 |
dest='network',
|
61 |
default=None,
|
62 |
help='The network to attach the port to'),
|
63 |
make_option( |
64 |
'--device',
|
65 |
dest='device_id',
|
66 |
default=None,
|
67 |
help='The VM id the port will be connected to'),
|
68 |
make_option( |
69 |
'--security-groups',
|
70 |
dest='security-groups',
|
71 |
default=None,
|
72 |
help='Security Groups associated with the port'),
|
73 |
) |
74 |
|
75 |
@convert_api_faults
|
76 |
def handle(self, *args, **options): |
77 |
if args:
|
78 |
raise CommandError("Command doesn't accept any arguments") |
79 |
|
80 |
name = options['name']
|
81 |
network = options['network']
|
82 |
device = options['device_id']
|
83 |
#assume giving security groups comma separated
|
84 |
security_groups = options['security-groups']
|
85 |
|
86 |
if not name: |
87 |
name=""
|
88 |
|
89 |
if not network: |
90 |
raise CommandError("network is required") |
91 |
if not device: |
92 |
raise CommandError("device is required") |
93 |
|
94 |
#get the network
|
95 |
network = get_network(network) |
96 |
|
97 |
#get the vm
|
98 |
vm = get_vm(device) |
99 |
|
100 |
#validate security groups
|
101 |
sg_list = [] |
102 |
if security_groups:
|
103 |
security_groups = security_groups.split(",")
|
104 |
for gid in security_groups: |
105 |
sg = util.get_security_group(int(gid))
|
106 |
sg_list.append(sg) |
107 |
|
108 |
new_port = ports.create(network, vm, security_groups=sg_list) |
109 |
self.stdout.write("Created port '%s' in DB.\n" % new_port) |