root / snf-cyclades-app / synnefo / logic / ports.py @ 6d5c0344
History | View | Annotate | Download (3.7 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, name="", security_groups=None, |
58 |
device_owner='vm'):
|
59 |
"""Create a new port by giving a vm and a network"""
|
60 |
if network.state != 'ACTIVE': |
61 |
raise faults.Conflict('Network build in process') |
62 |
|
63 |
user_id = machine.userid |
64 |
#create the port
|
65 |
port = NetworkInterface.objects.create(name=name, |
66 |
network=network, |
67 |
machine=machine, |
68 |
userid=user_id, |
69 |
device_owner=device_owner, |
70 |
state="BUILDING")
|
71 |
#add the security groups if any
|
72 |
if security_groups:
|
73 |
port.security_groups.add(*security_groups) |
74 |
|
75 |
ipaddress = None
|
76 |
if network.subnets.filter(ipversion=4).exists(): |
77 |
ipaddress = util.allocate_ip(network, user_id) |
78 |
ipaddress.nic = port |
79 |
ipaddress.save() |
80 |
|
81 |
jobID = backend.connect_to_network(machine, port) |
82 |
|
83 |
log.info("Created Port %s with IP Address: %s. Job: %s",
|
84 |
port, ipaddress, jobID) |
85 |
|
86 |
# TODO: Consider quotas for Ports
|
87 |
# TODO: Associate jobID with the port
|
88 |
|
89 |
return port
|
90 |
|
91 |
|
92 |
@transaction.commit_on_success
|
93 |
def delete(port): |
94 |
"""Delete a port by removing the NIC card the instance.
|
95 |
|
96 |
Send a Job to remove the NIC card from the instance. The port
|
97 |
will be deleted and the associated IPv4 addressess will be released
|
98 |
when the job completes successfully.
|
99 |
|
100 |
"""
|
101 |
|
102 |
jobID = backend.disconnect_from_network(port.machine, port) |
103 |
log.info("Removing port %s, Job: %s", port, jobID)
|
104 |
return port
|