def delete(self, request, obj_id): share = manila.share_get(request, obj_id) try: manila.share_delete( request, share.id, share_group_id=share.share_group_id) except Exception: msg = _('Unable to delete share "%s". ') % obj_id messages.error(request, msg)
def get_data(self): try: share_id = self.kwargs['share_id'] share = manila.share_get(self.request, share_id) except Exception: exceptions.handle( self.request, _('Unable to retrieve volume details.'), redirect=self.success_url) return share
def get_context_data(self, **kwargs): context = super(ManageRulesView, self).get_context_data(**kwargs) share = manila.share_get(self.request, self.kwargs['share_id']) context['share_display_name'] = share.name or share.id context["share"] = self.get_data() context["page_title"] = _("Share Rules: " "%(share_display_name)s") % \ {'share_display_name': context['share_display_name']} return context
def get_context_data(self, **kwargs): context = super(ManageRulesView, self).get_context_data(**kwargs) share = manila.share_get(self.request, self.kwargs['share_id']) context['share_display_name'] = share.name or share.id context["share"] = self.get_data() context["page_title"] = _("Share Rules: " "%(share_display_name)s") % { 'share_display_name': context['share_display_name']} return context
def delete(self, request, obj_id): share = manila.share_get(request, obj_id) try: manila.share_delete(request, share.id, share_group_id=share.share_group_id) except Exception: msg = _('Unable to delete share "%s". ') % obj_id messages.error(request, msg)
def get_data(self): try: share_id = self.kwargs['share_id'] share = manila.share_get(self.request, share_id) except Exception: exceptions.handle(self.request, _('Unable to retrieve volume details.'), redirect=self.success_url) return share
def get_object(self): if not hasattr(self, "_object"): vol_id = self.kwargs['share_id'] try: self._object = manila.share_get(self.request, vol_id) except Exception: msg = _('Unable to retrieve share.') url = reverse('horizon:project:shares:index') exceptions.handle(self.request, msg, redirect=url) return self._object
def check_size(self, request, share_id, new_size): share = manila.share_get(request, share_id) timeout = 30 interval = 0.35 time_elapsed = 0 while share.status != 'available': time.sleep(interval) time_elapsed += interval share = manila.share_get(request, share_id) if time_elapsed > timeout: raise exceptions.WorkflowError( _("The operation timed out while resizing. " "Please try again.")) if share.size == new_size: message = _('Resized share "%s"') % share.name messages.success(request, message) return True raise exceptions.WorkflowError(_("Unable to resize share. "))
def get_data(self): try: share_id = self.kwargs['share_id'] share = manila.share_get(self.request, share_id) share.rules = manila.share_rules_list(self.request, share_id) except Exception: redirect = reverse('horizon:project:shares:index') exceptions.handle(self.request, _('Unable to retrieve share details.'), redirect=redirect) return share
def get_data(self): try: snapshot_id = self.kwargs['snapshot_id'] snapshot = manila.share_snapshot_get(self.request, snapshot_id) share = manila.share_get(self.request, snapshot.share_id) snapshot.share_name_or_id = share.name or share.id except Exception: exceptions.handle(self.request, _('Unable to retrieve snapshot details.'), redirect=self.redirect_url) return snapshot
def get_data(self): try: snapshot_id = self.kwargs['snapshot_id'] snapshot = manila.share_snapshot_get(self.request, snapshot_id) share = manila.share_get(self.request, snapshot.share_id) snapshot.share_name_or_id = share.name or share.id except Exception: redirect = reverse('horizon:project:shares:index') exceptions.handle(self.request, _('Unable to retrieve snapshot details.'), redirect=redirect) return snapshot
def allowed(self, request, replica=None): if replica: share = manila.share_get(request, replica.share_id) replicas = manila.share_replica_list(request, replica.share_id) if share.replication_type is None: return False elif (share.replication_type is 'writable' and replica.status in DELETABLE_STATUSES and len(replicas) > 1) or (share.replication_type in ('dr', 'readable') and replica.status in DELETABLE_STATUSES and replica.replica_state != 'active'): return True return False
def get_data(self, request, share_id): share = manila.share_get(request, share_id) if not share.name: share.name = share_id if share.share_network_id: share_net = manila.share_network_get(request, share.share_network_id) share.share_network = share_net.name or share_net.id else: share.share_network = None share.metadata = utils.metadata_to_str(share.metadata) return share
def handle(self, request, data): share_id = self.initial['share_id'] try: share = manila.share_get(self.request, share_id) manila.share_extend(request, share.id, data['new_size']) message = _('Extend share "%s"') % data['name'] messages.success(request, message) return True except Exception: redirect = reverse("horizon:project:shares:index") exceptions.handle(request, _('Unable to extend share.'), redirect=redirect)
def handle(self, request, data): share_id = self.initial['share_id'] try: share = manila.share_get(self.request, share_id) manila.share_extend( request, share.id, data['new_size']) message = _('Extend share "%s"') % data['name'] messages.success(request, message) return True except Exception: redirect = reverse("horizon:project:shares:index") exceptions.handle(request, _('Unable to extend share.'), redirect=redirect)
def allowed(self, request, replica=None): if replica: share = manila.share_get(request, replica.share_id) replicas = manila.share_replica_list(request, replica.share_id) if share.replication_type is None: return False elif (share.replication_type is 'writable' and replica.status in DELETABLE_STATUSES and len(replicas) > 1) or ( share.replication_type in ('dr', 'readable') and replica.status in DELETABLE_STATUSES and replica.replica_state != 'active'): return True return False
def handle(self, request, data): share_id = self.initial['share_id'] try: share = manila.share_get(self.request, share_id) manila.share_update( request, share, data['name'], data['description'], is_public=data['is_public']) message = _('Updating share "%s"') % data['name'] messages.success(request, message) return True except Exception: redirect = reverse("horizon:project:shares:index") exceptions.handle(request, _('Unable to update share.'), redirect=redirect)
def get_data(self): try: share_id = self.kwargs['share_id'] share = manila.share_get(self.request, share_id) share.rules = manila.share_rules_list(self.request, share_id) share.export_locations = manila.share_export_location_list( self.request, share_id) share.el_size = self._calculate_size_of_longest_export_location( share.export_locations) except Exception: redirect = reverse('horizon:project:shares:index') exceptions.handle(self.request, _('Unable to retrieve share details.'), redirect=redirect) return share
def get_context_data(self, **kwargs): context = super(ManageReplicasView, self).get_context_data(**kwargs) try: share = manila.share_get(self.request, self.kwargs["share_id"]) except Exception: redirect = reverse(self._redirect_url) exceptions.handle( self.request, _('Unable to retrieve share. %s') % self.kwargs["share_id"], redirect=redirect) context["share_display_name"] = share.name or share.id context["share"] = self.get_data() context["page_title"] = _( "Share Replicas: %(share_display_name)s") % { "share_display_name": context["share_display_name"]} return context
def get_context_data(self, **kwargs): context = super(CreateSnapshotView, self).get_context_data(**kwargs) context['share_id'] = self.kwargs['share_id'] try: share = manila.share_get(self.request, context['share_id']) if (share.status == 'in-use'): context['attached'] = True context['form'].set_warning(_("This share is currently " "attached to an instance. " "In some cases, creating a " "snapshot from an attached " "share can result in a " "corrupted snapshot.")) context['usages'] = quotas.tenant_limit_usages(self.request) except Exception: exceptions.handle(self.request, _('Unable to retrieve share information.')) return context
def get_context_data(self, **kwargs): context = super(CreateSnapshotView, self).get_context_data(**kwargs) context['share_id'] = self.kwargs['share_id'] try: share = manila.share_get(self.request, context['share_id']) if (share.status == 'in-use'): context['attached'] = True context['form'].set_warning( _("This share is currently " "attached to an instance. " "In some cases, creating a " "snapshot from an attached " "share can result in a " "corrupted snapshot.")) context['usages'] = quotas.tenant_limit_usages(self.request) except Exception: exceptions.handle(self.request, _('Unable to retrieve share information.')) return context
def handle(self, request, data): share_id = self.initial['share_id'] try: share = manila.share_get(self.request, share_id) set_dict, unset_list = utils.parse_str_meta(data['metadata']) if set_dict: manila.share_set_metadata(request, share, set_dict) if unset_list: manila.share_delete_metadata(request, share, unset_list) message = _('Updating share metadata "%s"') % share.name messages.success(request, message) return True except ValidationError as e: self.api_error(e.messages[0]) return False except Exception: redirect = reverse("horizon:project:shares:index") exceptions.handle(request, _('Unable to update share metadata.'), redirect=redirect)
def handle(self, request, data): try: share = manila.share_get(request, data['share_id']) force = False message = _('Creating share snapshot "%s".') % data['name'] if share.status == 'in-use': force = True message = _('Forcing to create snapshot "%s" ' 'from attached share.') % data['name'] snapshot = manila.share_snapshot_create(request, data['share_id'], data['name'], data['description'], force=force) messages.success(request, message) return snapshot except Exception: redirect = reverse("horizon:project:shares:index") exceptions.handle(request, _('Unable to create share snapshot.'), redirect=redirect)
def get_data(self): try: snapshot_id = self.kwargs['snapshot_id'] snapshot = manila.share_snapshot_get(self.request, snapshot_id) share = manila.share_get(self.request, snapshot.share_id) if share.mount_snapshot_support: snapshot.rules = manila.share_snapshot_rules_list( self.request, snapshot_id) snapshot.export_locations = ( manila.share_snap_export_location_list( self.request, snapshot)) export_locations = [ exp['path'] for exp in snapshot.export_locations ] snapshot.el_size = ui_utils.calculate_longest_str_size( export_locations) snapshot.share_name_or_id = share.name or share.id except Exception: exceptions.handle(self.request, _('Unable to retrieve snapshot details.'), redirect=self.redirect_url) return snapshot
def get_data(self): try: snapshot_id = self.kwargs['snapshot_id'] snapshot = manila.share_snapshot_get(self.request, snapshot_id) share = manila.share_get(self.request, snapshot.share_id) if share.mount_snapshot_support: snapshot.rules = manila.share_snapshot_rules_list( self.request, snapshot_id) snapshot.export_locations = ( manila.share_snap_export_location_list( self.request, snapshot)) export_locations = [ exp['path'] for exp in snapshot.export_locations ] snapshot.el_size = ui_utils.calculate_longest_str_size( export_locations) snapshot.share_name_or_id = share.name or share.id except Exception: exceptions.handle( self.request, _('Unable to retrieve snapshot details.'), redirect=self.redirect_url) return snapshot
def allowed(self, request, datum=None): share = manila.share_get(request, self.table.kwargs['share_id']) return share.replication_type is not None
def get_context_data(self, **kwargs): context = super(ManageRulesView, self).get_context_data(**kwargs) share = manila.share_get(self.request, self.kwargs['share_id']) context['share_display_name'] = share.name or share.id context["share"] = self.get_data() return context
def __init__(self, request, *args, **kwargs): super(CreateForm, self).__init__(request, *args, **kwargs) # NOTE(vkmc): choose only those share protocols that are enabled # FIXME(vkmc): this should be better implemented by having a # capabilities endpoint on the control plane. manila_features = getattr(settings, 'OPENSTACK_MANILA_FEATURES', {}) self.enabled_share_protocols = manila_features.get( 'enabled_share_protocols', ['NFS', 'CIFS', 'GlusterFS', 'HDFS', 'CephFS', 'MapRFS']) self.enable_public_shares = manila_features.get( 'enable_public_shares', True) share_networks = manila.share_network_list(request) share_types = manila.share_type_list(request) self.fields['share_type'].choices = ( [("", "")] + [(st.name, st.name) for st in share_types]) availability_zones = manila.availability_zone_list(request) self.fields['availability_zone'].choices = ( [("", "")] + [(az.name, az.name) for az in availability_zones]) if features.is_share_groups_enabled(): share_groups = manila.share_group_list(request) self.fields['sg'] = forms.ChoiceField( label=_("Share Group"), choices=[("", "")] + [(sg.id, sg.name or sg.id) for sg in share_groups], required=False) self.sn_field_name_prefix = 'share-network-choices-' for st in share_types: extra_specs = st.get_keys() dhss = extra_specs.get('driver_handles_share_servers') # NOTE(vponomaryov): Set and tie share-network field only for # share types with enabled handling of share servers. if (isinstance(dhss, six.string_types) and dhss.lower() in ['true', '1']): sn_choices = ( [('', '')] + [(sn.id, sn.name or sn.id) for sn in share_networks]) sn_field_name = self.sn_field_name_prefix + st.name sn_field = forms.ChoiceField( label=_("Share Network"), required=True, choices=sn_choices, widget=forms.Select(attrs={ 'class': 'switched', 'data-switch-on': 'sharetype', 'data-sharetype-%s' % st.name: _("Share Network"), })) self.fields[sn_field_name] = sn_field self.fields['share_source_type'] = forms.ChoiceField( label=_("Share Source"), required=False, widget=forms.Select( attrs={'class': 'switchable', 'data-slug': 'source'})) self.fields['snapshot'] = forms.ChoiceField( label=_("Use snapshot as a source"), widget=forms.fields.SelectWidget( attrs={'class': 'switched', 'data-switch-on': 'source', 'data-source-snapshot': _('Snapshot')}, data_attrs=('size', 'name'), transform=lambda x: "%s (%sGiB)" % (x.name, x.size)), required=False) self.fields['metadata'] = forms.CharField( label=_("Metadata"), required=False, widget=forms.Textarea(attrs={'rows': 4})) if self.enable_public_shares: self.fields['is_public'] = forms.BooleanField( label=_("Make visible for all"), required=False, help_text=( "If set then all tenants will be able to see this share.")) self.fields['share_proto'].choices = [(sp, sp) for sp in self.enabled_share_protocols] if ("snapshot_id" in request.GET or kwargs.get("data", {}).get("snapshot")): try: snapshot = self.get_snapshot( request, request.GET.get("snapshot_id", kwargs.get("data", {}).get("snapshot"))) self.fields['name'].initial = snapshot.name self.fields['size'].initial = snapshot.size self.fields['snapshot'].choices = ((snapshot.id, snapshot),) try: # Set the share type from the original share orig_share = manila.share_get(request, snapshot.share_id) # NOTE(vponomaryov): we should use share type name, not ID, # because we use names in our choices above. self.fields['share_type'].initial = ( orig_share.share_type_name) except Exception: pass self.fields['size'].help_text = _( 'Share size must be equal to or greater than the snapshot ' 'size (%sGiB)') % snapshot.size del self.fields['share_source_type'] except Exception: exceptions.handle(request, _('Unable to load the specified snapshot.')) else: source_type_choices = [] try: snapshot_list = manila.share_snapshot_list(request) snapshots = [s for s in snapshot_list if s.status == 'available'] if snapshots: source_type_choices.append(("snapshot", _("Snapshot"))) choices = [('', _("Choose a snapshot"))] + \ [(s.id, s) for s in snapshots] self.fields['snapshot'].choices = choices else: del self.fields['snapshot'] except Exception: exceptions.handle(request, _("Unable to retrieve " "share snapshots.")) if source_type_choices: choices = ([('no_source_type', _("No source, empty share"))] + source_type_choices) self.fields['share_source_type'].choices = choices else: del self.fields['share_source_type']
def allowed(self, request, snapshot=None): share = manila.share_get(request, snapshot.share_id) return share.mount_snapshot_support
def __init__(self, request, *args, **kwargs): super(CreateForm, self).__init__(request, *args, **kwargs) share_protos = ('NFS', 'CIFS', 'GlusterFS', 'HDFS', 'CephFS') share_networks = manila.share_network_list(request) share_types = manila.share_type_list(request) self.fields['share_type'].choices = ([("", "")] + [(st.name, st.name) for st in share_types]) availability_zones = nova.availability_zone_list(request) self.fields['availability_zone'].choices = ( [("", "")] + [(az.zoneName, az.zoneName) for az in availability_zones]) self.sn_field_name_prefix = 'share-network-choices-' for st in share_types: extra_specs = st.get_keys() dhss = extra_specs.get('driver_handles_share_servers') # NOTE(vponomaryov): Set and tie share-network field only for # share types with enabled handling of share servers. if (isinstance(dhss, six.string_types) and dhss.lower() in ['true', '1']): sn_choices = ([('', '')] + [(sn.id, sn.name or sn.id) for sn in share_networks]) sn_field_name = self.sn_field_name_prefix + st.name sn_field = forms.ChoiceField( label=_("Share Network"), required=True, choices=sn_choices, widget=forms.Select( attrs={ 'class': 'switched', 'data-switch-on': 'sharetype', 'data-sharetype-%s' % st.name: _("Share Network"), })) self.fields[sn_field_name] = sn_field self.fields['share_source_type'] = forms.ChoiceField( label=_("Share Source"), required=False, widget=forms.Select(attrs={ 'class': 'switchable', 'data-slug': 'source' })) self.fields['snapshot'] = forms.ChoiceField( label=_("Use snapshot as a source"), widget=forms.fields.SelectWidget(attrs={ 'class': 'switched', 'data-switch-on': 'source', 'data-source-snapshot': _('Snapshot') }, data_attrs=('size', 'name'), transform=lambda x: "%s (%sGiB)" % (x.name, x.size)), required=False) self.fields['metadata'] = forms.CharField( label=_("Metadata"), required=False, widget=forms.Textarea(attrs={'rows': 4})) self.fields['is_public'] = forms.BooleanField( label=_("Make visible for all"), required=False, help_text=( "If set then all tenants will be able to see this share.")) self.fields['share_proto'].choices = [(sp, sp) for sp in share_protos] if "snapshot_id" in request.GET: try: snapshot = self.get_snapshot(request, request.GET["snapshot_id"]) self.fields['name'].initial = snapshot.name self.fields['size'].initial = snapshot.size self.fields['snapshot'].choices = ((snapshot.id, snapshot), ) try: # Set the share type from the original share orig_share = manila.share_get(request, snapshot.share_id) self.fields['share_type'].initial = orig_share.share_type except Exception: pass self.fields['size'].help_text = _( 'Share size must be equal to or greater than the snapshot ' 'size (%sGiB)') % snapshot.size del self.fields['share_source_type'] except Exception: exceptions.handle(request, _('Unable to load the specified snapshot.')) else: source_type_choices = [] try: snapshot_list = manila.share_snapshot_list(request) snapshots = [ s for s in snapshot_list if s.status == 'available' ] if snapshots: source_type_choices.append(("snapshot", _("Snapshot"))) choices = [('', _("Choose a snapshot"))] + \ [(s.id, s) for s in snapshots] self.fields['snapshot'].choices = choices else: del self.fields['snapshot'] except Exception: exceptions.handle(request, _("Unable to retrieve " "share snapshots.")) if source_type_choices: choices = ([('no_source_type', _("No source, empty share"))] + source_type_choices) self.fields['share_source_type'].choices = choices else: del self.fields['share_source_type']
def allowed(self, request, share=None): share = manila.share_get(request, self.table.kwargs['share_id']) return share.status in ("available", "in-use")
def test_share_get(self): api.share_get(self.request, self.id) self.manilaclient.shares.get.assert_called_once_with(self.id)
def __init__(self, request, *args, **kwargs): super(CreateForm, self).__init__(request, *args, **kwargs) share_protos = ('NFS', 'CIFS', 'GlusterFS', 'HDFS', 'CephFS') share_networks = manila.share_network_list(request) share_types = manila.share_type_list(request) self.fields['share_type'].choices = ( [("", "")] + [(st.name, st.name) for st in share_types]) availability_zones = nova.availability_zone_list(request) self.fields['availability_zone'].choices = ( [("", "")] + [(az.zoneName, az.zoneName) for az in availability_zones]) self.sn_field_name_prefix = 'share-network-choices-' for st in share_types: extra_specs = st.get_keys() dhss = extra_specs.get('driver_handles_share_servers') # NOTE(vponomaryov): Set and tie share-network field only for # share types with enabled handling of share servers. if (isinstance(dhss, six.string_types) and dhss.lower() in ['true', '1']): sn_choices = ( [('', '')] + [(sn.id, sn.name or sn.id) for sn in share_networks]) sn_field_name = self.sn_field_name_prefix + st.name sn_field = forms.ChoiceField( label=_("Share Network"), required=True, choices=sn_choices, widget=forms.Select(attrs={ 'class': 'switched', 'data-switch-on': 'sharetype', 'data-sharetype-%s' % st.name: _("Share Network"), })) self.fields[sn_field_name] = sn_field self.fields['share_source_type'] = forms.ChoiceField( label=_("Share Source"), required=False, widget=forms.Select( attrs={'class': 'switchable', 'data-slug': 'source'})) self.fields['snapshot'] = forms.ChoiceField( label=_("Use snapshot as a source"), widget=forms.fields.SelectWidget( attrs={'class': 'switched', 'data-switch-on': 'source', 'data-source-snapshot': _('Snapshot')}, data_attrs=('size', 'name'), transform=lambda x: "%s (%sGiB)" % (x.name, x.size)), required=False) self.fields['metadata'] = forms.CharField( label=_("Metadata"), required=False, widget=forms.Textarea(attrs={'rows': 4})) self.fields['is_public'] = forms.BooleanField( label=_("Make visible for all"), required=False, help_text=( "If set then all tenants will be able to see this share.")) self.fields['share_proto'].choices = [(sp, sp) for sp in share_protos] if "snapshot_id" in request.GET: try: snapshot = self.get_snapshot(request, request.GET["snapshot_id"]) self.fields['name'].initial = snapshot.name self.fields['size'].initial = snapshot.size self.fields['snapshot'].choices = ((snapshot.id, snapshot),) try: # Set the share type from the original share orig_share = manila.share_get(request, snapshot.share_id) self.fields['share_type'].initial = orig_share.share_type except Exception: pass self.fields['size'].help_text = _( 'Share size must be equal to or greater than the snapshot ' 'size (%sGiB)') % snapshot.size del self.fields['share_source_type'] except Exception: exceptions.handle(request, _('Unable to load the specified snapshot.')) else: source_type_choices = [] try: snapshot_list = manila.share_snapshot_list(request) snapshots = [s for s in snapshot_list if s.status == 'available'] if snapshots: source_type_choices.append(("snapshot", _("Snapshot"))) choices = [('', _("Choose a snapshot"))] + \ [(s.id, s) for s in snapshots] self.fields['snapshot'].choices = choices else: del self.fields['snapshot'] except Exception: exceptions.handle(request, _("Unable to retrieve " "share snapshots.")) if source_type_choices: choices = ([('no_source_type', _("No source, empty share"))] + source_type_choices) self.fields['share_source_type'].choices = choices else: del self.fields['share_source_type']
def get_object(self): try: return manila.share_get(self.request, self.kwargs['share_id']) except Exception: exceptions.handle(self.request, _('Unable to retrieve share.'))
def __init__(self, request, *args, **kwargs): super(CreateForm, self).__init__(request, *args, **kwargs) share_protos = ('NFS', 'CIFS', 'GlusterFS', 'HDFS', ) share_networks = manila.share_network_list(request) share_types = manila.share_type_list(request) self.fields['share_type'].choices = ( [("", "")] + [(st.name, st.name) for st in share_types]) self.fields['share_proto'].choices = [(sp, sp) for sp in share_protos] if "snapshot_id" in request.GET: try: snapshot = self.get_snapshot(request, request.GET["snapshot_id"]) self.fields['name'].initial = snapshot.name self.fields['size'].initial = snapshot.size self.fields['snapshot'].choices = ((snapshot.id, snapshot),) try: # Set the share type from the original share orig_share = manila.share_get(request, snapshot.share_id) self.fields['share_type'].initial = orig_share.share_type except Exception: pass self.fields['size'].help_text = _( 'Share size must be equal to or greater than the snapshot ' 'size (%sGB)') % snapshot.size del self.fields['share_source_type'] except Exception: exceptions.handle(request, _('Unable to load the specified snapshot.')) else: source_type_choices = [] try: snapshot_list = manila.share_snapshot_list(request) snapshots = [s for s in snapshot_list if s.status == 'available'] if snapshots: source_type_choices.append(("snapshot", _("Snapshot"))) choices = [('', _("Choose a snapshot"))] + \ [(s.id, s) for s in snapshots] self.fields['snapshot'].choices = choices else: del self.fields['snapshot'] except Exception: exceptions.handle(request, _("Unable to retrieve " "share snapshots.")) if source_type_choices: choices = ([('no_source_type', _("No source, empty share"))] + source_type_choices) self.fields['share_source_type'].choices = choices else: del self.fields['share_source_type'] self.sn_field_name_prefix = 'share-network-choices-' for st in share_types: extra_specs = st.get_keys() dhss = extra_specs.get('driver_handles_share_servers') # NOTE(vponomaryov): Set and tie share-network field only for # share types with enabled handling of share servers. if (isinstance(dhss, six.string_types) and dhss.lower() in ['true', '1']): sn_choices = ( [('', '')] + [(sn.id, sn.name or sn.id) for sn in share_networks]) sn_field_name = self.sn_field_name_prefix + st.name sn_field = forms.ChoiceField( label=_("Share Network"), required=True, choices=sn_choices, widget=forms.Select(attrs={ 'class': 'switched', 'data-switch-on': 'sharetype', 'data-sharetype-%s' % st.name: _("Share Network"), })) self.fields.insert(5, sn_field_name, sn_field)
def __init__(self, request, *args, **kwargs): super(CreateForm, self).__init__(request, *args, **kwargs) # NOTE(vkmc): choose only those share protocols that are enabled # FIXME(vkmc): this should be better implemented by having a # capabilities endpoint on the control plane. manila_features = getattr(settings, 'OPENSTACK_MANILA_FEATURES', {}) self.enabled_share_protocols = manila_features.get( 'enabled_share_protocols', ['NFS', 'CIFS', 'GlusterFS', 'HDFS', 'CephFS', 'MapRFS']) self.enable_public_shares = manila_features.get( 'enable_public_shares', True) share_networks = manila.share_network_list(request) share_types = manila.share_type_list(request) self.fields['share_type'].choices = ( [("", "")] + [(utils.transform_dashed_name(st.name), st.name) for st in share_types]) availability_zones = manila.availability_zone_list(request) self.fields['availability_zone'].choices = ( [("", "")] + [(az.name, az.name) for az in availability_zones]) if features.is_share_groups_enabled(): share_groups = manila.share_group_list(request) self.fields['sg'] = forms.ChoiceField(label=_("Share Group"), choices=[("", "")] + [(sg.id, sg.name or sg.id) for sg in share_groups], required=False) self.sn_field_name_prefix = 'share-network-choices-' for st in share_types: extra_specs = st.get_keys() dhss = extra_specs.get('driver_handles_share_servers') # NOTE(vponomaryov): Set and tie share-network field only for # share types with enabled handling of share servers. if (isinstance(dhss, str) and dhss.lower() in ['true', '1']): sn_choices = ([('', '')] + [(sn.id, sn.name or sn.id) for sn in share_networks]) sn_field_name = (self.sn_field_name_prefix + utils.transform_dashed_name(st.name)) sn_field = forms.ChoiceField( label=_("Share Network"), required=True, choices=sn_choices, widget=forms.Select( attrs={ 'class': 'switched', 'data-switch-on': 'sharetype', 'data-sharetype-%s' % utils.transform_dashed_name(st.name): _("Share Network"), })) self.fields[sn_field_name] = sn_field self.fields['share_source_type'] = forms.ChoiceField( label=_("Share Source"), required=False, widget=forms.Select(attrs={ 'class': 'switchable', 'data-slug': 'source' })) self.fields['snapshot'] = forms.ChoiceField( label=_("Use snapshot as a source"), widget=forms.fields.SelectWidget(attrs={ 'class': 'switched', 'data-switch-on': 'source', 'data-source-snapshot': _('Snapshot') }, data_attrs=('size', 'name'), transform=lambda x: "%s (%sGiB)" % (x.name, x.size)), required=False) self.fields['metadata'] = forms.CharField( label=_("Metadata"), required=False, widget=forms.Textarea(attrs={'rows': 4})) if self.enable_public_shares: self.fields['is_public'] = forms.BooleanField( label=_("Make visible to users from all projects"), required=False) self.fields['share_proto'].choices = [ (sp, sp) for sp in self.enabled_share_protocols ] if ("snapshot_id" in request.GET or kwargs.get("data", {}).get("snapshot")): try: snapshot = self.get_snapshot( request, request.GET.get("snapshot_id", kwargs.get("data", {}).get("snapshot"))) self.fields['name'].initial = snapshot.name self.fields['size'].initial = snapshot.size self.fields['snapshot'].choices = ((snapshot.id, snapshot), ) try: # Set the share type and az from the original share orig_share = manila.share_get(request, snapshot.share_id) self.fields['share_type'].initial = ( orig_share.share_type_name) self.fields['availability_zone'].initial = ( orig_share.availability_zone) except Exception: pass self.fields['size'].help_text = _( 'Share size must be equal to or greater than the snapshot ' 'size (%sGiB)') % snapshot.size del self.fields['share_source_type'] except Exception: exceptions.handle(request, _('Unable to load the specified snapshot.')) else: source_type_choices = [] try: snapshot_list = manila.share_snapshot_list(request) snapshots = [ s for s in snapshot_list if s.status == 'available' ] if snapshots: source_type_choices.append(("snapshot", _("Snapshot"))) choices = [('', _("Choose a snapshot"))] + \ [(s.id, s) for s in snapshots] self.fields['snapshot'].choices = choices else: del self.fields['snapshot'] except Exception: exceptions.handle(request, _("Unable to retrieve " "share snapshots.")) if source_type_choices: choices = ([('no_source_type', _("No source, empty share"))] + source_type_choices) self.fields['share_source_type'].choices = choices else: del self.fields['share_source_type']