Revision 724e17a6

b/snf-cyclades-app/synnefo/app_settings/__init__.py
6 6
    'synnefo.logic',
7 7
    'synnefo.plankton',
8 8
    'synnefo.helpdesk',
9
    'synnefo.nodeapi',
9 10
    'synnefo.ui.userdata',
10 11
    'synnefo.helpdesk',
11 12
]
b/snf-cyclades-app/synnefo/app_settings/urls.py
39 39
        name='ui_machines_console'),
40 40
    url(r'^machines/connect$', 'synnefo.ui.views.machines_connect',
41 41
        name='ui_machines_connect'),
42
    (r'^nodeapi/', include('synnefo.nodeapi.urls')),
42 43
    (r'^api/', include('synnefo.api.urls')),
43 44
    (r'^plankton/', include('synnefo.plankton.urls')),
44 45
    (r'^helpdesk/', include('synnefo.helpdesk.urls')),
b/snf-cyclades-app/synnefo/nodeapi/__init__.py
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 uuid import uuid4
35

  
36
from django.core.cache import get_cache
37
from django.core import signals
38

  
39
from synnefo.nodeapi.settings import CACHE_KEY_PREFIX, CACHE_BACKEND
40

  
41
def get_uuid():
42
    return str(uuid4())
43

  
44
def get_key(*args):
45
    args = map(str, filter(bool, list(args)))
46
    args.insert(0, CACHE_KEY_PREFIX)
47
    return "_".join(args)
48

  
49
# initialize serverparams cache backend
50
backend = get_cache(CACHE_BACKEND)
51

  
52
# Some caches -- pythont-memcached in particular -- need to do a cleanup at the
53
# end of a request cycle. If the cache provides a close() method, wire it up
54
# here.
55
if hasattr(backend, 'close'):
56
    signals.request_finished.connect(backend.close)
57

  
b/snf-cyclades-app/synnefo/nodeapi/models.py
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 django.conf import settings
35
from django.utils import simplejson as json
36
from django.core.urlresolvers import reverse
37

  
38
from synnefo.api.servers import server_created
39
from synnefo.nodeapi import backend, get_key, get_uuid
40

  
41

  
42
def create_server_params(sender, created_vm_params, **kwargs):
43
    json_value = json.dumps(created_vm_params)
44
    uuid = get_uuid()
45
    key = get_key(uuid)
46
    backend.set(key, json_value)
47

  
48
    # inject sender (vm) with its parameters url
49
    setattr(sender, 'params_url', reverse('nodeapi_server_params', args=[uuid]))
50
    return uuid
51

  
52
server_created.connect(create_server_params)
53

  
b/snf-cyclades-app/synnefo/nodeapi/settings.py
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 django.conf import settings
35

  
36
CACHE_BACKEND = getattr(settings, 'NODEAPI_CACHE_BACKEND',
37
    settings.CACHE_BACKEND)
38
CACHE_KEY_PREFIX = getattr(settings, 'NODEAPI_CACHE_KEY_PREFIX',
39
    'nodeapi')
40
RESET_PARAMS = getattr(settings, 'NODEAPI_RESET_PARAMS', True)
41

  
b/snf-cyclades-app/synnefo/nodeapi/tests.py
1
# -*- coding: utf8 -*-
2
# Copyright 2012 GRNET S.A. All rights reserved.
3
#
4
# Redistribution and use in source and binary forms, with or
5
# without modification, are permitted provided that the following
6
# conditions are met:
7
#
8
#   1. Redistributions of source code must retain the above
9
#      copyright notice, this list of conditions and the following
10
#      disclaimer.
11
#
12
#   2. Redistributions in binary form must reproduce the above
13
#      copyright notice, this list of conditions and the following
14
#      disclaimer in the documentation and/or other materials
15
#      provided with the distribution.
16
#
17
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
# POSSIBILITY OF SUCH DAMAGE.
29
#
30
# The views and conclusions contained in the software and
31
# documentation are those of the authors and should not be
32
# interpreted as representing official policies, either expressed
33
# or implied, of GRNET S.A.
34

  
35
from django.test import TestCase
36
from django.utils import simplejson as json
37

  
38
class TestServerParams(TestCase):
39

  
40
    def test_cache_backend(self):
41
        from synnefo.nodeapi import backend
42
        backend.set("test", 1)
43
        self.assertEqual(backend.get("test"), 1)
44
        backend.set("test", None)
45
        self.assertEqual(backend.get("test"), None)
46

  
47
    def test_get_key(self):
48
        from synnefo.nodeapi import get_key
49
        self.assertEqual(get_key("snf-1", "12345"), "nodeapi_snf-1_12345")
50
        self.assertEqual(get_key("snf-1", None, "12345"), "nodeapi_snf-1_12345")
51

  
52
    def test_params_create(self):
53
        from synnefo.nodeapi.models import create_server_params
54
        from synnefo.nodeapi import backend
55
        try:
56
            from synnefo.api.servers import server_created
57
            from synnefo.db.models import VirtualMachine
58
        except ImportError:
59
            print "Skipping test_params_create"
60
            return
61

  
62
        # mimic server creation signal called
63
        vm = VirtualMachine()
64
        params = {'password': 'X^942Jjfdsa', 'personality': {}}
65
        uuid = create_server_params(sender=vm, created_vm_params=params)
66

  
67
        self.assertEqual(vm.params_url, '/nodeapi/server-params/%s' % uuid)
68
        key = "nodeapi_%s" % uuid
69
        self.assertEqual(type(backend.get(key)), str)
70
        data = json.loads(backend.get(key))
71
        self.assertEqual('password' in data, True)
72
        self.assertEqual('personality' in data, True)
73
        self.assertEqual(data.get('password'), 'X^942Jjfdsa')
74

  
75
    def test_params_view(self):
76
        pass
77

  
b/snf-cyclades-app/synnefo/nodeapi/urls.py
1
# Copyright 2011 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 django.conf.urls.defaults import include, patterns, url
35

  
36
urlpatterns = patterns('synnefo.nodeapi.views',
37
    url(r'^server-params/(?P<uuid>.*)$', 'server_params', name="nodeapi_server_params"),
38
)
b/snf-cyclades-app/synnefo/nodeapi/views.py
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 django.http import Http404, HttpResponse
35

  
36
from synnefo.nodeapi import backend, get_key
37
from synnefo.nodeapi.settings import RESET_PARAMS
38

  
39
def server_params(request, uuid):
40
    uuid = request.GET.get('uuid', None)
41
    if not uuid:
42
        raise Http404
43

  
44
    params = backend.get(get_key(uuid))
45
    if not params:
46
        raise Http404
47

  
48
    if RESET_PARAMS:
49
        backend.set(uuid, None)
50

  
51
    return HttpResponse(params, content_type="application/json")
52

  

Also available in: Unified diff