codecraftai-agent-kit 1.3.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ccai/agentkit/__init__.py +304 -0
- ccai/agentkit/addressresolver.py +177 -0
- ccai/agentkit/agent.py +2945 -0
- ccai/agentkit/agentaddress.py +187 -0
- ccai/agentkit/agentconfiguration.py +581 -0
- ccai/agentkit/agentidentity.py +152 -0
- ccai/agentkit/agentregistry.py +221 -0
- ccai/agentkit/agentstatemachine.py +134 -0
- ccai/agentkit/alternateproviderstrategy.py +36 -0
- ccai/agentkit/approvalgate.py +238 -0
- ccai/agentkit/approvalrequest.py +250 -0
- ccai/agentkit/asyncagent.py +187 -0
- ccai/agentkit/breakpoint.py +150 -0
- ccai/agentkit/builtinagentmetrics.py +114 -0
- ccai/agentkit/builtins/__init__.py +11 -0
- ccai/agentkit/builtins/filereadtool.py +90 -0
- ccai/agentkit/builtins/filewritetool.py +93 -0
- ccai/agentkit/builtins/httprequesttool.py +169 -0
- ccai/agentkit/builtins/shellcommandtool.py +190 -0
- ccai/agentkit/cachedresultstrategy.py +43 -0
- ccai/agentkit/checkpointmanager.py +219 -0
- ccai/agentkit/circuitbreaker.py +230 -0
- ccai/agentkit/circuitbreakerconfiguration.py +158 -0
- ccai/agentkit/circuitbreakerevent.py +158 -0
- ccai/agentkit/circuitbreakerhealthcheck.py +32 -0
- ccai/agentkit/circuitbreakerregistry.py +116 -0
- ccai/agentkit/circuitbreakerstate.py +44 -0
- ccai/agentkit/clearcacheaction.py +46 -0
- ccai/agentkit/concurrencybridge.py +143 -0
- ccai/agentkit/concurrencymodel.py +19 -0
- ccai/agentkit/conditionevaluator.py +594 -0
- ccai/agentkit/configurationchangeevent.py +179 -0
- ccai/agentkit/configurationerrors.py +152 -0
- ccai/agentkit/configurationlayer.py +160 -0
- ccai/agentkit/configurationlayersource.py +46 -0
- ccai/agentkit/configurationmanager.py +680 -0
- ccai/agentkit/configurationschema.py +224 -0
- ccai/agentkit/configurationschemafield.py +217 -0
- ccai/agentkit/configurationserializer.py +163 -0
- ccai/agentkit/configurationvalidationresult.py +118 -0
- ccai/agentkit/contextsummarizer.py +170 -0
- ccai/agentkit/contextwindow.py +264 -0
- ccai/agentkit/contextwindowstrategy.py +57 -0
- ccai/agentkit/correlationcontext.py +156 -0
- ccai/agentkit/costestimate.py +139 -0
- ccai/agentkit/costtracker.py +121 -0
- ccai/agentkit/deadletterentry.py +134 -0
- ccai/agentkit/deadletterquery.py +119 -0
- ccai/agentkit/deadletterqueue.py +123 -0
- ccai/agentkit/deadletterstore.py +86 -0
- ccai/agentkit/debuginspector.py +211 -0
- ccai/agentkit/debugsession.py +223 -0
- ccai/agentkit/defaultvaluestrategy.py +43 -0
- ccai/agentkit/degradationchangeevent.py +120 -0
- ccai/agentkit/degradationlevel.py +44 -0
- ccai/agentkit/degradationmanager.py +212 -0
- ccai/agentkit/degradationpolicy.py +130 -0
- ccai/agentkit/degradationstrategy.py +66 -0
- ccai/agentkit/embeddingprovider.py +106 -0
- ccai/agentkit/errors.py +95 -0
- ccai/agentkit/executionrecord.py +213 -0
- ccai/agentkit/executionrecorder.py +180 -0
- ccai/agentkit/executionrecording.py +230 -0
- ccai/agentkit/executionreplayer.py +187 -0
- ccai/agentkit/expirationpolicy.py +116 -0
- ccai/agentkit/fallbackchain.py +82 -0
- ccai/agentkit/featureflag.py +154 -0
- ccai/agentkit/featureflagevaluator.py +109 -0
- ccai/agentkit/featureflagregistry.py +258 -0
- ccai/agentkit/filedeadletterstore.py +112 -0
- ccai/agentkit/filememorystore.py +291 -0
- ccai/agentkit/healthcheck.py +46 -0
- ccai/agentkit/healthcheckregistry.py +51 -0
- ccai/agentkit/healthmonitor.py +258 -0
- ccai/agentkit/healthstatus.py +105 -0
- ccai/agentkit/hotreloadmonitor.py +214 -0
- ccai/agentkit/hotreloadpolicy.py +160 -0
- ccai/agentkit/inmemorydeadletterstore.py +61 -0
- ccai/agentkit/inmemorystore.py +173 -0
- ccai/agentkit/jsonmessageserializer.py +89 -0
- ccai/agentkit/jsonoutputparser.py +50 -0
- ccai/agentkit/lifecyclestate.py +22 -0
- ccai/agentkit/llmconversation.py +153 -0
- ccai/agentkit/llmerrors.py +193 -0
- ccai/agentkit/llmmessage.py +158 -0
- ccai/agentkit/llmprovider.py +158 -0
- ccai/agentkit/llmproviderregistry.py +200 -0
- ccai/agentkit/llmresponse.py +190 -0
- ccai/agentkit/llmstreamchunk.py +65 -0
- ccai/agentkit/llmstreamhandler.py +48 -0
- ccai/agentkit/llmtoolbridge.py +154 -0
- ccai/agentkit/logexporter.py +316 -0
- ccai/agentkit/logformatter.py +169 -0
- ccai/agentkit/memoryconfiguration.py +243 -0
- ccai/agentkit/memoryentry.py +254 -0
- ccai/agentkit/memoryerrors.py +135 -0
- ccai/agentkit/memoryindex.py +213 -0
- ccai/agentkit/memoryquery.py +248 -0
- ccai/agentkit/memoryqueryresult.py +86 -0
- ccai/agentkit/memoryscope.py +54 -0
- ccai/agentkit/memorystore.py +260 -0
- ccai/agentkit/memorystorehealthcheck.py +33 -0
- ccai/agentkit/memorystoreregistry.py +106 -0
- ccai/agentkit/message.py +310 -0
- ccai/agentkit/messageenvelope.py +144 -0
- ccai/agentkit/messagepattern.py +50 -0
- ccai/agentkit/messagerouter.py +290 -0
- ccai/agentkit/messageserializer.py +106 -0
- ccai/agentkit/messageserializerregistry.py +163 -0
- ccai/agentkit/messagingconfiguration.py +397 -0
- ccai/agentkit/messagingerrors.py +338 -0
- ccai/agentkit/metricdefinition.py +164 -0
- ccai/agentkit/metricmeasurement.py +178 -0
- ccai/agentkit/metricscollector.py +258 -0
- ccai/agentkit/metricsexporter.py +303 -0
- ccai/agentkit/metricsregistry.py +130 -0
- ccai/agentkit/metricssnapshot.py +155 -0
- ccai/agentkit/metrictype.py +51 -0
- ccai/agentkit/msgpackmessageserializer.py +97 -0
- ccai/agentkit/observabilityconfiguration.py +546 -0
- ccai/agentkit/observabilityerror.py +67 -0
- ccai/agentkit/observabilitypipeline.py +252 -0
- ccai/agentkit/outputparser.py +82 -0
- ccai/agentkit/outputschema.py +142 -0
- ccai/agentkit/parallelbranch.py +87 -0
- ccai/agentkit/platform/__init__.py +43 -0
- ccai/agentkit/platform/adapters/__init__.py +18 -0
- ccai/agentkit/platform/adapters/awsplatformadapter.py +1272 -0
- ccai/agentkit/platform/adapters/azureplatformadapter.py +1227 -0
- ccai/agentkit/platform/adapters/gcpplatformadapter.py +1257 -0
- ccai/agentkit/platform/adapters/localplatformadapter.py +956 -0
- ccai/agentkit/platform/adapters/platformadapter.py +139 -0
- ccai/agentkit/platform/deployment/__init__.py +15 -0
- ccai/agentkit/platform/deployment/deploymentdescriptor.py +234 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorparser.py +178 -0
- ccai/agentkit/platform/deployment/deploymentdescriptorprinter.py +97 -0
- ccai/agentkit/platform/errors.py +224 -0
- ccai/agentkit/platform/platformconfiguration.py +412 -0
- ccai/agentkit/platform/platformdetector.py +125 -0
- ccai/agentkit/platform/platformruntime.py +331 -0
- ccai/agentkit/platform/platformserviceregistry.py +149 -0
- ccai/agentkit/platform/platformtype.py +60 -0
- ccai/agentkit/platform/services/__init__.py +19 -0
- ccai/agentkit/platform/services/computeservice.py +112 -0
- ccai/agentkit/platform/services/invocationresult.py +173 -0
- ccai/agentkit/platform/services/messagingservice.py +135 -0
- ccai/agentkit/platform/services/platformservice.py +111 -0
- ccai/agentkit/platform/services/queuemessage.py +193 -0
- ccai/agentkit/platform/services/storageobject.py +195 -0
- ccai/agentkit/platform/services/storageservice.py +159 -0
- ccai/agentkit/pluginconfiguration.py +241 -0
- ccai/agentkit/plugindependency.py +129 -0
- ccai/agentkit/plugindescriptor.py +225 -0
- ccai/agentkit/pluginlifecyclehook.py +41 -0
- ccai/agentkit/pluginmanager.py +360 -0
- ccai/agentkit/pluginstatus.py +47 -0
- ccai/agentkit/pooledagent.py +116 -0
- ccai/agentkit/prompttemplate.py +221 -0
- ccai/agentkit/prompttemplateregistry.py +145 -0
- ccai/agentkit/protobufmessageserializer.py +144 -0
- ccai/agentkit/pubsubbroker.py +311 -0
- ccai/agentkit/py.typed +0 -0
- ccai/agentkit/reconnecttransportaction.py +47 -0
- ccai/agentkit/regexoutputparser.py +68 -0
- ccai/agentkit/requestresponsehandler.py +274 -0
- ccai/agentkit/resetcircuitbreakeraction.py +48 -0
- ccai/agentkit/resilienceconfiguration.py +191 -0
- ccai/agentkit/resilienceerrors.py +103 -0
- ccai/agentkit/resiliencepipeline.py +201 -0
- ccai/agentkit/retryattempt.py +158 -0
- ccai/agentkit/retryexecutor.py +157 -0
- ccai/agentkit/retrypolicy.py +195 -0
- ccai/agentkit/retryresult.py +186 -0
- ccai/agentkit/selfhealingaction.py +42 -0
- ccai/agentkit/selfhealingpolicy.py +67 -0
- ccai/agentkit/selfhealingresult.py +130 -0
- ccai/agentkit/stepexecutionstatus.py +52 -0
- ccai/agentkit/stepresult.py +198 -0
- ccai/agentkit/steps/__init__.py +9 -0
- ccai/agentkit/steps/agentstep.py +247 -0
- ccai/agentkit/steps/approvalstep.py +276 -0
- ccai/agentkit/steps/llmstep.py +403 -0
- ccai/agentkit/steps/subworkflowstep.py +270 -0
- ccai/agentkit/steps/toolstep.py +265 -0
- ccai/agentkit/structuredlogentry.py +222 -0
- ccai/agentkit/structuredlogger.py +189 -0
- ccai/agentkit/subscription.py +141 -0
- ccai/agentkit/threadedagent.py +94 -0
- ccai/agentkit/tokenusage.py +136 -0
- ccai/agentkit/tool.py +203 -0
- ccai/agentkit/toolchain.py +268 -0
- ccai/agentkit/toolchainexecutor.py +134 -0
- ccai/agentkit/toolchainresult.py +166 -0
- ccai/agentkit/tooldescriptor.py +203 -0
- ccai/agentkit/toolerrors.py +160 -0
- ccai/agentkit/toolexecutor.py +168 -0
- ccai/agentkit/toolfactory.py +60 -0
- ccai/agentkit/toolregistry.py +184 -0
- ccai/agentkit/toolresult.py +142 -0
- ccai/agentkit/toolschema.py +197 -0
- ccai/agentkit/tracecontext.py +294 -0
- ccai/agentkit/traceevent.py +166 -0
- ccai/agentkit/traceexporter.py +305 -0
- ccai/agentkit/tracer.py +234 -0
- ccai/agentkit/tracespan.py +349 -0
- ccai/agentkit/transitionrecord.py +84 -0
- ccai/agentkit/transport.py +334 -0
- ccai/agentkit/transportconfiguration.py +436 -0
- ccai/agentkit/transporthealthcheck.py +36 -0
- ccai/agentkit/transportregistry.py +224 -0
- ccai/agentkit/transports/__init__.py +28 -0
- ccai/agentkit/transports/grpctransport.py +410 -0
- ccai/agentkit/transports/httptransport.py +423 -0
- ccai/agentkit/transports/messagequeuetransport.py +489 -0
- ccai/agentkit/transports/websockettransport.py +473 -0
- ccai/agentkit/workflowcheckpoint.py +208 -0
- ccai/agentkit/workflowconfiguration.py +295 -0
- ccai/agentkit/workflowcontext.py +249 -0
- ccai/agentkit/workflowdefinition.py +790 -0
- ccai/agentkit/workflowedge.py +128 -0
- ccai/agentkit/workflowerrors.py +295 -0
- ccai/agentkit/workflowexecutionstatus.py +48 -0
- ccai/agentkit/workflowexecutor.py +1121 -0
- ccai/agentkit/workflowregistry.py +185 -0
- ccai/agentkit/workflowresult.py +250 -0
- ccai/agentkit/workflowstep.py +195 -0
- codecraftai_agent_kit-1.3.0.dist-info/METADATA +99 -0
- codecraftai_agent_kit-1.3.0.dist-info/RECORD +230 -0
- codecraftai_agent_kit-1.3.0.dist-info/WHEEL +4 -0
- codecraftai_agent_kit-1.3.0.dist-info/licenses/LICENSE.md +204 -0
|
@@ -0,0 +1,1227 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Defines the AzurePlatformAdapter and its Azure service implementations
|
|
3
|
+
for running agents on Microsoft Azure. Provides AzureComputeService
|
|
4
|
+
(Azure Functions), AzureStorageService (Azure Blob Storage), and
|
|
5
|
+
AzureMessagingService (Azure Service Bus).
|
|
6
|
+
|
|
7
|
+
.. module:: ccai.agentkit.platform.adapters.azureplatformadapter
|
|
8
|
+
:platform: Darwin, Linux, Unix, Windows
|
|
9
|
+
:synopsis: Azure platform adapter with Functions, Blob Storage, and Service Bus services.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import logging
|
|
14
|
+
import os
|
|
15
|
+
import time
|
|
16
|
+
import uuid
|
|
17
|
+
|
|
18
|
+
from datetime import datetime
|
|
19
|
+
|
|
20
|
+
from ccai.agentkit.platform.adapters.platformadapter import PlatformAdapter
|
|
21
|
+
from ccai.agentkit.platform.errors import ComputeError
|
|
22
|
+
from ccai.agentkit.platform.errors import MessagingError
|
|
23
|
+
from ccai.agentkit.platform.errors import StorageError
|
|
24
|
+
from ccai.agentkit.platform.platformconfiguration import PlatformConfiguration
|
|
25
|
+
from ccai.agentkit.platform.platformtype import PlatformType
|
|
26
|
+
from ccai.agentkit.platform.services.computeservice import ComputeService
|
|
27
|
+
from ccai.agentkit.platform.services.invocationresult import InvocationResult
|
|
28
|
+
from ccai.agentkit.platform.services.messagingservice import MessagingService
|
|
29
|
+
from ccai.agentkit.platform.services.queuemessage import QueueMessage
|
|
30
|
+
from ccai.agentkit.platform.services.storageobject import StorageObject
|
|
31
|
+
from ccai.agentkit.platform.services.storageservice import StorageService
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
logger = logging.getLogger("ccai.agentkit.platform.adapters")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
# ---------------------------------------------------------------------------
|
|
38
|
+
# AzureComputeService
|
|
39
|
+
# ---------------------------------------------------------------------------
|
|
40
|
+
|
|
41
|
+
class AzureComputeService(ComputeService):
|
|
42
|
+
"""
|
|
43
|
+
Compute service implementation that wraps Azure Functions invocation
|
|
44
|
+
via the Azure SDK. Supports credential resolution from
|
|
45
|
+
PlatformConfiguration and service endpoint overrides.
|
|
46
|
+
|
|
47
|
+
:param platform_type: The PlatformType (expected to be AZURE).
|
|
48
|
+
:param configuration: An optional PlatformConfiguration.
|
|
49
|
+
:param region: The Azure region string.
|
|
50
|
+
:param functions_client: An optional pre-configured Azure Functions client.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def __init__(self, platform_type: PlatformType,
|
|
54
|
+
configuration: PlatformConfiguration = None,
|
|
55
|
+
region: str = None,
|
|
56
|
+
functions_client=None) -> None:
|
|
57
|
+
super().__init__(
|
|
58
|
+
service_name="azure-functions",
|
|
59
|
+
platform_type=platform_type,
|
|
60
|
+
configuration=configuration
|
|
61
|
+
)
|
|
62
|
+
self._region = region
|
|
63
|
+
self._functions_client = functions_client
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
def _get_client(self):
|
|
67
|
+
"""
|
|
68
|
+
Returns the Azure Functions client, creating one if not already
|
|
69
|
+
provided.
|
|
70
|
+
|
|
71
|
+
:returns: An Azure Functions client.
|
|
72
|
+
:raises: ComputeError
|
|
73
|
+
"""
|
|
74
|
+
if self._functions_client is not None:
|
|
75
|
+
rtnval = self._functions_client
|
|
76
|
+
return rtnval
|
|
77
|
+
|
|
78
|
+
try:
|
|
79
|
+
from azure.identity import DefaultAzureCredential
|
|
80
|
+
from azure.mgmt.web import WebSiteManagementClient
|
|
81
|
+
|
|
82
|
+
credential = None
|
|
83
|
+
if self._configuration is not None:
|
|
84
|
+
resolved = self._configuration.resolved_credentials
|
|
85
|
+
if len(resolved) > 0:
|
|
86
|
+
first_cred = list(resolved.values())[0]
|
|
87
|
+
credential = first_cred
|
|
88
|
+
|
|
89
|
+
if credential is None:
|
|
90
|
+
credential = DefaultAzureCredential()
|
|
91
|
+
|
|
92
|
+
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID", "")
|
|
93
|
+
|
|
94
|
+
client_kwargs = {}
|
|
95
|
+
if self._configuration is not None:
|
|
96
|
+
overrides = self._configuration.service_endpoint_overrides
|
|
97
|
+
if "functions" in overrides:
|
|
98
|
+
client_kwargs["base_url"] = overrides["functions"]
|
|
99
|
+
|
|
100
|
+
self._functions_client = WebSiteManagementClient(
|
|
101
|
+
credential=credential,
|
|
102
|
+
subscription_id=subscription_id,
|
|
103
|
+
**client_kwargs
|
|
104
|
+
)
|
|
105
|
+
rtnval = self._functions_client
|
|
106
|
+
|
|
107
|
+
except Exception as exc:
|
|
108
|
+
err_msg_lines = [
|
|
109
|
+
"Failed to create Azure Functions client.",
|
|
110
|
+
" Region: {!r}".format(self._region),
|
|
111
|
+
" Error: {}".format(str(exc))
|
|
112
|
+
]
|
|
113
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
114
|
+
logger.warning("%s", err_msg)
|
|
115
|
+
raise ComputeError(
|
|
116
|
+
err_msg,
|
|
117
|
+
function_name="client_init",
|
|
118
|
+
cause=exc,
|
|
119
|
+
platform_type=PlatformType.AZURE
|
|
120
|
+
)
|
|
121
|
+
|
|
122
|
+
return rtnval
|
|
123
|
+
|
|
124
|
+
def invoke(self, function_name: str, payload: dict,
|
|
125
|
+
timeout: float = None) -> InvocationResult:
|
|
126
|
+
"""
|
|
127
|
+
Invokes an Azure Function synchronously via HTTP trigger.
|
|
128
|
+
|
|
129
|
+
:param function_name: Name of the Azure Function to invoke.
|
|
130
|
+
:param payload: Dictionary payload serialized as JSON.
|
|
131
|
+
:param timeout: Optional timeout in seconds.
|
|
132
|
+
|
|
133
|
+
:returns: An InvocationResult with the execution outcome.
|
|
134
|
+
:raises: ComputeError
|
|
135
|
+
"""
|
|
136
|
+
request_id = str(uuid.uuid4())
|
|
137
|
+
start_time = time.monotonic()
|
|
138
|
+
|
|
139
|
+
try:
|
|
140
|
+
client = self._get_client()
|
|
141
|
+
|
|
142
|
+
response = client.invoke(
|
|
143
|
+
function_name=function_name,
|
|
144
|
+
payload=payload,
|
|
145
|
+
timeout=timeout
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
duration = time.monotonic() - start_time
|
|
149
|
+
|
|
150
|
+
if hasattr(response, "status_code"):
|
|
151
|
+
status_code = response.status_code
|
|
152
|
+
else:
|
|
153
|
+
status_code = getattr(response, "status", 200)
|
|
154
|
+
|
|
155
|
+
if hasattr(response, "text"):
|
|
156
|
+
response_body = response.text
|
|
157
|
+
elif hasattr(response, "content"):
|
|
158
|
+
response_body = response.content
|
|
159
|
+
else:
|
|
160
|
+
response_body = str(response)
|
|
161
|
+
|
|
162
|
+
if status_code >= 400:
|
|
163
|
+
rtnval = InvocationResult(
|
|
164
|
+
status="error",
|
|
165
|
+
response=response_body,
|
|
166
|
+
error_message="HTTP {}".format(status_code),
|
|
167
|
+
duration=duration,
|
|
168
|
+
request_id=request_id
|
|
169
|
+
)
|
|
170
|
+
else:
|
|
171
|
+
rtnval = InvocationResult(
|
|
172
|
+
status="success",
|
|
173
|
+
response=response_body,
|
|
174
|
+
duration=duration,
|
|
175
|
+
request_id=request_id
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
except ComputeError:
|
|
179
|
+
raise
|
|
180
|
+
|
|
181
|
+
except Exception as exc:
|
|
182
|
+
err_msg_lines = [
|
|
183
|
+
"Azure Functions invocation failed.",
|
|
184
|
+
" Function: {!r}".format(function_name),
|
|
185
|
+
" Region: {!r}".format(self._region),
|
|
186
|
+
" Error: {}".format(str(exc))
|
|
187
|
+
]
|
|
188
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
189
|
+
logger.warning("%s", err_msg)
|
|
190
|
+
raise ComputeError(
|
|
191
|
+
err_msg,
|
|
192
|
+
function_name=function_name,
|
|
193
|
+
cause=exc,
|
|
194
|
+
platform_type=PlatformType.AZURE
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
return rtnval
|
|
198
|
+
|
|
199
|
+
def invoke_async(self, function_name: str, payload: dict) -> str:
|
|
200
|
+
"""
|
|
201
|
+
Invokes an Azure Function asynchronously.
|
|
202
|
+
|
|
203
|
+
:param function_name: Name of the Azure Function to invoke.
|
|
204
|
+
:param payload: Dictionary payload serialized as JSON.
|
|
205
|
+
|
|
206
|
+
:returns: A request identifier string.
|
|
207
|
+
:raises: ComputeError
|
|
208
|
+
"""
|
|
209
|
+
request_id = str(uuid.uuid4())
|
|
210
|
+
|
|
211
|
+
try:
|
|
212
|
+
client = self._get_client()
|
|
213
|
+
client.invoke(
|
|
214
|
+
function_name=function_name,
|
|
215
|
+
payload=payload,
|
|
216
|
+
async_mode=True
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
except ComputeError:
|
|
220
|
+
raise
|
|
221
|
+
|
|
222
|
+
except Exception as exc:
|
|
223
|
+
err_msg_lines = [
|
|
224
|
+
"Azure Functions async invocation failed.",
|
|
225
|
+
" Function: {!r}".format(function_name),
|
|
226
|
+
" Region: {!r}".format(self._region),
|
|
227
|
+
" Error: {}".format(str(exc))
|
|
228
|
+
]
|
|
229
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
230
|
+
logger.warning("%s", err_msg)
|
|
231
|
+
raise ComputeError(
|
|
232
|
+
err_msg,
|
|
233
|
+
function_name=function_name,
|
|
234
|
+
cause=exc,
|
|
235
|
+
platform_type=PlatformType.AZURE
|
|
236
|
+
)
|
|
237
|
+
|
|
238
|
+
rtnval = request_id
|
|
239
|
+
return rtnval
|
|
240
|
+
|
|
241
|
+
def get_invocation_status(self, request_id: str) -> InvocationResult:
|
|
242
|
+
"""
|
|
243
|
+
Returns a status for async invocations.
|
|
244
|
+
|
|
245
|
+
:param request_id: The request identifier from invoke_async.
|
|
246
|
+
|
|
247
|
+
:returns: An InvocationResult representing the current status.
|
|
248
|
+
:raises: ComputeError
|
|
249
|
+
"""
|
|
250
|
+
rtnval = InvocationResult(
|
|
251
|
+
status="success",
|
|
252
|
+
response=None,
|
|
253
|
+
request_id=request_id
|
|
254
|
+
)
|
|
255
|
+
return rtnval
|
|
256
|
+
|
|
257
|
+
def list_functions(self) -> list:
|
|
258
|
+
"""
|
|
259
|
+
Returns a list of available Azure Function names.
|
|
260
|
+
|
|
261
|
+
:returns: List of function name strings.
|
|
262
|
+
:raises: ComputeError
|
|
263
|
+
"""
|
|
264
|
+
try:
|
|
265
|
+
client = self._get_client()
|
|
266
|
+
response = client.list_functions()
|
|
267
|
+
functions = getattr(response, "value", response) if response is not None else []
|
|
268
|
+
rtnval = [getattr(f, "name", str(f)) for f in functions]
|
|
269
|
+
|
|
270
|
+
except ComputeError:
|
|
271
|
+
raise
|
|
272
|
+
|
|
273
|
+
except Exception as exc:
|
|
274
|
+
err_msg_lines = [
|
|
275
|
+
"Failed to list Azure Functions.",
|
|
276
|
+
" Region: {!r}".format(self._region),
|
|
277
|
+
" Error: {}".format(str(exc))
|
|
278
|
+
]
|
|
279
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
280
|
+
logger.warning("%s", err_msg)
|
|
281
|
+
raise ComputeError(
|
|
282
|
+
err_msg,
|
|
283
|
+
function_name="list_functions",
|
|
284
|
+
cause=exc,
|
|
285
|
+
platform_type=PlatformType.AZURE
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
return rtnval
|
|
289
|
+
|
|
290
|
+
def is_available(self) -> bool:
|
|
291
|
+
"""
|
|
292
|
+
Checks whether the Azure Functions service is available.
|
|
293
|
+
|
|
294
|
+
:returns: True if available, False otherwise.
|
|
295
|
+
"""
|
|
296
|
+
try:
|
|
297
|
+
self.list_functions()
|
|
298
|
+
rtnval = True
|
|
299
|
+
except Exception:
|
|
300
|
+
rtnval = False
|
|
301
|
+
|
|
302
|
+
return rtnval
|
|
303
|
+
|
|
304
|
+
def health_check(self) -> bool:
|
|
305
|
+
"""
|
|
306
|
+
Health check for Azure Functions service.
|
|
307
|
+
|
|
308
|
+
:returns: True if the service is healthy.
|
|
309
|
+
"""
|
|
310
|
+
rtnval = self.is_available()
|
|
311
|
+
return rtnval
|
|
312
|
+
|
|
313
|
+
|
|
314
|
+
# ---------------------------------------------------------------------------
|
|
315
|
+
# AzureStorageService
|
|
316
|
+
# ---------------------------------------------------------------------------
|
|
317
|
+
|
|
318
|
+
class AzureStorageService(StorageService):
|
|
319
|
+
"""
|
|
320
|
+
Storage service implementation that wraps Azure Blob Storage
|
|
321
|
+
operations via the Azure SDK. Supports credential resolution from
|
|
322
|
+
PlatformConfiguration and service endpoint overrides.
|
|
323
|
+
|
|
324
|
+
:param platform_type: The PlatformType (expected to be AZURE).
|
|
325
|
+
:param configuration: An optional PlatformConfiguration.
|
|
326
|
+
:param region: The Azure region string.
|
|
327
|
+
:param blob_service_client: An optional pre-configured Azure Blob Service client.
|
|
328
|
+
"""
|
|
329
|
+
|
|
330
|
+
def __init__(self, platform_type: PlatformType,
|
|
331
|
+
configuration: PlatformConfiguration = None,
|
|
332
|
+
region: str = None,
|
|
333
|
+
blob_service_client=None) -> None:
|
|
334
|
+
super().__init__(
|
|
335
|
+
service_name="azure-blob-storage",
|
|
336
|
+
platform_type=platform_type,
|
|
337
|
+
configuration=configuration
|
|
338
|
+
)
|
|
339
|
+
self._region = region
|
|
340
|
+
self._blob_service_client = blob_service_client
|
|
341
|
+
return
|
|
342
|
+
|
|
343
|
+
def _get_client(self):
|
|
344
|
+
"""
|
|
345
|
+
Returns the Azure Blob Service client, creating one if not
|
|
346
|
+
already provided.
|
|
347
|
+
|
|
348
|
+
:returns: An Azure BlobServiceClient.
|
|
349
|
+
:raises: StorageError
|
|
350
|
+
"""
|
|
351
|
+
if self._blob_service_client is not None:
|
|
352
|
+
rtnval = self._blob_service_client
|
|
353
|
+
return rtnval
|
|
354
|
+
|
|
355
|
+
try:
|
|
356
|
+
from azure.identity import DefaultAzureCredential
|
|
357
|
+
from azure.storage.blob import BlobServiceClient
|
|
358
|
+
|
|
359
|
+
credential = None
|
|
360
|
+
if self._configuration is not None:
|
|
361
|
+
resolved = self._configuration.resolved_credentials
|
|
362
|
+
if len(resolved) > 0:
|
|
363
|
+
first_cred = list(resolved.values())[0]
|
|
364
|
+
credential = first_cred
|
|
365
|
+
|
|
366
|
+
if credential is None:
|
|
367
|
+
credential = DefaultAzureCredential()
|
|
368
|
+
|
|
369
|
+
account_url = None
|
|
370
|
+
if self._configuration is not None:
|
|
371
|
+
overrides = self._configuration.service_endpoint_overrides
|
|
372
|
+
if "blob_storage" in overrides:
|
|
373
|
+
account_url = overrides["blob_storage"]
|
|
374
|
+
|
|
375
|
+
if account_url is None:
|
|
376
|
+
account_name = os.environ.get("AZURE_STORAGE_ACCOUNT", "")
|
|
377
|
+
account_url = "https://{}.blob.core.windows.net".format(account_name)
|
|
378
|
+
|
|
379
|
+
self._blob_service_client = BlobServiceClient(
|
|
380
|
+
account_url=account_url,
|
|
381
|
+
credential=credential
|
|
382
|
+
)
|
|
383
|
+
rtnval = self._blob_service_client
|
|
384
|
+
|
|
385
|
+
except Exception as exc:
|
|
386
|
+
err_msg_lines = [
|
|
387
|
+
"Failed to create Azure Blob Storage client.",
|
|
388
|
+
" Region: {!r}".format(self._region),
|
|
389
|
+
" Error: {}".format(str(exc))
|
|
390
|
+
]
|
|
391
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
392
|
+
logger.warning("%s", err_msg)
|
|
393
|
+
raise StorageError(
|
|
394
|
+
err_msg,
|
|
395
|
+
operation_name="client_init",
|
|
396
|
+
container_name="",
|
|
397
|
+
cause=exc,
|
|
398
|
+
platform_type=PlatformType.AZURE
|
|
399
|
+
)
|
|
400
|
+
|
|
401
|
+
return rtnval
|
|
402
|
+
|
|
403
|
+
def put_object(self, container_name: str, object_key: str,
|
|
404
|
+
content: bytes, content_type: str = None,
|
|
405
|
+
metadata: dict = None) -> None:
|
|
406
|
+
"""
|
|
407
|
+
Stores an object in an Azure Blob Storage container.
|
|
408
|
+
|
|
409
|
+
:param container_name: Name of the blob container.
|
|
410
|
+
:param object_key: Key identifying the blob.
|
|
411
|
+
:param content: Object content as bytes.
|
|
412
|
+
:param content_type: Optional MIME content type string.
|
|
413
|
+
:param metadata: Optional metadata dictionary.
|
|
414
|
+
|
|
415
|
+
:raises: StorageError
|
|
416
|
+
"""
|
|
417
|
+
try:
|
|
418
|
+
client = self._get_client()
|
|
419
|
+
blob_client = client.get_blob_client(
|
|
420
|
+
container=container_name,
|
|
421
|
+
blob=object_key
|
|
422
|
+
)
|
|
423
|
+
|
|
424
|
+
upload_kwargs = {}
|
|
425
|
+
if content_type is not None:
|
|
426
|
+
from azure.storage.blob import ContentSettings
|
|
427
|
+
upload_kwargs["content_settings"] = ContentSettings(
|
|
428
|
+
content_type=content_type
|
|
429
|
+
)
|
|
430
|
+
|
|
431
|
+
if metadata is not None:
|
|
432
|
+
upload_kwargs["metadata"] = metadata
|
|
433
|
+
|
|
434
|
+
blob_client.upload_blob(content, overwrite=True, **upload_kwargs)
|
|
435
|
+
|
|
436
|
+
except StorageError:
|
|
437
|
+
raise
|
|
438
|
+
|
|
439
|
+
except Exception as exc:
|
|
440
|
+
err_msg_lines = [
|
|
441
|
+
"Failed to put object in Azure Blob Storage.",
|
|
442
|
+
" Container: {!r}".format(container_name),
|
|
443
|
+
" Key: {!r}".format(object_key),
|
|
444
|
+
" Error: {}".format(str(exc))
|
|
445
|
+
]
|
|
446
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
447
|
+
logger.warning("%s", err_msg)
|
|
448
|
+
raise StorageError(
|
|
449
|
+
err_msg,
|
|
450
|
+
operation_name="put_object",
|
|
451
|
+
container_name=container_name,
|
|
452
|
+
object_key=object_key,
|
|
453
|
+
cause=exc,
|
|
454
|
+
platform_type=PlatformType.AZURE
|
|
455
|
+
)
|
|
456
|
+
|
|
457
|
+
return
|
|
458
|
+
|
|
459
|
+
def get_object(self, container_name: str, object_key: str) -> StorageObject:
|
|
460
|
+
"""
|
|
461
|
+
Retrieves an object from an Azure Blob Storage container.
|
|
462
|
+
|
|
463
|
+
:param container_name: Name of the blob container.
|
|
464
|
+
:param object_key: Key identifying the blob.
|
|
465
|
+
|
|
466
|
+
:returns: A StorageObject instance.
|
|
467
|
+
:raises: StorageError
|
|
468
|
+
"""
|
|
469
|
+
try:
|
|
470
|
+
client = self._get_client()
|
|
471
|
+
blob_client = client.get_blob_client(
|
|
472
|
+
container=container_name,
|
|
473
|
+
blob=object_key
|
|
474
|
+
)
|
|
475
|
+
|
|
476
|
+
download = blob_client.download_blob()
|
|
477
|
+
content = download.readall()
|
|
478
|
+
|
|
479
|
+
properties = blob_client.get_blob_properties()
|
|
480
|
+
content_type = getattr(
|
|
481
|
+
getattr(properties, "content_settings", None),
|
|
482
|
+
"content_type", "application/octet-stream"
|
|
483
|
+
)
|
|
484
|
+
metadata = getattr(properties, "metadata", {}) or {}
|
|
485
|
+
size = getattr(properties, "size", len(content))
|
|
486
|
+
last_modified = getattr(properties, "last_modified", None)
|
|
487
|
+
|
|
488
|
+
rtnval = StorageObject(
|
|
489
|
+
key=object_key,
|
|
490
|
+
content=content,
|
|
491
|
+
content_type=content_type if content_type is not None else "application/octet-stream",
|
|
492
|
+
metadata=metadata,
|
|
493
|
+
size=size,
|
|
494
|
+
last_modified=last_modified
|
|
495
|
+
)
|
|
496
|
+
|
|
497
|
+
except StorageError:
|
|
498
|
+
raise
|
|
499
|
+
|
|
500
|
+
except Exception as exc:
|
|
501
|
+
err_msg_lines = [
|
|
502
|
+
"Failed to get object from Azure Blob Storage.",
|
|
503
|
+
" Container: {!r}".format(container_name),
|
|
504
|
+
" Key: {!r}".format(object_key),
|
|
505
|
+
" Error: {}".format(str(exc))
|
|
506
|
+
]
|
|
507
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
508
|
+
logger.warning("%s", err_msg)
|
|
509
|
+
raise StorageError(
|
|
510
|
+
err_msg,
|
|
511
|
+
operation_name="get_object",
|
|
512
|
+
container_name=container_name,
|
|
513
|
+
object_key=object_key,
|
|
514
|
+
cause=exc,
|
|
515
|
+
platform_type=PlatformType.AZURE
|
|
516
|
+
)
|
|
517
|
+
|
|
518
|
+
return rtnval
|
|
519
|
+
|
|
520
|
+
def delete_object(self, container_name: str, object_key: str) -> None:
|
|
521
|
+
"""
|
|
522
|
+
Removes a blob from an Azure Blob Storage container.
|
|
523
|
+
|
|
524
|
+
:param container_name: Name of the blob container.
|
|
525
|
+
:param object_key: Key identifying the blob.
|
|
526
|
+
|
|
527
|
+
:raises: StorageError
|
|
528
|
+
"""
|
|
529
|
+
try:
|
|
530
|
+
client = self._get_client()
|
|
531
|
+
blob_client = client.get_blob_client(
|
|
532
|
+
container=container_name,
|
|
533
|
+
blob=object_key
|
|
534
|
+
)
|
|
535
|
+
blob_client.delete_blob()
|
|
536
|
+
|
|
537
|
+
except StorageError:
|
|
538
|
+
raise
|
|
539
|
+
|
|
540
|
+
except Exception as exc:
|
|
541
|
+
err_msg_lines = [
|
|
542
|
+
"Failed to delete object from Azure Blob Storage.",
|
|
543
|
+
" Container: {!r}".format(container_name),
|
|
544
|
+
" Key: {!r}".format(object_key),
|
|
545
|
+
" Error: {}".format(str(exc))
|
|
546
|
+
]
|
|
547
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
548
|
+
logger.warning("%s", err_msg)
|
|
549
|
+
raise StorageError(
|
|
550
|
+
err_msg,
|
|
551
|
+
operation_name="delete_object",
|
|
552
|
+
container_name=container_name,
|
|
553
|
+
object_key=object_key,
|
|
554
|
+
cause=exc,
|
|
555
|
+
platform_type=PlatformType.AZURE
|
|
556
|
+
)
|
|
557
|
+
|
|
558
|
+
return
|
|
559
|
+
|
|
560
|
+
def list_objects(self, container_name: str, prefix: str = None) -> list:
|
|
561
|
+
"""
|
|
562
|
+
Returns a list of blob names in an Azure container.
|
|
563
|
+
|
|
564
|
+
:param container_name: Name of the blob container.
|
|
565
|
+
:param prefix: Optional prefix filter string.
|
|
566
|
+
|
|
567
|
+
:returns: List of blob name strings.
|
|
568
|
+
:raises: StorageError
|
|
569
|
+
"""
|
|
570
|
+
try:
|
|
571
|
+
client = self._get_client()
|
|
572
|
+
container_client = client.get_container_client(container_name)
|
|
573
|
+
|
|
574
|
+
list_kwargs = {}
|
|
575
|
+
if prefix is not None:
|
|
576
|
+
list_kwargs["name_starts_with"] = prefix
|
|
577
|
+
|
|
578
|
+
blobs = container_client.list_blobs(**list_kwargs)
|
|
579
|
+
rtnval = [blob.name for blob in blobs]
|
|
580
|
+
|
|
581
|
+
except StorageError:
|
|
582
|
+
raise
|
|
583
|
+
|
|
584
|
+
except Exception as exc:
|
|
585
|
+
err_msg_lines = [
|
|
586
|
+
"Failed to list objects in Azure Blob Storage.",
|
|
587
|
+
" Container: {!r}".format(container_name),
|
|
588
|
+
" Error: {}".format(str(exc))
|
|
589
|
+
]
|
|
590
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
591
|
+
logger.warning("%s", err_msg)
|
|
592
|
+
raise StorageError(
|
|
593
|
+
err_msg,
|
|
594
|
+
operation_name="list_objects",
|
|
595
|
+
container_name=container_name,
|
|
596
|
+
cause=exc,
|
|
597
|
+
platform_type=PlatformType.AZURE
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
return rtnval
|
|
601
|
+
|
|
602
|
+
def object_exists(self, container_name: str, object_key: str) -> bool:
|
|
603
|
+
"""
|
|
604
|
+
Checks whether a blob exists in an Azure container.
|
|
605
|
+
|
|
606
|
+
:param container_name: Name of the blob container.
|
|
607
|
+
:param object_key: Key identifying the blob.
|
|
608
|
+
|
|
609
|
+
:returns: True if the blob exists, False otherwise.
|
|
610
|
+
:raises: StorageError
|
|
611
|
+
"""
|
|
612
|
+
try:
|
|
613
|
+
client = self._get_client()
|
|
614
|
+
blob_client = client.get_blob_client(
|
|
615
|
+
container=container_name,
|
|
616
|
+
blob=object_key
|
|
617
|
+
)
|
|
618
|
+
blob_client.get_blob_properties()
|
|
619
|
+
rtnval = True
|
|
620
|
+
|
|
621
|
+
except StorageError:
|
|
622
|
+
raise
|
|
623
|
+
|
|
624
|
+
except Exception as exc:
|
|
625
|
+
error_code = getattr(exc, "error_code", "")
|
|
626
|
+
if error_code == "BlobNotFound" or "BlobNotFound" in str(exc):
|
|
627
|
+
rtnval = False
|
|
628
|
+
else:
|
|
629
|
+
err_msg_lines = [
|
|
630
|
+
"Failed to check object existence in Azure Blob Storage.",
|
|
631
|
+
" Container: {!r}".format(container_name),
|
|
632
|
+
" Key: {!r}".format(object_key),
|
|
633
|
+
" Error: {}".format(str(exc))
|
|
634
|
+
]
|
|
635
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
636
|
+
logger.warning("%s", err_msg)
|
|
637
|
+
raise StorageError(
|
|
638
|
+
err_msg,
|
|
639
|
+
operation_name="object_exists",
|
|
640
|
+
container_name=container_name,
|
|
641
|
+
object_key=object_key,
|
|
642
|
+
cause=exc,
|
|
643
|
+
platform_type=PlatformType.AZURE
|
|
644
|
+
)
|
|
645
|
+
|
|
646
|
+
return rtnval
|
|
647
|
+
|
|
648
|
+
def create_container(self, container_name: str) -> None:
|
|
649
|
+
"""
|
|
650
|
+
Creates an Azure Blob Storage container.
|
|
651
|
+
|
|
652
|
+
:param container_name: Name of the container to create.
|
|
653
|
+
|
|
654
|
+
:raises: StorageError
|
|
655
|
+
"""
|
|
656
|
+
try:
|
|
657
|
+
client = self._get_client()
|
|
658
|
+
client.create_container(container_name)
|
|
659
|
+
|
|
660
|
+
except StorageError:
|
|
661
|
+
raise
|
|
662
|
+
|
|
663
|
+
except Exception as exc:
|
|
664
|
+
err_msg_lines = [
|
|
665
|
+
"Failed to create container in Azure Blob Storage.",
|
|
666
|
+
" Container: {!r}".format(container_name),
|
|
667
|
+
" Error: {}".format(str(exc))
|
|
668
|
+
]
|
|
669
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
670
|
+
logger.warning("%s", err_msg)
|
|
671
|
+
raise StorageError(
|
|
672
|
+
err_msg,
|
|
673
|
+
operation_name="create_container",
|
|
674
|
+
container_name=container_name,
|
|
675
|
+
cause=exc,
|
|
676
|
+
platform_type=PlatformType.AZURE
|
|
677
|
+
)
|
|
678
|
+
|
|
679
|
+
return
|
|
680
|
+
|
|
681
|
+
def delete_container(self, container_name: str) -> None:
|
|
682
|
+
"""
|
|
683
|
+
Removes an Azure Blob Storage container.
|
|
684
|
+
|
|
685
|
+
:param container_name: Name of the container to remove.
|
|
686
|
+
|
|
687
|
+
:raises: StorageError
|
|
688
|
+
"""
|
|
689
|
+
try:
|
|
690
|
+
client = self._get_client()
|
|
691
|
+
client.delete_container(container_name)
|
|
692
|
+
|
|
693
|
+
except StorageError:
|
|
694
|
+
raise
|
|
695
|
+
|
|
696
|
+
except Exception as exc:
|
|
697
|
+
err_msg_lines = [
|
|
698
|
+
"Failed to delete container from Azure Blob Storage.",
|
|
699
|
+
" Container: {!r}".format(container_name),
|
|
700
|
+
" Error: {}".format(str(exc))
|
|
701
|
+
]
|
|
702
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
703
|
+
logger.warning("%s", err_msg)
|
|
704
|
+
raise StorageError(
|
|
705
|
+
err_msg,
|
|
706
|
+
operation_name="delete_container",
|
|
707
|
+
container_name=container_name,
|
|
708
|
+
cause=exc,
|
|
709
|
+
platform_type=PlatformType.AZURE
|
|
710
|
+
)
|
|
711
|
+
|
|
712
|
+
return
|
|
713
|
+
|
|
714
|
+
def list_containers(self) -> list:
|
|
715
|
+
"""
|
|
716
|
+
Returns a list of Azure Blob Storage container names.
|
|
717
|
+
|
|
718
|
+
:returns: List of container name strings.
|
|
719
|
+
:raises: StorageError
|
|
720
|
+
"""
|
|
721
|
+
try:
|
|
722
|
+
client = self._get_client()
|
|
723
|
+
containers = client.list_containers()
|
|
724
|
+
rtnval = [c.name for c in containers]
|
|
725
|
+
|
|
726
|
+
except StorageError:
|
|
727
|
+
raise
|
|
728
|
+
|
|
729
|
+
except Exception as exc:
|
|
730
|
+
err_msg_lines = [
|
|
731
|
+
"Failed to list containers in Azure Blob Storage.",
|
|
732
|
+
" Error: {}".format(str(exc))
|
|
733
|
+
]
|
|
734
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
735
|
+
logger.warning("%s", err_msg)
|
|
736
|
+
raise StorageError(
|
|
737
|
+
err_msg,
|
|
738
|
+
operation_name="list_containers",
|
|
739
|
+
container_name="",
|
|
740
|
+
cause=exc,
|
|
741
|
+
platform_type=PlatformType.AZURE
|
|
742
|
+
)
|
|
743
|
+
|
|
744
|
+
return rtnval
|
|
745
|
+
|
|
746
|
+
def is_available(self) -> bool:
|
|
747
|
+
"""
|
|
748
|
+
Checks whether the Azure Blob Storage service is available.
|
|
749
|
+
|
|
750
|
+
:returns: True if available, False otherwise.
|
|
751
|
+
"""
|
|
752
|
+
try:
|
|
753
|
+
self.list_containers()
|
|
754
|
+
rtnval = True
|
|
755
|
+
except Exception:
|
|
756
|
+
rtnval = False
|
|
757
|
+
|
|
758
|
+
return rtnval
|
|
759
|
+
|
|
760
|
+
def health_check(self) -> bool:
|
|
761
|
+
"""
|
|
762
|
+
Health check for Azure Blob Storage service.
|
|
763
|
+
|
|
764
|
+
:returns: True if the service is healthy.
|
|
765
|
+
"""
|
|
766
|
+
rtnval = self.is_available()
|
|
767
|
+
return rtnval
|
|
768
|
+
|
|
769
|
+
|
|
770
|
+
# ---------------------------------------------------------------------------
|
|
771
|
+
# AzureMessagingService
|
|
772
|
+
# ---------------------------------------------------------------------------
|
|
773
|
+
|
|
774
|
+
class AzureMessagingService(MessagingService):
|
|
775
|
+
"""
|
|
776
|
+
Messaging service implementation that wraps Azure Service Bus
|
|
777
|
+
operations via the Azure SDK. Supports credential resolution from
|
|
778
|
+
PlatformConfiguration and service endpoint overrides.
|
|
779
|
+
|
|
780
|
+
:param platform_type: The PlatformType (expected to be AZURE).
|
|
781
|
+
:param configuration: An optional PlatformConfiguration.
|
|
782
|
+
:param region: The Azure region string.
|
|
783
|
+
:param servicebus_client: An optional pre-configured Azure Service Bus client.
|
|
784
|
+
"""
|
|
785
|
+
|
|
786
|
+
def __init__(self, platform_type: PlatformType,
|
|
787
|
+
configuration: PlatformConfiguration = None,
|
|
788
|
+
region: str = None,
|
|
789
|
+
servicebus_client=None) -> None:
|
|
790
|
+
super().__init__(
|
|
791
|
+
service_name="azure-service-bus",
|
|
792
|
+
platform_type=platform_type,
|
|
793
|
+
configuration=configuration
|
|
794
|
+
)
|
|
795
|
+
self._region = region
|
|
796
|
+
self._servicebus_client = servicebus_client
|
|
797
|
+
return
|
|
798
|
+
|
|
799
|
+
def _get_client(self):
|
|
800
|
+
"""
|
|
801
|
+
Returns the Azure Service Bus client, creating one if not
|
|
802
|
+
already provided.
|
|
803
|
+
|
|
804
|
+
:returns: An Azure ServiceBusClient.
|
|
805
|
+
:raises: MessagingError
|
|
806
|
+
"""
|
|
807
|
+
if self._servicebus_client is not None:
|
|
808
|
+
rtnval = self._servicebus_client
|
|
809
|
+
return rtnval
|
|
810
|
+
|
|
811
|
+
try:
|
|
812
|
+
from azure.identity import DefaultAzureCredential
|
|
813
|
+
from azure.servicebus import ServiceBusClient
|
|
814
|
+
|
|
815
|
+
credential = None
|
|
816
|
+
if self._configuration is not None:
|
|
817
|
+
resolved = self._configuration.resolved_credentials
|
|
818
|
+
if len(resolved) > 0:
|
|
819
|
+
first_cred = list(resolved.values())[0]
|
|
820
|
+
credential = first_cred
|
|
821
|
+
|
|
822
|
+
if credential is None:
|
|
823
|
+
credential = DefaultAzureCredential()
|
|
824
|
+
|
|
825
|
+
namespace = None
|
|
826
|
+
if self._configuration is not None:
|
|
827
|
+
overrides = self._configuration.service_endpoint_overrides
|
|
828
|
+
if "service_bus" in overrides:
|
|
829
|
+
namespace = overrides["service_bus"]
|
|
830
|
+
|
|
831
|
+
if namespace is None:
|
|
832
|
+
namespace = os.environ.get(
|
|
833
|
+
"AZURE_SERVICEBUS_NAMESPACE",
|
|
834
|
+
""
|
|
835
|
+
)
|
|
836
|
+
|
|
837
|
+
self._servicebus_client = ServiceBusClient(
|
|
838
|
+
fully_qualified_namespace=namespace,
|
|
839
|
+
credential=credential
|
|
840
|
+
)
|
|
841
|
+
rtnval = self._servicebus_client
|
|
842
|
+
|
|
843
|
+
except Exception as exc:
|
|
844
|
+
err_msg_lines = [
|
|
845
|
+
"Failed to create Azure Service Bus client.",
|
|
846
|
+
" Region: {!r}".format(self._region),
|
|
847
|
+
" Error: {}".format(str(exc))
|
|
848
|
+
]
|
|
849
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
850
|
+
logger.warning("%s", err_msg)
|
|
851
|
+
raise MessagingError(
|
|
852
|
+
err_msg,
|
|
853
|
+
queue_name="",
|
|
854
|
+
cause=exc,
|
|
855
|
+
platform_type=PlatformType.AZURE
|
|
856
|
+
)
|
|
857
|
+
|
|
858
|
+
return rtnval
|
|
859
|
+
|
|
860
|
+
def send_message(self, queue_name: str, body: object,
|
|
861
|
+
attributes: dict = None) -> str:
|
|
862
|
+
"""
|
|
863
|
+
Sends a message to an Azure Service Bus queue.
|
|
864
|
+
|
|
865
|
+
:param queue_name: Name of the target queue.
|
|
866
|
+
:param body: Message body as a Python object (serialized to JSON).
|
|
867
|
+
:param attributes: Optional message attributes dictionary.
|
|
868
|
+
|
|
869
|
+
:returns: A message identifier string.
|
|
870
|
+
:raises: MessagingError
|
|
871
|
+
"""
|
|
872
|
+
try:
|
|
873
|
+
client = self._get_client()
|
|
874
|
+
from azure.servicebus import ServiceBusMessage
|
|
875
|
+
|
|
876
|
+
message_body = json.dumps(body)
|
|
877
|
+
message = ServiceBusMessage(message_body)
|
|
878
|
+
|
|
879
|
+
if attributes is not None:
|
|
880
|
+
message.application_properties = attributes
|
|
881
|
+
|
|
882
|
+
sender = client.get_queue_sender(queue_name=queue_name)
|
|
883
|
+
sender.send_messages(message)
|
|
884
|
+
|
|
885
|
+
message_id = getattr(message, "message_id", str(uuid.uuid4()))
|
|
886
|
+
rtnval = str(message_id)
|
|
887
|
+
|
|
888
|
+
except MessagingError:
|
|
889
|
+
raise
|
|
890
|
+
|
|
891
|
+
except Exception as exc:
|
|
892
|
+
err_msg_lines = [
|
|
893
|
+
"Failed to send message to Azure Service Bus.",
|
|
894
|
+
" Queue name: {!r}".format(queue_name),
|
|
895
|
+
" Error: {}".format(str(exc))
|
|
896
|
+
]
|
|
897
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
898
|
+
logger.warning("%s", err_msg)
|
|
899
|
+
raise MessagingError(
|
|
900
|
+
err_msg,
|
|
901
|
+
queue_name=queue_name,
|
|
902
|
+
cause=exc,
|
|
903
|
+
platform_type=PlatformType.AZURE
|
|
904
|
+
)
|
|
905
|
+
|
|
906
|
+
return rtnval
|
|
907
|
+
|
|
908
|
+
def receive_messages(self, queue_name: str, max_count: int = 1,
|
|
909
|
+
wait_time: float = None) -> list:
|
|
910
|
+
"""
|
|
911
|
+
Receives messages from an Azure Service Bus queue.
|
|
912
|
+
|
|
913
|
+
:param queue_name: Name of the source queue.
|
|
914
|
+
:param max_count: Maximum number of messages to receive.
|
|
915
|
+
:param wait_time: Optional wait time in seconds.
|
|
916
|
+
|
|
917
|
+
:returns: A list of QueueMessage objects.
|
|
918
|
+
:raises: MessagingError
|
|
919
|
+
"""
|
|
920
|
+
try:
|
|
921
|
+
client = self._get_client()
|
|
922
|
+
receiver = client.get_queue_receiver(queue_name=queue_name)
|
|
923
|
+
|
|
924
|
+
receive_kwargs = {
|
|
925
|
+
"max_message_count": max_count
|
|
926
|
+
}
|
|
927
|
+
if wait_time is not None:
|
|
928
|
+
receive_kwargs["max_wait_time"] = wait_time
|
|
929
|
+
|
|
930
|
+
sb_messages = receiver.receive_messages(**receive_kwargs)
|
|
931
|
+
|
|
932
|
+
messages = []
|
|
933
|
+
for sb_msg in sb_messages:
|
|
934
|
+
msg_body_str = str(sb_msg)
|
|
935
|
+
try:
|
|
936
|
+
msg_body = json.loads(msg_body_str)
|
|
937
|
+
except (json.JSONDecodeError, TypeError):
|
|
938
|
+
msg_body = msg_body_str
|
|
939
|
+
|
|
940
|
+
msg_attrs = getattr(sb_msg, "application_properties", {}) or {}
|
|
941
|
+
msg_id = str(getattr(sb_msg, "message_id", uuid.uuid4()))
|
|
942
|
+
lock_token = str(getattr(sb_msg, "lock_token", ""))
|
|
943
|
+
|
|
944
|
+
message = QueueMessage(
|
|
945
|
+
identifier=msg_id,
|
|
946
|
+
queue_name=queue_name,
|
|
947
|
+
body=msg_body,
|
|
948
|
+
attributes=msg_attrs,
|
|
949
|
+
receipt_handle=lock_token,
|
|
950
|
+
timestamp=datetime.utcnow()
|
|
951
|
+
)
|
|
952
|
+
messages.append(message)
|
|
953
|
+
|
|
954
|
+
rtnval = messages
|
|
955
|
+
|
|
956
|
+
except MessagingError:
|
|
957
|
+
raise
|
|
958
|
+
|
|
959
|
+
except Exception as exc:
|
|
960
|
+
err_msg_lines = [
|
|
961
|
+
"Failed to receive messages from Azure Service Bus.",
|
|
962
|
+
" Queue name: {!r}".format(queue_name),
|
|
963
|
+
" Error: {}".format(str(exc))
|
|
964
|
+
]
|
|
965
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
966
|
+
logger.warning("%s", err_msg)
|
|
967
|
+
raise MessagingError(
|
|
968
|
+
err_msg,
|
|
969
|
+
queue_name=queue_name,
|
|
970
|
+
cause=exc,
|
|
971
|
+
platform_type=PlatformType.AZURE
|
|
972
|
+
)
|
|
973
|
+
|
|
974
|
+
return rtnval
|
|
975
|
+
|
|
976
|
+
def acknowledge_message(self, queue_name: str, receipt_handle: str) -> None:
|
|
977
|
+
"""
|
|
978
|
+
Acknowledges a message by completing it in Azure Service Bus.
|
|
979
|
+
|
|
980
|
+
:param queue_name: Name of the queue.
|
|
981
|
+
:param receipt_handle: Lock token string for the message.
|
|
982
|
+
|
|
983
|
+
:raises: MessagingError
|
|
984
|
+
"""
|
|
985
|
+
try:
|
|
986
|
+
client = self._get_client()
|
|
987
|
+
receiver = client.get_queue_receiver(queue_name=queue_name)
|
|
988
|
+
receiver.complete_message(receipt_handle)
|
|
989
|
+
|
|
990
|
+
except MessagingError:
|
|
991
|
+
raise
|
|
992
|
+
|
|
993
|
+
except Exception as exc:
|
|
994
|
+
err_msg_lines = [
|
|
995
|
+
"Failed to acknowledge message in Azure Service Bus.",
|
|
996
|
+
" Queue name: {!r}".format(queue_name),
|
|
997
|
+
" Receipt handle: {!r}".format(receipt_handle),
|
|
998
|
+
" Error: {}".format(str(exc))
|
|
999
|
+
]
|
|
1000
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
1001
|
+
logger.warning("%s", err_msg)
|
|
1002
|
+
raise MessagingError(
|
|
1003
|
+
err_msg,
|
|
1004
|
+
queue_name=queue_name,
|
|
1005
|
+
cause=exc,
|
|
1006
|
+
platform_type=PlatformType.AZURE
|
|
1007
|
+
)
|
|
1008
|
+
|
|
1009
|
+
return
|
|
1010
|
+
|
|
1011
|
+
def create_queue(self, queue_name: str) -> None:
|
|
1012
|
+
"""
|
|
1013
|
+
Creates an Azure Service Bus queue.
|
|
1014
|
+
|
|
1015
|
+
:param queue_name: Name of the queue to create.
|
|
1016
|
+
|
|
1017
|
+
:raises: MessagingError
|
|
1018
|
+
"""
|
|
1019
|
+
try:
|
|
1020
|
+
client = self._get_client()
|
|
1021
|
+
admin = client.get_queue_admin()
|
|
1022
|
+
admin.create_queue(queue_name)
|
|
1023
|
+
|
|
1024
|
+
except MessagingError:
|
|
1025
|
+
raise
|
|
1026
|
+
|
|
1027
|
+
except Exception as exc:
|
|
1028
|
+
err_msg_lines = [
|
|
1029
|
+
"Failed to create queue in Azure Service Bus.",
|
|
1030
|
+
" Queue name: {!r}".format(queue_name),
|
|
1031
|
+
" Error: {}".format(str(exc))
|
|
1032
|
+
]
|
|
1033
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
1034
|
+
logger.warning("%s", err_msg)
|
|
1035
|
+
raise MessagingError(
|
|
1036
|
+
err_msg,
|
|
1037
|
+
queue_name=queue_name,
|
|
1038
|
+
cause=exc,
|
|
1039
|
+
platform_type=PlatformType.AZURE
|
|
1040
|
+
)
|
|
1041
|
+
|
|
1042
|
+
return
|
|
1043
|
+
|
|
1044
|
+
def delete_queue(self, queue_name: str) -> None:
|
|
1045
|
+
"""
|
|
1046
|
+
Removes an Azure Service Bus queue.
|
|
1047
|
+
|
|
1048
|
+
:param queue_name: Name of the queue to remove.
|
|
1049
|
+
|
|
1050
|
+
:raises: MessagingError
|
|
1051
|
+
"""
|
|
1052
|
+
try:
|
|
1053
|
+
client = self._get_client()
|
|
1054
|
+
admin = client.get_queue_admin()
|
|
1055
|
+
admin.delete_queue(queue_name)
|
|
1056
|
+
|
|
1057
|
+
except MessagingError:
|
|
1058
|
+
raise
|
|
1059
|
+
|
|
1060
|
+
except Exception as exc:
|
|
1061
|
+
err_msg_lines = [
|
|
1062
|
+
"Failed to delete queue from Azure Service Bus.",
|
|
1063
|
+
" Queue name: {!r}".format(queue_name),
|
|
1064
|
+
" Error: {}".format(str(exc))
|
|
1065
|
+
]
|
|
1066
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
1067
|
+
logger.warning("%s", err_msg)
|
|
1068
|
+
raise MessagingError(
|
|
1069
|
+
err_msg,
|
|
1070
|
+
queue_name=queue_name,
|
|
1071
|
+
cause=exc,
|
|
1072
|
+
platform_type=PlatformType.AZURE
|
|
1073
|
+
)
|
|
1074
|
+
|
|
1075
|
+
return
|
|
1076
|
+
|
|
1077
|
+
def list_queues(self) -> list:
|
|
1078
|
+
"""
|
|
1079
|
+
Returns a list of Azure Service Bus queue names.
|
|
1080
|
+
|
|
1081
|
+
:returns: List of queue name strings.
|
|
1082
|
+
:raises: MessagingError
|
|
1083
|
+
"""
|
|
1084
|
+
try:
|
|
1085
|
+
client = self._get_client()
|
|
1086
|
+
admin = client.get_queue_admin()
|
|
1087
|
+
queues = admin.list_queues()
|
|
1088
|
+
rtnval = [getattr(q, "name", str(q)) for q in queues]
|
|
1089
|
+
|
|
1090
|
+
except MessagingError:
|
|
1091
|
+
raise
|
|
1092
|
+
|
|
1093
|
+
except Exception as exc:
|
|
1094
|
+
err_msg_lines = [
|
|
1095
|
+
"Failed to list queues in Azure Service Bus.",
|
|
1096
|
+
" Error: {}".format(str(exc))
|
|
1097
|
+
]
|
|
1098
|
+
err_msg = os.linesep.join(err_msg_lines)
|
|
1099
|
+
logger.warning("%s", err_msg)
|
|
1100
|
+
raise MessagingError(
|
|
1101
|
+
err_msg,
|
|
1102
|
+
queue_name="",
|
|
1103
|
+
cause=exc,
|
|
1104
|
+
platform_type=PlatformType.AZURE
|
|
1105
|
+
)
|
|
1106
|
+
|
|
1107
|
+
return rtnval
|
|
1108
|
+
|
|
1109
|
+
def is_available(self) -> bool:
|
|
1110
|
+
"""
|
|
1111
|
+
Checks whether the Azure Service Bus service is available.
|
|
1112
|
+
|
|
1113
|
+
:returns: True if available, False otherwise.
|
|
1114
|
+
"""
|
|
1115
|
+
try:
|
|
1116
|
+
self.list_queues()
|
|
1117
|
+
rtnval = True
|
|
1118
|
+
except Exception:
|
|
1119
|
+
rtnval = False
|
|
1120
|
+
|
|
1121
|
+
return rtnval
|
|
1122
|
+
|
|
1123
|
+
def health_check(self) -> bool:
|
|
1124
|
+
"""
|
|
1125
|
+
Health check for Azure Service Bus service.
|
|
1126
|
+
|
|
1127
|
+
:returns: True if the service is healthy.
|
|
1128
|
+
"""
|
|
1129
|
+
rtnval = self.is_available()
|
|
1130
|
+
return rtnval
|
|
1131
|
+
|
|
1132
|
+
|
|
1133
|
+
# ---------------------------------------------------------------------------
|
|
1134
|
+
# AzurePlatformAdapter
|
|
1135
|
+
# ---------------------------------------------------------------------------
|
|
1136
|
+
|
|
1137
|
+
class AzurePlatformAdapter(PlatformAdapter):
|
|
1138
|
+
"""
|
|
1139
|
+
PlatformAdapter implementation providing Azure-specific service
|
|
1140
|
+
implementations using Azure SDK clients for Functions, Blob Storage,
|
|
1141
|
+
and Service Bus.
|
|
1142
|
+
|
|
1143
|
+
Resolves Azure credentials through CredentialReference entries in the
|
|
1144
|
+
PlatformConfiguration, falling back to the default Azure credential
|
|
1145
|
+
chain when no explicit credentials are configured.
|
|
1146
|
+
|
|
1147
|
+
:param configuration: The PlatformConfiguration for the Azure platform.
|
|
1148
|
+
:param functions_client: An optional pre-configured Azure Functions client.
|
|
1149
|
+
:param blob_service_client: An optional pre-configured Azure Blob Service client.
|
|
1150
|
+
:param servicebus_client: An optional pre-configured Azure Service Bus client.
|
|
1151
|
+
"""
|
|
1152
|
+
|
|
1153
|
+
def __init__(self, configuration: PlatformConfiguration,
|
|
1154
|
+
functions_client=None, blob_service_client=None,
|
|
1155
|
+
servicebus_client=None) -> None:
|
|
1156
|
+
super().__init__(configuration=configuration)
|
|
1157
|
+
self._region = self._resolve_region()
|
|
1158
|
+
self._compute_service = AzureComputeService(
|
|
1159
|
+
platform_type=PlatformType.AZURE,
|
|
1160
|
+
configuration=configuration,
|
|
1161
|
+
region=self._region,
|
|
1162
|
+
functions_client=functions_client
|
|
1163
|
+
)
|
|
1164
|
+
self._storage_service = AzureStorageService(
|
|
1165
|
+
platform_type=PlatformType.AZURE,
|
|
1166
|
+
configuration=configuration,
|
|
1167
|
+
region=self._region,
|
|
1168
|
+
blob_service_client=blob_service_client
|
|
1169
|
+
)
|
|
1170
|
+
self._messaging_service = AzureMessagingService(
|
|
1171
|
+
platform_type=PlatformType.AZURE,
|
|
1172
|
+
configuration=configuration,
|
|
1173
|
+
region=self._region,
|
|
1174
|
+
servicebus_client=servicebus_client
|
|
1175
|
+
)
|
|
1176
|
+
return
|
|
1177
|
+
|
|
1178
|
+
@property
|
|
1179
|
+
def platform_type(self) -> PlatformType:
|
|
1180
|
+
"""
|
|
1181
|
+
Returns PlatformType.AZURE.
|
|
1182
|
+
|
|
1183
|
+
:returns: PlatformType.AZURE.
|
|
1184
|
+
"""
|
|
1185
|
+
rtnval = PlatformType.AZURE
|
|
1186
|
+
return rtnval
|
|
1187
|
+
|
|
1188
|
+
def get_compute_service(self) -> ComputeService:
|
|
1189
|
+
"""
|
|
1190
|
+
Returns the AzureComputeService instance.
|
|
1191
|
+
|
|
1192
|
+
:returns: The AzureComputeService.
|
|
1193
|
+
"""
|
|
1194
|
+
rtnval = self._compute_service
|
|
1195
|
+
return rtnval
|
|
1196
|
+
|
|
1197
|
+
def get_storage_service(self) -> StorageService:
|
|
1198
|
+
"""
|
|
1199
|
+
Returns the AzureStorageService instance.
|
|
1200
|
+
|
|
1201
|
+
:returns: The AzureStorageService.
|
|
1202
|
+
"""
|
|
1203
|
+
rtnval = self._storage_service
|
|
1204
|
+
return rtnval
|
|
1205
|
+
|
|
1206
|
+
def get_messaging_service(self) -> MessagingService:
|
|
1207
|
+
"""
|
|
1208
|
+
Returns the AzureMessagingService instance.
|
|
1209
|
+
|
|
1210
|
+
:returns: The AzureMessagingService.
|
|
1211
|
+
"""
|
|
1212
|
+
rtnval = self._messaging_service
|
|
1213
|
+
return rtnval
|
|
1214
|
+
|
|
1215
|
+
def _resolve_region(self) -> str:
|
|
1216
|
+
"""
|
|
1217
|
+
Resolves the Azure region from configuration, falling back to
|
|
1218
|
+
the AZURE_REGION environment variable, defaulting to eastus.
|
|
1219
|
+
|
|
1220
|
+
:returns: The resolved region string.
|
|
1221
|
+
"""
|
|
1222
|
+
if self._configuration is not None and self._configuration.region is not None:
|
|
1223
|
+
rtnval = self._configuration.region
|
|
1224
|
+
return rtnval
|
|
1225
|
+
|
|
1226
|
+
rtnval = os.environ.get("AZURE_REGION", "eastus")
|
|
1227
|
+
return rtnval
|