nailo2c commented on code in PR #62391: URL: https://github.com/apache/airflow/pull/62391#discussion_r2844060574
########## providers/microsoft/azure/src/airflow/providers/microsoft/azure/hooks/compute.py: ########## @@ -0,0 +1,233 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from functools import cached_property +from typing import Any, cast + +from azure.common.client_factory import get_client_from_auth_file, get_client_from_json_dict +from azure.identity import ClientSecretCredential, DefaultAzureCredential +from azure.identity.aio import ClientSecretCredential as AsyncClientSecretCredential +from azure.mgmt.compute import ComputeManagementClient +from azure.mgmt.compute.aio import ComputeManagementClient as AsyncComputeManagementClient + +from airflow.providers.common.compat.connection import get_async_connection +from airflow.providers.common.compat.sdk import AirflowException +from airflow.providers.microsoft.azure.hooks.base_azure import AzureBaseHook +from airflow.providers.microsoft.azure.utils import ( + get_async_default_azure_credential, + get_sync_default_azure_credential, +) + + +class AzureComputeHook(AzureBaseHook): + """ + A hook to interact with Azure Compute to manage Virtual Machines. + + :param azure_conn_id: :ref:`Azure connection id<howto/connection:azure>` of + a service principal which will be used to manage virtual machines. + """ + + conn_name_attr = "azure_conn_id" + default_conn_name = "azure_default" + conn_type = "azure_compute" + hook_name = "Azure Compute" + + def __init__(self, azure_conn_id: str = default_conn_name) -> None: + super().__init__(sdk_client=ComputeManagementClient, conn_id=azure_conn_id) + + @cached_property + def connection(self) -> ComputeManagementClient: + return self.get_conn() + + def get_conn(self) -> Any: + """ + Authenticate the resource using the connection id passed during init. + + :return: the authenticated ComputeManagementClient. + """ + conn = self.get_connection(self.conn_id) + tenant = conn.extra_dejson.get("tenantId") + + key_path = conn.extra_dejson.get("key_path") + if key_path: + if not key_path.endswith(".json"): + raise AirflowException("Unrecognised extension for key file.") Review Comment: Nice, I didn't know that before. I'll update it accordingly. Thanks for the review! :D -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
