Revision 6d5c0344 snf-cyclades-app/synnefo/api/management/commands/port-create.py
b/snf-cyclades-app/synnefo/api/management/commands/port-create.py | ||
---|---|---|
1 |
# Copyright 2012 GRNET S.A. All rights reserved.
|
|
1 |
# Copyright 2013 GRNET S.A. All rights reserved.
|
|
2 | 2 |
# |
3 | 3 |
# Redistribution and use in source and binary forms, with or |
4 | 4 |
# without modification, are permitted provided that the following |
... | ... | |
34 | 34 |
from optparse import make_option |
35 | 35 |
|
36 | 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 |
|
37 |
from synnefo.management.common import convert_api_faults |
|
39 | 38 |
|
40 |
from synnefo.db.models import Network, NetworkInterface, SecurityGroup |
|
41 | 39 |
from synnefo.api import util |
42 | 40 |
from synnefo.management.common import get_network, get_vm |
43 | 41 |
from synnefo.logic import ports |
44 | 42 |
|
43 |
HELP_MSG = """Create a new port. |
|
44 |
|
|
45 |
Connect a server/router to a network by creating a new port. The port will |
|
46 |
get an IP address for each Subnet that is associated with the network.""" |
|
45 | 47 |
|
46 |
class Command(BaseCommand): |
|
47 |
can_import_settings = True |
|
48 |
output_transaction = True |
|
49 | 48 |
|
50 |
help = "Create a new port" |
|
49 |
class Command(BaseCommand): |
|
50 |
help = HELP_MSG |
|
51 | 51 |
|
52 | 52 |
option_list = BaseCommand.option_list + ( |
53 | 53 |
make_option( |
54 |
'--name',
|
|
55 |
dest='name',
|
|
54 |
"--name",
|
|
55 |
dest="name",
|
|
56 | 56 |
default=None, |
57 |
help="Name of port"),
|
|
57 |
help="Name of the port."),
|
|
58 | 58 |
make_option( |
59 |
'--network',
|
|
60 |
dest='network',
|
|
59 |
"--network",
|
|
60 |
dest="network_id",
|
|
61 | 61 |
default=None, |
62 |
help='The network to attach the port to'),
|
|
62 |
help="The ID of the network where the port will be created."),
|
|
63 | 63 |
make_option( |
64 |
'--server',
|
|
65 |
dest='server_id',
|
|
64 |
"--server",
|
|
65 |
dest="server_id",
|
|
66 | 66 |
default=None, |
67 |
help='The server id the port will be connected to'),
|
|
67 |
help="The ID of the server that the port will be connected to."),
|
|
68 | 68 |
make_option( |
69 |
'--router',
|
|
70 |
dest='router_id',
|
|
69 |
"--router",
|
|
70 |
dest="router_id",
|
|
71 | 71 |
default=None, |
72 |
help='The router id the port will be connected to'),
|
|
72 |
help="The ID of the router that the port will be connected to."),
|
|
73 | 73 |
make_option( |
74 |
'--security-groups',
|
|
75 |
dest='security-groups',
|
|
74 |
"--security-groups",
|
|
75 |
dest="security-groups",
|
|
76 | 76 |
default=None, |
77 |
help='Security Groups associated with the port'), |
|
77 |
help="Comma separated list of Security Group IDs to associate" |
|
78 |
" with the port."), |
|
78 | 79 |
) |
79 | 80 |
|
80 | 81 |
@convert_api_faults |
... | ... | |
82 | 83 |
if args: |
83 | 84 |
raise CommandError("Command doesn't accept any arguments") |
84 | 85 |
|
85 |
name = options['name']
|
|
86 |
network = options['network']
|
|
87 |
server = options['server_id']
|
|
88 |
router = options['router_id']
|
|
89 |
#assume giving security groups comma separated |
|
90 |
security_groups = options['security-groups']
|
|
86 |
name = options["name"]
|
|
87 |
network_id = options["network_id"]
|
|
88 |
server_id = options["server_id"]
|
|
89 |
router_id = options["router_id"]
|
|
90 |
# assume giving security groups comma separated
|
|
91 |
security_group_ids = options["security-groups"]
|
|
91 | 92 |
|
92 | 93 |
if not name: |
93 | 94 |
name = "" |
94 | 95 |
|
95 |
if (server and router) or not (server or router): |
|
96 |
raise CommandError('Please give either a server or a router id') |
|
97 |
|
|
98 |
if not network: |
|
99 |
raise CommandError("network is required") |
|
100 |
|
|
101 |
if server: |
|
102 |
device = server |
|
103 |
owner = 'vm' |
|
96 |
if (server_id and router_id) or not (server_id or router_id): |
|
97 |
raise CommandError("Please give either a server or a router id") |
|
98 |
|
|
99 |
if not network_id: |
|
100 |
raise CommandError("Please specify a 'network'") |
|
101 |
|
|
102 |
if server_id: |
|
103 |
owner = "vm" |
|
104 |
vm = get_vm(server_id) |
|
105 |
if vm.router: |
|
106 |
raise CommandError("Server '%s' does not exist." % server_id) |
|
107 |
elif router_id: |
|
108 |
owner = "router" |
|
109 |
vm = get_vm(router_id) |
|
110 |
if not vm.router: |
|
111 |
raise CommandError("Router '%s' does not exist." % router_id) |
|
104 | 112 |
else: |
105 |
device = router |
|
106 |
owner = 'router' |
|
107 |
|
|
108 |
#get the network |
|
109 |
network = get_network(network) |
|
113 |
raise CommandError("Neither server or router is specified") |
|
110 | 114 |
|
111 |
#get the vm
|
|
112 |
vm = get_vm(device)
|
|
115 |
# get the network
|
|
116 |
network = get_network(network_id)
|
|
113 | 117 |
|
114 |
#validate security groups |
|
118 |
# validate security groups
|
|
115 | 119 |
sg_list = [] |
116 |
if security_groups: |
|
117 |
security_groups = security_groups.split(",")
|
|
118 |
for gid in security_groups: |
|
120 |
if security_group_ids:
|
|
121 |
security_group_ids = security_group_ids.split(",")
|
|
122 |
for gid in security_group_ids:
|
|
119 | 123 |
sg = util.get_security_group(int(gid)) |
120 | 124 |
sg_list.append(sg) |
121 | 125 |
|
122 | 126 |
new_port = ports.create(network, vm, name, security_groups=sg_list, |
123 | 127 |
device_owner=owner) |
124 | 128 |
self.stdout.write("Created port '%s' in DB.\n" % new_port) |
129 |
# TODO: Display port information, like ip address |
|
130 |
# TODO: Add --wait argument to report progress about the Ganeti job. |
Also available in: Unified diff