Browse Source

Huawei manila driver support Read-Only share

- Change allow_access function: Add access_level parameter
to setting 'rw' and 'ro'.
- Add unit test.

Change-Id: I9f9202242ec89c0dad27befce8992481e298d407
Implements blueprint huawei-driver-read-only-share
tags/1.0.0.0b1
zhongjun 4 years ago
parent
commit
c15aec017b

+ 5
- 0
manila/share/drivers/huawei/constants.py View File

@@ -21,3 +21,8 @@ DEFAULT_TIMEOUT = 60
21 21
 MSG_SNAPSHOT_NOT_FOUND = 1073754118
22 22
 IP_ALLOCATIONS = 0
23 23
 SOCKET_TIMEOUT = 720
24
+
25
+ACCESS_NFS_RW = "1"
26
+ACCESS_NFS_RO = "0"
27
+ACCESS_CIFS_RW = "5"
28
+ACCESS_CIFS_RO = "0"

+ 29
- 8
manila/share/drivers/huawei/v3/connection.py View File

@@ -18,6 +18,7 @@ import time
18 18
 from oslo_log import log
19 19
 from oslo_utils import units
20 20
 
21
+from manila.common import constants as common_constants
21 22
 from manila import exception
22 23
 from manila.i18n import _, _LI, _LW
23 24
 from manila.share.drivers.huawei import base as driver
@@ -295,14 +296,32 @@ class V3StorageConnection(driver.HuaweiBase):
295 296
         share_name = share['name']
296 297
         share_type = self.helper._get_share_type(share_proto)
297 298
         access_type = access['access_type']
298
-        if share_proto == 'NFS' and access_type != 'ip':
299
-            message = _('Only IP access type is allowed for NFS shares.')
300
-            raise exception.InvalidShareAccess(reason=message)
301
-        elif share_proto == 'CIFS' and access_type != 'user':
302
-            message = _('Only USER access type is allowed for CIFS shares.')
303
-            raise exception.InvalidShareAccess(reason=message)
299
+        access_level = access['access_level']
304 300
 
305
-        access_to = access['access_to']
301
+        if access_level not in common_constants.ACCESS_LEVELS:
302
+            raise exception.InvalidShareAccess(
303
+                reason=(_('Unsupported level of access was provided - %s') %
304
+                        access_level))
305
+
306
+        if share_proto == 'NFS':
307
+            if access_type == 'ip':
308
+                if access_level == common_constants.ACCESS_LEVEL_RW:
309
+                    access_level = constants.ACCESS_NFS_RW
310
+                else:
311
+                    access_level = constants.ACCESS_NFS_RO
312
+            else:
313
+                message = _('Only IP access type is allowed for NFS shares.')
314
+                raise exception.InvalidShareAccess(reason=message)
315
+        elif share_proto == 'CIFS':
316
+            if access_type == 'user':
317
+                if access_level == common_constants.ACCESS_LEVEL_RW:
318
+                    access_level = constants.ACCESS_CIFS_RW
319
+                else:
320
+                    access_level = constants.ACCESS_CIFS_RO
321
+            else:
322
+                message = _('Only USER access type is allowed'
323
+                            ' for CIFS shares.')
324
+                raise exception.InvalidShareAccess(reason=message)
306 325
 
307 326
         share = self.helper._get_share_by_name(share_name, share_type)
308 327
         if not share:
@@ -312,7 +331,9 @@ class V3StorageConnection(driver.HuaweiBase):
312 331
             raise exception.InvalidShareAccess(reason=err_msg)
313 332
 
314 333
         share_id = share['ID']
315
-        self.helper._allow_access_rest(share_id, access_to, share_proto)
334
+        access_to = access['access_to']
335
+        self.helper._allow_access_rest(share_id, access_to,
336
+                                       share_proto, access_level)
316 337
 
317 338
     def allocate_container(self, share):
318 339
         """Creates filesystem associated to share by name."""

+ 4
- 3
manila/share/drivers/huawei/v3/helper.py View File

@@ -366,7 +366,8 @@ class RestHelper(object):
366 366
             if access_to == item['NAME']:
367 367
                 return item['ID']
368 368
 
369
-    def _allow_access_rest(self, share_id, access_to, share_proto):
369
+    def _allow_access_rest(self, share_id, access_to,
370
+                           share_proto, access_level):
370 371
         """Allow access to the share."""
371 372
         access_type = self._get_share_client_type(share_proto)
372 373
         url = self.url + "/" + access_type
@@ -377,7 +378,7 @@ class RestHelper(object):
377 378
                 "TYPE": "16409",
378 379
                 "NAME": access_to,
379 380
                 "PARENTID": share_id,
