forked from openstack/manila
/
client_cmode.py
3709 lines (3219 loc) · 136 KB
/
client_cmode.py
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) 2014 Alex Meade. All rights reserved.
# Copyright (c) 2015 Clinton Knight. All rights reserved.
# Copyright (c) 2015 Tom Barron. All rights reserved.
#
# 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 copy
import hashlib
import re
import time
from oslo_log import log
from oslo_utils import strutils
from oslo_utils import units
import six
from manila import exception
from manila.i18n import _
from manila.share.drivers.netapp.dataontap.client import api as netapp_api
from manila.share.drivers.netapp.dataontap.client import client_base
from manila.share.drivers.netapp import utils as na_utils
LOG = log.getLogger(__name__)
DELETED_PREFIX = 'deleted_manila_'
DEFAULT_IPSPACE = 'Default'
DEFAULT_MAX_PAGE_LENGTH = 50
CUTOVER_ACTION_MAP = {
'defer': 'defer_on_failure',
'abort': 'abort_on_failure',
'force': 'force',
'wait': 'wait',
}
class NetAppCmodeClient(client_base.NetAppBaseClient):
def __init__(self, **kwargs):
super(NetAppCmodeClient, self).__init__(**kwargs)
self.vserver = kwargs.get('vserver')
self.connection.set_vserver(self.vserver)
# Default values to run first api.
self.connection.set_api_version(1, 15)
(major, minor) = self.get_ontapi_version(cached=False)
self.connection.set_api_version(major, minor)
self._init_features()
def _init_features(self):
"""Initialize cDOT feature support map."""
super(NetAppCmodeClient, self)._init_features()
ontapi_version = self.get_ontapi_version(cached=True)
ontapi_1_20 = ontapi_version >= (1, 20)
ontapi_1_2x = (1, 20) <= ontapi_version < (1, 30)
ontapi_1_30 = ontapi_version >= (1, 30)
self.features.add_feature('SNAPMIRROR_V2', supported=ontapi_1_20)
self.features.add_feature('SYSTEM_METRICS', supported=ontapi_1_2x)
self.features.add_feature('SYSTEM_CONSTITUENT_METRICS',
supported=ontapi_1_30)
self.features.add_feature('BROADCAST_DOMAINS', supported=ontapi_1_30)
self.features.add_feature('IPSPACES', supported=ontapi_1_30)
self.features.add_feature('SUBNETS', supported=ontapi_1_30)
self.features.add_feature('CLUSTER_PEER_POLICY', supported=ontapi_1_30)
self.features.add_feature('ADVANCED_DISK_PARTITIONING',
supported=ontapi_1_30)
def _invoke_vserver_api(self, na_element, vserver):
server = copy.copy(self.connection)
server.set_vserver(vserver)
result = server.invoke_successfully(na_element, True)
return result
def _has_records(self, api_result_element):
if (not api_result_element.get_child_content('num-records') or
api_result_element.get_child_content('num-records') == '0'):
return False
else:
return True
def _get_record_count(self, api_result_element):
try:
return int(api_result_element.get_child_content('num-records'))
except TypeError:
msg = _('Missing record count for NetApp iterator API invocation.')
raise exception.NetAppException(msg)
def set_vserver(self, vserver):
self.vserver = vserver
self.connection.set_vserver(vserver)
def send_iter_request(self, api_name, api_args=None,
max_page_length=DEFAULT_MAX_PAGE_LENGTH):
"""Invoke an iterator-style getter API."""
if not api_args:
api_args = {}
api_args['max-records'] = max_page_length
# Get first page
result = self.send_request(api_name, api_args)
# Most commonly, we can just return here if there is no more data
next_tag = result.get_child_content('next-tag')
if not next_tag:
return result
# Ensure pagination data is valid and prepare to store remaining pages
num_records = self._get_record_count(result)
attributes_list = result.get_child_by_name('attributes-list')
if not attributes_list:
msg = _('Missing attributes list for API %s.') % api_name
raise exception.NetAppException(msg)
# Get remaining pages, saving data into first page
while next_tag is not None:
next_api_args = copy.deepcopy(api_args)
next_api_args['tag'] = next_tag
next_result = self.send_request(api_name, next_api_args)
next_attributes_list = next_result.get_child_by_name(
'attributes-list') or netapp_api.NaElement('none')
for record in next_attributes_list.get_children():
attributes_list.add_child_elem(record)
num_records += self._get_record_count(next_result)
next_tag = next_result.get_child_content('next-tag')
result.get_child_by_name('num-records').set_content(
six.text_type(num_records))
result.get_child_by_name('next-tag').set_content('')
return result
@na_utils.trace
def create_vserver(self, vserver_name, root_volume_aggregate_name,
root_volume_name, aggregate_names, ipspace_name):
"""Creates new vserver and assigns aggregates."""
create_args = {
'vserver-name': vserver_name,
'root-volume-security-style': 'unix',
'root-volume-aggregate': root_volume_aggregate_name,
'root-volume': root_volume_name,
'name-server-switch': {
'nsswitch': 'file',
},
}
if ipspace_name:
if not self.features.IPSPACES:
msg = 'IPSpaces are not supported on this backend.'
raise exception.NetAppException(msg)
else:
create_args['ipspace'] = ipspace_name
self.send_request('vserver-create', create_args)
aggr_list = [{'aggr-name': aggr_name} for aggr_name in aggregate_names]
modify_args = {
'aggr-list': aggr_list,
'vserver-name': vserver_name,
}
self.send_request('vserver-modify', modify_args)
@na_utils.trace
def vserver_exists(self, vserver_name):
"""Checks if Vserver exists."""
LOG.debug('Checking if Vserver %s exists', vserver_name)
api_args = {
'query': {
'vserver-info': {
'vserver-name': vserver_name,
},
},
'desired-attributes': {
'vserver-info': {
'vserver-name': None,
},
},
}
result = self.send_iter_request('vserver-get-iter', api_args)
return self._has_records(result)
@na_utils.trace
def get_vserver_root_volume_name(self, vserver_name):
"""Get the root volume name of the vserver."""
api_args = {
'query': {
'vserver-info': {
'vserver-name': vserver_name,
},
},
'desired-attributes': {
'vserver-info': {
'root-volume': None,
},
},
}
vserver_info = self.send_iter_request('vserver-get-iter', api_args)
try:
root_volume_name = vserver_info.get_child_by_name(
'attributes-list').get_child_by_name(
'vserver-info').get_child_content('root-volume')
except AttributeError:
msg = _('Could not determine root volume name '
'for Vserver %s.') % vserver_name
raise exception.NetAppException(msg)
return root_volume_name
@na_utils.trace
def get_vserver_ipspace(self, vserver_name):
"""Get the IPspace of the vserver, or None if not supported."""
if not self.features.IPSPACES:
return None
api_args = {
'query': {
'vserver-info': {
'vserver-name': vserver_name,
},
},
'desired-attributes': {
'vserver-info': {
'ipspace': None,
},
},
}
vserver_info = self.send_iter_request('vserver-get-iter', api_args)
try:
ipspace = vserver_info.get_child_by_name(
'attributes-list').get_child_by_name(
'vserver-info').get_child_content('ipspace')
except AttributeError:
msg = _('Could not determine IPspace for Vserver %s.')
raise exception.NetAppException(msg % vserver_name)
return ipspace
@na_utils.trace
def ipspace_has_data_vservers(self, ipspace_name):
"""Check whether an IPspace has any data Vservers assigned to it."""
if not self.features.IPSPACES:
return False
api_args = {
'query': {
'vserver-info': {
'ipspace': ipspace_name,
'vserver-type': 'data'
},
},
'desired-attributes': {
'vserver-info': {
'vserver-name': None,
},
},
}
result = self.send_iter_request('vserver-get-iter', api_args)
return self._has_records(result)
@na_utils.trace
def list_vservers(self, vserver_type='data'):
"""Get the names of vservers present, optionally filtered by type."""
query = {
'vserver-info': {
'vserver-type': vserver_type,
}
} if vserver_type else None
api_args = {
'desired-attributes': {
'vserver-info': {
'vserver-name': None,
},
},
}
if query:
api_args['query'] = query
result = self.send_iter_request('vserver-get-iter', api_args)
vserver_info_list = result.get_child_by_name(
'attributes-list') or netapp_api.NaElement('none')
return [vserver_info.get_child_content('vserver-name')
for vserver_info in vserver_info_list.get_children()]
@na_utils.trace
def get_vserver_volume_count(self):
"""Get the number of volumes present on a cluster or vserver.
Call this on a vserver client to see how many volumes exist
on that vserver.
"""
api_args = {
'desired-attributes': {
'volume-attributes': {
'volume-id-attributes': {
'name': None,
},
},
},
}
volumes_data = self.send_iter_request('volume-get-iter', api_args)
return self._get_record_count(volumes_data)
@na_utils.trace
def delete_vserver(self, vserver_name, vserver_client,
security_services=None):
"""Delete Vserver.
Checks if Vserver exists and does not have active shares.
Offlines and destroys root volumes. Deletes Vserver.
"""
if not self.vserver_exists(vserver_name):
LOG.error("Vserver %s does not exist.", vserver_name)
return
root_volume_name = self.get_vserver_root_volume_name(vserver_name)
volumes_count = vserver_client.get_vserver_volume_count()
if volumes_count == 1:
try:
vserver_client.offline_volume(root_volume_name)
except netapp_api.NaApiError as e:
if e.code == netapp_api.EVOLUMEOFFLINE:
LOG.error("Volume %s is already offline.",
root_volume_name)
else:
raise
vserver_client.delete_volume(root_volume_name)
elif volumes_count > 1:
msg = _("Cannot delete Vserver. Vserver %s has shares.")
raise exception.NetAppException(msg % vserver_name)
if security_services:
self._terminate_vserver_services(vserver_name, vserver_client,
security_services)
self.send_request('vserver-destroy', {'vserver-name': vserver_name})
@na_utils.trace
def _terminate_vserver_services(self, vserver_name, vserver_client,
security_services):
for service in security_services:
if service['type'] == 'active_directory':
api_args = {
'admin-password': service['password'],
'admin-username': service['user'],
}
try:
vserver_client.send_request('cifs-server-delete', api_args)
except netapp_api.NaApiError as e:
if e.code == netapp_api.EOBJECTNOTFOUND:
LOG.error('CIFS server does not exist for '
'Vserver %s.', vserver_name)
else:
vserver_client.send_request('cifs-server-delete')
@na_utils.trace
def list_cluster_nodes(self):
"""Get all available cluster nodes."""
api_args = {
'desired-attributes': {
'node-details-info': {
'node': None,
},
},
}
result = self.send_iter_request('system-node-get-iter', api_args)
nodes_info_list = result.get_child_by_name(
'attributes-list') or netapp_api.NaElement('none')
return [node_info.get_child_content('node') for node_info
in nodes_info_list.get_children()]
@na_utils.trace
def list_node_data_ports(self, node):
ports = self.get_node_data_ports(node)
return [port.get('port') for port in ports]
@na_utils.trace
def get_node_data_ports(self, node):
"""Get applicable data ports on the node."""
api_args = {
'query': {
'net-port-info': {
'node': node,
'link-status': 'up',
'port-type': 'physical|if_group',
'role': 'data',
},
},
'desired-attributes': {
'net-port-info': {
'port': None,
'node': None,
'operational-speed': None,
'ifgrp-port': None,
},
},
}
result = self.send_iter_request('net-port-get-iter', api_args)
net_port_info_list = result.get_child_by_name(
'attributes-list') or netapp_api.NaElement('none')
ports = []
for port_info in net_port_info_list.get_children():
# Skip physical ports that are part of interface groups.
if port_info.get_child_content('ifgrp-port'):
continue
port = {
'node': port_info.get_child_content('node'),
'port': port_info.get_child_content('port'),
'speed': port_info.get_child_content('operational-speed'),
}
ports.append(port)
return self._sort_data_ports_by_speed(ports)
@na_utils.trace
def _sort_data_ports_by_speed(self, ports):
def sort_key(port):
value = port.get('speed')
if not (value and isinstance(value, six.string_types)):
return 0
elif value.isdigit():
return int(value)
elif value == 'auto':
return 3
elif value == 'undef':
return 2
else:
return 1
return sorted(ports, key=sort_key, reverse=True)
@na_utils.trace
def list_root_aggregates(self):
"""Get names of all aggregates that contain node root volumes."""
desired_attributes = {
'aggr-attributes': {
'aggregate-name': None,
'aggr-raid-attributes': {
'has-local-root': None,
'has-partner-root': None,
},
},
}
aggrs = self._get_aggregates(desired_attributes=desired_attributes)
root_aggregates = []
for aggr in aggrs:
aggr_name = aggr.get_child_content('aggregate-name')
aggr_raid_attrs = aggr.get_child_by_name('aggr-raid-attributes')
local_root = strutils.bool_from_string(
aggr_raid_attrs.get_child_content('has-local-root'))
partner_root = strutils.bool_from_string(
aggr_raid_attrs.get_child_content('has-partner-root'))
if local_root or partner_root:
root_aggregates.append(aggr_name)
return root_aggregates
@na_utils.trace
def list_non_root_aggregates(self):
"""Get names of all aggregates that don't contain node root volumes."""
query = {
'aggr-attributes': {
'aggr-raid-attributes': {
'has-local-root': 'false',
'has-partner-root': 'false',
}
},
}
return self._list_aggregates(query=query)
@na_utils.trace
def _list_aggregates(self, query=None):
"""Get names of all aggregates."""
try:
api_args = {
'desired-attributes': {
'aggr-attributes': {
'aggregate-name': None,
},
},
}
if query:
api_args['query'] = query
result = self.send_iter_request('aggr-get-iter', api_args)
aggr_list = result.get_child_by_name(
'attributes-list').get_children()
except AttributeError:
msg = _("Could not list aggregates.")
raise exception.NetAppException(msg)
return [aggr.get_child_content('aggregate-name') for aggr
in aggr_list]
@na_utils.trace
def list_vserver_aggregates(self):
"""Returns a list of aggregates available to a vserver.
This must be called against a Vserver LIF.
"""
return list(self.get_vserver_aggregate_capacities().keys())
@na_utils.trace
def create_network_interface(self, ip, netmask, vlan, node, port,
vserver_name, lif_name, ipspace_name, mtu):
"""Creates LIF on VLAN port."""
home_port_name = port
if vlan:
self._create_vlan(node, port, vlan)
home_port_name = '%(port)s-%(tag)s' % {'port': port, 'tag': vlan}
if self.features.BROADCAST_DOMAINS:
self._ensure_broadcast_domain_for_port(
node, home_port_name, mtu, ipspace=ipspace_name)
LOG.debug('Creating LIF %(lif)s for Vserver %(vserver)s ',
{'lif': lif_name, 'vserver': vserver_name})
api_args = {
'address': ip,
'administrative-status': 'up',
'data-protocols': [
{'data-protocol': 'nfs'},
{'data-protocol': 'cifs'},
],
'home-node': node,
'home-port': home_port_name,
'netmask': netmask,
'interface-name': lif_name,
'role': 'data',
'vserver': vserver_name,
}
self.send_request('net-interface-create', api_args)
@na_utils.trace
def _create_vlan(self, node, port, vlan):
try:
api_args = {
'vlan-info': {
'parent-interface': port,
'node': node,
'vlanid': vlan,
},
}
self.send_request('net-vlan-create', api_args)
except netapp_api.NaApiError as e:
if e.code == netapp_api.EDUPLICATEENTRY:
LOG.debug('VLAN %(vlan)s already exists on port %(port)s',
{'vlan': vlan, 'port': port})
else:
msg = _('Failed to create VLAN %(vlan)s on '
'port %(port)s. %(err_msg)s')
msg_args = {'vlan': vlan, 'port': port, 'err_msg': e.message}
raise exception.NetAppException(msg % msg_args)
@na_utils.trace
def delete_vlan(self, node, port, vlan):
try:
api_args = {
'vlan-info': {
'parent-interface': port,
'node': node,
'vlanid': vlan,
},
}
self.send_request('net-vlan-delete', api_args)
except netapp_api.NaApiError as e:
p = re.compile('port already has a lif bound.*', re.IGNORECASE)
if (e.code == netapp_api.EAPIERROR and re.match(p, e.message)):
LOG.debug('VLAN %(vlan)s on port %(port)s node %(node)s '
'still used by LIF and cannot be deleted.',
{'vlan': vlan, 'port': port, 'node': node})
else:
msg = _('Failed to delete VLAN %(vlan)s on '
'port %(port)s node %(node)s: %(err_msg)s')
msg_args = {
'vlan': vlan,
'port': port,
'node': node,
'err_msg': e.message
}
raise exception.NetAppException(msg % msg_args)
@na_utils.trace
def create_route(self, gateway, destination=None):
if not gateway:
return
if not destination:
if ':' in gateway:
destination = '::/0'
else:
destination = '0.0.0.0/0'
try:
api_args = {
'destination': destination,
'gateway': gateway,
'return-record': 'true',
}
self.send_request('net-routes-create', api_args)
except netapp_api.NaApiError as e:
p = re.compile('.*Duplicate route exists.*', re.IGNORECASE)
if (e.code == netapp_api.EAPIERROR and re.match(p, e.message)):
LOG.debug('Route to %(destination)s via gateway %(gateway)s '
'exists.',
{'destination': destination, 'gateway': gateway})
else:
msg = _('Failed to create a route to %(destination)s via '
'gateway %(gateway)s: %(err_msg)s')
msg_args = {
'destination': destination,
'gateway': gateway,
'err_msg': e.message,
}
raise exception.NetAppException(msg % msg_args)
@na_utils.trace
def _ensure_broadcast_domain_for_port(self, node, port, mtu,
ipspace=DEFAULT_IPSPACE):
"""Ensure a port is in a broadcast domain. Create one if necessary.
If the IPspace:domain pair match for the given port, which commonly
happens in multi-node clusters, then there isn't anything to do.
Otherwise, we can assume the IPspace is correct and extant by this
point, so the remaining task is to remove the port from any domain it
is already in, create the domain for the IPspace if it doesn't exist,
and add the port to this domain.
"""
# Derive the broadcast domain name from the IPspace name since they
# need to be 1-1 and the default for both is the same name, 'Default'.
domain = re.sub(r'ipspace', 'domain', ipspace)
port_info = self._get_broadcast_domain_for_port(node, port)
# Port already in desired ipspace and broadcast domain.
if (port_info['ipspace'] == ipspace
and (port_info['broadcast-domain'] == domain
or port_info['broadcast-domain'] == 'OpenStack')):
self._modify_broadcast_domain(
port_info['broadcast-domain'], ipspace, mtu)
return
# If in another broadcast domain, remove port from it.
if port_info['broadcast-domain']:
self._remove_port_from_broadcast_domain(
node, port, port_info['broadcast-domain'],
port_info['ipspace'])
# If desired broadcast domain doesn't exist, create it.
if not self._broadcast_domain_exists(domain, ipspace):
self._create_broadcast_domain(domain, ipspace, mtu)
else:
self._modify_broadcast_domain(domain, ipspace, mtu)
# Move the port into the broadcast domain where it is needed.
self._add_port_to_broadcast_domain(node, port, domain, ipspace)
@na_utils.trace
def _get_broadcast_domain_for_port(self, node, port):
"""Get broadcast domain for a specific port."""
api_args = {
'query': {
'net-port-info': {
'node': node,
'port': port,
},
},
'desired-attributes': {
'net-port-info': {
'broadcast-domain': None,
'ipspace': None,
},
},
}
result = self.send_iter_request('net-port-get-iter', api_args)
net_port_info_list = result.get_child_by_name(
'attributes-list') or netapp_api.NaElement('none')
port_info = net_port_info_list.get_children()
if not port_info:
msg = _('Could not find port %(port)s on node %(node)s.')
msg_args = {'port': port, 'node': node}
raise exception.NetAppException(msg % msg_args)
port = {
'broadcast-domain':
port_info[0].get_child_content('broadcast-domain'),
'ipspace': port_info[0].get_child_content('ipspace')
}
return port
@na_utils.trace
def _broadcast_domain_exists(self, domain, ipspace):
"""Check if a broadcast domain exists."""
api_args = {
'query': {
'net-port-broadcast-domain-info': {
'ipspace': ipspace,
'broadcast-domain': domain,
},
},
'desired-attributes': {
'net-port-broadcast-domain-info': None,
},
}
result = self.send_iter_request('net-port-broadcast-domain-get-iter',
api_args)
return self._has_records(result)
@na_utils.trace
def _create_broadcast_domain(self, domain, ipspace, mtu):
"""Create a broadcast domain."""
api_args = {
'ipspace': ipspace,
'broadcast-domain': domain,
'mtu': mtu,
}
self.send_request('net-port-broadcast-domain-create', api_args)
@na_utils.trace
def _modify_broadcast_domain(self, domain, ipspace, mtu):
"""Modify a broadcast domain."""
api_args = {
'ipspace': ipspace,
'broadcast-domain': domain,
'mtu': mtu,
}
self.send_request('net-port-broadcast-domain-modify', api_args)
@na_utils.trace
def _delete_broadcast_domain(self, domain, ipspace):
"""Delete a broadcast domain."""
api_args = {
'ipspace': ipspace,
'broadcast-domain': domain,
}
self.send_request('net-port-broadcast-domain-destroy', api_args)
@na_utils.trace
def _delete_broadcast_domains_for_ipspace(self, ipspace_name):
"""Deletes all broadcast domains in an IPspace."""
ipspaces = self.get_ipspaces(ipspace_name=ipspace_name)
if not ipspaces:
return
ipspace = ipspaces[0]
for broadcast_domain_name in ipspace['broadcast-domains']:
self._delete_broadcast_domain(broadcast_domain_name, ipspace_name)
@na_utils.trace
def _add_port_to_broadcast_domain(self, node, port, domain, ipspace):
qualified_port_name = ':'.join([node, port])
try:
api_args = {
'ipspace': ipspace,
'broadcast-domain': domain,
'ports': {
'net-qualified-port-name': qualified_port_name,
}
}
self.send_request('net-port-broadcast-domain-add-ports', api_args)
except netapp_api.NaApiError as e:
if e.code == (netapp_api.
E_VIFMGR_PORT_ALREADY_ASSIGNED_TO_BROADCAST_DOMAIN):
LOG.debug('Port %(port)s already exists in broadcast domain '
'%(domain)s', {'port': port, 'domain': domain})
else:
msg = _('Failed to add port %(port)s to broadcast domain '
'%(domain)s. %(err_msg)s')
msg_args = {
'port': qualified_port_name,
'domain': domain,
'err_msg': e.message,
}
raise exception.NetAppException(msg % msg_args)
@na_utils.trace
def _remove_port_from_broadcast_domain(self, node, port, domain, ipspace):
qualified_port_name = ':'.join([node, port])
api_args = {
'ipspace': ipspace,
'broadcast-domain': domain,
'ports': {
'net-qualified-port-name': qualified_port_name,
}
}
self.send_request('net-port-broadcast-domain-remove-ports', api_args)
@na_utils.trace
def network_interface_exists(self, vserver_name, node, port, ip, netmask,
vlan):
"""Checks if LIF exists."""
home_port_name = (port if not vlan else
'%(port)s-%(tag)s' % {'port': port, 'tag': vlan})
api_args = {
'query': {
'net-interface-info': {
'address': ip,
'home-node': node,
'home-port': home_port_name,
'netmask': netmask,
'vserver': vserver_name,
},
},
'desired-attributes': {
'net-interface-info': {
'interface-name': None,
},
},
}
result = self.send_iter_request('net-interface-get-iter', api_args)
return self._has_records(result)
@na_utils.trace
def list_network_interfaces(self):
"""Get the names of available LIFs."""
api_args = {
'desired-attributes': {
'net-interface-info': {
'interface-name': None,
},
},
}
result = self.send_iter_request('net-interface-get-iter', api_args)
lif_info_list = result.get_child_by_name(
'attributes-list') or netapp_api.NaElement('none')
return [lif_info.get_child_content('interface-name') for lif_info
in lif_info_list.get_children()]
@na_utils.trace
def get_network_interfaces(self, protocols=None):
"""Get available LIFs."""
protocols = na_utils.convert_to_list(protocols)
protocols = [protocol.lower() for protocol in protocols]
api_args = {
'query': {
'net-interface-info': {
'data-protocols': {
'data-protocol': '|'.join(protocols),
}
}
}
} if protocols else None
result = self.send_iter_request('net-interface-get-iter', api_args)
lif_info_list = result.get_child_by_name(
'attributes-list') or netapp_api.NaElement('none')
interfaces = []
for lif_info in lif_info_list.get_children():
lif = {
'address': lif_info.get_child_content('address'),
'home-node': lif_info.get_child_content('home-node'),
'home-port': lif_info.get_child_content('home-port'),
'interface-name': lif_info.get_child_content('interface-name'),
'netmask': lif_info.get_child_content('netmask'),
'role': lif_info.get_child_content('role'),
'vserver': lif_info.get_child_content('vserver'),
}
interfaces.append(lif)
return interfaces
@na_utils.trace
def get_ipspaces(self, ipspace_name=None):
"""Gets one or more IPSpaces."""
if not self.features.IPSPACES:
return []
api_args = {}
if ipspace_name:
api_args['query'] = {
'net-ipspaces-info': {
'ipspace': ipspace_name,
}
}
result = self.send_iter_request('net-ipspaces-get-iter', api_args)
if not self._has_records(result):
return []
ipspaces = []
for net_ipspaces_info in result.get_child_by_name(
'attributes-list').get_children():
ipspace = {
'ports': [],
'vservers': [],
'broadcast-domains': [],
}
ports = net_ipspaces_info.get_child_by_name(
'ports') or netapp_api.NaElement('none')
for port in ports.get_children():
ipspace['ports'].append(port.get_content())
vservers = net_ipspaces_info.get_child_by_name(
'vservers') or netapp_api.NaElement('none')
for vserver in vservers.get_children():
ipspace['vservers'].append(vserver.get_content())
broadcast_domains = net_ipspaces_info.get_child_by_name(
'broadcast-domains') or netapp_api.NaElement('none')
for broadcast_domain in broadcast_domains.get_children():
ipspace['broadcast-domains'].append(
broadcast_domain.get_content())
ipspace['ipspace'] = net_ipspaces_info.get_child_content('ipspace')
ipspace['id'] = net_ipspaces_info.get_child_content('id')
ipspace['uuid'] = net_ipspaces_info.get_child_content('uuid')
ipspaces.append(ipspace)
return ipspaces
@na_utils.trace
def ipspace_exists(self, ipspace_name):
"""Checks if IPspace exists."""
if not self.features.IPSPACES:
return False
api_args = {
'query': {
'net-ipspaces-info': {
'ipspace': ipspace_name,
},
},
'desired-attributes': {
'net-ipspaces-info': {
'ipspace': None,
},
},
}
result = self.send_iter_request('net-ipspaces-get-iter', api_args)
return self._has_records(result)
@na_utils.trace
def create_ipspace(self, ipspace_name):
"""Creates an IPspace."""
api_args = {'ipspace': ipspace_name}
self.send_request('net-ipspaces-create', api_args)
@na_utils.trace
def delete_ipspace(self, ipspace_name):
"""Deletes an IPspace."""
self._delete_broadcast_domains_for_ipspace(ipspace_name)
api_args = {'ipspace': ipspace_name}
self.send_request('net-ipspaces-destroy', api_args)
@na_utils.trace
def add_vserver_to_ipspace(self, ipspace_name, vserver_name):
"""Assigns a vserver to an IPspace."""
api_args = {'ipspace': ipspace_name, 'vserver': vserver_name}
self.send_request('net-ipspaces-assign-vserver', api_args)
@na_utils.trace
def get_node_for_aggregate(self, aggregate_name):
"""Get home node for the specified aggregate.
This API could return None, most notably if it was sent
to a Vserver LIF, so the caller must be able to handle that case.
"""