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
5
# http://www.apache.org/licenses/LICENSE-2.0
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
11
# See the License for the specific language governing permissions and
12
# limitations under the License.
17
from django.utils.translation import ugettext_lazy as _
19
from horizon import exceptions
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
29
from saharaclient.api import base as api_base
31
LOG = logging.getLogger(__name__)
34
class NodeGroupsStep(clt_create_flow.ConfigureNodegroups):
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, )
46
def __init__(self, request, context_seed, entry_point, *args, **kwargs):
47
ScaleCluster._cls_registry = set([])
49
self.success_message = _("Scaled cluster successfully started.")
51
cluster_id = context_seed["cluster_id"]
53
cluster = saharaclient.cluster_get(request, cluster_id)
54
plugin = cluster.plugin_name
55
hadoop_version = cluster.hadoop_version
57
#init deletable nodegroups
59
for group in cluster.node_groups:
60
deletable[group["name"]] = "false"
61
request.GET = request.GET.copy()
63
"cluster_id": cluster_id,
64
"plugin_name": plugin,
65
"hadoop_version": hadoop_version,
66
"deletable": deletable
69
super(ScaleCluster, self).__init__(request, context_seed,
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
80
if 'forms_ids' not in request.POST:
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"],
92
"deletable": "false"})
94
whelpers.build_node_group_fields(ng_action,
99
exceptions.handle(request,
100
_("Unable to fetch cluster to scale"))
102
def format_status_message(self, message):
103
# Scaling form requires special handling because it has no Cluster name
106
error_description = getattr(self, 'error_description', None)
107
if error_description:
108
return error_description
110
return self.success_message
112
def handle(self, request, context):
113
cluster_id = request.GET["cluster_id"]
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"])
120
scale_object = dict()
122
ids = json.loads(context["ng_forms_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]
129
if name not in existing_node_groups:
130
if "add_node_groups" not in scale_object:
131
scale_object["add_node_groups"] = []
133
scale_object["add_node_groups"].append(
135
"node_group_template_id": template_id,
136
"count": int(count)})
139
for ng in cluster.node_groups:
140
if name == ng["name"]:
141
old_count = ng["count"]
144
if old_count != count:
145
if "resize_node_groups" not in scale_object:
146
scale_object["resize_node_groups"] = []
148
scale_object["resize_node_groups"].append(
154
exceptions.handle(request,
155
_("Unable to fetch cluster to scale."))
158
saharaclient.cluster_scale(request, cluster_id, scale_object)
160
except api_base.APIException as e:
161
self.error_description = str(e)
164
exceptions.handle(request,
165
_("Scale cluster operation failed"))