Statistics
| Branch: | Tag: | Revision:

root / snf-common / synnefo / lib / services.py @ b6b98d5e

History | View | Annotate | Download (4.5 kB)

1
# Copyright 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

    
34
from copy import deepcopy
35
from synnefo.lib import join_urls
36
from synnefo.util.keypath import get_path, set_path
37
from urlparse import urlparse
38

    
39

    
40
class ServiceNotFound(Exception):
41
    pass
42

    
43

    
44
def fill_endpoints(service, base_url):
45
    prefix = get_path(service, 'prefix')
46
    endpoints = get_path(service, 'endpoints')
47
    for endpoint in endpoints:
48
        version = get_path(endpoint, 'versionId')
49
        if version and version[0].isdigit():
50
            version = 'v' + version
51

    
52
        publicURL = get_path(endpoint, 'publicURL')
53
        if publicURL is not None:
54
            continue
55

    
56
    publicURL = join_urls(base_url, prefix, version).rstrip('/') + '/'
57
    set_path(endpoint, 'publicURL', publicURL)
58

    
59

    
60
def filter_public(services):
61
    public_services = {}
62
    for name, service in services.iteritems():
63
        if service.get('public', False):
64
            public_services[name] = deepcopy(service)
65
    return public_services
66

    
67

    
68
def filter_component(services, component_name):
69
    component_services = {}
70
    for name, service in services.iteritems():
71
        if service['component'] == component_name:
72
            component_services[name] = service
73
    return component_services
74

    
75

    
76
def get_service_prefix(services, service_name):
77
    return get_path(services, service_name + '.prefix')
78

    
79

    
80
def get_service_resources(services, service_name):
81
    return get_path(services, service_name + '.resources')
82

    
83

    
84
def get_public_endpoint(services, service_type, version=None):
85
    found_endpoints = {}
86
    for service_name, service in services.iteritems():
87
        if service_type != service['type']:
88
            continue
89

    
90
        for endpoint in service['endpoints']:
91
            endpoint_version = endpoint['versionId']
92
            if version is not None:
93
                if version != endpoint_version:
94
                    continue
95
            found_endpoints[endpoint_version] = endpoint
96

    
97
    if not found_endpoints:
98
        m = "No endpoint found for service type '{0}'".format(service_type)
99
        if version is not None:
100
            m += " and version '{0}'".format(version)
101
        raise ServiceNotFound(m)
102

    
103
    selected = sorted(found_endpoints.keys())[-1]
104
    return found_endpoints[selected]['publicURL']
105

    
106

    
107
def get_service_endpoints(services, service_name, version=None):
108
    endpoints = get_path(services, service_name + '.endpoints')
109
    if version is not None:
110
        filtered = []
111
        for endpoint in endpoints:
112
            if endpoint['versionId'] != version:
113
                continue
114
            filtered.append(endpoint)
115
        endpoints = filtered
116
    return endpoints
117

    
118

    
119
def get_service_path(services, service_name, version=None):
120
    endpoints = get_service_endpoints(services, service_name, version=version)
121
    if not endpoints:
122
        m = "No endpoint found for service '{0}'".format(service_name)
123
        if version is not None:
124
            m += " and version '{0}'".format(version)
125
        raise ServiceNotFound(m)
126
    service_url = endpoints[0]['publicURL']
127
    return urlparse(service_url).path.rstrip('/') + '/'