88from functools import cached_property
99
1010import pytz
11- from azure .core .exceptions import HttpResponseError
12- from azure .identity import ClientSecretCredential
13- from azure .mgmt .compute import ComputeManagementClient
14- from azure .mgmt .iothub import IotHubClient
15- from azure .mgmt .network import NetworkManagementClient
16- from azure .mgmt .network .models import NetworkSecurityGroup , SecurityRule
17- from azure .mgmt .resource .resources import ResourceManagementClient
18- from azure .mgmt .storage import StorageManagementClient
19- from azure .mgmt .subscription import SubscriptionClient
20- from azure .mgmt .subscription .models import SubscriptionState
21- from azure .storage .blob import BlobServiceClient
2211from dateutil import parser
23- from msrestazure .azure_exceptions import CloudError
2412from wait_for import wait_for
2513
2614from wrapanapi .entities import Instance , Template , TemplateMixin , VmMixin , VmState
2715from wrapanapi .exceptions import ImageNotFoundError , MultipleImagesError , VMInstanceNotFound
2816from wrapanapi .systems .base import System
2917
18+ # Lazy imports for Azure dependencies to avoid import errors when Azure packages
19+ # are not properly installed or when users don't need Azure functionality
20+ _azure_imports_loaded = False
21+
22+
23+ def _ensure_azure_imports ():
24+ """Lazily import Azure dependencies only when needed."""
25+ global _azure_imports_loaded
26+ global HttpResponseError , ClientSecretCredential , ComputeManagementClient
27+ global IotHubClient , NetworkManagementClient , NetworkSecurityGroup , SecurityRule
28+ global ResourceManagementClient , StorageManagementClient , SubscriptionClient
29+ global SubscriptionState , BlobServiceClient , CloudError
30+
31+ if not _azure_imports_loaded :
32+ from azure .core .exceptions import HttpResponseError as _HttpResponseError
33+ from azure .identity import ClientSecretCredential as _ClientSecretCredential
34+ from azure .mgmt .compute import ComputeManagementClient as _ComputeManagementClient
35+ from azure .mgmt .iothub import IotHubClient as _IotHubClient
36+ from azure .mgmt .network import NetworkManagementClient as _NetworkManagementClient
37+ from azure .mgmt .network .models import (
38+ NetworkSecurityGroup as _NetworkSecurityGroup ,
39+ )
40+ from azure .mgmt .network .models import (
41+ SecurityRule as _SecurityRule ,
42+ )
43+ from azure .mgmt .resource .resources import (
44+ ResourceManagementClient as _ResourceManagementClient ,
45+ )
46+ from azure .mgmt .storage import StorageManagementClient as _StorageManagementClient
47+ from azure .mgmt .subscription import SubscriptionClient as _SubscriptionClient
48+ from azure .mgmt .subscription .models import SubscriptionState as _SubscriptionState
49+ from azure .storage .blob import BlobServiceClient as _BlobServiceClient
50+ from msrestazure .azure_exceptions import CloudError as _CloudError
51+
52+ HttpResponseError = _HttpResponseError
53+ ClientSecretCredential = _ClientSecretCredential
54+ ComputeManagementClient = _ComputeManagementClient
55+ IotHubClient = _IotHubClient
56+ NetworkManagementClient = _NetworkManagementClient
57+ NetworkSecurityGroup = _NetworkSecurityGroup
58+ SecurityRule = _SecurityRule
59+ ResourceManagementClient = _ResourceManagementClient
60+ StorageManagementClient = _StorageManagementClient
61+ SubscriptionClient = _SubscriptionClient
62+ SubscriptionState = _SubscriptionState
63+ BlobServiceClient = _BlobServiceClient
64+ CloudError = _CloudError
65+
66+ _azure_imports_loaded = True
67+
3068
3169class AzureInstance (Instance ):
3270 state_map = {
@@ -47,6 +85,7 @@ def __init__(self, system, raw=None, **kwargs):
4785 name: name of instance
4886 resource_group: name of resource group this instance is in
4987 """
88+ _ensure_azure_imports ()
5089 self ._resource_group = kwargs .get ("resource_group" )
5190 self ._name = kwargs .get ("name" )
5291 if not self ._name or not self ._resource_group :
@@ -304,6 +343,7 @@ def __init__(self, system, raw=None, **kwargs):
304343 name: name of template
305344 container: container the template is stored in
306345 """
346+ _ensure_azure_imports ()
307347 self ._name = kwargs .get ("name" )
308348 self ._container = kwargs .get ("container" )
309349 if not self ._name or not self ._container :
@@ -488,6 +528,7 @@ class AzureSystem(System, VmMixin, TemplateMixin):
488528 }
489529
490530 def __init__ (self , ** kwargs ):
531+ _ensure_azure_imports ()
491532 super ().__init__ (** kwargs )
492533 self .client_id = kwargs .get ("username" )
493534 self .client_secret = kwargs .get ("password" )
0 commit comments