File: share.py

package info (click to toggle)
python-openstacksdk 4.4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,352 kB
  • sloc: python: 122,960; sh: 153; makefile: 23
file content (213 lines) | stat: -rw-r--r-- 8,241 bytes parent folder | download
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
# 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.

from openstack.common import metadata
from openstack import exceptions
from openstack import resource
from openstack import utils


class Share(resource.Resource, metadata.MetadataMixin):
    resource_key = "share"
    resources_key = "shares"
    base_path = "/shares"

    # capabilities
    allow_create = True
    allow_fetch = True
    allow_commit = True
    allow_list = True
    allow_head = False
    allow_delete = True

    #: Properties
    #: The share instance access rules status. A valid value is active,
    #: error, or syncing.
    access_rules_status = resource.Body("access_rules_status", type=str)
    #: The availability zone.
    availability_zone = resource.Body("availability_zone", type=str)
    #: The date and time stamp when the resource was created within the
    #: service’s database.
    created_at = resource.Body("created_at", type=str)
    #: The user defined description of the resource.
    description = resource.Body("description", type=str)
    #: The share host name.
    host = resource.Body("host", type=str)
    #: The level of visibility for the share.
    is_public = resource.Body("is_public", type=bool)
    #: Whether or not this share supports snapshots that can be
    #: cloned into new shares.
    is_creating_new_share_from_snapshot_supported = resource.Body(
        "create_share_from_snapshot_support", type=bool
    )
    #: Whether the share's snapshots can be mounted directly and access
    #: controlled independently or not.
    is_mounting_snapshot_supported = resource.Body(
        "mount_snapshot_support", type=bool
    )
    #: Whether the share can be reverted to its latest snapshot or not.
    is_reverting_to_snapshot_supported = resource.Body(
        "revert_to_snapshot_support", type=bool
    )
    #: An extra specification that filters back ends by whether the share
    #: supports snapshots or not.
    is_snapshot_supported = resource.Body("snapshot_support", type=bool)
    #: Indicates whether the share has replicas or not.
    is_replicated = resource.Body("has_replicas", type=bool)
    #: One or more metadata key and value pairs as a dictionary of strings.
    metadata = resource.Body("metadata", type=dict)
    #: The progress of the share creation.
    progress = resource.Body("progress", type=str)
    #: The ID of the project that owns the resource.
    project_id = resource.Body("project_id", type=str)
    #: The share replication type. Valid values are none, readable,
    #: writable and dr.
    replication_type = resource.Body("replication_type", type=str)
    #: The UUID of the share group that this shares belongs to.
    share_group_id = resource.Body("share_group_id", type=str)
    #: The share network ID.
    share_network_id = resource.Body("share_network_id", type=str)
    #: The Shared File Systems protocol. A valid value is NFS,
    #: CIFS, GlusterFS, HDFS, CephFS, MAPRFS
    share_protocol = resource.Body("share_proto", type=str)
    #: The UUID of the share server.
    share_server_id = resource.Body("share_server_id", type=str)
    #: The UUID of the share type. In minor versions, this parameter is a
    #: share type name, as a string.
    share_type = resource.Body("share_type", type=str)
    #: Name of the share type.
    share_type_name = resource.Body("share_type_name", type=str)
    #: The share size, in GiBs.
    size = resource.Body("size", type=int)
    #: The UUID of the snapshot that was used to create the
    #: share.
    snapshot_id = resource.Body("snapshot_id", type=str)
    #: The ID of the group snapshot instance that was used to create
    #: this share.
    source_share_group_snapshot_member_id = resource.Body(
        "source_share_group_snapshot_member_id", type=str
    )
    #: The share status
    status = resource.Body("status", type=str)
    #: For the share migration, the migration task state.
    task_state = resource.Body("task_state", type=str)
    #: ID of the user that the share was created by.
    user_id = resource.Body("user_id", type=str)
    #: Display name for updating name
    display_name = resource.Body("display_name", type=str)
    #: Display description for updating description
    display_description = resource.Body("display_description", type=str)

    def _action(self, session, body, action='patch', microversion=None):
        """Perform share instance actions given the message body"""
        url = utils.urljoin(self.base_path, self.id, 'action')
        headers = {'Accept': ''}

        if microversion is None:
            microversion = self._get_microversion(session, action=action)

        response = session.post(
            url, json=body, headers=headers, microversion=microversion
        )

        exceptions.raise_from_response(response)
        return response

    def extend_share(self, session, new_size, force=False):
        """Extend the share size.

        :param float new_size: The new size of the share
            in GiB.
        :param bool force: Whether or not to use force, bypassing
            the scheduler. Requires admin privileges. Defaults to False.
        :returns: The result of the action.
        :rtype: ``None``
        """

        extend_body = {"new_size": new_size}

        if force is True:
            extend_body['force'] = True

        body = {"extend": extend_body}
        self._action(session, body)

    def shrink_share(self, session, new_size):
        """Shrink the share size.

        :param float new_size: The new size of the share
            in GiB.
        :returns: ``None``
        """

        body = {"shrink": {'new_size': new_size}}
        self._action(session, body)

    def revert_to_snapshot(self, session, snapshot_id):
        """Revert the share to the given snapshot.

        :param str snapshot_id: The id of the snapshot to revert to.
        :returns: ``None``
        """
        body = {"revert": {"snapshot_id": snapshot_id}}
        self._action(session, body)

    def manage(self, session, protocol, export_path, service_host, **params):
        """Manage a share.

        :param session: A session object used for sending request.
        :param str protocol: The shared file systems protocol of this share.
        :param str export_path: The export path formatted according to the
            protocol.
        :param str service_host: The manage-share service host.
        :param kwargs params: Optional parameters to be sent. Available
            parameters include:

            * name: The user defined name of the resource.
            * share_type: The name or ID of the share type to be used to create
              the resource.
            * driver_options: A set of one or more key and value pairs, as a
              dictionary of strings, that describe driver options.
            * is_public: The level of visibility for the share.
            * description: The user defiend description of the resource.
            * share_server_id: The UUID of the share server.

        :returns: The share that was managed.
        """

        path = 'manage'
        attrs = {
            'share': {
                'protocol': protocol,
                'export_path': export_path,
                'service_host': service_host,
            }
        }

        attrs['share'].update(params)

        url = utils.urljoin(self.base_path, path)
        resp = session.post(url, json=attrs)

        self._translate_response(resp)
        return self

    def unmanage(self, session):
        """Unmanage a share.

        :param session: A session object used for sending request.
        :returns: ``None``
        """

        body = {'unmanage': None}

        self._action(session, body)