summaryrefslogtreecommitdiff
path: root/tuskar_ui/infrastructure/parameters/forms.py
blob: a0e91e6398d5412518985672c2e0c183645bd428 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
# -*- coding: utf8 -*-
#
#    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 django.forms
from django.utils.datastructures import SortedDict
from django.utils.translation import ugettext_lazy as _
import horizon.exceptions
import horizon.forms
import horizon.messages

from tuskar_ui import api
import tuskar_ui.forms
from tuskar_ui.utils import utils


LOG = logging.getLogger(__name__)


VIRT_TYPE_CHOICES = [
    ('qemu', _("Virtualized (qemu)")),
    ('kvm', _("Baremetal (kvm)")),
]

CINDER_ISCSI_HELPER_CHOICES = [
    ('tgtadm', _('tgtadm')),
    ('lioadm', _('lioadm')),
]


class ParameterAwareMixin(object):
    parameter = None


def parameter_fields(request, prefix=None, read_only=False):
    fields = SortedDict()
    plan = api.tuskar.Plan.get_the_plan(request)
    parameters = plan.parameter_list(include_key_parameters=False)

    for p in parameters:
        if prefix and not p.name.startswith(prefix):
            continue
        Field = django.forms.CharField
        field_kwargs = {}
        widget = None
        if read_only:
            if p.hidden:
                widget = tuskar_ui.forms.StaticTextPasswordWidget
            else:
                widget = tuskar_ui.forms.StaticTextWidget
        else:
            if p.hidden:
                widget = django.forms.PasswordInput(render_value=True)
            elif p.parameter_type == 'number':
                Field = django.forms.IntegerField
            elif p.parameter_type == 'boolean':
                Field = django.forms.BooleanField
            elif (p.parameter_type == 'string' and
                    p.get_constraint_by_type('allowed_values')):
                Field = django.forms.ChoiceField
                field_kwargs['choices'] = [
                    (choice, choice) for choice in
                    p.get_constraint_by_type('allowed_values')['definition']]
            elif (p.parameter_type in ['json', 'comma_delimited_list'] or
                  'Certificate' in p.name):
                widget = django.forms.Textarea

        fields[p.name] = Field(
            required=False,
            label=_parameter_label(p),
            initial=p.value,
            widget=widget,
            **field_kwargs
        )
        fields[p.name].__class__ = type('ParameterAwareField',
                                        (ParameterAwareMixin, Field), {})
        fields[p.name].parameter = p
    return fields


def _parameter_label(parameter):
    return tuskar_ui.forms.label_with_tooltip(
        parameter.label or utils.de_camel_case(parameter.stripped_name),
        parameter.description)


class ServiceConfig(horizon.forms.SelfHandlingForm):
    def __init__(self, *args, **kwargs):
        super(ServiceConfig, self).__init__(*args, **kwargs)
        self.fields.update(parameter_fields(self.request, read_only=True))

    def global_fieldset(self):
        return tuskar_ui.forms.fieldset(self, prefix='^(?!.*::)')

    def controller_fieldset(self):
        return tuskar_ui.forms.fieldset(self, prefix='Controller-1')

    def compute_fieldset(self):
        return tuskar_ui.forms.fieldset(self, prefix='Compute-1')

    def block_storage_fieldset(self):
        return tuskar_ui.forms.fieldset(self, prefix='Cinder-Storage-1')

    def object_storage_fieldset(self):
        return tuskar_ui.forms.fieldset(self, prefix='Swift-Storage-1')

    def ceph_storage_fieldset(self):
        return tuskar_ui.forms.fieldset(self, prefix='Ceph-Storage-1')

    def handle():
        pass


class AdvancedEditServiceConfig(ServiceConfig):
    def __init__(self, *args, **kwargs):
        super(AdvancedEditServiceConfig, self).__init__(*args, **kwargs)
        self.fields.update(parameter_fields(self.request))

    def handle(self, request, data):
        plan = api.tuskar.Plan.get_the_plan(self.request)

        # TODO(bcrochet): Commenting this out.
        # For advanced config, we should have a whitelist of which params
        # must be synced across roles.
        # data = self._sync_common_params_across_roles(plan, data)

        try:
            plan.patch(request, plan.uuid, data)
        except Exception as e:
            horizon.exceptions.handle(
                request,
                _("Unable to update the service configuration."))
            LOG.exception(e)
            return False
        else:
            horizon.messages.success(
                request,
                _("Service configuration updated."))
            return True

    @staticmethod
    def _sync_common_params_across_roles(plan, parameters_dict):
        for (p_key, p_value) in parameters_dict.iteritems():
            for role in plan.role_list:
                role_parameter_key = (role.parameter_prefix +
                                      api.tuskar.strip_prefix(p_key))
                if role_parameter_key in parameters_dict:
                    parameters_dict[role_parameter_key] = p_value
        return parameters_dict