380
-                "ACCESSVAL": "1",
381
+                "ACCESSVAL": access_level,
381 382
                 "SYNC": "0",
382 383
                 "ALLSQUASH": "1",
383 384
                 "ROOTSQUASH": "0",
@@ -386,7 +387,7 @@ class RestHelper(object):
386 387
             access = {
387 388
                 "NAME": access_to,
388 389
                 "PARENTID": share_id,
389
-                "PERMISSION": "5",
390
+                "PERMISSION": access_level,
390 391
                 "DOMAINTYPE": "2",
391 392
             }
392 393
         data = jsonutils.dumps(access)

+ 128
- 5
manila/tests/share/drivers/huawei/test_huawei_nas.py View File

@@ -73,6 +73,51 @@ def filesystem(method, data, fs_status_flag):
73 73
     return (data, extend_share_flag)
74 74
 
75 75
 
76
+def allow_access(type, method, data):
77
+    allow_ro_flag = False
78
+    allow_rw_flag = False
79
+    access_nfs = {
80
+        "TYPE": "16409",
81
+        "NAME": "1.2.3.4",
82
+        "PARENTID": "1",
83
+        "ACCESSVAL": "0",
84
+        "SYNC": "0",
85
+        "ALLSQUASH": "1",
86
+        "ROOTSQUASH": "0",
87
+    }
88
+    access_nfs_ro_data = jsonutils.dumps(access_nfs)
89
+    access_nfs["NAME"] = "100.112.0.1"
90
+    access_nfs["ACCESSVAL"] = "1"
91
+    access_nfs_rw_data = jsonutils.dumps(access_nfs)
92
+
93
+    access_cifs = {
94
+        "NAME": "user_name",
95
+        "PARENTID": "2",
96
+        "PERMISSION": "0",
97
+        "DOMAINTYPE": "2",
98
+    }
99
+    access_cifs_ro_data = jsonutils.dumps(access_cifs)
100
+
101
+    access_cifs["PERMISSION"] = "5"
102
+    access_cifs_rw_data = jsonutils.dumps(access_cifs)
103
+
104
+    if method != "POST":
105
+        data = """{"error":{"code":31755596}}"""
106
+        return data
107
+
108
+    if ((data == access_nfs_ro_data and type == "NFS")
109
+       or (data == access_cifs_ro_data and type == "CIFS")):
110
+        allow_ro_flag = True
111
+        data = """{"error":{"code":0}}"""
112
+    elif ((data == access_nfs_rw_data and type == 'NFS')
113
+          or (data == access_cifs_rw_data and type == 'CIFS')):
114
+        allow_rw_flag = True
115
+        data = """{"error":{"code":0}}"""
116
+    else:
117
+        data = """{"error":{"code":31755596}}"""
118
+    return (data, allow_ro_flag, allow_rw_flag)
119
+
120
+
76 121
 class FakeHuaweiNasHelper(helper.RestHelper):
77 122
 
78 123
     def __init__(self, *args, **kwargs):
@@ -91,6 +136,8 @@ class FakeHuaweiNasHelper(helper.RestHelper):
91 136
         self.share_exist = True
92 137
         self.service_nfs_status_flag = True
93 138
         self.create_share_data_flag = False
139
+        self.allow_ro_flag = False
140
+        self.allow_rw_flag = False
94 141
         self.extend_share_flag = False
95 142
 
96 143
     def _change_file_mode(self, filepath):
@@ -179,9 +226,14 @@ class FakeHuaweiNasHelper(helper.RestHelper):
179 226
                 data = """{"error":{"code":0}}"""
180 227
                 self.delete_flag = True
181 228
 
182
-            if url == "NFS_SHARE_AUTH_CLIENT"\
183
-                      or url == "CIFS_SHARE_AUTH_CLIENT":
184
-                data = """{"error":{"code":0}}"""
229
+            if url == "NFS_SHARE_AUTH_CLIENT":
230
+                data, self.allow_ro_flag, self.allow_rw_flag = \
231
+                    allow_access('NFS', method, data)
232
+                self.allow_flag = True
233
+
234
+            if url == "CIFS_SHARE_AUTH_CLIENT":
235
+                data, self.allow_ro_flag, self.allow_rw_flag = \
236
+                    allow_access('CIFS', method, data)
185 237
                 self.allow_flag = True
186 238
 
187 239
             if url == "FSSNAPSHOT?TYPE=48&PARENTID=4"\
@@ -327,6 +379,17 @@ class HuaweiShareDriverTestCase(test.TestCase):
327 379
             'share_server_id': 'fake-share-srv-id',
328 380
         }
329 381
 
