Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / api / management / commands / port-create.py @ d5a4a8d1

History | View | Annotate | Download (5.7 kB)

1
# Copyright 2013 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 convert_api_faults
38

    
39
from synnefo.api import util
40
from synnefo.management import common, pprint
41
from synnefo.logic import ports
42

    
43
HELP_MSG = """Create a new port.
44

45
Connect a server/router to a network by creating a new port. If 'floating_ip'
46
option is used, the specified floating IP will be assigned to the new port.
47
Otherwise, the port will get an IP address for each Subnet that is associated
48
with the network."""
49

    
50

    
51
class Command(BaseCommand):
52
    help = HELP_MSG
53

    
54
    option_list = BaseCommand.option_list + (
55
        make_option(
56
            "--name",
57
            dest="name",
58
            default=None,
59
            help="Name of the port."),
60
        make_option(
61
            "--network",
62
            dest="network_id",
63
            default=None,
64
            help="The ID of the network where the port will be created."),
65
        make_option(
66
            "--server",
67
            dest="server_id",
68
            default=None,
69
            help="The ID of the server that the port will be connected to."),
70
        #make_option(
71
        #    "--router",
72
        #    dest="router_id",
73
        #    default=None,
74
        #    help="The ID of the router that the port will be connected to."),
75
        make_option(
76
            "--floating-ip",
77
            dest="floating_ip_id",
78
            default=None,
79
            help="The ID of the floating IP to use for the port."),
80
        make_option(
81
            "--security-groups",
82
            dest="security-groups",
83
            default=None,
84
            help="Comma separated list of Security Group IDs to associate"
85
                 " with the port."),
86
    )
87

    
88
    @convert_api_faults
89
    def handle(self, *args, **options):
90
        if args:
91
            raise CommandError("Command doesn't accept any arguments")
92

    
93
        name = options["name"]
94
        network_id = options["network_id"]
95
        server_id = options["server_id"]
96
        #router_id = options["router_id"]
97
        router_id = None
98
        floating_ip_id = options["floating_ip_id"]
99
        # assume giving security groups comma separated
100
        security_group_ids = options["security-groups"]
101

    
102
        if not name:
103
            name = ""
104

    
105
        if (server_id and router_id) or not (server_id or router_id):
106
            raise CommandError("Please give either a server or a router id")
107

    
108
        if not network_id:
109
            raise CommandError("Please specify a 'network'")
110

    
111
        if server_id:
112
            owner = "vm"
113
            vm = common.get_vm(server_id)
114
            #if vm.router:
115
            #    raise CommandError("Server '%s' does not exist." % server_id)
116
        elif router_id:
117
            owner = "router"
118
            vm = common.get_vm(router_id)
119
            if not vm.router:
120
                raise CommandError("Router '%s' does not exist." % router_id)
121
        else:
122
            raise CommandError("Neither server or router is specified")
123

    
124
        floating_ip = None
125
        if floating_ip_id:
126
            floating_ip = common.get_floating_ip_by_id(floating_ip_id,
127
                                                       for_update=True)
128
            if floating_ip.userid != vm.userid:
129
                msg = "Floating IP %s does not belong to server/router owner."
130
                raise CommandError(msg % floating_ip)
131

    
132
        # get the network
133
        network = common.get_network(network_id)
134

    
135
        # validate security groups
136
        sg_list = []
137
        if security_group_ids:
138
            security_group_ids = security_group_ids.split(",")
139
            for gid in security_group_ids:
140
                sg = util.get_security_group(int(gid))
141
                sg_list.append(sg)
142

    
143
        new_port = ports.create(network, vm, name=name, ipaddress=floating_ip,
144
                                security_groups=sg_list,
145
                                device_owner=owner)
146
        self.stdout.write("Created port '%s' in DB:\n" % new_port)
147
        pprint.pprint_port(new_port, stdout=self.stdout)
148
        pprint.pprint_port_ips(new_port, stdout=self.stdout)
149
        # TODO: Display port information, like ip address
150
        # TODO: Add --wait argument to report progress about the Ganeti job.