1
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2
# not use this file except in compliance with the License. You may obtain
3
# 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, WITHOUT
9
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10
# License for the specific language governing permissions and limitations
13
from django.utils.translation import ugettext_lazy as _
15
from horizon import exceptions
16
from horizon import forms
17
from horizon import workflows
19
from openstack_dashboard import api
20
from openstack_dashboard.dashboards.admin.aggregates import constants
23
class SetAggregateInfoAction(workflows.Action):
24
name = forms.CharField(label=_("Name"),
27
availability_zone = forms.CharField(label=_("Availability Zone"),
32
name = _("Host Aggregate Info")
33
help_text = _("From here you can create a new "
34
"host aggregate to organize instances.")
35
slug = "set_aggregate_info"
38
cleaned_data = super(SetAggregateInfoAction, self).clean()
39
name = cleaned_data.get('name')
42
aggregates = api.nova.aggregate_details_list(self.request)
44
msg = _('Unable to get host aggregate list')
45
exceptions.check_message(["Connection", "refused"], msg)
47
if aggregates is not None:
48
for aggregate in aggregates:
49
if aggregate.name.lower() == name.lower():
50
raise forms.ValidationError(
51
_('The name "%s" is already used by '
52
'another host aggregate.')
58
class SetAggregateInfoStep(workflows.Step):
59
action_class = SetAggregateInfoAction
60
contributes = ("availability_zone",
64
class AddHostsToAggregateAction(workflows.MembershipAction):
65
def __init__(self, request, *args, **kwargs):
66
super(AddHostsToAggregateAction, self).__init__(request,
69
err_msg = _('Unable to get the available hosts')
71
default_role_field_name = self.get_default_role_field_name()
72
self.fields[default_role_field_name] = forms.CharField(required=False)
73
self.fields[default_role_field_name].initial = 'member'
75
field_name = self.get_member_field_name('member')
76
self.fields[field_name] = forms.MultipleChoiceField(required=False)
80
hosts = api.nova.host_list(request)
82
exceptions.handle(request, err_msg)
86
if host.host_name not in host_names and host.service == u'compute':
87
host_names.append(host.host_name)
90
self.fields[field_name].choices = \
91
[(host_name, host_name) for host_name in host_names]
94
name = _("Hosts within aggregate")
95
slug = "add_host_to_aggregate"
98
class ManageAggregateHostsAction(workflows.MembershipAction):
99
def __init__(self, request, *args, **kwargs):
100
super(ManageAggregateHostsAction, self).__init__(request,
103
err_msg = _('Unable to get the available hosts')
105
default_role_field_name = self.get_default_role_field_name()
106
self.fields[default_role_field_name] = forms.CharField(required=False)
107
self.fields[default_role_field_name].initial = 'member'
109
field_name = self.get_member_field_name('member')
110
self.fields[field_name] = forms.MultipleChoiceField(required=False)
112
aggregate_id = self.initial['id']
113
aggregate = api.nova.aggregate_get(request, aggregate_id)
114
aggregate_hosts = aggregate.hosts
118
hosts = api.nova.host_list(request)
120
exceptions.handle(request, err_msg)
124
if host.host_name not in host_names and host.service == u'compute':
125
host_names.append(host.host_name)
128
self.fields[field_name].choices = \
129
[(host_name, host_name) for host_name in host_names]
131
self.fields[field_name].initial = aggregate_hosts
134
name = _("Hosts within aggregate")
137
class AddHostsToAggregateStep(workflows.UpdateMembersStep):
138
action_class = AddHostsToAggregateAction
139
help_text = _("You can add hosts to this aggregate. One host can be added "
140
"to one or more aggregate. You can also add the hosts later "
141
"by editing the aggregate.")
142
available_list_title = _("All available hosts")
143
members_list_title = _("Selected hosts")
144
no_available_text = _("No hosts found.")
145
no_members_text = _("No host selected.")
147
contributes = ("hosts_aggregate",)
149
def contribute(self, data, context):
151
member_field_name = self.get_member_field_name('member')
152
context['hosts_aggregate'] = data.get(member_field_name, [])
156
class ManageAggregateHostsStep(workflows.UpdateMembersStep):
157
action_class = ManageAggregateHostsAction
158
help_text = _("You can add hosts to this aggregate, as well as remove "
160
available_list_title = _("All Available Hosts")
161
members_list_title = _("Selected Hosts")
162
no_available_text = _("No Hosts found.")
163
no_members_text = _("No Host selected.")
166
contributes = ("hosts_aggregate",)
168
def contribute(self, data, context):
170
member_field_name = self.get_member_field_name('member')
171
context['hosts_aggregate'] = data.get(member_field_name, [])
175
class CreateAggregateWorkflow(workflows.Workflow):
176
slug = "create_aggregate"
177
name = _("Create Host Aggregate")
178
finalize_button_name = _("Create Host Aggregate")
179
success_message = _('Created new host aggregate "%s".')
180
failure_message = _('Unable to create host aggregate "%s".')
181
success_url = constants.AGGREGATES_INDEX_URL
182
default_steps = (SetAggregateInfoStep, AddHostsToAggregateStep)
184
def format_status_message(self, message):
185
return message % self.context['name']
187
def handle(self, request, context):
190
api.nova.aggregate_create(
192
name=context['name'],
193
availability_zone=context['availability_zone'])
195
exceptions.handle(request, _('Unable to create host aggregate.'))
198
hosts = context['hosts_aggregate']
201
api.nova.add_host_to_aggregate(request, self.object.id, host)
204
request, _('Error adding Hosts to the aggregate.'))
210
class ManageAggregateHostsWorkflow(workflows.Workflow):
211
slug = "manage_hosts_aggregate"
212
name = _("Add/Remove Hosts to Aggregate")
213
finalize_button_name = _("Save")
214
success_message = _('The Aggregate was updated.')
215
failure_message = _('Unable to update the aggregate.')
216
success_url = constants.AGGREGATES_INDEX_URL
217
default_steps = (ManageAggregateHostsStep, )
219
def format_status_message(self, message):
222
def handle(self, request, context):
223
hosts_aggregate = context['hosts_aggregate']
224
aggregate_id = context['id']
225
aggregate = api.nova.aggregate_get(request, aggregate_id)
226
aggregate_hosts = aggregate.hosts
227
for host in aggregate_hosts:
228
api.nova.remove_host_from_aggregate(request, aggregate_id, host)
230
for host in hosts_aggregate:
232
api.nova.add_host_to_aggregate(request, aggregate_id, host)
235
request, _('Error updating the aggregate.'))