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 126KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445
  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_name(self, volume_name, new_volume_name):
  1347. """Set flexvol name."""
  1348. api_args = {
  1349. 'volume': volume_name,
  1350. 'new-volume-name': new_volume_name,
  1351. }
  1352. self.send_request('volume-rename', api_args)
  1353. @na_utils.trace
  1354. def manage_volume(self, aggregate_name, volume_name,
  1355. thin_provisioned=False, snapshot_policy=None,
  1356. language=None, dedup_enabled=False,
  1357. compression_enabled=False, max_files=None, **options):
  1358. """Update volume as needed to bring under management as a share."""
  1359. api_args = {
  1360. 'query': {
  1361. 'volume-attributes': {
  1362. 'volume-id-attributes': {
  1363. 'containing-aggregate-name': aggregate_name,
  1364. 'name': volume_name,
  1365. },
  1366. },
  1367. },
  1368. 'attributes': {
  1369. 'volume-attributes': {
  1370. 'volume-inode-attributes': {},
  1371. 'volume-language-attributes': {},
  1372. 'volume-snapshot-attributes': {},
  1373. 'volume-space-attributes': {
  1374. 'space-guarantee': ('none' if thin_provisioned else
  1375. 'volume')
  1376. },
  1377. },
  1378. },
  1379. }
  1380. if language:
  1381. api_args['attributes']['volume-attributes'][
  1382. 'volume-language-attributes']['language'] = language
  1383. if max_files:
  1384. api_args['attributes']['volume-attributes'][
  1385. 'volume-inode-attributes']['files-total'] = max_files
  1386. if snapshot_policy:
  1387. api_args['attributes']['volume-attributes'][
  1388. 'volume-snapshot-attributes'][
  1389. 'snapshot-policy'] = snapshot_policy
  1390. self.send_request('volume-modify-iter', api_args)
  1391. # Efficiency options must be handled separately
  1392. self.update_volume_efficiency_attributes(volume_name,
  1393. dedup_enabled,
  1394. compression_enabled)
  1395. @na_utils.trace
  1396. def update_volume_efficiency_attributes(self, volume_name, dedup_enabled,
  1397. compression_enabled):
  1398. """Update dedupe & compression attributes to match desired values."""
  1399. efficiency_status = self.get_volume_efficiency_status(volume_name)
  1400. if efficiency_status['compression'] != compression_enabled:
  1401. if compression_enabled:
  1402. self.enable_compression(volume_name)
  1403. else:
  1404. self.disable_compression(volume_name)
  1405. if efficiency_status['dedupe'] != dedup_enabled:
  1406. if dedup_enabled:
  1407. self.enable_dedup(volume_name)
  1408. else:
  1409. self.disable_dedup(volume_name)
  1410. @na_utils.trace
  1411. def volume_exists(self, volume_name):
  1412. """Checks if volume exists."""
  1413. LOG.debug('Checking if volume %s exists', volume_name)
  1414. api_args = {
  1415. 'query': {
  1416. 'volume-attributes': {
  1417. 'volume-id-attributes': {
  1418. 'name': volume_name,
  1419. },
  1420. },
  1421. },
  1422. 'desired-attributes': {
  1423. 'volume-attributes': {
  1424. 'volume-id-attributes': {
  1425. 'name': None,
  1426. },
  1427. },
  1428. },
  1429. }
  1430. result = self.send_iter_request('volume-get-iter', api_args)
  1431. return self._has_records(result)
  1432. @na_utils.trace
  1433. def get_aggregate_for_volume(self, volume_name):
  1434. """Get the name of the aggregate containing a volume."""
  1435. api_args = {
  1436. 'query': {
  1437. 'volume-attributes': {
  1438. 'volume-id-attributes': {
  1439. 'name': volume_name,
  1440. },
  1441. },
  1442. },
  1443. 'desired-attributes': {
  1444. 'volume-attributes': {
  1445. 'volume-id-attributes': {
  1446. 'containing-aggregate-name': None,
  1447. 'name': None,
  1448. },
  1449. },
  1450. },
  1451. }
  1452. result = self.send_iter_request('volume-get-iter', api_args)
  1453. attributes_list = result.get_child_by_name(
  1454. 'attributes-list') or netapp_api.NaElement('none')
  1455. volume_attributes = attributes_list.get_child_by_name(
  1456. 'volume-attributes') or netapp_api.NaElement('none')
  1457. volume_id_attributes = volume_attributes.get_child_by_name(
  1458. 'volume-id-attributes') or netapp_api.NaElement('none')
  1459. aggregate = volume_id_attributes.get_child_content(
  1460. 'containing-aggregate-name')
  1461. if not aggregate:
  1462. msg = _('Could not find aggregate for volume %s.')
  1463. raise exception.NetAppException(msg % volume_name)
  1464. return aggregate
  1465. @na_utils.trace
  1466. def volume_has_luns(self, volume_name):
  1467. """Checks if volume has LUNs."""
  1468. LOG.debug('Checking if volume %s has LUNs', volume_name)
  1469. api_args = {
  1470. 'query': {
  1471. 'lun-info': {
  1472. 'volume': volume_name,
  1473. },
  1474. },
  1475. 'desired-attributes': {
  1476. 'lun-info': {
  1477. 'path': None,
  1478. },
  1479. },
  1480. }
  1481. result = self.send_iter_request('lun-get-iter', api_args)
  1482. return self._has_records(result)
  1483. @na_utils.trace
  1484. def volume_has_junctioned_volumes(self, volume_name):
  1485. """Checks if volume has volumes mounted beneath its junction path."""
  1486. junction_path = self.get_volume_junction_path(volume_name)
  1487. if not junction_path:
  1488. return False
  1489. api_args = {
  1490. 'query': {
  1491. 'volume-attributes': {
  1492. 'volume-id-attributes': {
  1493. 'junction-path': junction_path + '/*',
  1494. },
  1495. },
  1496. },
  1497. 'desired-attributes': {
  1498. 'volume-attributes': {
  1499. 'volume-id-attributes': {
  1500. 'name': None,
  1501. },
  1502. },
  1503. },
  1504. }
  1505. result = self.send_iter_request('volume-get-iter', api_args)
  1506. return self._has_records(result)
  1507. @na_utils.trace
  1508. def get_volume(self, volume_name):
  1509. """Returns the volume with the specified name, if present."""
  1510. api_args = {
  1511. 'query': {
  1512. 'volume-attributes': {
  1513. 'volume-id-attributes': {
  1514. 'name': volume_name,
  1515. },
  1516. },
  1517. },
  1518. 'desired-attributes': {
  1519. 'volume-attributes': {
  1520. 'volume-id-attributes': {
  1521. 'containing-aggregate-name': None,
  1522. 'junction-path': None,
  1523. 'name': None,
  1524. 'owning-vserver-name': None,
  1525. 'type': None,
  1526. 'style': None,
  1527. },
  1528. 'volume-space-attributes': {
  1529. 'size': None,
  1530. }
  1531. },
  1532. },
  1533. }
  1534. result = self.send_request('volume-get-iter', api_args)
  1535. attributes_list = result.get_child_by_name(
  1536. 'attributes-list') or netapp_api.NaElement('none')
  1537. volume_attributes_list = attributes_list.get_children()
  1538. if not self._has_records(result):
  1539. raise exception.StorageResourceNotFound(name=volume_name)
  1540. elif len(volume_attributes_list) > 1:
  1541. msg = _('Could not find unique volume %(vol)s.')
  1542. msg_args = {'vol': volume_name}
  1543. raise exception.NetAppException(msg % msg_args)
  1544. volume_attributes = volume_attributes_list[0]
  1545. volume_id_attributes = volume_attributes.get_child_by_name(
  1546. 'volume-id-attributes') or netapp_api.NaElement('none')
  1547. volume_space_attributes = volume_attributes.get_child_by_name(
  1548. 'volume-space-attributes') or netapp_api.NaElement('none')
  1549. volume = {
  1550. 'aggregate': volume_id_attributes.get_child_content(
  1551. 'containing-aggregate-name'),
  1552. 'junction-path': volume_id_attributes.get_child_content(
  1553. 'junction-path'),
  1554. 'name': volume_id_attributes.get_child_content('name'),
  1555. 'owning-vserver-name': volume_id_attributes.get_child_content(
  1556. 'owning-vserver-name'),
  1557. 'type': volume_id_attributes.get_child_content('type'),
  1558. 'style': volume_id_attributes.get_child_content('style'),
  1559. 'size': volume_space_attributes.get_child_content('size'),
  1560. }
  1561. return volume
  1562. @na_utils.trace
  1563. def get_volume_at_junction_path(self, junction_path):
  1564. """Returns the volume with the specified junction path, if present."""
  1565. if not junction_path:
  1566. return None
  1567. api_args = {
  1568. 'query': {
  1569. 'volume-attributes': {
  1570. 'volume-id-attributes': {
  1571. 'junction-path': junction_path,
  1572. },
  1573. },
  1574. },
  1575. 'desired-attributes': {
  1576. 'volume-attributes': {
  1577. 'volume-id-attributes': {
  1578. 'containing-aggregate-name': None,
  1579. 'junction-path': None,
  1580. 'name': None,
  1581. 'type': None,
  1582. 'style': None,
  1583. },
  1584. 'volume-space-attributes': {
  1585. 'size': None,
  1586. }
  1587. },
  1588. },
  1589. }
  1590. result = self.send_iter_request('volume-get-iter', api_args)
  1591. if not self._has_records(result):
  1592. return None
  1593. attributes_list = result.get_child_by_name(
  1594. 'attributes-list') or netapp_api.NaElement('none')
  1595. volume_attributes = attributes_list.get_child_by_name(
  1596. 'volume-attributes') or netapp_api.NaElement('none')
  1597. volume_id_attributes = volume_attributes.get_child_by_name(
  1598. 'volume-id-attributes') or netapp_api.NaElement('none')
  1599. volume_space_attributes = volume_attributes.get_child_by_name(
  1600. 'volume-space-attributes') or netapp_api.NaElement('none')
  1601. volume = {
  1602. 'aggregate': volume_id_attributes.get_child_content(
  1603. 'containing-aggregate-name'),
  1604. 'junction-path': volume_id_attributes.get_child_content(
  1605. 'junction-path'),
  1606. 'name': volume_id_attributes.get_child_content('name'),
  1607. 'type': volume_id_attributes.get_child_content('type'),
  1608. 'style': volume_id_attributes.get_child_content('style'),
  1609. 'size': volume_space_attributes.get_child_content('size'),
  1610. }
  1611. return volume
  1612. @na_utils.trace
  1613. def get_volume_to_manage(self, aggregate_name, volume_name):
  1614. """Get flexvol to be managed by Manila."""
  1615. api_args = {
  1616. 'query': {
  1617. 'volume-attributes': {
  1618. 'volume-id-attributes': {
  1619. 'containing-aggregate-name': aggregate_name,
  1620. 'name': volume_name,
  1621. },
  1622. },
  1623. },
  1624. 'desired-attributes': {
  1625. 'volume-attributes': {
  1626. 'volume-id-attributes': {
  1627. 'containing-aggregate-name': None,
  1628. 'junction-path': None,
  1629. 'name': None,
  1630. 'type': None,
  1631. 'style': None,
  1632. 'owning-vserver-name': None,
  1633. },
  1634. 'volume-space-attributes': {
  1635. 'size': None,
  1636. }
  1637. },
  1638. },
  1639. }
  1640. result = self.send_iter_request('volume-get-iter', api_args)
  1641. if not self._has_records(result):
  1642. return None
  1643. attributes_list = result.get_child_by_name(
  1644. 'attributes-list') or netapp_api.NaElement('none')
  1645. volume_attributes = attributes_list.get_child_by_name(
  1646. 'volume-attributes') or netapp_api.NaElement('none')
  1647. volume_id_attributes = volume_attributes.get_child_by_name(
  1648. 'volume-id-attributes') or netapp_api.NaElement('none')
  1649. volume_space_attributes = volume_attributes.get_child_by_name(
  1650. 'volume-space-attributes') or netapp_api.NaElement('none')
  1651. volume = {
  1652. 'aggregate': volume_id_attributes.get_child_content(
  1653. 'containing-aggregate-name'),
  1654. 'junction-path': volume_id_attributes.get_child_content(
  1655. 'junction-path'),
  1656. 'name': volume_id_attributes.get_child_content('name'),
  1657. 'type': volume_id_attributes.get_child_content('type'),
  1658. 'style': volume_id_attributes.get_child_content('style'),
  1659. 'owning-vserver-name': volume_id_attributes.get_child_content(
  1660. 'owning-vserver-name'),
  1661. 'size': volume_space_attributes.get_child_content('size'),
  1662. }
  1663. return volume
  1664. @na_utils.trace
  1665. def create_volume_clone(self, volume_name, parent_volume_name,
  1666. parent_snapshot_name=None, split=False, **options):
  1667. """Clones a volume."""
  1668. api_args = {
  1669. 'volume': volume_name,
  1670. 'parent-volume': parent_volume_name,
  1671. 'parent-snapshot': parent_snapshot_name,
  1672. 'junction-path': '/%s' % volume_name,
  1673. }
  1674. self.send_request('volume-clone-create', api_args)
  1675. if split:
  1676. self.split_volume_clone(volume_name)
  1677. @na_utils.trace
  1678. def split_volume_clone(self, volume_name):
  1679. """Begins splitting a clone from its parent."""
  1680. try:
  1681. api_args = {'volume': volume_name}
  1682. self.send_request('volume-clone-split-start', api_args)
  1683. except netapp_api.NaApiError as e:
  1684. if e.code == netapp_api.EVOL_CLONE_BEING_SPLIT:
  1685. return
  1686. raise
  1687. @na_utils.trace
  1688. def get_clone_children_for_snapshot(self, volume_name, snapshot_name):
  1689. """Returns volumes that are keeping a snapshot locked."""
  1690. api_args = {
  1691. 'query': {
  1692. 'volume-attributes': {
  1693. 'volume-clone-attributes': {
  1694. 'volume-clone-parent-attributes': {
  1695. 'name': volume_name,
  1696. 'snapshot-name': snapshot_name,
  1697. },
  1698. },
  1699. },
  1700. },
  1701. 'desired-attributes': {
  1702. 'volume-attributes': {
  1703. 'volume-id-attributes': {
  1704. 'name': None,
  1705. },
  1706. },
  1707. },
  1708. }
  1709. result = self.send_iter_request('volume-get-iter', api_args)
  1710. if not self._has_records(result):
  1711. return []
  1712. volume_list = []
  1713. attributes_list = result.get_child_by_name(
  1714. 'attributes-list') or netapp_api.NaElement('none')
  1715. for volume_attributes in attributes_list.get_children():
  1716. volume_id_attributes = volume_attributes.get_child_by_name(
  1717. 'volume-id-attributes') or netapp_api.NaElement('none')
  1718. volume_list.append({
  1719. 'name': volume_id_attributes.get_child_content('name'),
  1720. })
  1721. return volume_list
  1722. @na_utils.trace
  1723. def get_volume_junction_path(self, volume_name, is_style_cifs=False):
  1724. """Gets a volume junction path."""
  1725. api_args = {
  1726. 'volume': volume_name,
  1727. 'is-style-cifs': six.text_type(is_style_cifs).lower(),
  1728. }
  1729. result = self.send_request('volume-get-volume-path', api_args)
  1730. return result.get_child_content('junction')
  1731. @na_utils.trace
  1732. def mount_volume(self, volume_name, junction_path=None):
  1733. """Mounts a volume on a junction path."""
  1734. api_args = {
  1735. 'volume-name': volume_name,
  1736. 'junction-path': (junction_path if junction_path
  1737. else '/%s' % volume_name)
  1738. }
  1739. self.send_request('volume-mount', api_args)
  1740. @na_utils.trace
  1741. def offline_volume(self, volume_name):
  1742. """Offlines a volume."""
  1743. try:
  1744. self.send_request('volume-offline', {'name': volume_name})
  1745. except netapp_api.NaApiError as e:
  1746. if e.code == netapp_api.EVOLUMEOFFLINE:
  1747. return
  1748. raise
  1749. @na_utils.trace
  1750. def _unmount_volume(self, volume_name, force=False):
  1751. """Unmounts a volume."""
  1752. api_args = {
  1753. 'volume-name': volume_name,
  1754. 'force': six.text_type(force).lower(),
  1755. }
  1756. try:
  1757. self.send_request('volume-unmount', api_args)
  1758. except netapp_api.NaApiError as e:
  1759. if e.code == netapp_api.EVOL_NOT_MOUNTED:
  1760. return
  1761. raise
  1762. @na_utils.trace
  1763. def unmount_volume(self, volume_name, force=False, wait_seconds=30):
  1764. """Unmounts a volume, retrying if a clone split is ongoing.
  1765. NOTE(cknight): While unlikely to happen in normal operation, any client
  1766. that tries to delete volumes immediately after creating volume clones
  1767. is likely to experience failures if cDOT isn't quite ready for the
  1768. delete. The volume unmount is the first operation in the delete
  1769. path that fails in this case, and there is no proactive check we can
  1770. use to reliably predict the failure. And there isn't a specific error
  1771. code from volume-unmount, so we have to check for a generic error code
  1772. plus certain language in the error code. It's ugly, but it works, and
  1773. it's better than hard-coding a fixed delay.
  1774. """
  1775. # Do the unmount, handling split-related errors with retries.
  1776. retry_interval = 3 # seconds
  1777. for retry in range(int(wait_seconds / retry_interval)):
  1778. try:
  1779. self._unmount_volume(volume_name, force=force)
  1780. LOG.debug('Volume %s unmounted.', volume_name)
  1781. return
  1782. except netapp_api.NaApiError as e:
  1783. if e.code == netapp_api.EAPIERROR and 'job ID' in e.message:
  1784. msg = _LW('Could not unmount volume %(volume)s due to '
  1785. 'ongoing volume operation: %(exception)s')
  1786. msg_args = {'volume': volume_name, 'exception': e}
  1787. LOG.warning(msg, msg_args)
  1788. time.sleep(retry_interval)
  1789. continue
  1790. raise
  1791. msg = _('Failed to unmount volume %(volume)s after '
  1792. 'waiting for %(wait_seconds)s seconds.')
  1793. msg_args = {'volume': volume_name, 'wait_seconds': wait_seconds}
  1794. LOG.error(msg, msg_args)
  1795. raise exception.NetAppException(msg % msg_args)
  1796. @na_utils.trace
  1797. def delete_volume(self, volume_name):
  1798. """Deletes a volume."""
  1799. self.send_request('volume-destroy', {'name': volume_name})
  1800. @na_utils.trace
  1801. def create_snapshot(self, volume_name, snapshot_name):
  1802. """Creates a volume snapshot."""
  1803. api_args = {'volume': volume_name, 'snapshot': snapshot_name}
  1804. self.send_request('snapshot-create', api_args)
  1805. @na_utils.trace
  1806. def snapshot_exists(self, snapshot_name, volume_name):
  1807. """Checks if Snapshot exists for a specified volume."""
  1808. LOG.debug('Checking if snapshot %(snapshot)s exists for '
  1809. 'volume %(volume)s',
  1810. {'snapshot': snapshot_name, 'volume': volume_name})
  1811. """Gets a single snapshot."""
  1812. api_args = {
  1813. 'query': {
  1814. 'snapshot-info': {
  1815. 'name': snapshot_name,
  1816. 'volume': volume_name,
  1817. },
  1818. },
  1819. 'desired-attributes': {
  1820. 'snapshot-info': {
  1821. 'name': None,
  1822. 'volume': None,
  1823. 'busy': None,
  1824. 'snapshot-owners-list': {
  1825. 'snapshot-owner': None,
  1826. }
  1827. },
  1828. },
  1829. }
  1830. result = self.send_request('snapshot-get-iter', api_args)
  1831. error_record_list = result.get_child_by_name(
  1832. 'volume-errors') or netapp_api.NaElement('none')
  1833. errors = error_record_list.get_children()
  1834. if errors:
  1835. error = errors[0]
  1836. error_code = error.get_child_content('errno')
  1837. error_reason = error.get_child_content('reason')
  1838. msg = _('Could not read information for snapshot %(name)s. '
  1839. 'Code: %(code)s. Reason: %(reason)s')
  1840. msg_args = {
  1841. 'name': snapshot_name,
  1842. 'code': error_code,
  1843. 'reason': error_reason
  1844. }
  1845. if error_code == netapp_api.ESNAPSHOTNOTALLOWED:
  1846. raise exception.SnapshotUnavailable(msg % msg_args)
  1847. else:
  1848. raise exception.NetAppException(msg % msg_args)
  1849. return self._has_records(result)
  1850. @na_utils.trace
  1851. def get_snapshot(self, volume_name, snapshot_name):
  1852. """Gets a single snapshot."""
  1853. api_args = {
  1854. 'query': {
  1855. 'snapshot-info': {
  1856. 'name': snapshot_name,
  1857. 'volume': volume_name,
  1858. },
  1859. },
  1860. 'desired-attributes': {
  1861. 'snapshot-info': {
  1862. 'access-time': None,
  1863. 'name': None,
  1864. 'volume': None,
  1865. 'busy': None,
  1866. 'snapshot-owners-list': {
  1867. 'snapshot-owner': None,
  1868. }
  1869. },
  1870. },
  1871. }
  1872. result = self.send_request('snapshot-get-iter', api_args)
  1873. error_record_list = result.get_child_by_name(
  1874. 'volume-errors') or netapp_api.NaElement('none')
  1875. errors = error_record_list.get_children()
  1876. if errors:
  1877. error = errors[0]
  1878. error_code = error.get_child_content('errno')
  1879. error_reason = error.get_child_content('reason')
  1880. msg = _('Could not read information for snapshot %(name)s. '
  1881. 'Code: %(code)s. Reason: %(reason)s')
  1882. msg_args = {
  1883. 'name': snapshot_name,
  1884. 'code': error_code,
  1885. 'reason': error_reason
  1886. }
  1887. if error_code == netapp_api.ESNAPSHOTNOTALLOWED:
  1888. raise exception.SnapshotUnavailable(msg % msg_args)
  1889. else:
  1890. raise exception.NetAppException(msg % msg_args)
  1891. attributes_list = result.get_child_by_name(
  1892. 'attributes-list') or netapp_api.NaElement('none')
  1893. snapshot_info_list = attributes_list.get_children()
  1894. if not self._has_records(result):
  1895. raise exception.SnapshotResourceNotFound(name=snapshot_name)
  1896. elif len(snapshot_info_list) > 1:
  1897. msg = _('Could not find unique snapshot %(snap)s on '
  1898. 'volume %(vol)s.')
  1899. msg_args = {'snap': snapshot_name, 'vol': volume_name}
  1900. raise exception.NetAppException(msg % msg_args)
  1901. snapshot_info = snapshot_info_list[0]
  1902. snapshot = {
  1903. 'access-time': snapshot_info.get_child_content('access-time'),
  1904. 'name': snapshot_info.get_child_content('name'),
  1905. 'volume': snapshot_info.get_child_content('volume'),
  1906. 'busy': strutils.bool_from_string(
  1907. snapshot_info.get_child_content('busy')),
  1908. }
  1909. snapshot_owners_list = snapshot_info.get_child_by_name(
  1910. 'snapshot-owners-list') or netapp_api.NaElement('none')
  1911. snapshot_owners = set([
  1912. snapshot_owner.get_child_content('owner')
  1913. for snapshot_owner in snapshot_owners_list.get_children()])
  1914. snapshot['owners'] = snapshot_owners
  1915. return snapshot
  1916. @na_utils.trace
  1917. def rename_snapshot(self, volume_name, snapshot_name, new_snapshot_name):
  1918. api_args = {
  1919. 'volume': volume_name,
  1920. 'current-name': snapshot_name,
  1921. 'new-name': new_snapshot_name
  1922. }
  1923. self.send_request('snapshot-rename', api_args)
  1924. @na_utils.trace
  1925. def restore_snapshot(self, volume_name, snapshot_name):
  1926. """Reverts a volume to the specified snapshot."""
  1927. api_args = {
  1928. 'volume': volume_name,
  1929. 'snapshot': snapshot_name,
  1930. }
  1931. self.send_request('snapshot-restore-volume', api_args)
  1932. @na_utils.trace
  1933. def delete_snapshot(self, volume_name, snapshot_name, ignore_owners=False):
  1934. """Deletes a volume snapshot."""
  1935. ignore_owners = ('true' if strutils.bool_from_string(ignore_owners)
  1936. else 'false')
  1937. api_args = {
  1938. 'volume': volume_name,
  1939. 'snapshot': snapshot_name,
  1940. 'ignore-owners': ignore_owners,
  1941. }
  1942. self.send_request('snapshot-delete', api_args)
  1943. @na_utils.trace
  1944. def soft_delete_snapshot(self, volume_name, snapshot_name):
  1945. """Deletes a volume snapshot, or renames it if delete fails."""
  1946. try:
  1947. self.delete_snapshot(volume_name, snapshot_name)
  1948. except netapp_api.NaApiError:
  1949. self.rename_snapshot(volume_name,
  1950. snapshot_name,
  1951. DELETED_PREFIX + snapshot_name)
  1952. msg = _('Soft-deleted snapshot %(snapshot)s on volume %(volume)s.')
  1953. msg_args = {'snapshot': snapshot_name, 'volume': volume_name}
  1954. LOG.info(msg, msg_args)
  1955. @na_utils.trace
  1956. def prune_deleted_snapshots(self):
  1957. """Deletes non-busy snapshots that were previously soft-deleted."""
  1958. deleted_snapshots_map = self._get_deleted_snapshots()
  1959. for vserver in deleted_snapshots_map:
  1960. client = copy.deepcopy(self)
  1961. client.set_vserver(vserver)
  1962. for snapshot in deleted_snapshots_map[vserver]:
  1963. try:
  1964. client.delete_snapshot(snapshot['volume'],
  1965. snapshot['name'])
  1966. except netapp_api.NaApiError:
  1967. msg = _('Could not delete snapshot %(snap)s on '
  1968. 'volume %(volume)s.')
  1969. msg_args = {
  1970. 'snap': snapshot['name'],
  1971. 'volume': snapshot['volume'],
  1972. }
  1973. LOG.exception(msg, msg_args)
  1974. @na_utils.trace
  1975. def _get_deleted_snapshots(self):
  1976. """Returns non-busy, soft-deleted snapshots suitable for reaping."""
  1977. api_args = {
  1978. 'query': {
  1979. 'snapshot-info': {
  1980. 'name': DELETED_PREFIX + '*',
  1981. 'busy': 'false',
  1982. },
  1983. },
  1984. 'desired-attributes': {
  1985. 'snapshot-info': {
  1986. 'name': None,
  1987. 'vserver': None,
  1988. 'volume': None,
  1989. },
  1990. },
  1991. }
  1992. result = self.send_iter_request('snapshot-get-iter', api_args)
  1993. attributes_list = result.get_child_by_name(
  1994. 'attributes-list') or netapp_api.NaElement('none')
  1995. # Build a map of snapshots, one list of snapshots per vserver
  1996. snapshot_map = {}
  1997. for snapshot_info in attributes_list.get_children():
  1998. vserver = snapshot_info.get_child_content('vserver')
  1999. snapshot_list = snapshot_map.get(vserver, [])
  2000. snapshot_list.append({
  2001. 'name': snapshot_info.get_child_content('name'),
  2002. 'volume': snapshot_info.get_child_content('volume'),
  2003. 'vserver': vserver,
  2004. })
  2005. snapshot_map[vserver] = snapshot_list
  2006. return snapshot_map
  2007. @na_utils.trace
  2008. def create_cg_snapshot(self, volume_names, snapshot_name):
  2009. """Creates a consistency group snapshot of one or more flexvols."""
  2010. cg_id = self._start_cg_snapshot(volume_names, snapshot_name)
  2011. if not cg_id:
  2012. msg = _('Could not start consistency group snapshot %s.')
  2013. raise exception.NetAppException(msg % snapshot_name)
  2014. self._commit_cg_snapshot(cg_id)
  2015. @na_utils.trace
  2016. def _start_cg_snapshot(self, volume_names, snapshot_name):
  2017. api_args = {
  2018. 'snapshot': snapshot_name,
  2019. 'timeout': 'relaxed',
  2020. 'volumes': [
  2021. {'volume-name': volume_name} for volume_name in volume_names
  2022. ],
  2023. }
  2024. result = self.send_request('cg-start', api_args)
  2025. return result.get_child_content('cg-id')
  2026. @na_utils.trace
  2027. def _commit_cg_snapshot(self, cg_id):
  2028. api_args = {'cg-id': cg_id}
  2029. self.send_request('cg-commit', api_args)
  2030. @na_utils.trace
  2031. def create_cifs_share(self, share_name):
  2032. share_path = '/%s' % share_name
  2033. api_args = {'path': share_path, 'share-name': share_name}
  2034. self.send_request('cifs-share-create', api_args)
  2035. @na_utils.trace
  2036. def get_cifs_share_access(self, share_name):
  2037. api_args = {
  2038. 'query': {
  2039. 'cifs-share-access-control': {
  2040. 'share': share_name,
  2041. },
  2042. },
  2043. 'desired-attributes': {
  2044. 'cifs-share-access-control': {
  2045. 'user-or-group': None,
  2046. 'permission': None,
  2047. },
  2048. },
  2049. }
  2050. result = self.send_iter_request('cifs-share-access-control-get-iter',
  2051. api_args)
  2052. attributes_list = result.get_child_by_name(
  2053. 'attributes-list') or netapp_api.NaElement('none')
  2054. rules = {}
  2055. for rule in attributes_list.get_children():
  2056. user_or_group = rule.get_child_content('user-or-group')
  2057. permission = rule.get_child_content('permission')
  2058. rules[user_or_group] = permission
  2059. return rules
  2060. @na_utils.trace
  2061. def add_cifs_share_access(self, share_name, user_name, readonly):
  2062. api_args = {
  2063. 'permission': 'read' if readonly else 'full_control',
  2064. 'share': share_name,
  2065. 'user-or-group': user_name,
  2066. }
  2067. self.send_request('cifs-share-access-control-create', api_args)
  2068. @na_utils.trace
  2069. def modify_cifs_share_access(self, share_name, user_name, readonly):
  2070. api_args = {
  2071. 'permission': 'read' if readonly else 'full_control',
  2072. 'share': share_name,
  2073. 'user-or-group': user_name,
  2074. }
  2075. self.send_request('cifs-share-access-control-modify', api_args)
  2076. @na_utils.trace
  2077. def remove_cifs_share_access(self, share_name, user_name):
  2078. api_args = {'user-or-group': user_name, 'share': share_name}
  2079. self.send_request('cifs-share-access-control-delete', api_args)
  2080. @na_utils.trace
  2081. def remove_cifs_share(self, share_name):
  2082. self.send_request('cifs-share-delete', {'share-name': share_name})
  2083. @na_utils.trace
  2084. def add_nfs_export_rule(self, policy_name, client_match, readonly):
  2085. rule_indices = self._get_nfs_export_rule_indices(policy_name,
  2086. client_match)
  2087. if not rule_indices:
  2088. self._add_nfs_export_rule(policy_name, client_match, readonly)
  2089. else:
  2090. # Update first rule and delete the rest
  2091. self._update_nfs_export_rule(
  2092. policy_name, client_match, readonly, rule_indices.pop(0))
  2093. self._remove_nfs_export_rules(policy_name, rule_indices)
  2094. @na_utils.trace
  2095. def _add_nfs_export_rule(self, policy_name, client_match, readonly):
  2096. api_args = {
  2097. 'policy-name': policy_name,
  2098. 'client-match': client_match,
  2099. 'ro-rule': {
  2100. 'security-flavor': 'sys',
  2101. },
  2102. 'rw-rule': {
  2103. 'security-flavor': 'sys' if not readonly else 'never',
  2104. },
  2105. 'super-user-security': {
  2106. 'security-flavor': 'sys',
  2107. },
  2108. }
  2109. self.send_request('export-rule-create', api_args)
  2110. @na_utils.trace
  2111. def _update_nfs_export_rule(self, policy_name, client_match, readonly,
  2112. rule_index):
  2113. api_args = {
  2114. 'policy-name': policy_name,
  2115. 'rule-index': rule_index,
  2116. 'client-match': client_match,
  2117. 'ro-rule': {
  2118. 'security-flavor': 'sys'
  2119. },
  2120. 'rw-rule': {
  2121. 'security-flavor': 'sys' if not readonly else 'never'
  2122. },
  2123. 'super-user-security': {
  2124. 'security-flavor': 'sys'
  2125. },
  2126. }
  2127. self.send_request('export-rule-modify', api_args)
  2128. @na_utils.trace
  2129. def _get_nfs_export_rule_indices(self, policy_name, client_match):
  2130. api_args = {
  2131. 'query': {
  2132. 'export-rule-info': {
  2133. 'policy-name': policy_name,
  2134. 'client-match': client_match,
  2135. },
  2136. },
  2137. 'desired-attributes': {
  2138. 'export-rule-info': {
  2139. 'vserver-name': None,
  2140. 'policy-name': None,
  2141. 'client-match': None,
  2142. 'rule-index': None,
  2143. },
  2144. },
  2145. }
  2146. result = self.send_iter_request('export-rule-get-iter', api_args)
  2147. attributes_list = result.get_child_by_name(
  2148. 'attributes-list') or netapp_api.NaElement('none')
  2149. export_rule_info_list = attributes_list.get_children()
  2150. rule_indices = [int(export_rule_info.get_child_content('rule-index'))
  2151. for export_rule_info in export_rule_info_list]
  2152. rule_indices.sort()
  2153. return [six.text_type(rule_index) for rule_index in rule_indices]
  2154. @na_utils.trace
  2155. def remove_nfs_export_rule(self, policy_name, client_match):
  2156. rule_indices = self._get_nfs_export_rule_indices(policy_name,
  2157. client_match)
  2158. self._remove_nfs_export_rules(policy_name, rule_indices)
  2159. @na_utils.trace
  2160. def _remove_nfs_export_rules(self, policy_name, rule_indices):
  2161. for rule_index in rule_indices:
  2162. api_args = {
  2163. 'policy-name': policy_name,
  2164. 'rule-index': rule_index
  2165. }
  2166. try:
  2167. self.send_request('export-rule-destroy', api_args)
  2168. except netapp_api.NaApiError as e:
  2169. if e.code != netapp_api.EOBJECTNOTFOUND:
  2170. raise
  2171. @na_utils.trace
  2172. def clear_nfs_export_policy_for_volume(self, volume_name):
  2173. self.set_nfs_export_policy_for_volume(volume_name, 'default')
  2174. @na_utils.trace
  2175. def set_nfs_export_policy_for_volume(self, volume_name, policy_name):
  2176. api_args = {
  2177. 'query': {
  2178. 'volume-attributes': {
  2179. 'volume-id-attributes': {
  2180. 'name': volume_name,
  2181. },
  2182. },
  2183. },
  2184. 'attributes': {
  2185. 'volume-attributes': {
  2186. 'volume-export-attributes': {
  2187. 'policy': policy_name,
  2188. },
  2189. },
  2190. },
  2191. }
  2192. self.send_request('volume-modify-iter', api_args)
  2193. @na_utils.trace
  2194. def get_nfs_export_policy_for_volume(self, volume_name):
  2195. """Get the name of the export policy for a volume."""
  2196. api_args = {
  2197. 'query': {
  2198. 'volume-attributes': {
  2199. 'volume-id-attributes': {
  2200. 'name': volume_name,
  2201. },
  2202. },
  2203. },
  2204. 'desired-attributes': {
  2205. 'volume-attributes': {
  2206. 'volume-export-attributes': {
  2207. 'policy': None,
  2208. },
  2209. },
  2210. },
  2211. }
  2212. result = self.send_iter_request('volume-get-iter', api_args)
  2213. attributes_list = result.get_child_by_name(
  2214. 'attributes-list') or netapp_api.NaElement('none')
  2215. volume_attributes = attributes_list.get_child_by_name(
  2216. 'volume-attributes') or netapp_api.NaElement('none')
  2217. volume_export_attributes = volume_attributes.get_child_by_name(
  2218. 'volume-export-attributes') or netapp_api.NaElement('none')
  2219. export_policy = volume_export_attributes.get_child_content('policy')
  2220. if not export_policy:
  2221. msg = _('Could not find export policy for volume %s.')
  2222. raise exception.NetAppException(msg % volume_name)
  2223. return export_policy
  2224. @na_utils.trace
  2225. def create_nfs_export_policy(self, policy_name):
  2226. api_args = {'policy-name': policy_name}
  2227. try:
  2228. self.send_request('export-policy-create', api_args)
  2229. except netapp_api.NaApiError as e:
  2230. if e.code != netapp_api.EDUPLICATEENTRY:
  2231. raise
  2232. @na_utils.trace
  2233. def soft_delete_nfs_export_policy(self, policy_name):
  2234. try:
  2235. self.delete_nfs_export_policy(policy_name)
  2236. except netapp_api.NaApiError:
  2237. # NOTE(cknight): Policy deletion can fail if called too soon after
  2238. # removing from a flexvol. So rename for later harvesting.
  2239. self.rename_nfs_export_policy(policy_name,
  2240. DELETED_PREFIX + policy_name)
  2241. @na_utils.trace
  2242. def delete_nfs_export_policy(self, policy_name):
  2243. api_args = {'policy-name': policy_name}
  2244. try:
  2245. self.send_request('export-policy-destroy', api_args)
  2246. except netapp_api.NaApiError as e:
  2247. if e.code == netapp_api.EOBJECTNOTFOUND:
  2248. return
  2249. raise
  2250. @na_utils.trace
  2251. def rename_nfs_export_policy(self, policy_name, new_policy_name):
  2252. api_args = {
  2253. 'policy-name': policy_name,
  2254. 'new-policy-name': new_policy_name
  2255. }
  2256. self.send_request('export-policy-rename', api_args)
  2257. @na_utils.trace
  2258. def prune_deleted_nfs_export_policies(self):
  2259. deleted_policy_map = self._get_deleted_nfs_export_policies()
  2260. for vserver in deleted_policy_map:
  2261. client = copy.deepcopy(self)
  2262. client.set_vserver(vserver)
  2263. for policy in deleted_policy_map[vserver]:
  2264. try:
  2265. client.delete_nfs_export_policy(policy)
  2266. except netapp_api.NaApiError:
  2267. LOG.debug('Could not delete export policy %s.' % policy)
  2268. @na_utils.trace
  2269. def _get_deleted_nfs_export_policies(self):
  2270. api_args = {
  2271. 'query': {
  2272. 'export-policy-info': {
  2273. 'policy-name': DELETED_PREFIX + '*',
  2274. },
  2275. },
  2276. 'desired-attributes': {
  2277. 'export-policy-info': {
  2278. 'policy-name': None,
  2279. 'vserver': None,
  2280. },
  2281. },
  2282. }
  2283. result = self.send_iter_request('export-policy-get-iter', api_args)
  2284. attributes_list = result.get_child_by_name(
  2285. 'attributes-list') or netapp_api.NaElement('none')
  2286. policy_map = {}
  2287. for export_info in attributes_list.get_children():
  2288. vserver = export_info.get_child_content('vserver')
  2289. policies = policy_map.get(vserver, [])
  2290. policies.append(export_info.get_child_content('policy-name'))
  2291. policy_map[vserver] = policies
  2292. return policy_map
  2293. @na_utils.trace
  2294. def _get_ems_log_destination_vserver(self):
  2295. """Returns the best vserver destination for EMS messages."""
  2296. major, minor = self.get_ontapi_version(cached=True)
  2297. if (major > 1) or (major == 1 and minor > 15):
  2298. # Prefer admin Vserver (requires cluster credentials).
  2299. admin_vservers = self.list_vservers(vserver_type='admin')
  2300. if admin_vservers:
  2301. return admin_vservers[0]
  2302. # Fall back to data Vserver.
  2303. data_vservers = self.list_vservers(vserver_type='data')
  2304. if data_vservers:
  2305. return data_vservers[0]
  2306. # If older API version, or no other Vservers found, use node Vserver.
  2307. node_vservers = self.list_vservers(vserver_type='node')
  2308. if node_vservers:
  2309. return node_vservers[0]
  2310. raise exception.NotFound("No Vserver found to receive EMS messages.")
  2311. @na_utils.trace
  2312. def send_ems_log_message(self, message_dict):
  2313. """Sends a message to the Data ONTAP EMS log."""
  2314. # NOTE(cknight): Cannot use deepcopy on the connection context
  2315. node_client = copy.copy(self)
  2316. node_client.connection = copy.copy(self.connection)
  2317. node_client.connection.set_timeout(25)
  2318. try:
  2319. node_client.set_vserver(self._get_ems_log_destination_vserver())
  2320. node_client.send_request('ems-autosupport-log', message_dict)
  2321. LOG.debug('EMS executed successfully.')
  2322. except netapp_api.NaApiError as e:
  2323. LOG.warning(_LW('Failed to invoke EMS. %s') % e)
  2324. @na_utils.trace
  2325. def get_aggregate(self, aggregate_name):
  2326. """Get aggregate attributes needed for the storage service catalog."""
  2327. if not aggregate_name:
  2328. return {}
  2329. desired_attributes = {
  2330. 'aggr-attributes': {
  2331. 'aggregate-name': None,
  2332. 'aggr-raid-attributes': {
  2333. 'raid-type': None,
  2334. 'is-hybrid': None,
  2335. },
  2336. },
  2337. }
  2338. try:
  2339. aggrs = self._get_aggregates(aggregate_names=[aggregate_name],
  2340. desired_attributes=desired_attributes)
  2341. except netapp_api.NaApiError:
  2342. msg = _('Failed to get info for aggregate %s.')
  2343. LOG.exception(msg % aggregate_name)
  2344. return {}
  2345. if len(aggrs) < 1:
  2346. return {}
  2347. aggr_attributes = aggrs[0]
  2348. aggr_raid_attrs = aggr_attributes.get_child_by_name(
  2349. 'aggr-raid-attributes') or netapp_api.NaElement('none')
  2350. aggregate = {
  2351. 'name': aggr_attributes.get_child_content('aggregate-name'),
  2352. 'raid-type': aggr_raid_attrs.get_child_content('raid-type'),
  2353. 'is-hybrid': strutils.bool_from_string(
  2354. aggr_raid_attrs.get_child_content('is-hybrid')),
  2355. }
  2356. return aggregate
  2357. @na_utils.trace
  2358. def get_aggregate_disk_types(self, aggregate_name):
  2359. """Get the disk type(s) of an aggregate."""
  2360. disk_types = set()
  2361. disk_types.update(self._get_aggregate_disk_types(aggregate_name))
  2362. if self.features.ADVANCED_DISK_PARTITIONING:
  2363. disk_types.update(self._get_aggregate_disk_types(aggregate_name,
  2364. shared=True))
  2365. return list(disk_types) if disk_types else None
  2366. @na_utils.trace
  2367. def _get_aggregate_disk_types(self, aggregate_name, shared=False):
  2368. """Get the disk type(s) of an aggregate."""
  2369. disk_types = set()
  2370. if shared:
  2371. disk_raid_info = {
  2372. 'disk-shared-info': {
  2373. 'aggregate-list': {
  2374. 'shared-aggregate-info': {
  2375. 'aggregate-name': aggregate_name,
  2376. },
  2377. },
  2378. },
  2379. }
  2380. else:
  2381. disk_raid_info = {
  2382. 'disk-aggregate-info': {
  2383. 'aggregate-name': aggregate_name,
  2384. },
  2385. }
  2386. api_args = {
  2387. 'query': {
  2388. 'storage-disk-info': {
  2389. 'disk-raid-info': disk_raid_info,
  2390. },
  2391. },
  2392. 'desired-attributes': {
  2393. 'storage-disk-info': {
  2394. 'disk-raid-info': {
  2395. 'effective-disk-type': None,
  2396. },
  2397. },
  2398. },
  2399. }
  2400. try:
  2401. result = self.send_iter_request('storage-disk-get-iter', api_args)
  2402. except netapp_api.NaApiError:
  2403. msg = _('Failed to get disk info for aggregate %s.')
  2404. LOG.exception(msg % aggregate_name)
  2405. return disk_types
  2406. attributes_list = result.get_child_by_name(
  2407. 'attributes-list') or netapp_api.NaElement('none')
  2408. for storage_disk_info in attributes_list.get_children():
  2409. disk_raid_info = storage_disk_info.get_child_by_name(
  2410. 'disk-raid-info') or netapp_api.NaElement('none')
  2411. disk_type = disk_raid_info.get_child_content(
  2412. 'effective-disk-type')
  2413. if disk_type:
  2414. disk_types.add(disk_type)
  2415. return disk_types
  2416. @na_utils.trace
  2417. def check_for_cluster_credentials(self):
  2418. try:
  2419. self.list_cluster_nodes()
  2420. # API succeeded, so definitely a cluster management LIF
  2421. return True
  2422. except netapp_api.NaApiError as e:
  2423. if e.code == netapp_api.EAPINOTFOUND:
  2424. LOG.debug('Not connected to cluster management LIF.')
  2425. return False
  2426. else:
  2427. raise
  2428. @na_utils.trace
  2429. def create_cluster_peer(self, addresses, username=None, password=None,
  2430. passphrase=None):
  2431. """Creates a cluster peer relationship."""
  2432. api_args = {
  2433. 'peer-addresses': [
  2434. {'remote-inet-address': address} for address in addresses
  2435. ],
  2436. }
  2437. if username:
  2438. api_args['user-name'] = username
  2439. if password:
  2440. api_args['password'] = password
  2441. if passphrase:
  2442. api_args['passphrase'] = passphrase
  2443. self.send_request('cluster-peer-create', api_args)
  2444. @na_utils.trace
  2445. def get_cluster_peers(self, remote_cluster_name=None):
  2446. """Gets one or more cluster peer relationships."""
  2447. api_args = {}
  2448. if remote_cluster_name:
  2449. api_args['query'] = {
  2450. 'cluster-peer-info': {
  2451. 'remote-cluster-name': remote_cluster_name,
  2452. }
  2453. }
  2454. result = self.send_iter_request('cluster-peer-get-iter', api_args)
  2455. if not self._has_records(result):
  2456. return []
  2457. cluster_peers = []
  2458. for cluster_peer_info in result.get_child_by_name(
  2459. 'attributes-list').get_children():
  2460. cluster_peer = {
  2461. 'active-addresses': [],
  2462. 'peer-addresses': []
  2463. }
  2464. active_addresses = cluster_peer_info.get_child_by_name(
  2465. 'active-addresses') or netapp_api.NaElement('none')
  2466. for address in active_addresses.get_children():
  2467. cluster_peer['active-addresses'].append(address.get_content())
  2468. peer_addresses = cluster_peer_info.get_child_by_name(
  2469. 'peer-addresses') or netapp_api.NaElement('none')
  2470. for address in peer_addresses.get_children():
  2471. cluster_peer['peer-addresses'].append(address.get_content())
  2472. cluster_peer['availability'] = cluster_peer_info.get_child_content(
  2473. 'availability')
  2474. cluster_peer['cluster-name'] = cluster_peer_info.get_child_content(
  2475. 'cluster-name')
  2476. cluster_peer['cluster-uuid'] = cluster_peer_info.get_child_content(
  2477. 'cluster-uuid')
  2478. cluster_peer['remote-cluster-name'] = (
  2479. cluster_peer_info.get_child_content('remote-cluster-name'))
  2480. cluster_peer['serial-number'] = (
  2481. cluster_peer_info.get_child_content('serial-number'))
  2482. cluster_peer['timeout'] = cluster_peer_info.get_child_content(
  2483. 'timeout')
  2484. cluster_peers.append(cluster_peer)
  2485. return cluster_peers
  2486. @na_utils.trace
  2487. def delete_cluster_peer(self, cluster_name):
  2488. """Deletes a cluster peer relationship."""
  2489. api_args = {'cluster-name': cluster_name}
  2490. self.send_request('cluster-peer-delete', api_args)
  2491. @na_utils.trace
  2492. def get_cluster_peer_policy(self):
  2493. """Gets the cluster peering policy configuration."""
  2494. if not self.features.CLUSTER_PEER_POLICY:
  2495. return {}
  2496. result = self.send_request('cluster-peer-policy-get')
  2497. attributes = result.get_child_by_name(
  2498. 'attributes') or netapp_api.NaElement('none')
  2499. cluster_peer_policy = attributes.get_child_by_name(
  2500. 'cluster-peer-policy') or netapp_api.NaElement('none')
  2501. policy = {
  2502. 'is-unauthenticated-access-permitted':
  2503. cluster_peer_policy.get_child_content(
  2504. 'is-unauthenticated-access-permitted'),
  2505. 'passphrase-minimum-length':
  2506. cluster_peer_policy.get_child_content(
  2507. 'passphrase-minimum-length'),
  2508. }
  2509. if policy['is-unauthenticated-access-permitted'] is not None:
  2510. policy['is-unauthenticated-access-permitted'] = (
  2511. strutils.bool_from_string(
  2512. policy['is-unauthenticated-access-permitted']))
  2513. if policy['passphrase-minimum-length'] is not None:
  2514. policy['passphrase-minimum-length'] = int(
  2515. policy['passphrase-minimum-length'])
  2516. return policy
  2517. @na_utils.trace
  2518. def set_cluster_peer_policy(self, is_unauthenticated_access_permitted=None,
  2519. passphrase_minimum_length=None):
  2520. """Modifies the cluster peering policy configuration."""
  2521. if not self.features.CLUSTER_PEER_POLICY:
  2522. return
  2523. if (is_unauthenticated_access_permitted is None and
  2524. passphrase_minimum_length is None):
  2525. return
  2526. api_args = {}
  2527. if is_unauthenticated_access_permitted is not None:
  2528. api_args['is-unauthenticated-access-permitted'] = (
  2529. 'true' if strutils.bool_from_string(
  2530. is_unauthenticated_access_permitted) else 'false')
  2531. if passphrase_minimum_length is not None:
  2532. api_args['passphrase-minlength'] = six.text_type(
  2533. passphrase_minimum_length)
  2534. self.send_request('cluster-peer-policy-modify', api_args)
  2535. @na_utils.trace
  2536. def create_vserver_peer(self, vserver_name, peer_vserver_name):
  2537. """Creates a Vserver peer relationship for SnapMirrors."""
  2538. api_args = {
  2539. 'vserver': vserver_name,
  2540. 'peer-vserver': peer_vserver_name,
  2541. 'applications': [
  2542. {'vserver-peer-application': 'snapmirror'},
  2543. ],
  2544. }
  2545. self.send_request('vserver-peer-create', api_args)
  2546. @na_utils.trace
  2547. def delete_vserver_peer(self, vserver_name, peer_vserver_name):
  2548. """Deletes a Vserver peer relationship."""
  2549. api_args = {'vserver': vserver_name, 'peer-vserver': peer_vserver_name}
  2550. self.send_request('vserver-peer-delete', api_args)
  2551. @na_utils.trace
  2552. def accept_vserver_peer(self, vserver_name, peer_vserver_name):
  2553. """Accepts a pending Vserver peer relationship."""
  2554. api_args = {'vserver': vserver_name, 'peer-vserver': peer_vserver_name}
  2555. self.send_request('vserver-peer-accept', api_args)
  2556. @na_utils.trace
  2557. def get_vserver_peers(self, vserver_name=None, peer_vserver_name=None):
  2558. """Gets one or more Vserver peer relationships."""
  2559. api_args = None
  2560. if vserver_name or peer_vserver_name:
  2561. api_args = {'query': {'vserver-peer-info': {}}}
  2562. if vserver_name:
  2563. api_args['query']['vserver-peer-info']['vserver'] = (
  2564. vserver_name)
  2565. if peer_vserver_name:
  2566. api_args['query']['vserver-peer-info']['peer-vserver'] = (
  2567. peer_vserver_name)
  2568. result = self.send_iter_request('vserver-peer-get-iter', api_args)
  2569. if not self._has_records(result):
  2570. return []
  2571. vserver_peers = []
  2572. for vserver_peer_info in result.get_child_by_name(
  2573. 'attributes-list').get_children():
  2574. vserver_peer = {
  2575. 'vserver': vserver_peer_info.get_child_content('vserver'),
  2576. 'peer-vserver':
  2577. vserver_peer_info.get_child_content('peer-vserver'),
  2578. 'peer-state':
  2579. vserver_peer_info.get_child_content('peer-state'),
  2580. 'peer-cluster':
  2581. vserver_peer_info.get_child_content('peer-cluster'),
  2582. }
  2583. vserver_peers.append(vserver_peer)
  2584. return vserver_peers
  2585. def _ensure_snapmirror_v2(self):
  2586. """Verify support for SnapMirror control plane v2."""
  2587. if not self.features.SNAPMIRROR_V2:
  2588. msg = _('SnapMirror features require Data ONTAP 8.2 or later.')
  2589. raise exception.NetAppException(msg)
  2590. @na_utils.trace
  2591. def create_snapmirror(self, source_vserver, source_volume,
  2592. destination_vserver, destination_volume,
  2593. schedule=None, policy=None,
  2594. relationship_type='data_protection'):
  2595. """Creates a SnapMirror relationship (cDOT 8.2 or later only)."""
  2596. self._ensure_snapmirror_v2()
  2597. api_args = {
  2598. 'source-volume': source_volume,
  2599. 'source-vserver': source_vserver,
  2600. 'destination-volume': destination_volume,
  2601. 'destination-vserver': destination_vserver,
  2602. 'relationship-type': relationship_type,
  2603. }
  2604. if schedule:
  2605. api_args['schedule'] = schedule
  2606. if policy:
  2607. api_args['policy'] = policy
  2608. try:
  2609. self.send_request('snapmirror-create', api_args)
  2610. except netapp_api.NaApiError as e:
  2611. if e.code != netapp_api.ERELATION_EXISTS:
  2612. raise
  2613. @na_utils.trace
  2614. def initialize_snapmirror(self, source_vserver, source_volume,
  2615. destination_vserver, destination_volume,
  2616. source_snapshot=None, transfer_priority=None):
  2617. """Initializes a SnapMirror relationship (cDOT 8.2 or later only)."""
  2618. self._ensure_snapmirror_v2()
  2619. api_args = {
  2620. 'source-volume': source_volume,
  2621. 'source-vserver': source_vserver,
  2622. 'destination-volume': destination_volume,
  2623. 'destination-vserver': destination_vserver,
  2624. }
  2625. if source_snapshot:
  2626. api_args['source-snapshot'] = source_snapshot
  2627. if transfer_priority:
  2628. api_args['transfer-priority'] = transfer_priority
  2629. result = self.send_request('snapmirror-initialize', api_args)
  2630. result_info = {}
  2631. result_info['operation-id'] = result.get_child_content(
  2632. 'result-operation-id')
  2633. result_info['status'] = result.get_child_content('result-status')
  2634. result_info['jobid'] = result.get_child_content('result-jobid')
  2635. result_info['error-code'] = result.get_child_content(
  2636. 'result-error-code')
  2637. result_info['error-message'] = result.get_child_content(
  2638. 'result-error-message')
  2639. return result_info
  2640. @na_utils.trace
  2641. def release_snapmirror(self, source_vserver, source_volume,
  2642. destination_vserver, destination_volume,
  2643. relationship_info_only=False):
  2644. """Removes a SnapMirror relationship on the source endpoint."""
  2645. self._ensure_snapmirror_v2()
  2646. api_args = {
  2647. 'query': {
  2648. 'snapmirror-destination-info': {
  2649. 'source-volume': source_volume,
  2650. 'source-vserver': source_vserver,
  2651. 'destination-volume': destination_volume,
  2652. 'destination-vserver': destination_vserver,
  2653. 'relationship-info-only': ('true' if relationship_info_only
  2654. else 'false'),
  2655. }
  2656. }
  2657. }
  2658. self.send_request('snapmirror-release-iter', api_args)
  2659. @na_utils.trace
  2660. def quiesce_snapmirror(self, source_vserver, source_volume,
  2661. destination_vserver, destination_volume):
  2662. """Disables future transfers to a SnapMirror destination."""
  2663. self._ensure_snapmirror_v2()
  2664. api_args = {
  2665. 'source-volume': source_volume,
  2666. 'source-vserver': source_vserver,
  2667. 'destination-volume': destination_volume,
  2668. 'destination-vserver': destination_vserver,
  2669. }
  2670. self.send_request('snapmirror-quiesce', api_args)
  2671. @na_utils.trace
  2672. def abort_snapmirror(self, source_vserver, source_volume,
  2673. destination_vserver, destination_volume,
  2674. clear_checkpoint=False):
  2675. """Stops ongoing transfers for a SnapMirror relationship."""
  2676. self._ensure_snapmirror_v2()
  2677. api_args = {
  2678. 'source-volume': source_volume,
  2679. 'source-vserver': source_vserver,
  2680. 'destination-volume': destination_volume,
  2681. 'destination-vserver': destination_vserver,
  2682. 'clear-checkpoint': 'true' if clear_checkpoint else 'false',
  2683. }
  2684. try:
  2685. self.send_request('snapmirror-abort', api_args)
  2686. except netapp_api.NaApiError as e:
  2687. if e.code != netapp_api.ENOTRANSFER_IN_PROGRESS:
  2688. raise
  2689. @na_utils.trace
  2690. def break_snapmirror(self, source_vserver, source_volume,
  2691. destination_vserver, destination_volume):
  2692. """Breaks a data protection SnapMirror relationship."""
  2693. self._ensure_snapmirror_v2()
  2694. api_args = {
  2695. 'source-volume': source_volume,
  2696. 'source-vserver': source_vserver,
  2697. 'destination-volume': destination_volume,
  2698. 'destination-vserver': destination_vserver,
  2699. }
  2700. self.send_request('snapmirror-break', api_args)
  2701. @na_utils.trace
  2702. def modify_snapmirror(self, source_vserver, source_volume,
  2703. destination_vserver, destination_volume,
  2704. schedule=None, policy=None, tries=None,
  2705. max_transfer_rate=None):
  2706. """Modifies a SnapMirror relationship."""
  2707. self._ensure_snapmirror_v2()
  2708. api_args = {
  2709. 'source-volume': source_volume,
  2710. 'source-vserver': source_vserver,
  2711. 'destination-volume': destination_volume,
  2712. 'destination-vserver': destination_vserver,
  2713. }
  2714. if schedule:
  2715. api_args['schedule'] = schedule
  2716. if policy:
  2717. api_args['policy'] = policy
  2718. if tries is not None:
  2719. api_args['tries'] = tries
  2720. if max_transfer_rate is not None:
  2721. api_args['max-transfer-rate'] = max_transfer_rate
  2722. self.send_request('snapmirror-modify', api_args)
  2723. @na_utils.trace
  2724. def delete_snapmirror(self, source_vserver, source_volume,
  2725. destination_vserver, destination_volume):
  2726. """Destroys a SnapMirror relationship."""
  2727. self._ensure_snapmirror_v2()
  2728. api_args = {
  2729. 'query': {
  2730. 'snapmirror-info': {
  2731. 'source-volume': source_volume,
  2732. 'source-vserver': source_vserver,
  2733. 'destination-volume': destination_volume,
  2734. 'destination-vserver': destination_vserver,
  2735. }
  2736. }
  2737. }
  2738. self.send_request('snapmirror-destroy-iter', api_args)
  2739. @na_utils.trace
  2740. def update_snapmirror(self, source_vserver, source_volume,
  2741. destination_vserver, destination_volume):
  2742. """Schedules a snapmirror update."""
  2743. self._ensure_snapmirror_v2()
  2744. api_args = {
  2745. 'source-volume': source_volume,
  2746. 'source-vserver': source_vserver,
  2747. 'destination-volume': destination_volume,
  2748. 'destination-vserver': destination_vserver,
  2749. }
  2750. try:
  2751. self.send_request('snapmirror-update', api_args)
  2752. except netapp_api.NaApiError as e:
  2753. if (e.code != netapp_api.ETRANSFER_IN_PROGRESS and
  2754. e.code != netapp_api.EANOTHER_OP_ACTIVE):
  2755. raise
  2756. @na_utils.trace
  2757. def resume_snapmirror(self, source_vserver, source_volume,
  2758. destination_vserver, destination_volume):
  2759. """Resume a SnapMirror relationship if it is quiesced."""
  2760. self._ensure_snapmirror_v2()
  2761. api_args = {
  2762. 'source-volume': source_volume,
  2763. 'source-vserver': source_vserver,
  2764. 'destination-volume': destination_volume,
  2765. 'destination-vserver': destination_vserver,
  2766. }
  2767. try:
  2768. self.send_request('snapmirror-resume', api_args)
  2769. except netapp_api.NaApiError as e:
  2770. if e.code != netapp_api.ERELATION_NOT_QUIESCED:
  2771. raise
  2772. @na_utils.trace
  2773. def resync_snapmirror(self, source_vserver, source_volume,
  2774. destination_vserver, destination_volume):
  2775. """Resync a SnapMirror relationship."""
  2776. self._ensure_snapmirror_v2()
  2777. api_args = {
  2778. 'source-volume': source_volume,
  2779. 'source-vserver': source_vserver,
  2780. 'destination-volume': destination_volume,
  2781. 'destination-vserver': destination_vserver,
  2782. }
  2783. self.send_request('snapmirror-resync', api_args)
  2784. @na_utils.trace
  2785. def _get_snapmirrors(self, source_vserver=None, source_volume=None,
  2786. destination_vserver=None, destination_volume=None,
  2787. desired_attributes=None):
  2788. query = None
  2789. if (source_vserver or source_volume or destination_vserver or
  2790. destination_volume):
  2791. query = {'snapmirror-info': {}}
  2792. if source_volume:
  2793. query['snapmirror-info']['source-volume'] = source_volume
  2794. if destination_volume:
  2795. query['snapmirror-info']['destination-volume'] = (
  2796. destination_volume)
  2797. if source_vserver:
  2798. query['snapmirror-info']['source-vserver'] = source_vserver
  2799. if destination_vserver:
  2800. query['snapmirror-info']['destination-vserver'] = (
  2801. destination_vserver)
  2802. api_args = {}
  2803. if query:
  2804. api_args['query'] = query
  2805. if desired_attributes:
  2806. api_args['desired-attributes'] = desired_attributes
  2807. result = self.send_iter_request('snapmirror-get-iter', api_args)
  2808. if not self._has_records(result):
  2809. return []
  2810. else:
  2811. return result.get_child_by_name('attributes-list').get_children()
  2812. @na_utils.trace
  2813. def get_snapmirrors(self, source_vserver, source_volume,
  2814. destination_vserver, destination_volume,
  2815. desired_attributes=None):
  2816. """Gets one or more SnapMirror relationships.
  2817. Either the source or destination info may be omitted.
  2818. Desired attributes should be a flat list of attribute names.
  2819. """
  2820. self._ensure_snapmirror_v2()
  2821. if desired_attributes is not None:
  2822. desired_attributes = {
  2823. 'snapmirror-info': {attr: None for attr in desired_attributes},
  2824. }
  2825. result = self._get_snapmirrors(
  2826. source_vserver=source_vserver,
  2827. source_volume=source_volume,
  2828. destination_vserver=destination_vserver,
  2829. destination_volume=destination_volume,
  2830. desired_attributes=desired_attributes)
  2831. snapmirrors = []
  2832. for snapmirror_info in result:
  2833. snapmirror = {}
  2834. for child in snapmirror_info.get_children():
  2835. name = self._strip_xml_namespace(child.get_name())
  2836. snapmirror[name] = child.get_content()
  2837. snapmirrors.append(snapmirror)
  2838. return snapmirrors
  2839. def volume_has_snapmirror_relationships(self, volume):
  2840. """Return True if snapmirror relationships exist for a given volume.
  2841. If we have snapmirror control plane license, we can verify whether
  2842. the given volume is part of any snapmirror relationships.
  2843. """
  2844. try:
  2845. # Check if volume is a source snapmirror volume
  2846. snapmirrors = self.get_snapmirrors(
  2847. volume['owning-vserver-name'], volume['name'], None, None)
  2848. # Check if volume is a destination snapmirror volume
  2849. if not snapmirrors:
  2850. snapmirrors = self.get_snapmirrors(
  2851. None, None, volume['owning-vserver-name'], volume['name'])
  2852. has_snapmirrors = len(snapmirrors) > 0
  2853. except netapp_api.NaApiError:
  2854. msg = _LE("Could not determine if volume %s is part of "
  2855. "existing snapmirror relationships.")
  2856. LOG.exception(msg, volume['name'])
  2857. has_snapmirrors = False
  2858. return has_snapmirrors
  2859. def list_snapmirror_snapshots(self, volume_name, newer_than=None):
  2860. """Gets SnapMirror snapshots on a volume."""
  2861. api_args = {
  2862. 'query': {
  2863. 'snapshot-info': {
  2864. 'dependency': 'snapmirror',
  2865. 'volume': volume_name,
  2866. },
  2867. },
  2868. }
  2869. if newer_than:
  2870. api_args['query']['snapshot-info'][
  2871. 'access-time'] = '>' + newer_than
  2872. result = self.send_iter_request('snapshot-get-iter', api_args)
  2873. attributes_list = result.get_child_by_name(
  2874. 'attributes-list') or netapp_api.NaElement('none')
  2875. return [snapshot_info.get_child_content('name')
  2876. for snapshot_info in attributes_list.get_children()]
  2877. @na_utils.trace
  2878. def start_volume_move(self, volume_name, vserver, destination_aggregate,
  2879. cutover_action='wait'):
  2880. """Moves a FlexVol across Vserver aggregates.
  2881. Requires cluster-scoped credentials.
  2882. """
  2883. self._send_volume_move_request(
  2884. volume_name, vserver, destination_aggregate,
  2885. cutover_action=cutover_action)
  2886. @na_utils.trace
  2887. def check_volume_move(self, volume_name, vserver, destination_aggregate):
  2888. """Moves a FlexVol across Vserver aggregates.
  2889. Requires cluster-scoped credentials.
  2890. """
  2891. self._send_volume_move_request(
  2892. volume_name, vserver, destination_aggregate, validation_only=True)
  2893. @na_utils.trace
  2894. def _send_volume_move_request(self, volume_name, vserver,
  2895. destination_aggregate,
  2896. cutover_action='wait',
  2897. validation_only=False):
  2898. """Send request to check if vol move is possible, or start it.
  2899. :param volume_name: Name of the FlexVol to be moved.
  2900. :param destination_aggregate: Name of the destination aggregate
  2901. :param cutover_action: can have one of ['force', 'defer', 'abort',
  2902. 'wait']. 'force' will force a cutover despite errors (causing
  2903. possible client disruptions), 'wait' will wait for cutover to be
  2904. triggered manually. 'abort' will rollback move on errors on
  2905. cutover, 'defer' will attempt a cutover, but wait for manual
  2906. intervention in case of errors.
  2907. :param validation_only: If set to True, only validates if the volume
  2908. move is possible, does not trigger data copy.
  2909. """
  2910. api_args = {
  2911. 'source-volume': volume_name,
  2912. 'vserver': vserver,
  2913. 'dest-aggr': destination_aggregate,
  2914. 'cutover-action': CUTOVER_ACTION_MAP[cutover_action],
  2915. }
  2916. if validation_only:
  2917. api_args['perform-validation-only'] = 'true'
  2918. self.send_request('volume-move-start', api_args)
  2919. @na_utils.trace
  2920. def abort_volume_move(self, volume_name, vserver):
  2921. """Aborts an existing volume move operation."""
  2922. api_args = {
  2923. 'source-volume': volume_name,
  2924. 'vserver': vserver,
  2925. }
  2926. self.send_request('volume-move-trigger-abort', api_args)
  2927. @na_utils.trace
  2928. def trigger_volume_move_cutover(self, volume_name, vserver, force=True):
  2929. """Triggers the cut-over for a volume in data motion."""
  2930. api_args = {
  2931. 'source-volume': volume_name,
  2932. 'vserver': vserver,
  2933. 'force': 'true' if force else 'false',
  2934. }
  2935. self.send_request('volume-move-trigger-cutover', api_args)
  2936. @na_utils.trace
  2937. def get_volume_move_status(self, volume_name, vserver):
  2938. """Gets the current state of a volume move operation."""
  2939. api_args = {
  2940. 'query': {
  2941. 'volume-move-info': {
  2942. 'volume': volume_name,
  2943. 'vserver': vserver,
  2944. },
  2945. },
  2946. 'desired-attributes': {
  2947. 'volume-move-info': {
  2948. 'percent-complete': None,
  2949. 'estimated-completion-time': None,
  2950. 'state': None,
  2951. 'details': None,
  2952. 'cutover-action': None,
  2953. 'phase': None,
  2954. },
  2955. },
  2956. }
  2957. result = self.send_iter_request('volume-move-get-iter', api_args)
  2958. if not self._has_records(result):
  2959. msg = _("Volume %(vol)s in Vserver %(server)s is not part of any "
  2960. "data motion operations.")
  2961. msg_args = {'vol': volume_name, 'server': vserver}
  2962. raise exception.NetAppException(msg % msg_args)
  2963. attributes_list = result.get_child_by_name(
  2964. 'attributes-list') or netapp_api.NaElement('none')
  2965. volume_move_info = attributes_list.get_child_by_name(
  2966. 'volume-move-info') or netapp_api.NaElement('none')
  2967. status_info = {
  2968. 'percent-complete': volume_move_info.get_child_content(
  2969. 'percent-complete'),
  2970. 'estimated-completion-time': volume_move_info.get_child_content(
  2971. 'estimated-completion-time'),
  2972. 'state': volume_move_info.get_child_content('state'),
  2973. 'details': volume_move_info.get_child_content('details'),
  2974. 'cutover-action': volume_move_info.get_child_content(
  2975. 'cutover-action'),
  2976. 'phase': volume_move_info.get_child_content('phase'),
  2977. }
  2978. return status_info