def __init__(self, *args, **kwargs): super(LaunchForm, self).__init__(*args, **kwargs) flavorlist = kwargs.get('initial', {}).get('flavorlist', []) self.fields['flavor'] = forms.ChoiceField( choices=flavorlist, label=_("Flavor"), help_text="Size of Image to launch") keynamelist = kwargs.get('initial', {}).get('keynamelist', []) self.fields['key_name'] = forms.ChoiceField( choices=keynamelist, label=_("Key Name"), required=False, help_text="Which keypair to use for authentication") securitygrouplist = kwargs.get('initial', {}).get('securitygrouplist', []) self.fields['security_groups'] = forms.MultipleChoiceField( choices=securitygrouplist, label=_("Security Groups"), required=True, initial=['default'], widget=forms.CheckboxSelectMultiple(), help_text="Launch instance in these Security Groups") # setting self.fields.keyOrder seems to break validation, # so ordering fields manually field_list = ('name', 'user_data', 'flavor', 'key_name') for field in field_list[::-1]: self.fields.insert(0, field, self.fields.pop(field))
class AddRule(forms.SelfHandlingForm): ip_protocol = forms.ChoiceField(choices=[('tcp', 'tcp'), ('udp', 'udp'), ('icmp', 'icmp')]) from_port = forms.CharField() to_port = forms.CharField() cidr = forms.CharField() # TODO (anthony) source group support # group_id = forms.CharField() security_group_id = forms.CharField(widget=forms.HiddenInput()) tenant_id = forms.CharField(widget=forms.HiddenInput()) def handle(self, request, data): tenant_id = data['tenant_id'] try: LOG.info('Add security_group_rule: "%s"' % data) rule = api.security_group_rule_create(request, data['security_group_id'], data['ip_protocol'], data['from_port'], data['to_port'], data['cidr']) messages.success(request, _('Successfully added rule: %s') \ % rule.id) except engineclient_exceptions.ClientException, e: LOG.exception("ClientException in AddRule") messages.error(request, _('Error adding rule security group: %s') % e.message) return shortcuts.redirect(request.build_absolute_uri())
class CopyObject(forms.SelfHandlingForm): new_container_name = forms.ChoiceField( label=_("Container to store object in")) new_object_name = forms.CharField(max_length="255", label=_("New object name")) orig_container_name = forms.CharField(widget=forms.HiddenInput()) orig_object_name = forms.CharField(widget=forms.HiddenInput()) def __init__(self, *args, **kwargs): containers = kwargs.pop('containers') super(CopyObject, self).__init__(*args, **kwargs) self.fields['new_container_name'].choices = containers def handle(self, request, data): orig_container_name = data['orig_container_name'] orig_object_name = data['orig_object_name'] new_container_name = data['new_container_name'] new_object_name = data['new_object_name'] api.chase_copy_object(request, orig_container_name, orig_object_name, new_container_name, new_object_name) messages.success( request, _('Object was successfully copied to %(container)s\%(obj)s') % { "container": new_container_name, "obj": new_object_name }) return shortcuts.redirect("steer:engine:containers:object_index", data['new_container_name'])
class AttachPort(forms.SelfHandlingForm): network = forms.CharField(widget=forms.HiddenInput()) port = forms.CharField(widget=forms.HiddenInput()) vif_id = forms.ChoiceField(label=_("Select VIF to connect")) def __init__(self, request, *args, **kwargs): super(AttachPort, self).__init__(*args, **kwargs) # Populate VIF choices vif_choices = [('', "Select a VIF")] for vif in api.get_vif_ids(request): if vif['available']: name = "Instance %s VIF %s" % (vif['instance_name'], vif['id']) vif_choices.append((vif['id'], name,)) self.fields['vif_id'].choices = vif_choices @classmethod def _instantiate(cls, request, *args, **kwargs): return cls(request, *args, **kwargs) def handle(self, request, data): try: LOG.info('Attaching %s port to VIF %s' % (data['port'], data['vif_id'])) body = {'attachment': {'id': '%s' % data['vif_id']}} api.quantum_attach_port(request, data['network'], data['port'], body) except Exception, e: if not hasattr(e, 'message'): e.message = str(e) messages.error(request, _('Unable to attach port %(port)s to VIF %(vif)s: %(msg)s') % {"port": data['port'], "vif": data['vif_id'], "msg": e.message}) else:
class UpdateUserForm(BaseUserForm): id = forms.CharField( label=_("ID"), widget=forms.TextInput(attrs={'readonly': 'readonly'})) # FIXME: keystone doesn't return the username from a get API call. #name = forms.CharField(label=_("Name")) email = forms.CharField(label=_("Email")) password = forms.CharField(label=_("Password"), widget=forms.PasswordInput(render_value=False), required=False) tenant_id = forms.ChoiceField(label=_("Primary Tenant")) def handle(self, request, data): updated = [] if data['email']: updated.append('email') api.user_update_email(request, data['id'], data['email']) if data['password']: updated.append('password') api.user_update_password(request, data['id'], data['password']) if data['tenant_id']: updated.append('tenant') api.user_update_tenant(request, data['id'], data['tenant_id']) messages.success( request, _('Updated %(attrib)s for %(user)s.') % { "attrib": ', '.join(updated), "user": data['id'] }) return shortcuts.redirect('steer:syspanel:users:index')
class FloatingIpAssociate(forms.SelfHandlingForm): floating_ip_id = forms.CharField(widget=forms.HiddenInput()) floating_ip = forms.CharField(widget=forms.TextInput( attrs={'readonly': 'readonly'})) instance_id = forms.ChoiceField() def __init__(self, *args, **kwargs): super(FloatingIpAssociate, self).__init__(*args, **kwargs) instancelist = kwargs.get('initial', {}).get('instances', []) self.fields['instance_id'] = forms.ChoiceField(choices=instancelist, label=_("Instance")) def handle(self, request, data): try: api.server_add_floating_ip(request, data['instance_id'], data['floating_ip_id']) LOG.info('Associating Floating IP "%s" with Instance "%s"' % (data['floating_ip'], data['instance_id'])) messages.info( request, _('Successfully associated Floating IP \ %(ip)s with Instance: %(inst)s' % { "ip": data['floating_ip'], "inst": data['instance_id'] })) except engineclient_exceptions.ClientException, e: LOG.exception("ClientException in FloatingIpAssociate") messages.error(request, _('Error associating Floating IP: %s') % e) return shortcuts.redirect('steer:engine:access_and_security:index')
def __init__(self, *args, **kwargs): super(AttachForm, self).__init__(*args, **kwargs) # populate volume_id volume_id = kwargs.get('initial', {}).get('volume_id', []) self.fields['volume_id'] = forms.CharField(widget=forms.HiddenInput(), initial=volume_id) # Populate instance choices instance_list = kwargs.get('initial', {}).get('instances', []) instances = [('', "Select an instance")] for instance in instance_list: instances.append( (instance.id, '%s (%s)' % (instance.name, instance.id))) self.fields['instance'] = forms.ChoiceField( choices=instances, label="Attach to Instance", help_text="Select an instance to attach to.")
class CreateUserForm(BaseUserForm): name = forms.CharField(label=_("Name")) email = forms.CharField(label=_("Email")) password = forms.CharField(label=_("Password"), widget=forms.PasswordInput(render_value=False), required=False) tenant_id = forms.ChoiceField(label=_("Primary Tenant")) def handle(self, request, data): try: LOG.info('Creating user with name "%s"' % data['name']) new_user = api.user_create(request, data['name'], data['email'], data['password'], data['tenant_id'], True) messages.success( request, _('User "%s" was successfully created.') % data['name']) try: api.role_add_for_tenant_user(request, data['tenant_id'], new_user.id, settings.X7_KEYSTONE_DEFAULT_ROLE) except Exception, e: LOG.exception('Exception while assigning \ role to new user: %s' % new_user.id) if not hasattr(e, 'message'): e.message = str(e) messages.error( request, _('Error assigning role to user: %s') % e.message) return shortcuts.redirect('steer:syspanel:users:index') except Exception, e: LOG.exception('Exception while creating user\n' 'name: "%s", email: "%s", tenant_id: "%s"' % (data['name'], data['email'], data['tenant_id'])) if not hasattr(e, 'message'): e.message = str(e) messages.error(request, _('Error creating user: %s') % e.message) return shortcuts.redirect('steer:syspanel:users:index')
def __init__(self, *args, **kwargs): super(FloatingIpAssociate, self).__init__(*args, **kwargs) instancelist = kwargs.get('initial', {}).get('instances', []) self.fields['instance_id'] = forms.ChoiceField(choices=instancelist, label=_("Instance"))