summaryrefslogtreecommitdiff
path: root/openstack_dashboard/dashboards/project/stacks/mappings.py
blob: ac6d526ac4622afb4bbfdc42679002383d1f93db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# vim: tabstop=4 shiftwidth=4 softtabstop=4

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import json
import logging
import re
import urlparse

from django.core.urlresolvers import reverse
from django.template.defaultfilters import register

from openstack_dashboard.api.swift import FOLDER_DELIMITER

LOG = logging.getLogger(__name__)


resource_urls = {
    "AWS::EC2::Instance": {
        'link': 'horizon:project:instances:detail'},
    "AWS::EC2::NetworkInterface": {
        'link': 'horizon:project:networks:ports:detail'},
    "AWS::EC2::RouteTable": {
        'link': 'horizon:project:routers:detail'},
    "AWS::EC2::Subnet": {
        'link': 'horizon:project:networks:subnets:detail'},
    "AWS::EC2::Volume": {
        'link': 'horizon:project:volumes:detail'},
    "AWS::EC2::VPC": {
        'link': 'horizon:project:networks:detail'},
    "AWS::S3::Bucket": {
        'link': 'horizon:project:containers:index'},
    "OS::Quantum::Net": {
        'link': 'horizon:project:networks:detail'},
    "OS::Quantum::Port": {
        'link': 'horizon:project:networks:ports:detail'},
    "OS::Quantum::Router": {
        'link': 'horizon:project:routers:detail'},
    "OS::Quantum::Subnet": {
        'link': 'horizon:project:networks:subnets:detail'},
    "OS::Swift::Container": {
        'link': 'horizon:project:containers:index',
        'format_pattern': '%s' + FOLDER_DELIMITER},
}


def resource_to_url(resource):
    if not resource or not resource.physical_resource_id:
        return None

    mapping = resource_urls.get(resource.resource_type, {})
    try:
        if 'link' not in mapping:
            return None
        format_pattern = mapping.get('format_pattern') or '%s'
        rid = format_pattern % resource.physical_resource_id
        url = reverse(mapping['link'], args=(rid,))
    except Exception as e:
        LOG.exception(e)
        return None
    return url


@register.filter
def stack_output(output):
    if not output:
        return u''
    if isinstance(output, dict) or isinstance(output, list):
        return u'<pre>%s</pre>' % json.dumps(output, indent=2)
    if isinstance(output, basestring):
        parts = urlparse.urlsplit(output)
        if parts.netloc and parts.scheme in ('http', 'https'):
            return u'<a href="%s" target="_blank">%s</a>' % (output, output)
    return unicode(output)


resource_images = {
    'LB_FAILED': '/static/dashboard/img/lb-red.svg',
    'LB_DELETE': '/static/dashboard/img/lb-red.svg',
    'LB_IN_PROGRESS': '/static/dashboard/img/lb-gray.gif',
    'LB_COMPLETE': '/static/dashboard/img/lb-green.svg',
    'DB_FAILED': '/static/dashboard/img/db-red.svg',
    'DB_DELETE': '/static/dashboard/img/db-red.svg',
    'DB_IN_PROGRESS': '/static/dashboard/img/db-gray.gif',
    'DB_COMPLETE': '/static/dashboard/img/db-green.svg',
    'STACK_FAILED': '/static/dashboard/img/stack-red.svg',
    'STACK_DELETE': '/static/dashboard/img/stack-red.svg',
    'STACK_IN_PROGRESS': '/static/dashboard/img/stack-gray.gif',
    'STACK_COMPLETE': '/static/dashboard/img/stack-green.svg',
    'SERVER_FAILED': '/static/dashboard/img/server-red.svg',
    'SERVER_DELETE': '/static/dashboard/img/server-red.svg',
    'SERVER_IN_PROGRESS': '/static/dashboard/img/server-gray.gif',
    'SERVER_COMPLETE': '/static/dashboard/img/server-green.svg',
    'UNKNOWN_FAILED': '/static/dashboard/img/unknown-red.svg',
    'UNKNOWN_DELETE': '/static/dashboard/img/unknown-red.svg',
    'UNKNOWN_IN_PROGRESS': '/static/dashboard/img/unknown-gray.gif',
    'UNKNOWN_COMPLETE': '/static/dashboard/img/unknown-green.svg',
}


def get_resource_type(type):
    if re.search('LoadBalancer', type):
        return 'LB'
    elif re.search('DBInstance', type):
        return 'DB'
    elif re.search('Instance', type):
        return 'SERVER'
    elif re.search('stack', type):
        return 'STACK'
    else:
        return 'UNKNOWN'


def get_resource_status(status):
    if re.search('IN_PROGRESS', status):
        return 'IN_PROGRESS'
    elif re.search('FAILED', status):
        return 'FAILED'
    elif re.search('DELETE', status):
        return 'DELETE'
    else:
        return 'COMPLETE'


def get_resource_image(status, type):
    '''
    Sets the image url and in_progress action sw based on status.
    '''
    resource_type = get_resource_type(type)
    resource_status = get_resource_status(status)
    resource_state = resource_type + "_" + resource_status

    for key in resource_images:
        if key == resource_state:
            return resource_images.get(key)