Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4 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.logic import ports
43

    
44

    
45
class Command(BaseCommand):
46
    can_import_settings = True
47
    output_transaction = True
48

    
49
    help = "Create a new port"
50

    
51
    option_list = BaseCommand.option_list + (
52
        make_option(
53
            '--name',
54
            dest='name',
55
            default=None,
56
            help="Name of port"),
57
        make_option(
58
            '--owner',
59
            dest='owner',
60
            default=None,
61
            help="The owner of the port"),
62
        make_option(
63
            '--network',
64
            dest='network',
65
            default=None,
66
            help='The network to attach the port to'),
67
        make_option(
68
            '--device',
69
            dest='device_id',
70
            default=None,
71
            help='The VM id the port will be connected to'),
72
        make_option(
73
            '--security-groups',
74
            dest='security-groups',
75
            default=None,
76
            help='Security Groups associated with the port'),
77
    )
78

    
79
    @convert_api_faults
80
    def handle(self, *args, **options):
81
        if args:
82
            raise CommandError("Command doesn't accept any arguments")
83

    
84
        name = options['name']
85
        network = options['network']
86
        device = options['device_id']
87
        #assume giving security groups comma separated
88
        security_groups = options['security-groups']
89
        userid = options["owner"]
90

    
91
        if not name:
92
            name=""
93

    
94
        if not network:
95
            raise CommandError("network is required")
96
        if not device:
97
            raise CommandError("device is required")
98
        if not userid:
99
            raise CommandError("owner is required")
100

    
101
        #get the network
102
        network = util.get_network(network, userid, non_deleted=True)
103

    
104
        #get the vm
105
        vm = util.get_vm(device, userid)
106

    
107
        #validate security groups
108
        sg_list = []
109
        if security_groups:
110
            security_groups = security_groups.split(",")
111
            for gid in security_groups:
112
                sg = util.get_security_group(int(gid))
113
                sg_list.append(sg)
114

    
115
        new_port = ports.create(userid, network, vm, security_groups=sg_list)
116
        self.stdout.write("Created port '%s' in DB.\n" % new_port)