382
+        self.share_proto_fail = {
383
+            'id': 'fake_uuid',
384
+            'project_id': 'fake_tenant_id',
385
+            'display_name': 'fake',
386
+            'name': 'share-fake-uuid',
387
+            'size': 1,
388
+            'share_proto': 'proto_fail',
389
+            'share_network_id': 'fake_net_id',
390
+            'share_server_id': 'fake-share-srv-id',
391
+        }
392
+
330 393
         self.share_cifs = {
331 394
             'id': 'fake_uuid',
332 395
             'project_id': 'fake_tenant_id',
@@ -371,11 +434,13 @@ class HuaweiShareDriverTestCase(test.TestCase):
371 434
         self.access_ip = {
372 435
             'access_type': 'ip',
373 436
             'access_to': '100.112.0.1',
437
+            'access_level': 'rw',
374 438
         }
375 439
 
376 440
         self.access_user = {
377 441
             'access_type': 'user',
378 442
             'access_to': 'user_name',
443
+            'access_level': 'rw',
379 444
         }
380 445
 
381 446
         self.share_server = None
@@ -677,21 +742,79 @@ class HuaweiShareDriverTestCase(test.TestCase):
677 742
         self.assertEqual(2, capacity['TOTALCAPACITY'])
678 743
         self.assertEqual(1, capacity['CAPACITY'])
679 744
 
680
-    def test_allow_access_ip_success(self):
745
+    def test_allow_access_proto_fail(self):
746
+        self.driver.plugin.helper.login()
747
+        self.assertRaises(exception.InvalidInput,
748
+                          self.driver.allow_access,
749
+                          self._context,
750
+                          self.share_proto_fail,
751
+                          self.access_ip,
752
+                          self.share_server)
753
+
754
+    def test_allow_access_ip_rw_success(self):
681 755
         self.driver.plugin.helper.login()
682 756
         self.allow_flag = False
757
+        self.allow_rw_flag = False
683 758
         self.driver.allow_access(self._context,
684 759
                                  self.share_nfs,
685 760
                                  self.access_ip,
686 761
                                  self.share_server)
687 762
         self.assertTrue(self.driver.plugin.helper.allow_flag)
763
+        self.assertTrue(self.driver.plugin.helper.allow_rw_flag)
764
+
765
+    def test_allow_access_ip_ro_success(self):
766
+        access_ro = {
767
+            'access_type': 'ip',
768
+            'access_to': '1.2.3.4',
769
+            'access_level': 'ro',
770
+        }
771
+
772
+        self.driver.plugin.helper.login()
773
+        self.allow_flag = False
774
+        self.allow_ro_flag = False
775
+        self.driver.allow_access(self._context,
776
+                                 self.share_nfs,
777
+                                 access_ro,
778
+                                 self.share_server)
779
+        self.assertTrue(self.driver.plugin.helper.allow_flag)
780
+        self.assertTrue(self.driver.plugin.helper.allow_ro_flag)
688 781
 
689
-    def test_allow_access_user_success(self):
782
+    def test_allow_access_user_rw_success(self):
690 783
         self.driver.plugin.helper.login()
691 784
         self.allow_flag = False
785
+        self.allow_rw_flag = False
692 786
         self.driver.allow_access(self._context, self.share_cifs,
693 787
                                  self.access_user, self.share_server)
694 788
         self.assertTrue(self.driver.plugin.helper.allow_flag)
789
+        self.assertTrue(self.driver.plugin.helper.allow_rw_flag)
790
+
791
+    def test_allow_access_user_ro_success(self):
792
+        access_ro = {
793
+            'access_type': 'user',
794
+            'access_to': 'user_name',
795
+            'access_level': 'ro',
796
+        }
797
+
798
+        self.driver.plugin.helper.login()
799
+        self.allow_flag = False
800
+        self.allow_ro_flag = False
801
+        self.driver.allow_access(self._context, self.share_cifs,
802
+                                 access_ro, self.share_server)
803
+        self.assertTrue(self.driver.plugin.helper.allow_flag)
804
+        self.assertTrue(self.driver.plugin.helper.allow_ro_flag)
805
+
806
+    def test_allow_access_level_fail(self):
807
+        access_fail = {
808
+            'access_type': 'user',
809
+            'access_to': 'user_name',
810
+            'access_level': 'fail',
811
+        }
812
+
813
+        self.driver.plugin.helper.login()
814
+        self.assertRaises(exception.InvalidShareAccess,
815
+                          self.driver.allow_access,
816
+                          self._context, self.share_cifs,
817
+                          access_fail, self.share_server)
695 818
 
696 819
     def test_get_share_client_type_fail(self):
697 820
         share_proto = 'fake_proto'

Loading…
Cancel
Save