~ubuntu-branches/ubuntu/utopic/horizon/utopic

« back to all changes in this revision

Viewing changes to openstack_dashboard/dashboards/project/data_processing/clusters/workflows/scale.py

  • Committer: Package Import Robot
  • Author(s): Chuck Short
  • Date: 2014-07-25 11:39:09 UTC
  • mfrom: (1.1.42)
  • Revision ID: package-import@ubuntu.com-20140725113909-b8920pdy87itn1ro
Tags: 1:2014.2~b2-0ubuntu1
* New upstream release.
* debian/patches/ubuntu_settings.patch: Refresed
* debian/patches/fix-dashboard-manage.patch: Refreshed
* debian/patches/fix-dashboard-django-wsgi.patch: Refreshed

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Licensed under the Apache License, Version 2.0 (the "License");
 
2
# you may not use this file except in compliance with the License.
 
3
# You may obtain a copy of the License at
 
4
#
 
5
#    http://www.apache.org/licenses/LICENSE-2.0
 
6
#
 
7
# Unless required by applicable law or agreed to in writing, software
 
8
# distributed under the License is distributed on an "AS IS" BASIS,
 
9
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
10
# implied.
 
11
# See the License for the specific language governing permissions and
 
12
# limitations under the License.
 
13
 
 
14
import json
 
15
import logging
 
16
 
 
17
from django.utils.translation import ugettext_lazy as _
 
18
 
 
19
from horizon import exceptions
 
20
 
 
21
from openstack_dashboard.api import sahara as saharaclient
 
22
import openstack_dashboard.dashboards.project.data_processing. \
 
23
    cluster_templates.workflows.create as clt_create_flow
 
24
import openstack_dashboard.dashboards.project.data_processing. \
 
25
    clusters.workflows.create as cl_create_flow
 
26
import openstack_dashboard.dashboards.project.data_processing. \
 
27
    utils.workflow_helpers as whelpers
 
28
 
 
29
from saharaclient.api import base as api_base
 
30
 
 
31
LOG = logging.getLogger(__name__)
 
32
 
 
33
 
 
34
class NodeGroupsStep(clt_create_flow.ConfigureNodegroups):
 
35
    pass
 
36
 
 
37
 
 
38
class ScaleCluster(cl_create_flow.ConfigureCluster,
 
39
                   whelpers.StatusFormatMixin):
 
40
    slug = "scale_cluster"
 
41
    name = _("Scale Cluster")
 
42
    finalize_button_name = _("Scale")
 
43
    success_url = "horizon:project:data_processing.clusters:index"
 
44
    default_steps = (NodeGroupsStep, )
 
45
 
 
46
    def __init__(self, request, context_seed, entry_point, *args, **kwargs):
 
47
        ScaleCluster._cls_registry = set([])
 
48
 
 
49
        self.success_message = _("Scaled cluster successfully started.")
 
50
 
 
51
        cluster_id = context_seed["cluster_id"]
 
52
        try:
 
53
            cluster = saharaclient.cluster_get(request, cluster_id)
 
54
            plugin = cluster.plugin_name
 
55
            hadoop_version = cluster.hadoop_version
 
56
 
 
57
            #init deletable nodegroups
 
58
            deletable = dict()
 
59
            for group in cluster.node_groups:
 
60
                deletable[group["name"]] = "false"
 
61
            request.GET = request.GET.copy()
 
62
            request.GET.update({
 
63
                "cluster_id": cluster_id,
 
64
                "plugin_name": plugin,
 
65
                "hadoop_version": hadoop_version,
 
66
                "deletable": deletable
 
67
            })
 
68
 
 
69
            super(ScaleCluster, self).__init__(request, context_seed,
 
70
                                           entry_point, *args,
 
71
                                           **kwargs)
 
72
 
 
73
            #init Node Groups
 
74
 
 
75
            for step in self.steps:
 
76
                if isinstance(step, clt_create_flow.ConfigureNodegroups):
 
77
                    ng_action = step.action
 
78
                    template_ngs = cluster.node_groups
 
79
 
 
80
                    if 'forms_ids' not in request.POST:
 
81
                        ng_action.groups = []
 
82
                        for id in range(0, len(template_ngs), 1):
 
83
                            group_name = "group_name_" + str(id)
 
84
                            template_id = "template_id_" + str(id)
 
85
                            count = "count_" + str(id)
 
86
                            templ_ng = template_ngs[id]
 
87
                            ng_action.groups.append(
 
88
                                {"name": templ_ng["name"],
 
89
                                 "template_id": templ_ng["node_group_template_id"],
 
90
                                 "count": templ_ng["count"],
 
91
                                 "id": id,
 
92
                                 "deletable": "false"})
 
93
 
 
94
                            whelpers.build_node_group_fields(ng_action,
 
95
                                                             group_name,
 
96
                                                             template_id,
 
97
                                                             count)
 
98
        except Exception:
 
99
            exceptions.handle(request,
 
100
                              _("Unable to fetch cluster to scale"))
 
101
 
 
102
    def format_status_message(self, message):
 
103
        # Scaling form requires special handling because it has no Cluster name
 
104
        # in it's context
 
105
 
 
106
        error_description = getattr(self, 'error_description', None)
 
107
        if error_description:
 
108
            return error_description
 
109
        else:
 
110
            return self.success_message
 
111
 
 
112
    def handle(self, request, context):
 
113
        cluster_id = request.GET["cluster_id"]
 
114
        try:
 
115
            cluster = saharaclient.cluster_get(request, cluster_id)
 
116
            existing_node_groups = set([])
 
117
            for ng in cluster.node_groups:
 
118
                existing_node_groups.add(ng["name"])
 
119
 
 
120
            scale_object = dict()
 
121
 
 
122
            ids = json.loads(context["ng_forms_ids"])
 
123
 
 
124
            for _id in ids:
 
125
                name = context["ng_group_name_%s" % _id]
 
126
                template_id = context["ng_template_id_%s" % _id]
 
127
                count = context["ng_count_%s" % _id]
 
128
 
 
129
                if name not in existing_node_groups:
 
130
                    if "add_node_groups" not in scale_object:
 
131
                        scale_object["add_node_groups"] = []
 
132
 
 
133
                    scale_object["add_node_groups"].append(
 
134
                        {"name": name,
 
135
                         "node_group_template_id": template_id,
 
136
                         "count": int(count)})
 
137
                else:
 
138
                    old_count = None
 
139
                    for ng in cluster.node_groups:
 
140
                        if name == ng["name"]:
 
141
                            old_count = ng["count"]
 
142
                            break
 
143
 
 
144
                    if old_count != count:
 
145
                        if "resize_node_groups" not in scale_object:
 
146
                            scale_object["resize_node_groups"] = []
 
147
 
 
148
                        scale_object["resize_node_groups"].append(
 
149
                            {"name": name,
 
150
                             "count": int(count)}
 
151
                        )
 
152
        except Exception:
 
153
            scale_object = {}
 
154
            exceptions.handle(request,
 
155
                              _("Unable to fetch cluster to scale."))
 
156
 
 
157
        try:
 
158
            saharaclient.cluster_scale(request, cluster_id, scale_object)
 
159
            return True
 
160
        except api_base.APIException as e:
 
161
            self.error_description = str(e)
 
162
            return False
 
163
        except Exception:
 
164
            exceptions.handle(request,
 
165
                              _("Scale cluster operation failed"))
 
166
            return False