class SimpleEditServiceConfig(horizon.forms.SelfHandlingForm):
    virt_type = django.forms.ChoiceField(
        label=_("Deployment Type"),
        choices=VIRT_TYPE_CHOICES,
        required=True,
        help_text=_('If you are testing OpenStack in a virtual machine, '
                    'you must configure Compute to use qemu without KVM '
                    'and hardware virtualization.'))
    neutron_public_interface = django.forms.CharField(
        label=_("Public Interface"),
        required=True,
        initial='eth0',
        help_text=_('What interface to bridge onto br-ex for network nodes. '
                    'If you are testing OpenStack in a virtual machine'
                    'you must configure interface to eth0.'))
    snmp_password = django.forms.CharField(
        label=_("SNMP Password"),
        required=True,
        help_text=_('The user password for SNMPd with readonly '
                    'rights running on all Overcloud nodes'),
        widget=django.forms.PasswordInput(render_value=True))
    cloud_name = django.forms.CharField(
        label=_("Cloud name"),
        required=True,
        initial="overcloud",
        help_text=_('The DNS name of this cloud. '
                    'E.g. ci-overcloud.tripleo.org'))
    cinder_iscsi_helper = django.forms.ChoiceField(
        label=_("Cinder ISCSI helper"),
        choices=CINDER_ISCSI_HELPER_CHOICES,
        required=True,
        help_text=_('The iSCSI helper to use with cinder.'))
    ntp_server = django.forms.CharField(
        label=_("NTP server"),
        required=False,
        initial="",
        help_text=_('Address of the NTP server. If blank, public NTP servers '
                    'will be used.'))
    extra_config = django.forms.CharField(
        label=_("Extra Config"),
        required=False,
        widget=django.forms.Textarea(attrs={'rows': 2}),
        help_text=("Additional configuration to inject into the cluster."
                   "The data format of this field is JSON."
                   "See http://git.io/PuwLXQ for more information."))

    def clean_extra_config(self):
        data = self.cleaned_data['extra_config']
        try:
            json.loads(data)
        except Exception as json_error:
            raise django.forms.ValidationError(
                _("%(err_msg)s"), params={'err_msg': json_error.message})
        return data

    @staticmethod
    def _load_additional_parameters(plan, data, form_key, param_name):
        params = {}
        param_value = data.get(form_key)
        # Set the same parameter and value in all roles.
        for role in plan.role_list:
            key = role.parameter_prefix + param_name
            if key in [parameter.name
                       for parameter in role.parameter_list(plan)]:
                params[key] = param_value

        return params

    def handle(self, request, data):
        plan = api.tuskar.Plan.get_the_plan(self.request)
        compute_prefix = plan.get_role_by_name('Compute').parameter_prefix
        controller_prefix = plan.get_role_by_name(
            'Controller').parameter_prefix
        cinder_prefix = plan.get_role_by_name(
            'Cinder-Storage').parameter_prefix

        virt_type = data.get('virt_type')
        neutron_public_interface = data.get('neutron_public_interface')
        cloud_name = data.get('cloud_name')
        cinder_iscsi_helper = data.get('cinder_iscsi_helper')
        ntp_server = data.get('ntp_server')

        parameters = {
            compute_prefix + 'NovaComputeLibvirtType': virt_type,
            controller_prefix + 'CinderISCSIHelper': cinder_iscsi_helper,
            cinder_prefix + 'CinderISCSIHelper': cinder_iscsi_helper,
            controller_prefix + 'CloudName': cloud_name,
            controller_prefix + 'NeutronPublicInterface':
                neutron_public_interface,
            compute_prefix + 'NeutronPublicInterface':
                neutron_public_interface,
            controller_prefix + 'NtpServer':
                ntp_server,
            compute_prefix + 'NtpServer':
                ntp_server,
        }

        parameters.update(self._load_additional_parameters(
            plan, data,
            'snmp_password', 'SnmpdReadonlyUserPassword'))
        parameters.update(self._load_additional_parameters(
            plan, data,
            'extra_config', 'ExtraConfig'))

        try:
            plan.patch(request, plan.uuid, parameters)
        except Exception as e:
            horizon.exceptions.handle(
                request,
                _("Unable to update the service configuration."))
            LOG.exception(e)
            return False
        else:
            horizon.messages.success(
                request,
                _("Service configuration updated."))
            return True