Revision f16aa9e6

b/snf-cyclades-app/synnefo/api/ports.py
43 43

  
44 44
from synnefo.api import util
45 45
from synnefo.db.models import NetworkInterface, SecurityGroup, IPAddress
46
from synnefo.logic import ports
46 47

  
47 48
from logging import getLogger
48 49

  
......
96 97

  
97 98

  
98 99
@api.api_method(http_method='POST', user_required=True, logger=log)
99
@transaction.commit_manually
100 100
def create_port(request):
101 101
    '''
102 102
    '''
103 103
    user_id = request.user_uniq
104 104
    req = api.utils.get_request_dict(request)
105 105
    log.info('create_port %s', req)
106
    try:
107
        port_dict = api.utils.get_attribute(req, "port")
108
        net_id = api.utils.get_attribute(port_dict, "network_id")
109
        dev_id = api.utils.get_attribute(port_dict, "device_id")
110 106

  
111
        network = util.get_network(net_id, request.user_uniq, non_deleted=True)
107
    port_dict = api.utils.get_attribute(req, "port")
108
    net_id = api.utils.get_attribute(port_dict, "network_id")
109
    dev_id = api.utils.get_attribute(port_dict, "device_id")
112 110

  
113
        if network.public:
114
            raise api.faults.Forbidden('forbidden')
111
    network = util.get_network(net_id, request.user_uniq, non_deleted=True)
115 112

  
116
        if network.state != 'ACTIVE':
117
            raise api.faults.Conflict('Network build in process')
113
    if network.public:
114
        raise api.faults.Forbidden('forbidden')
118 115

  
119
        vm = util.get_vm(dev_id, request.user_uniq)
120 116

  
121
        name = api.utils.get_attribute(port_dict, "name", required=False)
117
    vm = util.get_vm(dev_id, request.user_uniq)
122 118

  
123
        if name is None:
124
            name = ""
119
    name = api.utils.get_attribute(port_dict, "name", required=False)
125 120

  
126
        sg_list = []
127
        security_groups = api.utils.get_attribute(port_dict,
128
                                                  "security_groups",
129
                                                  required=False)
130
        #validate security groups
131
        # like get security group from db
132
        if security_groups:
133
            for gid in security_groups:
134
                try:
135
                    sg = SecurityGroup.objects.get(id=int(gid))
136
                    sg_list.append(sg)
137
                except (ValueError, SecurityGroup.DoesNotExist):
138
                    raise api.faults.ItemNotFound("Not valid security group")
139

  
140
        #create the port
141
        new_port = NetworkInterface.objects.create(name=name,
142
                                                   network=network,
143
                                                   machine=vm,
144
                                                   device_owner="vm",
145
                                                   state="BUILDING")
146
        #add the security groups
147
        new_port.security_groups.add(*sg_list)
148

  
149
        #add new port to every subnet of the network
150
        for subn in network.subnets.all():
151
            IPAddress.objects.create(subnet=subn,
152
                                     network=network,
153
                                     nic=new_port,
154
                                     userid=user_id,
155
                                     # FIXME
156
                                     address="192.168.0." + str(subn.id))
157

  
158
    except:
159
        transaction.rollback()
160
        log.info("roll")
161
        raise
121
    if name is None:
122
        name = ""
162 123

  
163
    else:
164
        transaction.commit()
165
        log.info("commit")
124
    sg_list = []
125
    security_groups = api.utils.get_attribute(port_dict,
126
                                              "security_groups",
127
                                              required=False)
128
    #validate security groups
129
    # like get security group from db
130
    if security_groups:
131
        for gid in security_groups:
132
            try:
133
                sg = SecurityGroup.objects.get(id=int(gid))
134
                sg_list.append(sg)
135
            except (ValueError, SecurityGroup.DoesNotExist):
136
                raise api.faults.ItemNotFound("Not valid security group")
137

  
138
    new_port = ports.create(user_id, network, vm, security_groups=sg_list)
166 139

  
167 140
    response = render_port(request, port_to_dict(new_port), status=201)
168 141

  
b/snf-cyclades-app/synnefo/logic/ports.py
1
# Copyright 2011-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
import ipaddr
34

  
35
from functools import wraps
36
from django.db import transaction
37

  
38
from django.conf import settings
39
from snf_django.lib.api import faults
40
from synnefo.api import util
41
from synnefo import quotas
42
from synnefo.db.models import NetworkInterface, IPAddress
43

  
44
from logging import getLogger
45
log = getLogger(__name__)
46

  
47

  
48
def port_command(action):
49
    def decorator(func):
50
        @wraps(func)
51
        @transaction.commit_on_success()
52
        def wrapper(port, *args, **kwargs):
53
            return func(port, *args, **kwargs)
54
        return wrapper
55
    return decorator
56

  
57
@transaction.commit_on_success
58
def create(userid, network, machine, name="", security_groups=None,
59
           device_owner='vm'):
60

  
61
    if network.state != 'ACTIVE':
62
        raise faults.Conflict('Network build in process')
63

  
64
    #create the port
65
    port = NetworkInterface.objects.create(name=name,
66
                                           network=network,
67
                                           machine=machine,
68
                                           device_owner=device_owner,
69
                                           state="BUILDING")
70
    #add the security groups if any
71
    if security_groups:
72
        port.security_groups.add(*security_groups)
73

  
74
    #add new port to every subnet of the network
75
    for subn in network.subnets.all():
76
        IPAddress.objects.create(subnet=subn,
77
                                 network=network,
78
                                 nic=port,
79
                                 userid=userid,
80
                                 # FIXME
81
                                 address="192.168.0." + str(subn.id))
82

  
83
    # Issue commission to Quotaholder and accept it since at the end of
84
    # this transaction the Network object will be created in the DB.
85
    # Note: the following call does a commit!
86
    #quotas.issue_and_accept_commission(new_port)
87

  
88
    return port

Also available in: Unified diff