container_volume.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. from typing import Any, Dict
  19. from azure.mgmt.containerinstance.models import AzureFileVolume, Volume
  20. from airflow.hooks.base import BaseHook
  21. class AzureContainerVolumeHook(BaseHook):
  22. """
  23. A hook which wraps an Azure Volume.
  24. :param azure_container_volume_conn_id: Reference to the
  25. :ref:`Azure Container Volume connection id <howto/connection:azure_container_volume>`
  26. of an Azure account of which container volumes should be used.
  27. """
  28. conn_name_attr = "azure_container_volume_conn_id"
  29. default_conn_name = 'azure_container_volume_default'
  30. conn_type = 'azure_container_volume'
  31. hook_name = 'Azure Container Volume'
  32. def __init__(self, azure_container_volume_conn_id: str = 'azure_container_volume_default') -> None:
  33. super().__init__()
  34. self.conn_id = azure_container_volume_conn_id
  35. @staticmethod
  36. def get_connection_form_widgets() -> Dict[str, Any]:
  37. """Returns connection widgets to add to connection form"""
  38. from flask_appbuilder.fieldwidgets import BS3PasswordFieldWidget
  39. from flask_babel import lazy_gettext
  40. from wtforms import PasswordField
  41. return {
  42. "extra__azure_container_volume__connection_string": PasswordField(
  43. lazy_gettext('Blob Storage Connection String (optional)'), widget=BS3PasswordFieldWidget()
  44. ),
  45. }
  46. @staticmethod
  47. def get_ui_field_behaviour() -> Dict[str, Any]:
  48. """Returns custom field behaviour"""
  49. return {
  50. "hidden_fields": ['schema', 'port', 'host', "extra"],
  51. "relabeling": {
  52. 'login': 'Azure Client ID',
  53. 'password': 'Azure Secret',
  54. },
  55. "placeholders": {
  56. 'login': 'client_id (token credentials auth)',
  57. 'password': 'secret (token credentials auth)',
  58. 'extra__azure_container_volume__connection_string': 'connection string auth',
  59. },
  60. }
  61. def get_storagekey(self) -> str:
  62. """Get Azure File Volume storage key"""
  63. conn = self.get_connection(self.conn_id)
  64. service_options = conn.extra_dejson
  65. if 'extra__azure_container_volume__connection_string' in service_options:
  66. for keyvalue in service_options['extra__azure_container_volume__connection_string'].split(";"):
  67. key, value = keyvalue.split("=", 1)
  68. if key == "AccountKey":
  69. return value
  70. return conn.password
  71. def get_file_volume(
  72. self, mount_name: str, share_name: str, storage_account_name: str, read_only: bool = False
  73. ) -> Volume:
  74. """Get Azure File Volume"""
  75. return Volume(
  76. name=mount_name,
  77. azure_file=AzureFileVolume(
  78. share_name=share_name,
  79. storage_account_name=storage_account_name,
  80. read_only=read_only,
  81. storage_account_key=self.get_storagekey(),
  82. ),
  83. )