Shared filesystem management project for OpenStack.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

client_cmode.py 127KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475
  1. # Copyright (c) 2014 Alex Meade. All rights reserved.
  2. # Copyright (c) 2015 Clinton Knight. All rights reserved.
  3. # Copyright (c) 2015 Tom Barron. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16. import copy
  17. import hashlib
  18. import re
  19. import time
  20. from oslo_log import log
  21. from oslo_utils import strutils
  22. from oslo_utils import units
  23. import six
  24. from manila import exception
  25. from manila.i18n import _, _LE, _LW
  26. from manila.share.drivers.netapp.dataontap.client import api as netapp_api
  27. from manila.share.drivers.netapp.dataontap.client import client_base
  28. from manila.share.drivers.netapp import utils as na_utils
  29. LOG = log.getLogger(__name__)
  30. DELETED_PREFIX = 'deleted_manila_'
  31. DEFAULT_IPSPACE = 'Default'
  32. DEFAULT_MAX_PAGE_LENGTH = 50
  33. CUTOVER_ACTION_MAP = {
  34. 'defer': 'defer_on_failure',
  35. 'abort': 'abort_on_failure',
  36. 'force': 'force',
  37. 'wait': 'wait',
  38. }
  39. class NetAppCmodeClient(client_base.NetAppBaseClient):
  40. def __init__(self, **kwargs):
  41. super(NetAppCmodeClient, self).__init__(**kwargs)
  42. self.vserver = kwargs.get('vserver')
  43. self.connection.set_vserver(self.vserver)
  44. # Default values to run first api.
  45. self.connection.set_api_version(1, 15)
  46. (major, minor) = self.get_ontapi_version(cached=False)
  47. self.connection.set_api_version(major, minor)
  48. self._init_features()
  49. def _init_features(self):
  50. """Initialize cDOT feature support map."""
  51. super(NetAppCmodeClient, self)._init_features()
  52. ontapi_version = self.get_ontapi_version(cached=True)
  53. ontapi_1_20 = ontapi_version >= (1, 20)
  54. ontapi_1_2x = (1, 20) <= ontapi_version < (1, 30)
  55. ontapi_1_30 = ontapi_version >= (1, 30)
  56. self.features.add_feature('SNAPMIRROR_V2', supported=ontapi_1_20)
  57. self.features.add_feature('SYSTEM_METRICS', supported=ontapi_1_2x)
  58. self.features.add_feature('SYSTEM_CONSTITUENT_METRICS',
  59. supported=ontapi_1_30)
  60. self.features.add_feature('BROADCAST_DOMAINS', supported=ontapi_1_30)
  61. self.features.add_feature('IPSPACES', supported=ontapi_1_30)
  62. self.features.add_feature('SUBNETS', supported=ontapi_1_30)
  63. self.features.add_feature('CLUSTER_PEER_POLICY', supported=ontapi_1_30)
  64. self.features.add_feature('ADVANCED_DISK_PARTITIONING',
  65. supported=ontapi_1_30)
  66. def _invoke_vserver_api(self, na_element, vserver):
  67. server = copy.copy(self.connection)
  68. server.set_vserver(vserver)
  69. result = server.invoke_successfully(na_element, True)
  70. return result
  71. def _has_records(self, api_result_element):
  72. if (not api_result_element.get_child_content('num-records') or
  73. api_result_element.get_child_content('num-records') == '0'):
  74. return False
  75. else:
  76. return True
  77. def _get_record_count(self, api_result_element):
  78. try:
  79. return int(api_result_element.get_child_content('num-records'))
  80. except TypeError:
  81. msg = _('Missing record count for NetApp iterator API invocation.')
  82. raise exception.NetAppException(msg)
  83. def set_vserver(self, vserver):
  84. self.vserver = vserver
  85. self.connection.set_vserver(vserver)
  86. def send_iter_request(self, api_name, api_args=None,
  87. max_page_length=DEFAULT_MAX_PAGE_LENGTH):
  88. """Invoke an iterator-style getter API."""
  89. if not api_args:
  90. api_args = {}
  91. api_args['max-records'] = max_page_length
  92. # Get first page
  93. result = self.send_request(api_name, api_args)
  94. # Most commonly, we can just return here if there is no more data
  95. next_tag = result.get_child_content('next-tag')
  96. if not next_tag:
  97. return result
  98. # Ensure pagination data is valid and prepare to store remaining pages
  99. num_records = self._get_record_count(result)
  100. attributes_list = result.get_child_by_name('attributes-list')
  101. if not attributes_list:
  102. msg = _('Missing attributes list for API %s.') % api_name
  103. raise exception.NetAppException(msg)
  104. # Get remaining pages, saving data into first page
  105. while next_tag is not None:
  106. next_api_args = copy.deepcopy(api_args)
  107. next_api_args['tag'] = next_tag
  108. next_result = self.send_request(api_name, next_api_args)
  109. next_attributes_list = next_result.get_child_by_name(
  110. 'attributes-list') or netapp_api.NaElement('none')
  111. for record in next_attributes_list.get_children():
  112. attributes_list.add_child_elem(record)
  113. num_records += self._get_record_count(next_result)
  114. next_tag = next_result.get_child_content('next-tag')
  115. result.get_child_by_name('num-records').set_content(
  116. six.text_type(num_records))
  117. result.get_child_by_name('next-tag').set_content('')
  118. return result
  119. @na_utils.trace
  120. def create_vserver(self, vserver_name, root_volume_aggregate_name,
  121. root_volume_name, aggregate_names, ipspace_name):
  122. """Creates new vserver and assigns aggregates."""
  123. create_args = {
  124. 'vserver-name': vserver_name,
  125. 'root-volume-security-style': 'unix',
  126. 'root-volume-aggregate': root_volume_aggregate_name,
  127. 'root-volume': root_volume_name,
  128. 'name-server-switch': {
  129. 'nsswitch': 'file',
  130. },
  131. }
  132. if ipspace_name:
  133. if not self.features.IPSPACES:
  134. msg = 'IPSpaces are not supported on this backend.'
  135. raise exception.NetAppException(msg)
  136. else:
  137. create_args['ipspace'] = ipspace_name
  138. self.send_request('vserver-create', create_args)
  139. aggr_list = [{'aggr-name': aggr_name} for aggr_name in aggregate_names]
  140. modify_args = {
  141. 'aggr-list': aggr_list,
  142. 'vserver-name': vserver_name,
  143. }
  144. self.send_request('vserver-modify', modify_args)
  145. @na_utils.trace
  146. def vserver_exists(self, vserver_name):
  147. """Checks if Vserver exists."""
  148. LOG.debug('Checking if Vserver %s exists', vserver_name)
  149. api_args = {
  150. 'query': {
  151. 'vserver-info': {
  152. 'vserver-name': vserver_name,
  153. },
  154. },
  155. 'desired-attributes': {
  156. 'vserver-info': {
  157. 'vserver-name': None,
  158. },
  159. },
  160. }
  161. result = self.send_iter_request('vserver-get-iter', api_args)
  162. return self._has_records(result)
  163. @na_utils.trace
  164. def get_vserver_root_volume_name(self, vserver_name):
  165. """Get the root volume name of the vserver."""
  166. api_args = {
  167. 'query': {
  168. 'vserver-info': {
  169. 'vserver-name': vserver_name,
  170. },
  171. },
  172. 'desired-attributes': {
  173. 'vserver-info': {
  174. 'root-volume': None,
  175. },
  176. },
  177. }
  178. vserver_info = self.send_iter_request('vserver-get-iter', api_args)
  179. try:
  180. root_volume_name = vserver_info.get_child_by_name(
  181. 'attributes-list').get_child_by_name(
  182. 'vserver-info').get_child_content('root-volume')
  183. except AttributeError:
  184. msg = _('Could not determine root volume name '
  185. 'for Vserver %s.') % vserver_name
  186. raise exception.NetAppException(msg)
  187. return root_volume_name
  188. @na_utils.trace
  189. def get_vserver_ipspace(self, vserver_name):
  190. """Get the IPspace of the vserver, or None if not supported."""
  191. if not self.features.IPSPACES:
  192. return None
  193. api_args = {
  194. 'query': {
  195. 'vserver-info': {
  196. 'vserver-name': vserver_name,
  197. },
  198. },
  199. 'desired-attributes': {
  200. 'vserver-info': {
  201. 'ipspace': None,
  202. },
  203. },
  204. }
  205. vserver_info = self.send_iter_request('vserver-get-iter', api_args)
  206. try:
  207. ipspace = vserver_info.get_child_by_name(
  208. 'attributes-list').get_child_by_name(
  209. 'vserver-info').get_child_content('ipspace')
  210. except AttributeError:
  211. msg = _('Could not determine IPspace for Vserver %s.')
  212. raise exception.NetAppException(msg % vserver_name)
  213. return ipspace
  214. @na_utils.trace
  215. def ipspace_has_data_vservers(self, ipspace_name):
  216. """Check whether an IPspace has any data Vservers assigned to it."""
  217. if not self.features.IPSPACES:
  218. return False
  219. api_args = {
  220. 'query': {
  221. 'vserver-info': {
  222. 'ipspace': ipspace_name,
  223. 'vserver-type': 'data'
  224. },
  225. },
  226. 'desired-attributes': {
  227. 'vserver-info': {
  228. 'vserver-name': None,
  229. },
  230. },
  231. }
  232. result = self.send_iter_request('vserver-get-iter', api_args)
  233. return self._has_records(result)
  234. @na_utils.trace
  235. def list_vservers(self, vserver_type='data'):
  236. """Get the names of vservers present, optionally filtered by type."""
  237. query = {
  238. 'vserver-info': {
  239. 'vserver-type': vserver_type,
  240. }
  241. } if vserver_type else None
  242. api_args = {
  243. 'desired-attributes': {
  244. 'vserver-info': {
  245. 'vserver-name': None,
  246. },
  247. },
  248. }
  249. if query:
  250. api_args['query'] = query
  251. result = self.send_iter_request('vserver-get-iter', api_args)
  252. vserver_info_list = result.get_child_by_name(
  253. 'attributes-list') or netapp_api.NaElement('none')
  254. return [vserver_info.get_child_content('vserver-name')
  255. for vserver_info in vserver_info_list.get_children()]
  256. @na_utils.trace
  257. def get_vserver_volume_count(self):
  258. """Get the number of volumes present on a cluster or vserver.
  259. Call this on a vserver client to see how many volumes exist
  260. on that vserver.
  261. """
  262. api_args = {
  263. 'desired-attributes': {
  264. 'volume-attributes': {
  265. 'volume-id-attributes': {
  266. 'name': None,
  267. },
  268. },
  269. },
  270. }
  271. volumes_data = self.send_iter_request('volume-get-iter', api_args)
  272. return self._get_record_count(volumes_data)
  273. @na_utils.trace
  274. def delete_vserver(self, vserver_name, vserver_client,
  275. security_services=None):
  276. """Delete Vserver.
  277. Checks if Vserver exists and does not have active shares.
  278. Offlines and destroys root volumes. Deletes Vserver.
  279. """
  280. if not self.vserver_exists(vserver_name):
  281. LOG.error(_LE("Vserver %s does not exist."), vserver_name)
  282. return
  283. root_volume_name = self.get_vserver_root_volume_name(vserver_name)
  284. volumes_count = vserver_client.get_vserver_volume_count()
  285. if volumes_count == 1:
  286. try:
  287. vserver_client.offline_volume(root_volume_name)
  288. except netapp_api.NaApiError as e:
  289. if e.code == netapp_api.EVOLUMEOFFLINE:
  290. LOG.error(_LE("Volume %s is already offline."),
  291. root_volume_name)
  292. else:
  293. raise
  294. vserver_client.delete_volume(root_volume_name)
  295. elif volumes_count > 1:
  296. msg = _("Cannot delete Vserver. Vserver %s has shares.")
  297. raise exception.NetAppException(msg % vserver_name)
  298. if security_services:
  299. self._terminate_vserver_services(vserver_name, vserver_client,
  300. security_services)
  301. self.send_request('vserver-destroy', {'vserver-name': vserver_name})
  302. @na_utils.trace
  303. def _terminate_vserver_services(self, vserver_name, vserver_client,
  304. security_services):
  305. for service in security_services:
  306. if service['type'] == 'active_directory':
  307. api_args = {
  308. 'admin-password': service['password'],
  309. 'admin-username': service['user'],
  310. }
  311. try:
  312. vserver_client.send_request('cifs-server-delete', api_args)
  313. except netapp_api.NaApiError as e:
  314. if e.code == netapp_api.EOBJECTNOTFOUND:
  315. LOG.error(_LE('CIFS server does not exist for '
  316. 'Vserver %s.'), vserver_name)
  317. else:
  318. vserver_client.send_request('cifs-server-delete')
  319. @na_utils.trace
  320. def list_cluster_nodes(self):
  321. """Get all available cluster nodes."""
  322. api_args = {
  323. 'desired-attributes': {
  324. 'node-details-info': {
  325. 'node': None,
  326. },
  327. },
  328. }
  329. result = self.send_iter_request('system-node-get-iter', api_args)
  330. nodes_info_list = result.get_child_by_name(
  331. 'attributes-list') or netapp_api.NaElement('none')
  332. return [node_info.get_child_content('node') for node_info
  333. in nodes_info_list.get_children()]
  334. @na_utils.trace
  335. def list_node_data_ports(self, node):
  336. ports = self.get_node_data_ports(node)
  337. return [port.get('port') for port in ports]
  338. @na_utils.trace
  339. def get_node_data_ports(self, node):
  340. """Get applicable data ports on the node."""
  341. api_args = {
  342. 'query': {
  343. 'net-port-info': {
  344. 'node': node,
  345. 'link-status': 'up',
  346. 'port-type': 'physical|if_group',
  347. 'role': 'data',
  348. },
  349. },
  350. 'desired-attributes': {
  351. 'net-port-info': {
  352. 'port': None,
  353. 'node': None,
  354. 'operational-speed': None,
  355. 'ifgrp-port': None,
  356. },
  357. },
  358. }
  359. result = self.send_iter_request('net-port-get-iter', api_args)
  360. net_port_info_list = result.get_child_by_name(
  361. 'attributes-list') or netapp_api.NaElement('none')
  362. ports = []
  363. for port_info in net_port_info_list.get_children():
  364. # Skip physical ports that are part of interface groups.
  365. if port_info.get_child_content('ifgrp-port'):
  366. continue
  367. port = {
  368. 'node': port_info.get_child_content('node'),
  369. 'port': port_info.get_child_content('port'),
  370. 'speed': port_info.get_child_content('operational-speed'),
  371. }
  372. ports.append(port)
  373. return self._sort_data_ports_by_speed(ports)
  374. @na_utils.trace
  375. def _sort_data_ports_by_speed(self, ports):
  376. def sort_key(port):
  377. value = port.get('speed')
  378. if not (value and isinstance(value, six.string_types)):
  379. return 0
  380. elif value.isdigit():
  381. return int(value)
  382. elif value == 'auto':
  383. return 3
  384. elif value == 'undef':
  385. return 2
  386. else:
  387. return 1
  388. return sorted(ports, key=sort_key, reverse=True)
  389. @na_utils.trace
  390. def list_root_aggregates(self):
  391. """Get names of all aggregates that contain node root volumes."""
  392. desired_attributes = {
  393. 'aggr-attributes': {
  394. 'aggregate-name': None,
  395. 'aggr-raid-attributes': {
  396. 'has-local-root': None,
  397. 'has-partner-root': None,
  398. },
  399. },
  400. }
  401. aggrs = self._get_aggregates(desired_attributes=desired_attributes)
  402. root_aggregates = []
  403. for aggr in aggrs:
  404. aggr_name = aggr.get_child_content('aggregate-name')
  405. aggr_raid_attrs = aggr.get_child_by_name('aggr-raid-attributes')
  406. local_root = strutils.bool_from_string(
  407. aggr_raid_attrs.get_child_content('has-local-root'))
  408. partner_root = strutils.bool_from_string(
  409. aggr_raid_attrs.get_child_content('has-partner-root'))
  410. if local_root or partner_root:
  411. root_aggregates.append(aggr_name)
  412. return root_aggregates
  413. @na_utils.trace
  414. def list_non_root_aggregates(self):
  415. """Get names of all aggregates that don't contain node root volumes."""
  416. query = {
  417. 'aggr-attributes': {
  418. 'aggr-raid-attributes': {
  419. 'has-local-root': 'false',
  420. 'has-partner-root': 'false',
  421. }
  422. },
  423. }
  424. return self._list_aggregates(query=query)
  425. @na_utils.trace
  426. def _list_aggregates(self, query=None):
  427. """Get names of all aggregates."""
  428. try:
  429. api_args = {
  430. 'desired-attributes': {
  431. 'aggr-attributes': {
  432. 'aggregate-name': None,
  433. },
  434. },
  435. }
  436. if query:
  437. api_args['query'] = query
  438. result = self.send_iter_request('aggr-get-iter', api_args)
  439. aggr_list = result.get_child_by_name(
  440. 'attributes-list').get_children()
  441. except AttributeError:
  442. msg = _("Could not list aggregates.")
  443. raise exception.NetAppException(msg)
  444. return [aggr.get_child_content('aggregate-name') for aggr
  445. in aggr_list]
  446. @na_utils.trace
  447. def list_vserver_aggregates(self):
  448. """Returns a list of aggregates available to a vserver.
  449. This must be called against a Vserver LIF.
  450. """
  451. return list(self.get_vserver_aggregate_capacities().keys())
  452. @na_utils.trace
  453. def create_network_interface(self, ip, netmask, vlan, node, port,
  454. vserver_name, lif_name, ipspace_name, mtu):
  455. """Creates LIF on VLAN port."""
  456. home_port_name = port
  457. if vlan:
  458. self._create_vlan(node, port, vlan)
  459. home_port_name = '%(port)s-%(tag)s' % {'port': port, 'tag': vlan}
  460. if self.features.BROADCAST_DOMAINS:
  461. self._ensure_broadcast_domain_for_port(
  462. node, home_port_name, mtu, ipspace=ipspace_name)
  463. LOG.debug('Creating LIF %(lif)s for Vserver %(vserver)s ',
  464. {'lif': lif_name, 'vserver': vserver_name})
  465. api_args = {
  466. 'address': ip,
  467. 'administrative-status': 'up',
  468. 'data-protocols': [
  469. {'data-protocol': 'nfs'},
  470. {'data-protocol': 'cifs'},
  471. ],
  472. 'home-node': node,
  473. 'home-port': home_port_name,
  474. 'netmask': netmask,
  475. 'interface-name': lif_name,
  476. 'role': 'data',
  477. 'vserver': vserver_name,
  478. }
  479. self.send_request('net-interface-create', api_args)
  480. @na_utils.trace
  481. def _create_vlan(self, node, port, vlan):
  482. try:
  483. api_args = {
  484. 'vlan-info': {
  485. 'parent-interface': port,
  486. 'node': node,
  487. 'vlanid': vlan,
  488. },
  489. }
  490. self.send_request('net-vlan-create', api_args)
  491. except netapp_api.NaApiError as e:
  492. if e.code == netapp_api.EDUPLICATEENTRY:
  493. LOG.debug('VLAN %(vlan)s already exists on port %(port)s',
  494. {'vlan': vlan, 'port': port})
  495. else:
  496. msg = _('Failed to create VLAN %(vlan)s on '
  497. 'port %(port)s. %(err_msg)s')
  498. msg_args = {'vlan': vlan, 'port': port, 'err_msg': e.message}
  499. raise exception.NetAppException(msg % msg_args)
  500. @na_utils.trace
  501. def delete_vlan(self, node, port, vlan):
  502. try:
  503. api_args = {
  504. 'vlan-info': {
  505. 'parent-interface': port,
  506. 'node': node,
  507. 'vlanid': vlan,
  508. },
  509. }
  510. self.send_request('net-vlan-delete', api_args)
  511. except netapp_api.NaApiError as e:
  512. p = re.compile('port already has a lif bound.*', re.IGNORECASE)
  513. if (e.code == netapp_api.EAPIERROR and re.match(p, e.message)):
  514. LOG.debug('VLAN %(vlan)s on port %(port)s node %(node)s '
  515. 'still used by LIF and cannot be deleted.',
  516. {'vlan': vlan, 'port': port, 'node': node})
  517. else:
  518. msg = _('Failed to delete VLAN %(vlan)s on '
  519. 'port %(port)s node %(node)s: %(err_msg)s')
  520. msg_args = {
  521. 'vlan': vlan,
  522. 'port': port,
  523. 'node': node,
  524. 'err_msg': e.message
  525. }
  526. raise exception.NetAppException(msg % msg_args)
  527. @na_utils.trace
  528. def create_route(self, gateway, destination='0.0.0.0/0'):
  529. try:
  530. api_args = {
  531. 'destination': destination,
  532. 'gateway': gateway,
  533. 'return-record': 'true',
  534. }
  535. self.send_request('net-routes-create', api_args)
  536. except netapp_api.NaApiError as e:
  537. p = re.compile('.*Duplicate route exists.*', re.IGNORECASE)
  538. if (e.code == netapp_api.EAPIERROR and re.match(p, e.message)):
  539. LOG.debug('Route to %(destination)s via gateway %(gateway)s '
  540. 'exists.',
  541. {'destination': destination, 'gateway': gateway})
  542. else:
  543. msg = _('Failed to create a route to %(destination)s via '
  544. 'gateway %(gateway)s: %(err_msg)s')
  545. msg_args = {
  546. 'destination': destination,
  547. 'gateway': gateway,
  548. 'err_msg': e.message,
  549. }
  550. raise exception.NetAppException(msg % msg_args)
  551. @na_utils.trace
  552. def _ensure_broadcast_domain_for_port(self, node, port, mtu,
  553. ipspace=DEFAULT_IPSPACE):
  554. """Ensure a port is in a broadcast domain. Create one if necessary.
  555. If the IPspace:domain pair match for the given port, which commonly
  556. happens in multi-node clusters, then there isn't anything to do.
  557. Otherwise, we can assume the IPspace is correct and extant by this
  558. point, so the remaining task is to remove the port from any domain it
  559. is already in, create the domain for the IPspace if it doesn't exist,
  560. and add the port to this domain.
  561. """
  562. # Derive the broadcast domain name from the IPspace name since they
  563. # need to be 1-1 and the default for both is the same name, 'Default'.
  564. domain = re.sub(r'ipspace', 'domain', ipspace)
  565. port_info = self._get_broadcast_domain_for_port(node, port)
  566. # Port already in desired ipspace and broadcast domain.
  567. if (port_info['ipspace'] == ipspace
  568. and port_info['broadcast-domain'] == domain):
  569. self._modify_broadcast_domain(domain, ipspace, mtu)
  570. return
  571. # If in another broadcast domain, remove port from it.
  572. if port_info['broadcast-domain']:
  573. self._remove_port_from_broadcast_domain(
  574. node, port, port_info['broadcast-domain'],
  575. port_info['ipspace'])
  576. # If desired broadcast domain doesn't exist, create it.
  577. if not self._broadcast_domain_exists(domain, ipspace):
  578. self._create_broadcast_domain(domain, ipspace, mtu)
  579. else:
  580. self._modify_broadcast_domain(domain, ipspace, mtu)
  581. # Move the port into the broadcast domain where it is needed.
  582. self._add_port_to_broadcast_domain(node, port, domain, ipspace)
  583. @na_utils.trace
  584. def _get_broadcast_domain_for_port(self, node, port):
  585. """Get broadcast domain for a specific port."""
  586. api_args = {
  587. 'query': {
  588. 'net-port-info': {
  589. 'node': node,
  590. 'port': port,
  591. },
  592. },
  593. 'desired-attributes': {
  594. 'net-port-info': {
  595. 'broadcast-domain': None,
  596. 'ipspace': None,
  597. },
  598. },
  599. }
  600. result = self.send_iter_request('net-port-get-iter', api_args)
  601. net_port_info_list = result.get_child_by_name(
  602. 'attributes-list') or netapp_api.NaElement('none')
  603. port_info = net_port_info_list.get_children()
  604. if not port_info:
  605. msg = _('Could not find port %(port)s on node %(node)s.')
  606. msg_args = {'port': port, 'node': node}
  607. raise exception.NetAppException(msg % msg_args)
  608. port = {
  609. 'broadcast-domain':
  610. port_info[0].get_child_content('broadcast-domain'),
  611. 'ipspace': port_info[0].get_child_content('ipspace')
  612. }
  613. return port
  614. @na_utils.trace
  615. def _broadcast_domain_exists(self, domain, ipspace):
  616. """Check if a broadcast domain exists."""
  617. api_args = {
  618. 'query': {
  619. 'net-port-broadcast-domain-info': {
  620. 'ipspace': ipspace,
  621. 'broadcast-domain': domain,
  622. },
  623. },
  624. 'desired-attributes': {
  625. 'net-port-broadcast-domain-info': None,
  626. },
  627. }
  628. result = self.send_iter_request('net-port-broadcast-domain-get-iter',
  629. api_args)
  630. return self._has_records(result)
  631. @na_utils.trace
  632. def _create_broadcast_domain(self, domain, ipspace, mtu):
  633. """Create a broadcast domain."""
  634. api_args = {
  635. 'ipspace': ipspace,
  636. 'broadcast-domain': domain,
  637. 'mtu': mtu,
  638. }
  639. self.send_request('net-port-broadcast-domain-create', api_args)
  640. @na_utils.trace
  641. def _modify_broadcast_domain(self, domain, ipspace, mtu):
  642. """Modify a broadcast domain."""
  643. api_args = {
  644. 'ipspace': ipspace,
  645. 'broadcast-domain': domain,
  646. 'mtu': mtu,
  647. }
  648. self.send_request('net-port-broadcast-domain-modify', api_args)
  649. @na_utils.trace
  650. def _delete_broadcast_domain(self, domain, ipspace):
  651. """Delete a broadcast domain."""
  652. api_args = {
  653. 'ipspace': ipspace,
  654. 'broadcast-domain': domain,
  655. }
  656. self.send_request('net-port-broadcast-domain-destroy', api_args)
  657. @na_utils.trace
  658. def _delete_broadcast_domains_for_ipspace(self, ipspace_name):
  659. """Deletes all broadcast domains in an IPspace."""
  660. ipspaces = self.get_ipspaces(ipspace_name=ipspace_name)
  661. if not ipspaces:
  662. return
  663. ipspace = ipspaces[0]
  664. for broadcast_domain_name in ipspace['broadcast-domains']:
  665. self._delete_broadcast_domain(broadcast_domain_name, ipspace_name)
  666. @na_utils.trace
  667. def _add_port_to_broadcast_domain(self, node, port, domain, ipspace):
  668. qualified_port_name = ':'.join([node, port])
  669. try:
  670. api_args = {
  671. 'ipspace': ipspace,
  672. 'broadcast-domain': domain,
  673. 'ports': {
  674. 'net-qualified-port-name': qualified_port_name,
  675. }
  676. }
  677. self.send_request('net-port-broadcast-domain-add-ports', api_args)
  678. except netapp_api.NaApiError as e:
  679. if e.code == (netapp_api.
  680. E_VIFMGR_PORT_ALREADY_ASSIGNED_TO_BROADCAST_DOMAIN):
  681. LOG.debug('Port %(port)s already exists in broadcast domain '
  682. '%(domain)s', {'port': port, 'domain': domain})
  683. else:
  684. msg = _('Failed to add port %(port)s to broadcast domain '
  685. '%(domain)s. %(err_msg)s')
  686. msg_args = {
  687. 'port': qualified_port_name,
  688. 'domain': domain,
  689. 'err_msg': e.message,
  690. }
  691. raise exception.NetAppException(msg % msg_args)
  692. @na_utils.trace
  693. def _remove_port_from_broadcast_domain(self, node, port, domain, ipspace):
  694. qualified_port_name = ':'.join([node, port])
  695. api_args = {
  696. 'ipspace': ipspace,
  697. 'broadcast-domain': domain,
  698. 'ports': {
  699. 'net-qualified-port-name': qualified_port_name,
  700. }
  701. }
  702. self.send_request('net-port-broadcast-domain-remove-ports', api_args)
  703. @na_utils.trace
  704. def network_interface_exists(self, vserver_name, node, port, ip, netmask,
  705. vlan):
  706. """Checks if LIF exists."""
  707. home_port_name = (port if not vlan else
  708. '%(port)s-%(tag)s' % {'port': port, 'tag': vlan})
  709. api_args = {
  710. 'query': {
  711. 'net-interface-info': {
  712. 'address': ip,
  713. 'home-node': node,
  714. 'home-port': home_port_name,
  715. 'netmask': netmask,
  716. 'vserver': vserver_name,
  717. },
  718. },
  719. 'desired-attributes': {
  720. 'net-interface-info': {
  721. 'interface-name': None,
  722. },
  723. },
  724. }
  725. result = self.send_iter_request('net-interface-get-iter', api_args)
  726. return self._has_records(result)
  727. @na_utils.trace
  728. def list_network_interfaces(self):
  729. """Get the names of available LIFs."""
  730. api_args = {
  731. 'desired-attributes': {
  732. 'net-interface-info': {
  733. 'interface-name': None,
  734. },
  735. },
  736. }
  737. result = self.send_iter_request('net-interface-get-iter', api_args)
  738. lif_info_list = result.get_child_by_name(
  739. 'attributes-list') or netapp_api.NaElement('none')
  740. return [lif_info.get_child_content('interface-name') for lif_info
  741. in lif_info_list.get_children()]
  742. @na_utils.trace
  743. def get_network_interfaces(self, protocols=None):
  744. """Get available LIFs."""
  745. protocols = na_utils.convert_to_list(protocols)
  746. protocols = [protocol.lower() for protocol in protocols]
  747. api_args = {
  748. 'query': {
  749. 'net-interface-info': {
  750. 'data-protocols': {
  751. 'data-protocol': '|'.join(protocols),
  752. }
  753. }
  754. }
  755. } if protocols else None
  756. result = self.send_iter_request('net-interface-get-iter', api_args)
  757. lif_info_list = result.get_child_by_name(
  758. 'attributes-list') or netapp_api.NaElement('none')
  759. interfaces = []
  760. for lif_info in lif_info_list.get_children():
  761. lif = {
  762. 'address': lif_info.get_child_content('address'),
  763. 'home-node': lif_info.get_child_content('home-node'),
  764. 'home-port': lif_info.get_child_content('home-port'),
  765. 'interface-name': lif_info.get_child_content('interface-name'),
  766. 'netmask': lif_info.get_child_content('netmask'),
  767. 'role': lif_info.get_child_content('role'),
  768. 'vserver': lif_info.get_child_content('vserver'),
  769. }
  770. interfaces.append(lif)
  771. return interfaces
  772. @na_utils.trace
  773. def get_ipspaces(self, ipspace_name=None):
  774. """Gets one or more IPSpaces."""
  775. if not self.features.IPSPACES:
  776. return []
  777. api_args = {}
  778. if ipspace_name:
  779. api_args['query'] = {
  780. 'net-ipspaces-info': {
  781. 'ipspace': ipspace_name,
  782. }
  783. }
  784. result = self.send_iter_request('net-ipspaces-get-iter', api_args)
  785. if not self._has_records(result):
  786. return []
  787. ipspaces = []
  788. for net_ipspaces_info in result.get_child_by_name(
  789. 'attributes-list').get_children():
  790. ipspace = {
  791. 'ports': [],
  792. 'vservers': [],
  793. 'broadcast-domains': [],
  794. }
  795. ports = net_ipspaces_info.get_child_by_name(
  796. 'ports') or netapp_api.NaElement('none')
  797. for port in ports.get_children():
  798. ipspace['ports'].append(port.get_content())
  799. vservers = net_ipspaces_info.get_child_by_name(
  800. 'vservers') or netapp_api.NaElement('none')
  801. for vserver in vservers.get_children():
  802. ipspace['vservers'].append(vserver.get_content())
  803. broadcast_domains = net_ipspaces_info.get_child_by_name(
  804. 'broadcast-domains') or netapp_api.NaElement('none')
  805. for broadcast_domain in broadcast_domains.get_children():
  806. ipspace['broadcast-domains'].append(
  807. broadcast_domain.get_content())
  808. ipspace['ipspace'] = net_ipspaces_info.get_child_content('ipspace')
  809. ipspace['id'] = net_ipspaces_info.get_child_content('id')
  810. ipspace['uuid'] = net_ipspaces_info.get_child_content('uuid')
  811. ipspaces.append(ipspace)
  812. return ipspaces
  813. @na_utils.trace
  814. def ipspace_exists(self, ipspace_name):
  815. """Checks if IPspace exists."""
  816. if not self.features.IPSPACES:
  817. return False
  818. api_args = {
  819. 'query': {
  820. 'net-ipspaces-info': {
  821. 'ipspace': ipspace_name,
  822. },
  823. },
  824. 'desired-attributes': {
  825. 'net-ipspaces-info': {
  826. 'ipspace': None,
  827. },
  828. },
  829. }
  830. result = self.send_iter_request('net-ipspaces-get-iter', api_args)
  831. return self._has_records(result)
  832. @na_utils.trace
  833. def create_ipspace(self, ipspace_name):
  834. """Creates an IPspace."""
  835. api_args = {'ipspace': ipspace_name}
  836. self.send_request('net-ipspaces-create', api_args)
  837. @na_utils.trace
  838. def delete_ipspace(self, ipspace_name):
  839. """Deletes an IPspace."""
  840. self._delete_broadcast_domains_for_ipspace(ipspace_name)
  841. api_args = {'ipspace': ipspace_name}
  842. self.send_request('net-ipspaces-destroy', api_args)
  843. @na_utils.trace
  844. def add_vserver_to_ipspace(self, ipspace_name, vserver_name):
  845. """Assigns a vserver to an IPspace."""
  846. api_args = {'ipspace': ipspace_name, 'vserver': vserver_name}
  847. self.send_request('net-ipspaces-assign-vserver', api_args)
  848. @na_utils.trace
  849. def get_node_for_aggregate(self, aggregate_name):
  850. """Get home node for the specified aggregate.
  851. This API could return None, most notably if it was sent
  852. to a Vserver LIF, so the caller must be able to handle that case.
  853. """
  854. if not aggregate_name:
  855. return None
  856. desired_attributes = {
  857. 'aggr-attributes': {
  858. 'aggregate-name': None,
  859. 'aggr-ownership-attributes': {
  860. 'home-name': None,
  861. },
  862. },
  863. }
  864. try:
  865. aggrs = self._get_aggregates(aggregate_names=[aggregate_name],
  866. desired_attributes=desired_attributes)
  867. except netapp_api.NaApiError as e:
  868. if e.code == netapp_api.EAPINOTFOUND:
  869. return None
  870. else:
  871. raise
  872. if len(aggrs) < 1:
  873. return None
  874. aggr_ownership_attrs = aggrs[0].get_child_by_name(
  875. 'aggr-ownership-attributes') or netapp_api.NaElement('none')
  876. return aggr_ownership_attrs.get_child_content('home-name')
  877. @na_utils.trace
  878. def get_cluster_aggregate_capacities(self, aggregate_names):
  879. """Calculates capacity of one or more aggregates.
  880. Returns dictionary of aggregate capacity metrics.
  881. 'size-used' is the actual space consumed on the aggregate.
  882. 'size-available' is the actual space remaining.
  883. 'size-total' is the defined total aggregate size, such that
  884. used + available = total.
  885. """
  886. if aggregate_names is not None and len(aggregate_names) == 0:
  887. return {}
  888. desired_attributes = {
  889. 'aggr-attributes': {
  890. 'aggregate-name': None,
  891. 'aggr-space-attributes': {
  892. 'size-available': None,
  893. 'size-total': None,
  894. 'size-used': None,
  895. },
  896. },
  897. }
  898. aggrs = self._get_aggregates(aggregate_names=aggregate_names,
  899. desired_attributes=desired_attributes)
  900. aggr_space_dict = dict()
  901. for aggr in aggrs:
  902. aggr_name = aggr.get_child_content('aggregate-name')
  903. aggr_space_attrs = aggr.get_child_by_name('aggr-space-attributes')
  904. aggr_space_dict[aggr_name] = {
  905. 'available':
  906. int(aggr_space_attrs.get_child_content('size-available')),
  907. 'total':
  908. int(aggr_space_attrs.get_child_content('size-total')),
  909. 'used':
  910. int(aggr_space_attrs.get_child_content('size-used')),
  911. }
  912. return aggr_space_dict
  913. @na_utils.trace
  914. def get_vserver_aggregate_capacities(self, aggregate_names=None):
  915. """Calculates capacity of one or more aggregates for a vserver.
  916. Returns dictionary of aggregate capacity metrics. This must
  917. be called against a Vserver LIF.
  918. """
  919. if aggregate_names is not None and len(aggregate_names) == 0:
  920. return {}
  921. api_args = {
  922. 'desired-attributes': {
  923. 'vserver-info': {
  924. 'vserver-name': None,
  925. 'vserver-aggr-info-list': {
  926. 'vserver-aggr-info': {
  927. 'aggr-name': None,
  928. 'aggr-availsize': None,
  929. },
  930. },
  931. },
  932. },
  933. }
  934. result = self.send_request('vserver-get', api_args)
  935. attributes = result.get_child_by_name('attributes')
  936. if not attributes:
  937. raise exception.NetAppException('Failed to read Vserver info')
  938. vserver_info = attributes.get_child_by_name('vserver-info')
  939. vserver_name = vserver_info.get_child_content('vserver-name')
  940. vserver_aggr_info_element = vserver_info.get_child_by_name(
  941. 'vserver-aggr-info-list') or netapp_api.NaElement('none')
  942. vserver_aggr_info_list = vserver_aggr_info_element.get_children()
  943. if not vserver_aggr_info_list:
  944. LOG.warning(_LW('No aggregates assigned to Vserver %s.'),
  945. vserver_name)
  946. # Return dict of key-value pair of aggr_name:aggr_size_available.
  947. aggr_space_dict = {}
  948. for aggr_info in vserver_aggr_info_list:
  949. aggr_name = aggr_info.get_child_content('aggr-name')
  950. if aggregate_names is None or aggr_name in aggregate_names:
  951. aggr_size = int(aggr_info.get_child_content('aggr-availsize'))
  952. aggr_space_dict[aggr_name] = {'available': aggr_size}
  953. LOG.debug('Found available Vserver aggregates: %s', aggr_space_dict)
  954. return aggr_space_dict
  955. @na_utils.trace
  956. def _get_aggregates(self, aggregate_names=None, desired_attributes=None):
  957. query = {
  958. 'aggr-attributes': {
  959. 'aggregate-name': '|'.join(aggregate_names),
  960. }
  961. } if aggregate_names else None
  962. api_args = {}
  963. if query:
  964. api_args['query'] = query
  965. if desired_attributes:
  966. api_args['desired-attributes'] = desired_attributes
  967. result = self.send_iter_request('aggr-get-iter', api_args)
  968. if not self._has_records(result):
  969. return []
  970. else:
  971. return result.get_child_by_name('attributes-list').get_children()
  972. def get_performance_instance_uuids(self, object_name, node_name):
  973. """Get UUIDs of performance instances for a cluster node."""
  974. api_args = {
  975. 'objectname': object_name,
  976. 'query': {
  977. 'instance-info': {
  978. 'uuid': node_name + ':*',
  979. }
  980. }
  981. }
  982. result = self.send_request('perf-object-instance-list-info-iter',
  983. api_args)
  984. uuids = []
  985. instances = result.get_child_by_name(
  986. 'attributes-list') or netapp_api.NaElement('None')
  987. for instance_info in instances.get_children():
  988. uuids.append(instance_info.get_child_content('uuid'))
  989. return uuids
  990. def get_performance_counter_info(self, object_name, counter_name):
  991. """Gets info about one or more Data ONTAP performance counters."""
  992. api_args = {'objectname': object_name}
  993. result = self.send_request('perf-object-counter-list-info', api_args)
  994. counters = result.get_child_by_name(
  995. 'counters') or netapp_api.NaElement('None')
  996. for counter in counters.get_children():
  997. if counter.get_child_content('name') == counter_name:
  998. labels = []
  999. label_list = counter.get_child_by_name(
  1000. 'labels') or netapp_api.NaElement('None')
  1001. for label in label_list.get_children():
  1002. labels.extend(label.get_content().split(','))
  1003. base_counter = counter.get_child_content('base-counter')
  1004. return {
  1005. 'name': counter_name,
  1006. 'labels': labels,
  1007. 'base-counter': base_counter,
  1008. }
  1009. else:
  1010. raise exception.NotFound(_('Counter %s not found') % counter_name)
  1011. def get_performance_counters(self, object_name, instance_uuids,
  1012. counter_names):
  1013. """Gets one or more cDOT performance counters."""
  1014. api_args = {
  1015. 'objectname': object_name,
  1016. 'instance-uuids': [
  1017. {'instance-uuid': instance_uuid}
  1018. for instance_uuid in instance_uuids
  1019. ],
  1020. 'counters': [
  1021. {'counter': counter} for counter in counter_names
  1022. ],
  1023. }
  1024. result = self.send_request('perf-object-get-instances', api_args)
  1025. counter_data = []
  1026. timestamp = result.get_child_content('timestamp')
  1027. instances = result.get_child_by_name(
  1028. 'instances') or netapp_api.NaElement('None')
  1029. for instance in instances.get_children():
  1030. instance_name = instance.get_child_content('name')
  1031. instance_uuid = instance.get_child_content('uuid')
  1032. node_name = instance_uuid.split(':')[0]
  1033. counters = instance.get_child_by_name(
  1034. 'counters') or netapp_api.NaElement('None')
  1035. for counter in counters.get_children():
  1036. counter_name = counter.get_child_content('name')
  1037. counter_value = counter.get_child_content('value')
  1038. counter_data.append({
  1039. 'instance-name': instance_name,
  1040. 'instance-uuid': instance_uuid,
  1041. 'node-name': node_name,
  1042. 'timestamp': timestamp,
  1043. counter_name: counter_value,
  1044. })
  1045. return counter_data
  1046. @na_utils.trace
  1047. def setup_security_services(self, security_services, vserver_client,
  1048. vserver_name):
  1049. api_args = {
  1050. 'name-mapping-switch': [
  1051. {'nmswitch': 'ldap'},
  1052. {'nmswitch': 'file'}
  1053. ],
  1054. 'name-server-switch': [
  1055. {'nsswitch': 'ldap'},
  1056. {'nsswitch': 'file'}
  1057. ],
  1058. 'vserver-name': vserver_name,
  1059. }
  1060. self.send_request('vserver-modify', api_args)
  1061. for security_service in security_services:
  1062. if security_service['type'].lower() == 'ldap':
  1063. vserver_client.configure_ldap(security_service)
  1064. elif security_service['type'].lower() == 'active_directory':
  1065. vserver_client.configure_active_directory(security_service,
  1066. vserver_name)
  1067. elif security_service['type'].lower() == 'kerberos':
  1068. self.create_kerberos_realm(security_service)
  1069. vserver_client.configure_kerberos(security_service,
  1070. vserver_name)
  1071. else:
  1072. msg = _('Unsupported security service type %s for '
  1073. 'Data ONTAP driver')
  1074. raise exception.NetAppException(msg % security_service['type'])
  1075. @na_utils.trace
  1076. def enable_nfs(self, versions):
  1077. """Enables NFS on Vserver."""
  1078. self.send_request('nfs-enable')
  1079. self._enable_nfs_protocols(versions)
  1080. self._create_default_nfs_export_rule()
  1081. @na_utils.trace
  1082. def _enable_nfs_protocols(self, versions):
  1083. """Set the enabled NFS protocol versions."""
  1084. nfs3 = 'true' if 'nfs3' in versions else 'false'
  1085. nfs40 = 'true' if 'nfs4.0' in versions else 'false'
  1086. nfs41 = 'true' if 'nfs4.1' in versions else 'false'
  1087. nfs_service_modify_args = {
  1088. 'is-nfsv3-enabled': nfs3,
  1089. 'is-nfsv40-enabled': nfs40,
  1090. 'is-nfsv41-enabled': nfs41,
  1091. }
  1092. self.send_request('nfs-service-modify', nfs_service_modify_args)
  1093. @na_utils.trace
  1094. def _create_default_nfs_export_rule(self):
  1095. """Create the default export rule for the NFS service."""
  1096. export_rule_create_args = {
  1097. 'client-match': '0.0.0.0/0',
  1098. 'policy-name': 'default',
  1099. 'ro-rule': {
  1100. 'security-flavor': 'any',
  1101. },
  1102. 'rw-rule': {
  1103. 'security-flavor': 'never',
  1104. },
  1105. }
  1106. self.send_request('export-rule-create', export_rule_create_args)
  1107. @na_utils.trace
  1108. def configure_ldap(self, security_service):
  1109. """Configures LDAP on Vserver."""
  1110. config_name = hashlib.md5(six.b(security_service['id'])).hexdigest()
  1111. api_args = {
  1112. 'ldap-client-config': config_name,
  1113. 'servers': {
  1114. 'ip-address': security_service['server'],
  1115. },
  1116. 'tcp-port': '389',
  1117. 'schema': 'RFC-2307',
  1118. 'bind-password': security_service['password'],
  1119. }
  1120. self.send_request('ldap-client-create', api_args)
  1121. api_args = {'client-config': config_name, 'client-enabled': 'true'}
  1122. self.send_request('ldap-config-create', api_args)
  1123. @na_utils.trace
  1124. def configure_active_directory(self, security_service, vserver_name):
  1125. """Configures AD on Vserver."""
  1126. self.configure_dns(security_service)
  1127. # 'cifs-server' is CIFS Server NetBIOS Name, max length is 15.
  1128. # Should be unique within each domain (data['domain']).
  1129. cifs_server = (vserver_name[0:7] + '..' + vserver_name[-6:]).upper()
  1130. api_args = {
  1131. 'admin-username': security_service['user'],
  1132. 'admin-password': security_service['password'],
  1133. 'force-account-overwrite': 'true',
  1134. 'cifs-server': cifs_server,
  1135. 'domain': security_service['domain'],
  1136. }
  1137. try:
  1138. LOG.debug("Trying to setup CIFS server with data: %s", api_args)
  1139. self.send_request('cifs-server-create', api_args)
  1140. except netapp_api.NaApiError as e:
  1141. msg = _("Failed to create CIFS server entry. %s")
  1142. raise exception.NetAppException(msg % e.message)
  1143. @na_utils.trace
  1144. def create_kerberos_realm(self, security_service):
  1145. """Creates Kerberos realm on cluster."""
  1146. api_args = {
  1147. 'admin-server-ip': security_service['server'],
  1148. 'admin-server-port': '749',
  1149. 'clock-skew': '5',
  1150. 'comment': '',
  1151. 'config-name': security_service['id'],
  1152. 'kdc-ip': security_service['server'],
  1153. 'kdc-port': '88',
  1154. 'kdc-vendor': 'other',
  1155. 'password-server-ip': security_service['server'],
  1156. 'password-server-port': '464',
  1157. 'realm': security_service['domain'].upper(),
  1158. }
  1159. try:
  1160. self.send_request('kerberos-realm-create', api_args)
  1161. except netapp_api.NaApiError as e:
  1162. if e.code == netapp_api.EDUPLICATEENTRY:
  1163. LOG.debug('Kerberos realm config already exists.')
  1164. else:
  1165. msg = _('Failed to create Kerberos realm. %s')
  1166. raise exception.NetAppException(msg % e.message)
  1167. @na_utils.trace
  1168. def configure_kerberos(self, security_service, vserver_name):
  1169. """Configures Kerberos for NFS on Vserver."""
  1170. self.configure_dns(security_service)
  1171. spn = self._get_kerberos_service_principal_name(
  1172. security_service, vserver_name)
  1173. lifs = self.list_network_interfaces()
  1174. if not lifs:
  1175. msg = _("Cannot set up Kerberos. There are no LIFs configured.")
  1176. raise exception.NetAppException(msg)
  1177. for lif_name in lifs:
  1178. api_args = {
  1179. 'admin-password': security_service['password'],
  1180. 'admin-user-name': security_service['user'],
  1181. 'interface-name': lif_name,
  1182. 'is-kerberos-enabled': 'true',
  1183. 'service-principal-name': spn,
  1184. }
  1185. self.send_request('kerberos-config-modify', api_args)
  1186. @na_utils.trace
  1187. def _get_kerberos_service_principal_name(self, security_service,
  1188. vserver_name):
  1189. return 'nfs/' + vserver_name.replace('_', '-') + '.' + \
  1190. security_service['domain'] + '@' + \
  1191. security_service['domain'].upper()
  1192. @na_utils.trace
  1193. def configure_dns(self, security_service):
  1194. api_args = {
  1195. 'domains': {
  1196. 'string': security_service['domain'],
  1197. },
  1198. 'name-servers': {
  1199. 'ip-address': security_service['dns_ip'],
  1200. },
  1201. 'dns-state': 'enabled',
  1202. }
  1203. try:
  1204. self.send_request('net-dns-create', api_args)
  1205. except netapp_api.NaApiError as e:
  1206. if e.code == netapp_api.EDUPLICATEENTRY:
  1207. LOG.error(_LE("DNS exists for Vserver."))
  1208. else:
  1209. msg = _("Failed to configure DNS. %s")
  1210. raise exception.NetAppException(msg % e.message)
  1211. @na_utils.trace
  1212. def create_volume(self, aggregate_name, volume_name, size_gb,
  1213. thin_provisioned=False, snapshot_policy=None,
  1214. language=None, dedup_enabled=False,
  1215. compression_enabled=False, max_files=None,
  1216. snapshot_reserve=None, volume_type='rw', **options):
  1217. """Creates a volume."""
  1218. api_args = {
  1219. 'containing-aggr-name': aggregate_name,
  1220. 'size': six.text_type(size_gb) + 'g',
  1221. 'volume': volume_name,
  1222. 'volume-type': volume_type,
  1223. }
  1224. if volume_type != 'dp':
  1225. api_args['junction-path'] = '/%s' % volume_name
  1226. if thin_provisioned:
  1227. api_args['space-reserve'] = 'none'
  1228. if snapshot_policy is not None:
  1229. api_args['snapshot-policy'] = snapshot_policy
  1230. if language is not None:
  1231. api_args['language-code'] = language
  1232. if snapshot_reserve is not None:
  1233. api_args['percentage-snapshot-reserve'] = six.text_type(
  1234. snapshot_reserve)
  1235. self.send_request('volume-create', api_args)
  1236. # cDOT compression requires that deduplication be enabled.
  1237. if dedup_enabled or compression_enabled:
  1238. self.enable_dedup(volume_name)
  1239. if compression_enabled:
  1240. self.enable_compression(volume_name)
  1241. if max_files is not None:
  1242. self.set_volume_max_files(volume_name, max_files)
  1243. @na_utils.trace
  1244. def enable_dedup(self, volume_name):
  1245. """Enable deduplication on volume."""
  1246. api_args = {'path': '/vol/%s' % volume_name}
  1247. self.send_request('sis-enable', api_args)
  1248. @na_utils.trace
  1249. def disable_dedup(self, volume_name):
  1250. """Disable deduplication on volume."""
  1251. api_args = {'path': '/vol/%s' % volume_name}
  1252. self.send_request('sis-disable', api_args)
  1253. @na_utils.trace
  1254. def enable_compression(self, volume_name):
  1255. """Enable compression on volume."""
  1256. api_args = {
  1257. 'path': '/vol/%s' % volume_name,
  1258. 'enable-compression': 'true'
  1259. }
  1260. self.send_request('sis-set-config', api_args)
  1261. @na_utils.trace
  1262. def disable_compression(self, volume_name):
  1263. """Disable compression on volume."""
  1264. api_args = {
  1265. 'path': '/vol/%s' % volume_name,
  1266. 'enable-compression': 'false'
  1267. }
  1268. self.send_request('sis-set-config', api_args)
  1269. @na_utils.trace
  1270. def get_volume_efficiency_status(self, volume_name):
  1271. """Get dedupe & compression status for a volume."""
  1272. api_args = {
  1273. 'query': {
  1274. 'sis-status-info': {
  1275. 'path': '/vol/%s' % volume_name,
  1276. },
  1277. },
  1278. 'desired-attributes': {
  1279. 'sis-status-info': {
  1280. 'state': None,
  1281. 'is-compression-enabled': None,
  1282. },
  1283. },
  1284. }
  1285. result = self.send_iter_request('sis-get-iter', api_args)
  1286. attributes_list = result.get_child_by_name(
  1287. 'attributes-list') or netapp_api.NaElement('none')
  1288. sis_status_info = attributes_list.get_child_by_name(
  1289. 'sis-status-info') or netapp_api.NaElement('none')
  1290. return {
  1291. 'dedupe': True if 'enabled' == sis_status_info.get_child_content(
  1292. 'state') else False,
  1293. 'compression': True if 'true' == sis_status_info.get_child_content(
  1294. 'is-compression-enabled') else False,
  1295. }
  1296. @na_utils.trace
  1297. def set_volume_max_files(self, volume_name, max_files):
  1298. """Set flexvol file limit."""
  1299. api_args = {
  1300. 'query': {
  1301. 'volume-attributes': {
  1302. 'volume-id-attributes': {
  1303. 'name': volume_name,
  1304. },
  1305. },
  1306. },
  1307. 'attributes': {
  1308. 'volume-attributes': {
  1309. 'volume-inode-attributes': {
  1310. 'files-total': max_files,
  1311. },
  1312. },
  1313. },
  1314. }
  1315. self.send_request('volume-modify-iter', api_args)
  1316. @na_utils.trace
  1317. def set_volume_size(self, volume_name, size_gb):
  1318. """Set volume size."""
  1319. api_args = {
  1320. 'query': {
  1321. 'volume-attributes': {
  1322. 'volume-id-attributes': {
  1323. 'name': volume_name,
  1324. },
  1325. },
  1326. },
  1327. 'attributes': {
  1328. 'volume-attributes': {
  1329. 'volume-space-attributes': {
  1330. 'size': int(size_gb) * units.Gi,
  1331. },
  1332. },
  1333. },
  1334. }
  1335. result = self.send_request('volume-modify-iter', api_args)
  1336. failures = result.get_child_content('num-failed')
  1337. if failures and int(failures) > 0:
  1338. failure_list = result.get_child_by_name(
  1339. 'failure-list') or netapp_api.NaElement('none')
  1340. errors = failure_list.get_children()
  1341. if errors:
  1342. raise netapp_api.NaApiError(
  1343. errors[0].get_child_content('error-code'),
  1344. errors[0].get_child_content('error-message'))
  1345. @na_utils.trace
  1346. def set_volume_security_style(self, volume_name, security_style='unix'):
  1347. """Set volume security style"""
  1348. api_args = {
  1349. 'query': {
  1350. 'volume-attributes': {
  1351. 'volume-id-attributes': {
  1352. 'name': volume_name,
  1353. },
  1354. },
  1355. },
  1356. 'attributes': {
  1357. 'volume-attributes': {
  1358. 'volume-security-attributes': {
  1359. 'style': security_style,
  1360. },
  1361. },
  1362. },
  1363. }
  1364. result = self.send_request('volume-modify-iter', api_args)
  1365. failures = result.get_child_content('num-failed')
  1366. if failures and int(failures) > 0:
  1367. failure_list = result.get_child_by_name(
  1368. 'failure-list') or netapp_api.NaElement('none')
  1369. errors = failure_list.get_children()
  1370. if errors:
  1371. raise netapp_api.NaApiError(
  1372. errors[0].get_child_content('error-code'),
  1373. errors[0].get_child_content('error-message'))
  1374. @na_utils.trace
  1375. def set_volume_name(self, volume_name, new_volume_name):
  1376. """Set flexvol name."""
  1377. api_args = {
  1378. 'volume': volume_name,
  1379. 'new-volume-name': new_volume_name,
  1380. }
  1381. self.send_request('volume-rename', api_args)
  1382. @na_utils.trace
  1383. def manage_volume(self, aggregate_name, volume_name,
  1384. thin_provisioned=False, snapshot_policy=None,
  1385. language=None, dedup_enabled=False,
  1386. compression_enabled=False, max_files=None, **options):
  1387. """Update volume as needed to bring under management as a share."""
  1388. api_args = {
  1389. 'query': {
  1390. 'volume-attributes': {
  1391. 'volume-id-attributes': {
  1392. 'containing-aggregate-name': aggregate_name,
  1393. 'name': volume_name,
  1394. },
  1395. },
  1396. },
  1397. 'attributes': {
  1398. 'volume-attributes': {
  1399. 'volume-inode-attributes': {},
  1400. 'volume-language-attributes': {},
  1401. 'volume-snapshot-attributes': {},
  1402. 'volume-space-attributes': {
  1403. 'space-guarantee': ('none' if thin_provisioned else
  1404. 'volume')
  1405. },
  1406. },
  1407. },
  1408. }
  1409. if language:
  1410. api_args['attributes']['volume-attributes'][
  1411. 'volume-language-attributes']['language'] = language
  1412. if max_files:
  1413. api_args['attributes']['volume-attributes'][
  1414. 'volume-inode-attributes']['files-total'] = max_files
  1415. if snapshot_policy:
  1416. api_args['attributes']['volume-attributes'][
  1417. 'volume-snapshot-attributes'][
  1418. 'snapshot-policy'] = snapshot_policy
  1419. self.send_request('volume-modify-iter', api_args)
  1420. # Efficiency options must be handled separately
  1421. self.update_volume_efficiency_attributes(volume_name,
  1422. dedup_enabled,
  1423. compression_enabled)
  1424. @na_utils.trace
  1425. def update_volume_efficiency_attributes(self, volume_name, dedup_enabled,
  1426. compression_enabled):
  1427. """Update dedupe & compression attributes to match desired values."""
  1428. efficiency_status = self.get_volume_efficiency_status(volume_name)
  1429. if efficiency_status['compression'] != compression_enabled:
  1430. if compression_enabled:
  1431. self.enable_compression(volume_name)
  1432. else:
  1433. self.disable_compression(volume_name)
  1434. if efficiency_status['dedupe'] != dedup_enabled:
  1435. if dedup_enabled:
  1436. self.enable_dedup(volume_name)
  1437. else:
  1438. self.disable_dedup(volume_name)
  1439. @na_utils.trace
  1440. def volume_exists(self, volume_name):
  1441. """Checks if volume exists."""
  1442. LOG.debug('Checking if volume %s exists', volume_name)
  1443. api_args = {
  1444. 'query': {
  1445. 'volume-attributes': {
  1446. 'volume-id-attributes': {
  1447. 'name': volume_name,
  1448. },
  1449. },
  1450. },
  1451. 'desired-attributes': {
  1452. 'volume-attributes': {
  1453. 'volume-id-attributes': {
  1454. 'name': None,
  1455. },
  1456. },
  1457. },
  1458. }
  1459. result = self.send_iter_request('volume-get-iter', api_args)
  1460. return self._has_records(result)
  1461. @na_utils.trace
  1462. def get_aggregate_for_volume(self, volume_name):
  1463. """Get the name of the aggregate containing a volume."""
  1464. api_args = {
  1465. 'query': {
  1466. 'volume-attributes': {
  1467. 'volume-id-attributes': {
  1468. 'name': volume_name,
  1469. },
  1470. },
  1471. },
  1472. 'desired-attributes': {
  1473. 'volume-attributes': {
  1474. 'volume-id-attributes': {
  1475. 'containing-aggregate-name': None,
  1476. 'name': None,
  1477. },
  1478. },
  1479. },
  1480. }
  1481. result = self.send_iter_request('volume-get-iter', api_args)
  1482. attributes_list = result.get_child_by_name(
  1483. 'attributes-list') or netapp_api.NaElement('none')
  1484. volume_attributes = attributes_list.get_child_by_name(
  1485. 'volume-attributes') or netapp_api.NaElement('none')
  1486. volume_id_attributes = volume_attributes.get_child_by_name(
  1487. 'volume-id-attributes') or netapp_api.NaElement('none')
  1488. aggregate = volume_id_attributes.get_child_content(
  1489. 'containing-aggregate-name')
  1490. if not aggregate:
  1491. msg = _('Could not find aggregate for volume %s.')
  1492. raise exception.NetAppException(msg % volume_name)
  1493. return aggregate
  1494. @na_utils.trace
  1495. def volume_has_luns(self, volume_name):
  1496. """Checks if volume has LUNs."""
  1497. LOG.debug('Checking if volume %s has LUNs', volume_name)
  1498. api_args = {
  1499. 'query': {
  1500. 'lun-info': {
  1501. 'volume': volume_name,
  1502. },
  1503. },
  1504. 'desired-attributes': {
  1505. 'lun-info': {
  1506. 'path': None,
  1507. },
  1508. },
  1509. }
  1510. result = self.send_iter_request('lun-get-iter', api_args)
  1511. return self._has_records(result)
  1512. @na_utils.trace
  1513. def volume_has_junctioned_volumes(self, volume_name):
  1514. """Checks if volume has volumes mounted beneath its junction path."""
  1515. junction_path = self.get_volume_junction_path(volume_name)
  1516. if not junction_path:
  1517. return False
  1518. api_args = {
  1519. 'query': {
  1520. 'volume-attributes': {
  1521. 'volume-id-attributes': {
  1522. 'junction-path': junction_path + '/*',
  1523. },
  1524. },
  1525. },
  1526. 'desired-attributes': {
  1527. 'volume-attributes': {
  1528. 'volume-id-attributes': {
  1529. 'name': None,
  1530. },
  1531. },
  1532. },
  1533. }
  1534. result = self.send_iter_request('volume-get-iter', api_args)
  1535. return self._has_records(result)
  1536. @na_utils.trace
  1537. def get_volume(self, volume_name):
  1538. """Returns the volume with the specified name, if present."""
  1539. api_args = {
  1540. 'query': {
  1541. 'volume-attributes': {
  1542. 'volume-id-attributes': {
  1543. 'name': volume_name,
  1544. },
  1545. },
  1546. },
  1547. 'desired-attributes': {
  1548. 'volume-attributes': {
  1549. 'volume-id-attributes': {
  1550. 'containing-aggregate-name': None,
  1551. 'junction-path': None,
  1552. 'name': None,
  1553. 'owning-vserver-name': None,
  1554. 'type': None,
  1555. 'style': None,
  1556. },
  1557. 'volume-space-attributes': {
  1558. 'size': None,
  1559. }
  1560. },
  1561. },
  1562. }
  1563. result = self.send_request('volume-get-iter', api_args)
  1564. attributes_list = result.get_child_by_name(
  1565. 'attributes-list') or netapp_api.NaElement('none')
  1566. volume_attributes_list = attributes_list.get_children()
  1567. if not self._has_records(result):
  1568. raise exception.StorageResourceNotFound(name=volume_name)
  1569. elif len(volume_attributes_list) > 1:
  1570. msg = _('Could not find unique volume %(vol)s.')
  1571. msg_args = {'vol': volume_name}
  1572. raise exception.NetAppException(msg % msg_args)
  1573. volume_attributes = volume_attributes_list[0]
  1574. volume_id_attributes = volume_attributes.get_child_by_name(
  1575. 'volume-id-attributes') or netapp_api.NaElement('none')
  1576. volume_space_attributes = volume_attributes.get_child_by_name(
  1577. 'volume-space-attributes') or netapp_api.NaElement('none')
  1578. volume = {
  1579. 'aggregate': volume_id_attributes.get_child_content(
  1580. 'containing-aggregate-name'),
  1581. 'junction-path': volume_id_attributes.get_child_content(
  1582. 'junction-path'),
  1583. 'name': volume_id_attributes.get_child_content('name'),
  1584. 'owning-vserver-name': volume_id_attributes.get_child_content(
  1585. 'owning-vserver-name'),
  1586. 'type': volume_id_attributes.get_child_content('type'),
  1587. 'style': volume_id_attributes.get_child_content('style'),
  1588. 'size': volume_space_attributes.get_child_content('size'),
  1589. }
  1590. return volume
  1591. @na_utils.trace
  1592. def get_volume_at_junction_path(self, junction_path):
  1593. """Returns the volume with the specified junction path, if present."""
  1594. if not junction_path:
  1595. return None
  1596. api_args = {
  1597. 'query': {
  1598. 'volume-attributes': {
  1599. 'volume-id-attributes': {
  1600. 'junction-path': junction_path,
  1601. },
  1602. },
  1603. },
  1604. 'desired-attributes': {
  1605. 'volume-attributes': {
  1606. 'volume-id-attributes': {
  1607. 'containing-aggregate-name': None,
  1608. 'junction-path': None,
  1609. 'name': None,
  1610. 'type': None,
  1611. 'style': None,
  1612. },
  1613. 'volume-space-attributes': {
  1614. 'size': None,
  1615. }
  1616. },
  1617. },
  1618. }
  1619. result = self.send_iter_request('volume-get-iter', api_args)
  1620. if not self._has_records(result):
  1621. return None
  1622. attributes_list = result.get_child_by_name(
  1623. 'attributes-list') or netapp_api.NaElement('none')
  1624. volume_attributes = attributes_list.get_child_by_name(
  1625. 'volume-attributes') or netapp_api.NaElement('none')
  1626. volume_id_attributes = volume_attributes.get_child_by_name(
  1627. 'volume-id-attributes') or netapp_api.NaElement('none')
  1628. volume_space_attributes = volume_attributes.get_child_by_name(
  1629. 'volume-space-attributes') or netapp_api.NaElement('none')
  1630. volume = {
  1631. 'aggregate': volume_id_attributes.get_child_content(
  1632. 'containing-aggregate-name'),
  1633. 'junction-path': volume_id_attributes.get_child_content(
  1634. 'junction-path'),
  1635. 'name': volume_id_attributes.get_child_content('name'),
  1636. 'type': volume_id_attributes.get_child_content('type'),
  1637. 'style': volume_id_attributes.get_child_content('style'),
  1638. 'size': volume_space_attributes.get_child_content('size'),
  1639. }
  1640. return volume
  1641. @na_utils.trace
  1642. def get_volume_to_manage(self, aggregate_name, volume_name):
  1643. """Get flexvol to be managed by Manila."""
  1644. api_args = {
  1645. 'query': {
  1646. 'volume-attributes': {
  1647. 'volume-id-attributes': {
  1648. 'containing-aggregate-name': aggregate_name,
  1649. 'name': volume_name,
  1650. },
  1651. },
  1652. },
  1653. 'desired-attributes': {
  1654. 'volume-attributes': {
  1655. 'volume-id-attributes': {
  1656. 'containing-aggregate-name': None,
  1657. 'junction-path': None,
  1658. 'name': None,
  1659. 'type': None,
  1660. 'style': None,
  1661. 'owning-vserver-name': None,
  1662. },
  1663. 'volume-space-attributes': {
  1664. 'size': None,
  1665. }
  1666. },
  1667. },
  1668. }
  1669. result = self.send_iter_request('volume-get-iter', api_args)
  1670. if not self._has_records(result):
  1671. return None
  1672. attributes_list = result.get_child_by_name(
  1673. 'attributes-list') or netapp_api.NaElement('none')
  1674. volume_attributes = attributes_list.get_child_by_name(
  1675. 'volume-attributes') or netapp_api.NaElement('none')
  1676. volume_id_attributes = volume_attributes.get_child_by_name(
  1677. 'volume-id-attributes') or netapp_api.NaElement('none')
  1678. volume_space_attributes = volume_attributes.get_child_by_name(
  1679. 'volume-space-attributes') or netapp_api.NaElement('none')
  1680. volume = {
  1681. 'aggregate': volume_id_attributes.get_child_content(
  1682. 'containing-aggregate-name'),
  1683. 'junction-path': volume_id_attributes.get_child_content(
  1684. 'junction-path'),
  1685. 'name': volume_id_attributes.get_child_content('name'),
  1686. 'type': volume_id_attributes.get_child_content('type'),
  1687. 'style': volume_id_attributes.get_child_content('style'),
  1688. 'owning-vserver-name': volume_id_attributes.get_child_content(
  1689. 'owning-vserver-name'),
  1690. 'size': volume_space_attributes.get_child_content('size'),
  1691. }
  1692. return volume
  1693. @na_utils.trace
  1694. def create_volume_clone(self, volume_name, parent_volume_name,
  1695. parent_snapshot_name=None, split=False, **options):
  1696. """Clones a volume."""
  1697. api_args = {
  1698. 'volume': volume_name,
  1699. 'parent-volume': parent_volume_name,
  1700. 'parent-snapshot': parent_snapshot_name,
  1701. 'junction-path': '/%s' % volume_name,
  1702. }
  1703. self.send_request('volume-clone-create', api_args)
  1704. if split:
  1705. self.split_volume_clone(volume_name)
  1706. @na_utils.trace
  1707. def split_volume_clone(self, volume_name):
  1708. """Begins splitting a clone from its parent."""
  1709. try:
  1710. api_args = {'volume': volume_name}
  1711. self.send_request('volume-clone-split-start', api_args)
  1712. except netapp_api.NaApiError as e:
  1713. if e.code == netapp_api.EVOL_CLONE_BEING_SPLIT:
  1714. return
  1715. raise
  1716. @na_utils.trace
  1717. def get_clone_children_for_snapshot(self, volume_name, snapshot_name):
  1718. """Returns volumes that are keeping a snapshot locked."""
  1719. api_args = {
  1720. 'query': {
  1721. 'volume-attributes': {
  1722. 'volume-clone-attributes': {
  1723. 'volume-clone-parent-attributes': {
  1724. 'name': volume_name,
  1725. 'snapshot-name': snapshot_name,
  1726. },
  1727. },
  1728. },
  1729. },
  1730. 'desired-attributes': {
  1731. 'volume-attributes': {
  1732. 'volume-id-attributes': {
  1733. 'name': None,
  1734. },
  1735. },
  1736. },
  1737. }
  1738. result = self.send_iter_request('volume-get-iter', api_args)
  1739. if not self._has_records(result):
  1740. return []
  1741. volume_list = []
  1742. attributes_list = result.get_child_by_name(
  1743. 'attributes-list') or netapp_api.NaElement('none')
  1744. for volume_attributes in attributes_list.get_children():
  1745. volume_id_attributes = volume_attributes.get_child_by_name(
  1746. 'volume-id-attributes') or netapp_api.NaElement('none')
  1747. volume_list.append({
  1748. 'name': volume_id_attributes.get_child_content('name'),
  1749. })
  1750. return volume_list
  1751. @na_utils.trace
  1752. def get_volume_junction_path(self, volume_name, is_style_cifs=False):
  1753. """Gets a volume junction path."""
  1754. api_args = {
  1755. 'volume': volume_name,
  1756. 'is-style-cifs': six.text_type(is_style_cifs).lower(),
  1757. }
  1758. result = self.send_request('volume-get-volume-path', api_args)
  1759. return result.get_child_content('junction')
  1760. @na_utils.trace
  1761. def mount_volume(self, volume_name, junction_path=None):
  1762. """Mounts a volume on a junction path."""
  1763. api_args = {
  1764. 'volume-name': volume_name,
  1765. 'junction-path': (junction_path if junction_path
  1766. else '/%s' % volume_name)
  1767. }
  1768. self.send_request('volume-mount', api_args)
  1769. @na_utils.trace
  1770. def offline_volume(self, volume_name):
  1771. """Offlines a volume."""
  1772. try:
  1773. self.send_request('volume-offline', {'name': volume_name})
  1774. except netapp_api.NaApiError as e:
  1775. if e.code == netapp_api.EVOLUMEOFFLINE:
  1776. return
  1777. raise
  1778. @na_utils.trace
  1779. def _unmount_volume(self, volume_name, force=False):
  1780. """Unmounts a volume."""
  1781. api_args = {
  1782. 'volume-name': volume_name,
  1783. 'force': six.text_type(force).lower(),
  1784. }
  1785. try:
  1786. self.send_request('volume-unmount', api_args)
  1787. except netapp_api.NaApiError as e:
  1788. if e.code == netapp_api.EVOL_NOT_MOUNTED:
  1789. return
  1790. raise
  1791. @na_utils.trace
  1792. def unmount_volume(self, volume_name, force=False, wait_seconds=30):
  1793. """Unmounts a volume, retrying if a clone split is ongoing.
  1794. NOTE(cknight): While unlikely to happen in normal operation, any client
  1795. that tries to delete volumes immediately after creating volume clones
  1796. is likely to experience failures if cDOT isn't quite ready for the
  1797. delete. The volume unmount is the first operation in the delete
  1798. path that fails in this case, and there is no proactive check we can
  1799. use to reliably predict the failure. And there isn't a specific error
  1800. code from volume-unmount, so we have to check for a generic error code
  1801. plus certain language in the error code. It's ugly, but it works, and
  1802. it's better than hard-coding a fixed delay.
  1803. """
  1804. # Do the unmount, handling split-related errors with retries.
  1805. retry_interval = 3 # seconds
  1806. for retry in range(int(wait_seconds / retry_interval)):
  1807. try:
  1808. self._unmount_volume(volume_name, force=force)
  1809. LOG.debug('Volume %s unmounted.', volume_name)
  1810. return
  1811. except netapp_api.NaApiError as e:
  1812. if e.code == netapp_api.EAPIERROR and 'job ID' in e.message:
  1813. msg = _LW('Could not unmount volume %(volume)s due to '
  1814. 'ongoing volume operation: %(exception)s')
  1815. msg_args = {'volume': volume_name, 'exception': e}
  1816. LOG.warning(msg, msg_args)
  1817. time.sleep(retry_interval)
  1818. continue
  1819. raise
  1820. msg = _('Failed to unmount volume %(volume)s after '
  1821. 'waiting for %(wait_seconds)s seconds.')
  1822. msg_args = {'volume': volume_name, 'wait_seconds': wait_seconds}
  1823. LOG.error(msg, msg_args)
  1824. raise exception.NetAppException(msg % msg_args)
  1825. @na_utils.trace
  1826. def delete_volume(self, volume_name):
  1827. """Deletes a volume."""
  1828. self.send_request('volume-destroy', {'name': volume_name})
  1829. @na_utils.trace
  1830. def create_snapshot(self, volume_name, snapshot_name):
  1831. """Creates a volume snapshot."""
  1832. api_args = {'volume': volume_name, 'snapshot': snapshot_name}
  1833. self.send_request('snapshot-create', api_args)
  1834. @na_utils.trace
  1835. def snapshot_exists(self, snapshot_name, volume_name):
  1836. """Checks if Snapshot exists for a specified volume."""
  1837. LOG.debug('Checking if snapshot %(snapshot)s exists for '
  1838. 'volume %(volume)s',
  1839. {'snapshot': snapshot_name, 'volume': volume_name})
  1840. """Gets a single snapshot."""
  1841. api_args = {
  1842. 'query': {
  1843. 'snapshot-info': {
  1844. 'name': snapshot_name,
  1845. 'volume': volume_name,
  1846. },
  1847. },
  1848. 'desired-attributes': {
  1849. 'snapshot-info': {
  1850. 'name': None,
  1851. 'volume': None,
  1852. 'busy': None,
  1853. 'snapshot-owners-list': {
  1854. 'snapshot-owner': None,
  1855. }
  1856. },
  1857. },
  1858. }
  1859. result = self.send_request('snapshot-get-iter', api_args)
  1860. error_record_list = result.get_child_by_name(
  1861. 'volume-errors') or netapp_api.NaElement('none')
  1862. errors = error_record_list.get_children()
  1863. if errors:
  1864. error = errors[0]
  1865. error_code = error.get_child_content('errno')
  1866. error_reason = error.get_child_content('reason')
  1867. msg = _('Could not read information for snapshot %(name)s. '
  1868. 'Code: %(code)s. Reason: %(reason)s')
  1869. msg_args = {
  1870. 'name': snapshot_name,
  1871. 'code': error_code,
  1872. 'reason': error_reason
  1873. }
  1874. if error_code == netapp_api.ESNAPSHOTNOTALLOWED:
  1875. raise exception.SnapshotUnavailable(msg % msg_args)
  1876. else:
  1877. raise exception.NetAppException(msg % msg_args)
  1878. return self._has_records(result)
  1879. @na_utils.trace
  1880. def get_snapshot(self, volume_name, snapshot_name):
  1881. """Gets a single snapshot."""
  1882. api_args = {
  1883. 'query': {
  1884. 'snapshot-info': {
  1885. 'name': snapshot_name,
  1886. 'volume': volume_name,
  1887. },
  1888. },
  1889. 'desired-attributes': {
  1890. 'snapshot-info': {
  1891. 'access-time': None,
  1892. 'name': None,
  1893. 'volume': None,
  1894. 'busy': None,
  1895. 'snapshot-owners-list': {
  1896. 'snapshot-owner': None,
  1897. }
  1898. },
  1899. },
  1900. }
  1901. result = self.send_request('snapshot-get-iter', api_args)
  1902. error_record_list = result.get_child_by_name(
  1903. 'volume-errors') or netapp_api.NaElement('none')
  1904. errors = error_record_list.get_children()
  1905. if errors:
  1906. error = errors[0]
  1907. error_code = error.get_child_content('errno')
  1908. error_reason = error.get_child_content('reason')
  1909. msg = _('Could not read information for snapshot %(name)s. '
  1910. 'Code: %(code)s. Reason: %(reason)s')
  1911. msg_args = {
  1912. 'name': snapshot_name,
  1913. 'code': error_code,
  1914. 'reason': error_reason
  1915. }
  1916. if error_code == netapp_api.ESNAPSHOTNOTALLOWED:
  1917. raise exception.SnapshotUnavailable(msg % msg_args)
  1918. else:
  1919. raise exception.NetAppException(msg % msg_args)
  1920. attributes_list = result.get_child_by_name(
  1921. 'attributes-list') or netapp_api.NaElement('none')
  1922. snapshot_info_list = attributes_list.get_children()
  1923. if not self._has_records(result):
  1924. raise exception.SnapshotResourceNotFound(name=snapshot_name)
  1925. elif len(snapshot_info_list) > 1:
  1926. msg = _('Could not find unique snapshot %(snap)s on '
  1927. 'volume %(vol)s.')
  1928. msg_args = {'snap': snapshot_name, 'vol': volume_name}
  1929. raise exception.NetAppException(msg % msg_args)
  1930. snapshot_info = snapshot_info_list[0]
  1931. snapshot = {
  1932. 'access-time': snapshot_info.get_child_content('access-time'),
  1933. 'name': snapshot_info.get_child_content('name'),
  1934. 'volume': snapshot_info.get_child_content('volume'),
  1935. 'busy': strutils.bool_from_string(
  1936. snapshot_info.get_child_content('busy')),
  1937. }
  1938. snapshot_owners_list = snapshot_info.get_child_by_name(
  1939. 'snapshot-owners-list') or netapp_api.NaElement('none')
  1940. snapshot_owners = set([
  1941. snapshot_owner.get_child_content('owner')
  1942. for snapshot_owner in snapshot_owners_list.get_children()])
  1943. snapshot['owners'] = snapshot_owners
  1944. return snapshot
  1945. @na_utils.trace
  1946. def rename_snapshot(self, volume_name, snapshot_name, new_snapshot_name):
  1947. api_args = {
  1948. 'volume': volume_name,
  1949. 'current-name': snapshot_name,
  1950. 'new-name': new_snapshot_name
  1951. }
  1952. self.send_request('snapshot-rename', api_args)
  1953. @na_utils.trace
  1954. def restore_snapshot(self, volume_name, snapshot_name):
  1955. """Reverts a volume to the specified snapshot."""
  1956. api_args = {
  1957. 'volume': volume_name,
  1958. 'snapshot': snapshot_name,
  1959. }
  1960. self.send_request('snapshot-restore-volume', api_args)
  1961. @na_utils.trace
  1962. def delete_snapshot(self, volume_name, snapshot_name, ignore_owners=False):
  1963. """Deletes a volume snapshot."""
  1964. ignore_owners = ('true' if strutils.bool_from_string(ignore_owners)
  1965. else 'false')
  1966. api_args = {
  1967. 'volume': volume_name,
  1968. 'snapshot': snapshot_name,
  1969. 'ignore-owners': ignore_owners,
  1970. }
  1971. self.send_request('snapshot-delete', api_args)
  1972. @na_utils.trace
  1973. def soft_delete_snapshot(self, volume_name, snapshot_name):
  1974. """Deletes a volume snapshot, or renames it if delete fails."""
  1975. try:
  1976. self.delete_snapshot(volume_name, snapshot_name)
  1977. except netapp_api.NaApiError:
  1978. self.rename_snapshot(volume_name,
  1979. snapshot_name,
  1980. DELETED_PREFIX + snapshot_name)
  1981. msg = _('Soft-deleted snapshot %(snapshot)s on volume %(volume)s.')
  1982. msg_args = {'snapshot': snapshot_name, 'volume': volume_name}
  1983. LOG.info(msg, msg_args)
  1984. @na_utils.trace
  1985. def prune_deleted_snapshots(self):
  1986. """Deletes non-busy snapshots that were previously soft-deleted."""
  1987. deleted_snapshots_map = self._get_deleted_snapshots()
  1988. for vserver in deleted_snapshots_map:
  1989. client = copy.deepcopy(self)
  1990. client.set_vserver(vserver)
  1991. for snapshot in deleted_snapshots_map[vserver]:
  1992. try:
  1993. client.delete_snapshot(snapshot['volume'],
  1994. snapshot['name'])
  1995. except netapp_api.NaApiError:
  1996. msg = _('Could not delete snapshot %(snap)s on '
  1997. 'volume %(volume)s.')
  1998. msg_args = {
  1999. 'snap': snapshot['name'],
  2000. 'volume': snapshot['volume'],
  2001. }
  2002. LOG.exception(msg, msg_args)
  2003. @na_utils.trace
  2004. def _get_deleted_snapshots(self):
  2005. """Returns non-busy, soft-deleted snapshots suitable for reaping."""
  2006. api_args = {
  2007. 'query': {
  2008. 'snapshot-info': {
  2009. 'name': DELETED_PREFIX + '*',
  2010. 'busy': 'false',
  2011. },
  2012. },
  2013. 'desired-attributes': {
  2014. 'snapshot-info': {
  2015. 'name': None,
  2016. 'vserver': None,
  2017. 'volume': None,
  2018. },
  2019. },
  2020. }
  2021. result = self.send_iter_request('snapshot-get-iter', api_args)
  2022. attributes_list = result.get_child_by_name(
  2023. 'attributes-list') or netapp_api.NaElement('none')
  2024. # Build a map of snapshots, one list of snapshots per vserver
  2025. snapshot_map = {}
  2026. for snapshot_info in attributes_list.get_children():
  2027. vserver = snapshot_info.get_child_content('vserver')
  2028. snapshot_list = snapshot_map.get(vserver, [])
  2029. snapshot_list.append({
  2030. 'name': snapshot_info.get_child_content('name'),
  2031. 'volume': snapshot_info.get_child_content('volume'),
  2032. 'vserver': vserver,
  2033. })
  2034. snapshot_map[vserver] = snapshot_list
  2035. return snapshot_map
  2036. @na_utils.trace
  2037. def create_cg_snapshot(self, volume_names, snapshot_name):
  2038. """Creates a consistency group snapshot of one or more flexvols."""
  2039. cg_id = self._start_cg_snapshot(volume_names, snapshot_name)
  2040. if not cg_id:
  2041. msg = _('Could not start consistency group snapshot %s.')
  2042. raise exception.NetAppException(msg % snapshot_name)
  2043. self._commit_cg_snapshot(cg_id)
  2044. @na_utils.trace
  2045. def _start_cg_snapshot(self, volume_names, snapshot_name):
  2046. api_args = {
  2047. 'snapshot': snapshot_name,
  2048. 'timeout': 'relaxed',
  2049. 'volumes': [
  2050. {'volume-name': volume_name} for volume_name in volume_names
  2051. ],
  2052. }
  2053. result = self.send_request('cg-start', api_args)
  2054. return result.get_child_content('cg-id')
  2055. @na_utils.trace
  2056. def _commit_cg_snapshot(self, cg_id):
  2057. api_args = {'cg-id': cg_id}
  2058. self.send_request('cg-commit', api_args)
  2059. @na_utils.trace
  2060. def create_cifs_share(self, share_name):
  2061. share_path = '/%s' % share_name
  2062. api_args = {'path': share_path, 'share-name': share_name}
  2063. self.send_request('cifs-share-create', api_args)
  2064. @na_utils.trace
  2065. def get_cifs_share_access(self, share_name):
  2066. api_args = {
  2067. 'query': {
  2068. 'cifs-share-access-control': {
  2069. 'share': share_name,
  2070. },
  2071. },
  2072. 'desired-attributes': {
  2073. 'cifs-share-access-control': {
  2074. 'user-or-group': None,
  2075. 'permission': None,
  2076. },
  2077. },
  2078. }
  2079. result = self.send_iter_request('cifs-share-access-control-get-iter',
  2080. api_args)
  2081. attributes_list = result.get_child_by_name(
  2082. 'attributes-list') or netapp_api.NaElement('none')
  2083. rules = {}
  2084. for rule in attributes_list.get_children():
  2085. user_or_group = rule.get_child_content('user-or-group')
  2086. permission = rule.get_child_content('permission')
  2087. rules[user_or_group] = permission
  2088. return rules
  2089. @na_utils.trace
  2090. def add_cifs_share_access(self, share_name, user_name, readonly):
  2091. api_args = {
  2092. 'permission': 'read' if readonly else 'full_control',
  2093. 'share': share_name,
  2094. 'user-or-group': user_name,
  2095. }
  2096. self.send_request('cifs-share-access-control-create', api_args)
  2097. @na_utils.trace
  2098. def modify_cifs_share_access(self, share_name, user_name, readonly):
  2099. api_args = {
  2100. 'permission': 'read' if readonly else 'full_control',
  2101. 'share': share_name,
  2102. 'user-or-group': user_name,
  2103. }
  2104. self.send_request('cifs-share-access-control-modify', api_args)
  2105. @na_utils.trace
  2106. def remove_cifs_share_access(self, share_name, user_name):
  2107. api_args = {'user-or-group': user_name, 'share': share_name}
  2108. self.send_request('cifs-share-access-control-delete', api_args)
  2109. @na_utils.trace
  2110. def remove_cifs_share(self, share_name):
  2111. self.send_request('cifs-share-delete', {'share-name': share_name})
  2112. @na_utils.trace
  2113. def add_nfs_export_rule(self, policy_name, client_match, readonly):
  2114. rule_indices = self._get_nfs_export_rule_indices(policy_name,
  2115. client_match)
  2116. if not rule_indices:
  2117. self._add_nfs_export_rule(policy_name, client_match, readonly)
  2118. else:
  2119. # Update first rule and delete the rest
  2120. self._update_nfs_export_rule(
  2121. policy_name, client_match, readonly, rule_indices.pop(0))
  2122. self._remove_nfs_export_rules(policy_name, rule_indices)
  2123. @na_utils.trace
  2124. def _add_nfs_export_rule(self, policy_name, client_match, readonly):
  2125. api_args = {
  2126. 'policy-name': policy_name,
  2127. 'client-match': client_match,
  2128. 'ro-rule': {
  2129. 'security-flavor': 'sys',
  2130. },
  2131. 'rw-rule': {
  2132. 'security-flavor': 'sys' if not readonly else 'never',
  2133. },
  2134. 'super-user-security': {
  2135. 'security-flavor': 'sys',
  2136. },
  2137. }
  2138. self.send_request('export-rule-create', api_args)
  2139. @na_utils.trace
  2140. def _update_nfs_export_rule(self, policy_name, client_match, readonly,
  2141. rule_index):
  2142. api_args = {
  2143. 'policy-name': policy_name,
  2144. 'rule-index': rule_index,
  2145. 'client-match': client_match,
  2146. 'ro-rule': {
  2147. 'security-flavor': 'sys'
  2148. },
  2149. 'rw-rule': {
  2150. 'security-flavor': 'sys' if not readonly else 'never'
  2151. },
  2152. 'super-user-security': {
  2153. 'security-flavor': 'sys'
  2154. },
  2155. }
  2156. self.send_request('export-rule-modify', api_args)
  2157. @na_utils.trace
  2158. def _get_nfs_export_rule_indices(self, policy_name, client_match):
  2159. api_args = {
  2160. 'query': {
  2161. 'export-rule-info': {
  2162. 'policy-name': policy_name,
  2163. 'client-match': client_match,
  2164. },
  2165. },
  2166. 'desired-attributes': {
  2167. 'export-rule-info': {
  2168. 'vserver-name': None,
  2169. 'policy-name': None,
  2170. 'client-match': None,
  2171. 'rule-index': None,
  2172. },
  2173. },
  2174. }
  2175. result = self.send_iter_request('export-rule-get-iter', api_args)
  2176. attributes_list = result.get_child_by_name(
  2177. 'attributes-list') or netapp_api.NaElement('none')
  2178. export_rule_info_list = attributes_list.get_children()
  2179. rule_indices = [int(export_rule_info.get_child_content('rule-index'))
  2180. for export_rule_info in export_rule_info_list]
  2181. rule_indices.sort()
  2182. return [six.text_type(rule_index) for rule_index in rule_indices]
  2183. @na_utils.trace
  2184. def remove_nfs_export_rule(self, policy_name, client_match):
  2185. rule_indices = self._get_nfs_export_rule_indices(policy_name,
  2186. client_match)
  2187. self._remove_nfs_export_rules(policy_name, rule_indices)
  2188. @na_utils.trace
  2189. def _remove_nfs_export_rules(self, policy_name, rule_indices):
  2190. for rule_index in rule_indices:
  2191. api_args = {
  2192. 'policy-name': policy_name,
  2193. 'rule-index': rule_index
  2194. }
  2195. try:
  2196. self.send_request('export-rule-destroy', api_args)
  2197. except netapp_api.NaApiError as e:
  2198. if e.code != netapp_api.EOBJECTNOTFOUND:
  2199. raise
  2200. @na_utils.trace
  2201. def clear_nfs_export_policy_for_volume(self, volume_name):
  2202. self.set_nfs_export_policy_for_volume(volume_name, 'default')
  2203. @na_utils.trace
  2204. def set_nfs_export_policy_for_volume(self, volume_name, policy_name):
  2205. api_args = {
  2206. 'query': {
  2207. 'volume-attributes': {
  2208. 'volume-id-attributes': {
  2209. 'name': volume_name,
  2210. },
  2211. },
  2212. },
  2213. 'attributes': {
  2214. 'volume-attributes': {
  2215. 'volume-export-attributes': {
  2216. 'policy': policy_name,
  2217. },
  2218. },
  2219. },
  2220. }
  2221. self.send_request('volume-modify-iter', api_args)
  2222. @na_utils.trace
  2223. def get_nfs_export_policy_for_volume(self, volume_name):
  2224. """Get the name of the export policy for a volume."""
  2225. api_args = {
  2226. 'query': {
  2227. 'volume-attributes': {
  2228. 'volume-id-attributes': {
  2229. 'name': volume_name,
  2230. },
  2231. },
  2232. },
  2233. 'desired-attributes': {
  2234. 'volume-attributes': {
  2235. 'volume-export-attributes': {
  2236. 'policy': None,
  2237. },
  2238. },
  2239. },
  2240. }
  2241. result = self.send_iter_request('volume-get-iter', api_args)
  2242. attributes_list = result.get_child_by_name(
  2243. 'attributes-list') or netapp_api.NaElement('none')
  2244. volume_attributes = attributes_list.get_child_by_name(
  2245. 'volume-attributes') or netapp_api.NaElement('none')
  2246. volume_export_attributes = volume_attributes.get_child_by_name(
  2247. 'volume-export-attributes') or netapp_api.NaElement('none')
  2248. export_policy = volume_export_attributes.get_child_content('policy')
  2249. if not export_policy:
  2250. msg = _('Could not find export policy for volume %s.')
  2251. raise exception.NetAppException(msg % volume_name)
  2252. return export_policy
  2253. @na_utils.trace
  2254. def create_nfs_export_policy(self, policy_name):
  2255. api_args = {'policy-name': policy_name}
  2256. try:
  2257. self.send_request('export-policy-create', api_args)
  2258. except netapp_api.NaApiError as e:
  2259. if e.code != netapp_api.EDUPLICATEENTRY:
  2260. raise
  2261. @na_utils.trace
  2262. def soft_delete_nfs_export_policy(self, policy_name):
  2263. try:
  2264. self.delete_nfs_export_policy(policy_name)
  2265. except netapp_api.NaApiError:
  2266. # NOTE(cknight): Policy deletion can fail if called too soon after
  2267. # removing from a flexvol. So rename for later harvesting.
  2268. self.rename_nfs_export_policy(policy_name,
  2269. DELETED_PREFIX + policy_name)
  2270. @na_utils.trace
  2271. def delete_nfs_export_policy(self, policy_name):
  2272. api_args = {'policy-name': policy_name}
  2273. try:
  2274. self.send_request('export-policy-destroy', api_args)
  2275. except netapp_api.NaApiError as e:
  2276. if e.code == netapp_api.EOBJECTNOTFOUND:
  2277. return
  2278. raise
  2279. @na_utils.trace
  2280. def rename_nfs_export_policy(self, policy_name, new_policy_name):
  2281. api_args = {
  2282. 'policy-name': policy_name,
  2283. 'new-policy-name': new_policy_name
  2284. }
  2285. self.send_request('export-policy-rename', api_args)
  2286. @na_utils.trace
  2287. def prune_deleted_nfs_export_policies(self):
  2288. deleted_policy_map = self._get_deleted_nfs_export_policies()
  2289. for vserver in deleted_policy_map:
  2290. client = copy.deepcopy(self)
  2291. client.set_vserver(vserver)
  2292. for policy in deleted_policy_map[vserver]:
  2293. try:
  2294. client.delete_nfs_export_policy(policy)
  2295. except netapp_api.NaApiError:
  2296. LOG.debug('Could not delete export policy %s.' % policy)
  2297. @na_utils.trace
  2298. def _get_deleted_nfs_export_policies(self):
  2299. api_args = {
  2300. 'query': {
  2301. 'export-policy-info': {
  2302. 'policy-name': DELETED_PREFIX + '*',
  2303. },
  2304. },
  2305. 'desired-attributes': {
  2306. 'export-policy-info': {
  2307. 'policy-name': None,
  2308. 'vserver': None,
  2309. },
  2310. },
  2311. }
  2312. result = self.send_iter_request('export-policy-get-iter', api_args)
  2313. attributes_list = result.get_child_by_name(
  2314. 'attributes-list') or netapp_api.NaElement('none')
  2315. policy_map = {}
  2316. for export_info in attributes_list.get_children():
  2317. vserver = export_info.get_child_content('vserver')
  2318. policies = policy_map.get(vserver, [])
  2319. policies.append(export_info.get_child_content('policy-name'))
  2320. policy_map[vserver] = policies
  2321. return policy_map
  2322. @na_utils.trace
  2323. def _get_ems_log_destination_vserver(self):
  2324. """Returns the best vserver destination for EMS messages."""
  2325. major, minor = self.get_ontapi_version(cached=True)
  2326. if (major > 1) or (major == 1 and minor > 15):
  2327. # Prefer admin Vserver (requires cluster credentials).
  2328. admin_vservers = self.list_vservers(vserver_type='admin')
  2329. if admin_vservers:
  2330. return admin_vservers[0]
  2331. # Fall back to data Vserver.
  2332. data_vservers = self.list_vservers(vserver_type='data')
  2333. if data_vservers:
  2334. return data_vservers[0]
  2335. # If older API version, or no other Vservers found, use node Vserver.
  2336. node_vservers = self.list_vservers(vserver_type='node')
  2337. if node_vservers:
  2338. return node_vservers[0]
  2339. raise exception.NotFound("No Vserver found to receive EMS messages.")
  2340. @na_utils.trace
  2341. def send_ems_log_message(self, message_dict):
  2342. """Sends a message to the Data ONTAP EMS log."""
  2343. # NOTE(cknight): Cannot use deepcopy on the connection context
  2344. node_client = copy.copy(self)
  2345. node_client.connection = copy.copy(self.connection)
  2346. node_client.connection.set_timeout(25)
  2347. try:
  2348. node_client.set_vserver(self._get_ems_log_destination_vserver())
  2349. node_client.send_request('ems-autosupport-log', message_dict)
  2350. LOG.debug('EMS executed successfully.')
  2351. except netapp_api.NaApiError as e:
  2352. LOG.warning(_LW('Failed to invoke EMS. %s') % e)
  2353. @na_utils.trace
  2354. def get_aggregate(self, aggregate_name):
  2355. """Get aggregate attributes needed for the storage service catalog."""
  2356. if not aggregate_name:
  2357. return {}
  2358. desired_attributes = {
  2359. 'aggr-attributes': {
  2360. 'aggregate-name': None,
  2361. 'aggr-raid-attributes': {
  2362. 'raid-type': None,
  2363. 'is-hybrid': None,
  2364. },
  2365. },
  2366. }
  2367. try:
  2368. aggrs = self._get_aggregates(aggregate_names=[aggregate_name],
  2369. desired_attributes=desired_attributes)
  2370. except netapp_api.NaApiError:
  2371. msg = _('Failed to get info for aggregate %s.')
  2372. LOG.exception(msg % aggregate_name)
  2373. return {}
  2374. if len(aggrs) < 1:
  2375. return {}
  2376. aggr_attributes = aggrs[0]
  2377. aggr_raid_attrs = aggr_attributes.get_child_by_name(
  2378. 'aggr-raid-attributes') or netapp_api.NaElement('none')
  2379. aggregate = {
  2380. 'name': aggr_attributes.get_child_content('aggregate-name'),
  2381. 'raid-type': aggr_raid_attrs.get_child_content('raid-type'),
  2382. 'is-hybrid': strutils.bool_from_string(
  2383. aggr_raid_attrs.get_child_content('is-hybrid')),
  2384. }
  2385. return aggregate
  2386. @na_utils.trace
  2387. def get_aggregate_disk_types(self, aggregate_name):
  2388. """Get the disk type(s) of an aggregate."""
  2389. disk_types = set()
  2390. disk_types.update(self._get_aggregate_disk_types(aggregate_name))
  2391. if self.features.ADVANCED_DISK_PARTITIONING:
  2392. disk_types.update(self._get_aggregate_disk_types(aggregate_name,
  2393. shared=True))
  2394. return list(disk_types) if disk_types else None
  2395. @na_utils.trace
  2396. def _get_aggregate_disk_types(self, aggregate_name, shared=False):
  2397. """Get the disk type(s) of an aggregate."""
  2398. disk_types = set()
  2399. if shared:
  2400. disk_raid_info = {
  2401. 'disk-shared-info': {
  2402. 'aggregate-list': {
  2403. 'shared-aggregate-info': {
  2404. 'aggregate-name': aggregate_name,
  2405. },
  2406. },
  2407. },
  2408. }
  2409. else:
  2410. disk_raid_info = {
  2411. 'disk-aggregate-info': {
  2412. 'aggregate-name': aggregate_name,
  2413. },
  2414. }
  2415. api_args = {
  2416. 'query': {
  2417. 'storage-disk-info': {
  2418. 'disk-raid-info': disk_raid_info,
  2419. },
  2420. },
  2421. 'desired-attributes': {
  2422. 'storage-disk-info': {
  2423. 'disk-raid-info': {
  2424. 'effective-disk-type': None,
  2425. },
  2426. },
  2427. },
  2428. }
  2429. try:
  2430. result = self.send_iter_request('storage-disk-get-iter', api_args)
  2431. except netapp_api.NaApiError:
  2432. msg = _('Failed to get disk info for aggregate %s.')
  2433. LOG.exception(msg % aggregate_name)
  2434. return disk_types
  2435. attributes_list = result.get_child_by_name(
  2436. 'attributes-list') or netapp_api.NaElement('none')
  2437. for storage_disk_info in attributes_list.get_children():
  2438. disk_raid_info = storage_disk_info.get_child_by_name(
  2439. 'disk-raid-info') or netapp_api.NaElement('none')
  2440. disk_type = disk_raid_info.get_child_content(
  2441. 'effective-disk-type')
  2442. if disk_type:
  2443. disk_types.add(disk_type)
  2444. return disk_types
  2445. @na_utils.trace
  2446. def check_for_cluster_credentials(self):
  2447. try:
  2448. self.list_cluster_nodes()
  2449. # API succeeded, so definitely a cluster management LIF
  2450. return True
  2451. except netapp_api.NaApiError as e:
  2452. if e.code == netapp_api.EAPINOTFOUND:
  2453. LOG.debug('Not connected to cluster management LIF.')
  2454. return False
  2455. else:
  2456. raise
  2457. @na_utils.trace
  2458. def create_cluster_peer(self, addresses, username=None, password=None,
  2459. passphrase=None):
  2460. """Creates a cluster peer relationship."""
  2461. api_args = {
  2462. 'peer-addresses': [
  2463. {'remote-inet-address': address} for address in addresses
  2464. ],
  2465. }
  2466. if username:
  2467. api_args['user-name'] = username
  2468. if password:
  2469. api_args['password'] = password
  2470. if passphrase:
  2471. api_args['passphrase'] = passphrase
  2472. self.send_request('cluster-peer-create', api_args)
  2473. @na_utils.trace
  2474. def get_cluster_peers(self, remote_cluster_name=None):
  2475. """Gets one or more cluster peer relationships."""
  2476. api_args = {}
  2477. if remote_cluster_name:
  2478. api_args['query'] = {
  2479. 'cluster-peer-info': {
  2480. 'remote-cluster-name': remote_cluster_name,
  2481. }
  2482. }
  2483. result = self.send_iter_request('cluster-peer-get-iter', api_args)
  2484. if not self._has_records(result):
  2485. return []
  2486. cluster_peers = []
  2487. for cluster_peer_info in result.get_child_by_name(
  2488. 'attributes-list').get_children():
  2489. cluster_peer = {
  2490. 'active-addresses': [],
  2491. 'peer-addresses': []
  2492. }
  2493. active_addresses = cluster_peer_info.get_child_by_name(
  2494. 'active-addresses') or netapp_api.NaElement('none')
  2495. for address in active_addresses.get_children():
  2496. cluster_peer['active-addresses'].append(address.get_content())
  2497. peer_addresses = cluster_peer_info.get_child_by_name(
  2498. 'peer-addresses') or netapp_api.NaElement('none')
  2499. for address in peer_addresses.get_children():
  2500. cluster_peer['peer-addresses'].append(address.get_content())
  2501. cluster_peer['availability'] = cluster_peer_info.get_child_content(
  2502. 'availability')
  2503. cluster_peer['cluster-name'] = cluster_peer_info.get_child_content(
  2504. 'cluster-name')
  2505. cluster_peer['cluster-uuid'] = cluster_peer_info.get_child_content(
  2506. 'cluster-uuid')
  2507. cluster_peer['remote-cluster-name'] = (
  2508. cluster_peer_info.get_child_content('remote-cluster-name'))
  2509. cluster_peer['serial-number'] = (
  2510. cluster_peer_info.get_child_content('serial-number'))
  2511. cluster_peer['timeout'] = cluster_peer_info.get_child_content(
  2512. 'timeout')
  2513. cluster_peers.append(cluster_peer)
  2514. return cluster_peers
  2515. @na_utils.trace
  2516. def delete_cluster_peer(self, cluster_name):
  2517. """Deletes a cluster peer relationship."""
  2518. api_args = {'cluster-name': cluster_name}
  2519. self.send_request('cluster-peer-delete', api_args)
  2520. @na_utils.trace
  2521. def get_cluster_peer_policy(self):
  2522. """Gets the cluster peering policy configuration."""
  2523. if not self.features.CLUSTER_PEER_POLICY:
  2524. return {}
  2525. result = self.send_request('cluster-peer-policy-get')
  2526. attributes = result.get_child_by_name(
  2527. 'attributes') or netapp_api.NaElement('none')
  2528. cluster_peer_policy = attributes.get_child_by_name(
  2529. 'cluster-peer-policy') or netapp_api.NaElement('none')
  2530. policy = {
  2531. 'is-unauthenticated-access-permitted':
  2532. cluster_peer_policy.get_child_content(
  2533. 'is-unauthenticated-access-permitted'),
  2534. 'passphrase-minimum-length':
  2535. cluster_peer_policy.get_child_content(
  2536. 'passphrase-minimum-length'),
  2537. }
  2538. if policy['is-unauthenticated-access-permitted'] is not None:
  2539. policy['is-unauthenticated-access-permitted'] = (
  2540. strutils.bool_from_string(
  2541. policy['is-unauthenticated-access-permitted']))
  2542. if policy['passphrase-minimum-length'] is not None:
  2543. policy['passphrase-minimum-length'] = int(
  2544. policy['passphrase-minimum-length'])
  2545. return policy
  2546. @na_utils.trace
  2547. def set_cluster_peer_policy(self, is_unauthenticated_access_permitted=None,
  2548. passphrase_minimum_length=None):
  2549. """Modifies the cluster peering policy configuration."""
  2550. if not self.features.CLUSTER_PEER_POLICY:
  2551. return
  2552. if (is_unauthenticated_access_permitted is None and
  2553. passphrase_minimum_length is None):
  2554. return
  2555. api_args = {}
  2556. if is_unauthenticated_access_permitted is not None:
  2557. api_args['is-unauthenticated-access-permitted'] = (
  2558. 'true' if strutils.bool_from_string(
  2559. is_unauthenticated_access_permitted) else 'false')
  2560. if passphrase_minimum_length is not None:
  2561. api_args['passphrase-minlength'] = six.text_type(
  2562. passphrase_minimum_length)
  2563. self.send_request('cluster-peer-policy-modify', api_args)
  2564. @na_utils.trace
  2565. def create_vserver_peer(self, vserver_name, peer_vserver_name):
  2566. """Creates a Vserver peer relationship for SnapMirrors."""
  2567. api_args = {
  2568. 'vserver': vserver_name,
  2569. 'peer-vserver': peer_vserver_name,
  2570. 'applications': [
  2571. {'vserver-peer-application': 'snapmirror'},
  2572. ],
  2573. }
  2574. self.send_request('vserver-peer-create', api_args)
  2575. @na_utils.trace
  2576. def delete_vserver_peer(self, vserver_name, peer_vserver_name):
  2577. """Deletes a Vserver peer relationship."""
  2578. api_args = {'vserver': vserver_name, 'peer-vserver': peer_vserver_name}
  2579. self.send_request('vserver-peer-delete', api_args)
  2580. @na_utils.trace
  2581. def accept_vserver_peer(self, vserver_name, peer_vserver_name):
  2582. """Accepts a pending Vserver peer relationship."""
  2583. api_args = {'vserver': vserver_name, 'peer-vserver': peer_vserver_name}
  2584. self.send_request('vserver-peer-accept', api_args)
  2585. @na_utils.trace
  2586. def get_vserver_peers(self, vserver_name=None, peer_vserver_name=None):
  2587. """Gets one or more Vserver peer relationships."""
  2588. api_args = None
  2589. if vserver_name or peer_vserver_name:
  2590. api_args = {'query': {'vserver-peer-info': {}}}
  2591. if vserver_name:
  2592. api_args['query']['vserver-peer-info']['vserver'] = (
  2593. vserver_name)
  2594. if peer_vserver_name:
  2595. api_args['query']['vserver-peer-info']['peer-vserver'] = (
  2596. peer_vserver_name)
  2597. result = self.send_iter_request('vserver-peer-get-iter', api_args)
  2598. if not self._has_records(result):
  2599. return []
  2600. vserver_peers = []
  2601. for vserver_peer_info in result.get_child_by_name(
  2602. 'attributes-list').get_children():
  2603. vserver_peer = {
  2604. 'vserver': vserver_peer_info.get_child_content('vserver'),
  2605. 'peer-vserver':
  2606. vserver_peer_info.get_child_content('peer-vserver'),
  2607. 'peer-state':
  2608. vserver_peer_info.get_child_content('peer-state'),
  2609. 'peer-cluster':
  2610. vserver_peer_info.get_child_content('peer-cluster'),
  2611. }
  2612. vserver_peers.append(vserver_peer)
  2613. return vserver_peers
  2614. def _ensure_snapmirror_v2(self):
  2615. """Verify support for SnapMirror control plane v2."""
  2616. if not self.features.SNAPMIRROR_V2:
  2617. msg = _('SnapMirror features require Data ONTAP 8.2 or later.')
  2618. raise exception.NetAppException(msg)
  2619. @na_utils.trace
  2620. def create_snapmirror(self, source_vserver, source_volume,
  2621. destination_vserver, destination_volume,
  2622. schedule=None, policy=None,
  2623. relationship_type='data_protection'):
  2624. """Creates a SnapMirror relationship (cDOT 8.2 or later only)."""
  2625. self._ensure_snapmirror_v2()
  2626. api_args = {
  2627. 'source-volume': source_volume,
  2628. 'source-vserver': source_vserver,
  2629. 'destination-volume': destination_volume,
  2630. 'destination-vserver': destination_vserver,
  2631. 'relationship-type': relationship_type,
  2632. }
  2633. if schedule:
  2634. api_args['schedule'] = schedule
  2635. if policy:
  2636. api_args['policy'] = policy
  2637. try:
  2638. self.send_request('snapmirror-create', api_args)
  2639. except netapp_api.NaApiError as e:
  2640. if e.code != netapp_api.ERELATION_EXISTS:
  2641. raise
  2642. @na_utils.trace
  2643. def initialize_snapmirror(self, source_vserver, source_volume,
  2644. destination_vserver, destination_volume,
  2645. source_snapshot=None, transfer_priority=None):
  2646. """Initializes a SnapMirror relationship (cDOT 8.2 or later only)."""
  2647. self._ensure_snapmirror_v2()
  2648. api_args = {
  2649. 'source-volume': source_volume,
  2650. 'source-vserver': source_vserver,
  2651. 'destination-volume': destination_volume,
  2652. 'destination-vserver': destination_vserver,
  2653. }
  2654. if source_snapshot:
  2655. api_args['source-snapshot'] = source_snapshot
  2656. if transfer_priority:
  2657. api_args['transfer-priority'] = transfer_priority
  2658. result = self.send_request('snapmirror-initialize', api_args)
  2659. result_info = {}
  2660. result_info['operation-id'] = result.get_child_content(
  2661. 'result-operation-id')
  2662. result_info['status'] = result.get_child_content('result-status')
  2663. result_info['jobid'] = result.get_child_content('result-jobid')
  2664. result_info['error-code'] = result.get_child_content(
  2665. 'result-error-code')
  2666. result_info['error-message'] = result.get_child_content(
  2667. 'result-error-message')
  2668. return result_info
  2669. @na_utils.trace
  2670. def release_snapmirror(self, source_vserver, source_volume,
  2671. destination_vserver, destination_volume,
  2672. relationship_info_only=False):
  2673. """Removes a SnapMirror relationship on the source endpoint."""
  2674. self._ensure_snapmirror_v2()
  2675. api_args = {
  2676. 'query': {
  2677. 'snapmirror-destination-info': {
  2678. 'source-volume': source_volume,
  2679. 'source-vserver': source_vserver,
  2680. 'destination-volume': destination_volume,
  2681. 'destination-vserver': destination_vserver,
  2682. 'relationship-info-only': ('true' if relationship_info_only
  2683. else 'false'),
  2684. }
  2685. }
  2686. }
  2687. self.send_request('snapmirror-release-iter', api_args)
  2688. @na_utils.trace
  2689. def quiesce_snapmirror(self, source_vserver, source_volume,
  2690. destination_vserver, destination_volume):
  2691. """Disables future transfers to a SnapMirror destination."""
  2692. self._ensure_snapmirror_v2()
  2693. api_args = {
  2694. 'source-volume': source_volume,
  2695. 'source-vserver': source_vserver,
  2696. 'destination-volume': destination_volume,
  2697. 'destination-vserver': destination_vserver,
  2698. }
  2699. self.send_request('snapmirror-quiesce', api_args)
  2700. @na_utils.trace
  2701. def abort_snapmirror(self, source_vserver, source_volume,
  2702. destination_vserver, destination_volume,
  2703. clear_checkpoint=False):
  2704. """Stops ongoing transfers for a SnapMirror relationship."""
  2705. self._ensure_snapmirror_v2()
  2706. api_args = {
  2707. 'source-volume': source_volume,
  2708. 'source-vserver': source_vserver,
  2709. 'destination-volume': destination_volume,
  2710. 'destination-vserver': destination_vserver,
  2711. 'clear-checkpoint': 'true' if clear_checkpoint else 'false',
  2712. }
  2713. try:
  2714. self.send_request('snapmirror-abort', api_args)
  2715. except netapp_api.NaApiError as e:
  2716. if e.code != netapp_api.ENOTRANSFER_IN_PROGRESS:
  2717. raise
  2718. @na_utils.trace
  2719. def break_snapmirror(self, source_vserver, source_volume,
  2720. destination_vserver, destination_volume):
  2721. """Breaks a data protection SnapMirror relationship."""
  2722. self._ensure_snapmirror_v2()
  2723. api_args = {
  2724. 'source-volume': source_volume,
  2725. 'source-vserver': source_vserver,
  2726. 'destination-volume': destination_volume,
  2727. 'destination-vserver': destination_vserver,
  2728. }
  2729. self.send_request('snapmirror-break', api_args)
  2730. @na_utils.trace
  2731. def modify_snapmirror(self, source_vserver, source_volume,
  2732. destination_vserver, destination_volume,
  2733. schedule=None, policy=None, tries=None,
  2734. max_transfer_rate=None):
  2735. """Modifies a SnapMirror relationship."""
  2736. self._ensure_snapmirror_v2()
  2737. api_args = {
  2738. 'source-volume': source_volume,
  2739. 'source-vserver': source_vserver,
  2740. 'destination-volume': destination_volume,
  2741. 'destination-vserver': destination_vserver,
  2742. }
  2743. if schedule:
  2744. api_args['schedule'] = schedule
  2745. if policy:
  2746. api_args['policy'] = policy
  2747. if tries is not None:
  2748. api_args['tries'] = tries
  2749. if max_transfer_rate is not None:
  2750. api_args['max-transfer-rate'] = max_transfer_rate
  2751. self.send_request('snapmirror-modify', api_args)
  2752. @na_utils.trace
  2753. def delete_snapmirror(self, source_vserver, source_volume,
  2754. destination_vserver, destination_volume):
  2755. """Destroys a SnapMirror relationship."""
  2756. self._ensure_snapmirror_v2()
  2757. api_args = {
  2758. 'query': {
  2759. 'snapmirror-info': {
  2760. 'source-volume': source_volume,
  2761. 'source-vserver': source_vserver,
  2762. 'destination-volume': destination_volume,
  2763. 'destination-vserver': destination_vserver,
  2764. }
  2765. }
  2766. }
  2767. self.send_request('snapmirror-destroy-iter', api_args)
  2768. @na_utils.trace
  2769. def update_snapmirror(self, source_vserver, source_volume,
  2770. destination_vserver, destination_volume):
  2771. """Schedules a snapmirror update."""
  2772. self._ensure_snapmirror_v2()
  2773. api_args = {
  2774. 'source-volume': source_volume,
  2775. 'source-vserver': source_vserver,
  2776. 'destination-volume': destination_volume,
  2777. 'destination-vserver': destination_vserver,
  2778. }
  2779. try:
  2780. self.send_request('snapmirror-update', api_args)
  2781. except netapp_api.NaApiError as e:
  2782. if (e.code != netapp_api.ETRANSFER_IN_PROGRESS and
  2783. e.code != netapp_api.EANOTHER_OP_ACTIVE):
  2784. raise
  2785. @na_utils.trace
  2786. def resume_snapmirror(self, source_vserver, source_volume,
  2787. destination_vserver, destination_volume):
  2788. """Resume a SnapMirror relationship if it is quiesced."""
  2789. self._ensure_snapmirror_v2()
  2790. api_args = {
  2791. 'source-volume': source_volume,
  2792. 'source-vserver': source_vserver,
  2793. 'destination-volume': destination_volume,
  2794. 'destination-vserver': destination_vserver,
  2795. }
  2796. try:
  2797. self.send_request('snapmirror-resume', api_args)
  2798. except netapp_api.NaApiError as e:
  2799. if e.code != netapp_api.ERELATION_NOT_QUIESCED:
  2800. raise
  2801. @na_utils.trace
  2802. def resync_snapmirror(self, source_vserver, source_volume,
  2803. destination_vserver, destination_volume):
  2804. """Resync a SnapMirror relationship."""
  2805. self._ensure_snapmirror_v2()
  2806. api_args = {
  2807. 'source-volume': source_volume,
  2808. 'source-vserver': source_vserver,
  2809. 'destination-volume': destination_volume,
  2810. 'destination-vserver': destination_vserver,
  2811. }
  2812. self.send_request('snapmirror-resync', api_args)
  2813. @na_utils.trace
  2814. def _get_snapmirrors(self, source_vserver=None, source_volume=None,
  2815. destination_vserver=None, destination_volume=None,
  2816. desired_attributes=None):
  2817. query = None
  2818. if (source_vserver or source_volume or destination_vserver or
  2819. destination_volume):
  2820. query = {'snapmirror-info': {}}
  2821. if source_volume:
  2822. query['snapmirror-info']['source-volume'] = source_volume
  2823. if destination_volume:
  2824. query['snapmirror-info']['destination-volume'] = (
  2825. destination_volume)
  2826. if source_vserver:
  2827. query['snapmirror-info']['source-vserver'] = source_vserver
  2828. if destination_vserver:
  2829. query['snapmirror-info']['destination-vserver'] = (
  2830. destination_vserver)
  2831. api_args = {}
  2832. if query:
  2833. api_args['query'] = query
  2834. if desired_attributes:
  2835. api_args['desired-attributes'] = desired_attributes
  2836. result = self.send_iter_request('snapmirror-get-iter', api_args)
  2837. if not self._has_records(result):
  2838. return []
  2839. else:
  2840. return result.get_child_by_name('attributes-list').get_children()
  2841. @na_utils.trace
  2842. def get_snapmirrors(self, source_vserver, source_volume,
  2843. destination_vserver, destination_volume,
  2844. desired_attributes=None):
  2845. """Gets one or more SnapMirror relationships.
  2846. Either the source or destination info may be omitted.
  2847. Desired attributes should be a flat list of attribute names.
  2848. """
  2849. self._ensure_snapmirror_v2()
  2850. if desired_attributes is not None:
  2851. desired_attributes = {
  2852. 'snapmirror-info': {attr: None for attr in desired_attributes},
  2853. }
  2854. result = self._get_snapmirrors(
  2855. source_vserver=source_vserver,
  2856. source_volume=source_volume,
  2857. destination_vserver=destination_vserver,
  2858. destination_volume=destination_volume,
  2859. desired_attributes=desired_attributes)
  2860. snapmirrors = []
  2861. for snapmirror_info in result:
  2862. snapmirror = {}
  2863. for child in snapmirror_info.get_children():
  2864. name = self._strip_xml_namespace(child.get_name())
  2865. snapmirror[name] = child.get_content()
  2866. snapmirrors.append(snapmirror)
  2867. return snapmirrors
  2868. def volume_has_snapmirror_relationships(self, volume):
  2869. """Return True if snapmirror relationships exist for a given volume.
  2870. If we have snapmirror control plane license, we can verify whether
  2871. the given volume is part of any snapmirror relationships.
  2872. """
  2873. try:
  2874. # Check if volume is a source snapmirror volume
  2875. snapmirrors = self.get_snapmirrors(
  2876. volume['owning-vserver-name'], volume['name'], None, None)
  2877. # Check if volume is a destination snapmirror volume
  2878. if not snapmirrors:
  2879. snapmirrors = self.get_snapmirrors(
  2880. None, None, volume['owning-vserver-name'], volume['name'])
  2881. has_snapmirrors = len(snapmirrors) > 0
  2882. except netapp_api.NaApiError:
  2883. msg = _LE("Could not determine if volume %s is part of "
  2884. "existing snapmirror relationships.")
  2885. LOG.exception(msg, volume['name'])
  2886. has_snapmirrors = False
  2887. return has_snapmirrors
  2888. def list_snapmirror_snapshots(self, volume_name, newer_than=None):
  2889. """Gets SnapMirror snapshots on a volume."""
  2890. api_args = {
  2891. 'query': {
  2892. 'snapshot-info': {
  2893. 'dependency': 'snapmirror',
  2894. 'volume': volume_name,
  2895. },
  2896. },
  2897. }
  2898. if newer_than:
  2899. api_args['query']['snapshot-info'][
  2900. 'access-time'] = '>' + newer_than
  2901. result = self.send_iter_request('snapshot-get-iter', api_args)
  2902. attributes_list = result.get_child_by_name(
  2903. 'attributes-list') or netapp_api.NaElement('none')
  2904. return [snapshot_info.get_child_content('name')
  2905. for snapshot_info in attributes_list.get_children()]
  2906. @na_utils.trace
  2907. def start_volume_move(self, volume_name, vserver, destination_aggregate,
  2908. cutover_action='wait'):
  2909. """Moves a FlexVol across Vserver aggregates.
  2910. Requires cluster-scoped credentials.
  2911. """
  2912. self._send_volume_move_request(
  2913. volume_name, vserver, destination_aggregate,
  2914. cutover_action=cutover_action)
  2915. @na_utils.trace
  2916. def check_volume_move(self, volume_name, vserver, destination_aggregate):
  2917. """Moves a FlexVol across Vserver aggregates.
  2918. Requires cluster-scoped credentials.
  2919. """
  2920. self._send_volume_move_request(
  2921. volume_name, vserver, destination_aggregate, validation_only=True)
  2922. @na_utils.trace
  2923. def _send_volume_move_request(self, volume_name, vserver,
  2924. destination_aggregate,
  2925. cutover_action='wait',
  2926. validation_only=False):
  2927. """Send request to check if vol move is possible, or start it.
  2928. :param volume_name: Name of the FlexVol to be moved.
  2929. :param destination_aggregate: Name of the destination aggregate
  2930. :param cutover_action: can have one of ['force', 'defer', 'abort',
  2931. 'wait']. 'force' will force a cutover despite errors (causing
  2932. possible client disruptions), 'wait' will wait for cutover to be
  2933. triggered manually. 'abort' will rollback move on errors on
  2934. cutover, 'defer' will attempt a cutover, but wait for manual
  2935. intervention in case of errors.
  2936. :param validation_only: If set to True, only validates if the volume
  2937. move is possible, does not trigger data copy.
  2938. """
  2939. api_args = {
  2940. 'source-volume': volume_name,
  2941. 'vserver': vserver,
  2942. 'dest-aggr': destination_aggregate,
  2943. 'cutover-action': CUTOVER_ACTION_MAP[cutover_action],
  2944. }
  2945. if validation_only:
  2946. api_args['perform-validation-only'] = 'true'
  2947. self.send_request('volume-move-start', api_args)
  2948. @na_utils.trace
  2949. def abort_volume_move(self, volume_name, vserver):
  2950. """Aborts an existing volume move operation."""
  2951. api_args = {
  2952. 'source-volume': volume_name,
  2953. 'vserver': vserver,
  2954. }
  2955. self.send_request('volume-move-trigger-abort', api_args)
  2956. @na_utils.trace
  2957. def trigger_volume_move_cutover(self, volume_name, vserver, force=True):
  2958. """Triggers the cut-over for a volume in data motion."""
  2959. api_args = {
  2960. 'source-volume': volume_name,
  2961. 'vserver': vserver,
  2962. 'force': 'true' if force else 'false',
  2963. }
  2964. self.send_request('volume-move-trigger-cutover', api_args)
  2965. @na_utils.trace
  2966. def get_volume_move_status(self, volume_name, vserver):
  2967. """Gets the current state of a volume move operation."""
  2968. api_args = {
  2969. 'query': {
  2970. 'volume-move-info': {
  2971. 'volume': volume_name,
  2972. 'vserver': vserver,
  2973. },
  2974. },
  2975. 'desired-attributes': {
  2976. 'volume-move-info': {
  2977. 'percent-complete': None,
  2978. 'estimated-completion-time': None,
  2979. 'state': None,
  2980. 'details': None,
  2981. 'cutover-action': None,
  2982. 'phase': None,
  2983. },
  2984. },
  2985. }
  2986. result = self.send_iter_request('volume-move-get-iter', api_args)
  2987. if not self._has_records(result):
  2988. msg = _("Volume %(vol)s in Vserver %(server)s is not part of any "
  2989. "data motion operations.")
  2990. msg_args = {'vol': volume_name, 'server': vserver}
  2991. raise exception.NetAppException(msg % msg_args)
  2992. attributes_list = result.get_child_by_name(
  2993. 'attributes-list') or netapp_api.NaElement('none')
  2994. volume_move_info = attributes_list.get_child_by_name(
  2995. 'volume-move-info') or netapp_api.NaElement('none')
  2996. status_info = {
  2997. 'percent-complete': volume_move_info.get_child_content(
  2998. 'percent-complete'),
  2999. 'estimated-completion-time': volume_move_info.get_child_content(
  3000. 'estimated-completion-time'),
  3001. 'state': volume_move_info.get_child_content('state'),
  3002. 'details': volume_move_info.get_child_content('details'),
  3003. 'cutover-action': volume_move_info.get_child_content(
  3004. 'cutover-action'),
  3005. 'phase': volume_move_info.get_child_content('phase'),
  3006. }
  3007. return status_info