Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / ports.py @ 79f4eec0

History | View | Annotate | Download (4.4 kB)

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
from functools import wraps
34
from django.db import transaction
35

    
36
#from django.conf import settings
37
from synnefo.api import util
38
from snf_django.lib.api import faults
39
from synnefo.db.models import NetworkInterface
40
from synnefo.logic import backend
41

    
42
from logging import getLogger
43
log = getLogger(__name__)
44

    
45

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

    
55

    
56
@transaction.commit_on_success
57
def create(network, machine, ipaddress, name="", security_groups=None,
58
           device_owner='vm'):
59
    """Create a new port connecting a server/router to a network.
60

61
    Connect a server/router to a network by creating a new Port. If
62
    'ipaddress' argument is specified, the port will be assigned this
63
    IPAddress. Otherwise, an IPv4 address from the IPv4 subnet will be
64
    allocated.
65

66
    """
67
    if network.state != 'ACTIVE':
68
        raise faults.Conflict('Network build in process')
69

    
70
    user_id = machine.userid
71

    
72
    if ipaddress is None:
73
        if network.subnets.filter(ipversion=4).exists():
74
            ipaddress = util.allocate_ip(network, user_id)
75
    else:
76
        if ipaddress.nic is not None:
77
            raise faults.BadRequest("Address '%s' already in use." %
78
                                    ipaddress.address)
79
    #create the port
80
    port = NetworkInterface.objects.create(name=name,
81
                                           network=network,
82
                                           machine=machine,
83
                                           userid=user_id,
84
                                           device_owner=device_owner,
85
                                           state="BUILDING")
86
    #add the security groups if any
87
    if security_groups:
88
        port.security_groups.add(*security_groups)
89

    
90
    # If no IPAddress is specified, try to allocate one
91
    if ipaddress is None and network.subnets.filter(ipversion=4).exists():
92
        ipaddress = util.allocate_ip(network, user_id)
93

    
94
    # Associate the IPAddress with the NIC
95
    if ipaddress is not None:
96
        ipaddress.nic = port
97
        ipaddress.save()
98

    
99
    jobID = backend.connect_to_network(machine, port)
100

    
101
    log.info("Created Port %s with IP Address: %s. Job: %s",
102
             port, ipaddress, jobID)
103

    
104
    # TODO: Consider quotas for Ports
105
    # TODO: Associate jobID with the port
106

    
107
    return port
108

    
109

    
110
@transaction.commit_on_success
111
def delete(port):
112
    """Delete a port by removing the NIC card the instance.
113

114
    Send a Job to remove the NIC card from the instance. The port
115
    will be deleted and the associated IPv4 addressess will be released
116
    when the job completes successfully.
117

118
    """
119

    
120
    jobID = backend.disconnect_from_network(port.machine, port)
121
    log.info("Removing port %s, Job: %s", port, jobID)
122
    return port