blueprint-faas 0.2.0-alpha.2

FaaS provider integrations for Blueprint SDK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Azure Functions FaaS integration
//!
//! This module provides full integration with Azure Functions for executing blueprint jobs.

use super::*;
use azure_core::auth::TokenCredential;
use azure_identity::{DefaultAzureCredential, TokenCredentialOptions};
use blueprint_core::{JobCall, JobResult};
use reqwest::Client;
use serde::Deserialize;
use std::sync::Arc;
use std::time::Instant;
use tracing::{debug, info, warn};

/// Azure Functions executor for blueprint jobs
///
/// Integrates with Azure Functions REST API for deploying and invoking serverless functions.
///
/// # Authentication
///
/// Uses Azure Default Credentials (Environment, Managed Identity, or Azure CLI).
/// Set these environment variables:
/// - `AZURE_TENANT_ID` - Your Azure AD tenant ID
/// - `AZURE_CLIENT_ID` - Service principal client ID
/// - `AZURE_CLIENT_SECRET` - Service principal secret
///
/// Or use Azure CLI: `az login`
///
/// # Example
///
/// ```rust,ignore
/// let executor = AzureFunctionExecutor::new("my-subscription-id", "eastus").await?;
///
/// BlueprintRunner::builder(config, env)
///     .with_faas_executor(0, executor)
///     .run().await
/// ```
#[derive(Debug, Clone)]
pub struct AzureFunctionExecutor {
    subscription_id: String,
    region: String,
    resource_group: String,
    function_app_name: String,
    client: Client,
    credential: Arc<DefaultAzureCredential>,
}

impl AzureFunctionExecutor {
    /// Create a new Azure Functions executor
    ///
    /// # Arguments
    ///
    /// * `subscription_id` - Azure subscription ID
    /// * `region` - Azure region (e.g., "eastus", "westus2")
    ///
    /// # Authentication
    ///
    /// Requires Azure credentials via environment variables or Azure CLI.
    /// The service principal needs Contributor role on the subscription.
    pub async fn new(
        subscription_id: impl Into<String>,
        region: impl Into<String>,
    ) -> Result<Self, FaasError> {
        let subscription_id = subscription_id.into();
        let region = region.into();

        debug!(
            subscription_id = %subscription_id,
            region = %region,
            "Creating Azure Functions executor"
        );

        let credential = DefaultAzureCredential::create(TokenCredentialOptions::default())
            .map_err(|e| {
                FaasError::InfrastructureError(format!("Failed to create Azure credentials: {}", e))
            })?;

        Ok(Self {
            subscription_id,
            region: region.clone(),
            resource_group: format!("blueprint-rg-{}", region),
            function_app_name: format!("blueprint-functions-{}", region),
            client: Client::new(),
            credential: Arc::new(credential),
        })
    }

    /// Set the resource group name (default: "blueprint-rg-{region}")
    #[must_use]
    pub fn with_resource_group(mut self, resource_group: impl Into<String>) -> Self {
        self.resource_group = resource_group.into();
        self
    }

    /// Set the function app name (default: "blueprint-functions-{region}")
    #[must_use]
    pub fn with_app_name(mut self, app_name: impl Into<String>) -> Self {
        self.function_app_name = app_name.into();
        self
    }

    fn function_name(&self, job_id: u32) -> String {
        format!("job{}", job_id)
    }

    /// Get an authenticated access token for Azure ARM API calls
    async fn get_access_token(&self) -> Result<String, FaasError> {
        let token = self
            .credential
            .get_token(&["https://management.azure.com/.default"])
            .await
            .map_err(|e| {
                FaasError::InfrastructureError(format!(
                    "Failed to get Azure access token: {}. \
                    Set AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET or use 'az login'.",
                    e
                ))
            })?;

        Ok(token.token.secret().to_string())
    }

    /// Build Azure Resource Manager API endpoint
    fn arm_endpoint(&self, path: &str) -> String {
        format!(
            "https://management.azure.com{}?api-version=2022-03-01",
            path.trim_start_matches('/')
        )
    }

    /// Get the HTTP trigger URL for a function
    async fn get_function_url(&self, job_id: u32) -> Result<String, FaasError> {
        let function_name = self.function_name(job_id);

        // Get function keys to construct invoke URL
        let token = self.get_access_token().await?;

        let keys_url = self.arm_endpoint(&format!(
            "/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Web/sites/{}/functions/{}/keys",
            self.subscription_id, self.resource_group, self.function_app_name, function_name
        ));

        let response = self
            .client
            .post(&keys_url.replace("?api-version", "/listKeys?api-version"))
            .bearer_auth(&token)
            .send()
            .await
            .map_err(|e| {
                FaasError::InfrastructureError(format!("Failed to get function keys: {}", e))
            })?;

        if !response.status().is_success() {
            return Err(FaasError::InfrastructureError(format!(
                "Function not found or not deployed: {}",
                function_name
            )));
        }

        let keys_info: FunctionKeysInfo = response
            .json()
            .await
            .map_err(|e| FaasError::SerializationError(e.to_string()))?;

        let default_key = keys_info.default.ok_or_else(|| {
            FaasError::InfrastructureError("No default function key available".into())
        })?;

        // Construct function URL with key
        Ok(format!(
            "https://{}.azurewebsites.net/api/{}?code={}",
            self.function_app_name, function_name, default_key
        ))
    }

    /// Invoke function via HTTP trigger
    async fn invoke_http_trigger(&self, job_call: JobCall) -> Result<JobResult, FaasError> {
        let job_id: u32 = job_call.job_id().into();
        let function_name = self.function_name(job_id);

        // Get function URL with auth key
        let function_url = self.get_function_url(job_id).await?;

        debug!(
            job_id = job_id,
            function = %function_name,
            "Invoking Azure Function via HTTP"
        );

        // Convert JobCall to FaasPayload
        let payload: FaasPayload = job_call.into();

        let start = Instant::now();

        // Invoke via HTTP POST (no Bearer token needed, URL contains function key)
        let response = self
            .client
            .post(&function_url)
            .json(&payload)
            .send()
            .await
            .map_err(|e| {
                warn!(error = %e, "Azure Function HTTP invocation failed");
                FaasError::InvocationFailed(e.to_string())
            })?;

        let duration = start.elapsed();

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(FaasError::FunctionError(format!(
                "HTTP {} - {}",
                status, body
            )));
        }

        let faas_response: FaasResponse = response
            .json()
            .await
            .map_err(|e| FaasError::SerializationError(e.to_string()))?;

        info!(
            job_id = job_id,
            duration_ms = duration.as_millis(),
            "Azure Function invocation successful"
        );

        Ok(faas_response.into())
    }
}

#[async_trait::async_trait]
impl FaasExecutor for AzureFunctionExecutor {
    async fn invoke(&self, job_call: JobCall) -> Result<JobResult, FaasError> {
        self.invoke_http_trigger(job_call).await
    }

    async fn invoke_with_metrics(
        &self,
        job_call: JobCall,
    ) -> Result<(JobResult, FaasMetrics), FaasError> {
        let start = Instant::now();
        let result = self.invoke(job_call).await?;
        let total_duration = start.elapsed();

        // Azure Functions cold start detection via duration heuristic
        let cold_start = total_duration.as_millis() > 800;

        let metrics = FaasMetrics {
            total_duration_ms: total_duration.as_millis() as u64,
            execution_duration_ms: total_duration.as_millis() as u64,
            cold_start,
            memory_used_mb: None,
            billed_duration_ms: total_duration.as_millis() as u64, // Azure bills per ms
        };

        Ok((result, metrics))
    }

    async fn deploy_job(
        &self,
        job_id: u32,
        binary: &[u8],
        config: &FaasConfig,
    ) -> Result<FaasDeployment, FaasError> {
        let function_name = self.function_name(job_id);

        info!(
            job_id,
            function = %function_name,
            memory_mb = config.memory_mb,
            timeout_secs = config.timeout_secs,
            "Deploying Azure Function"
        );

        // Package binary for Azure Functions (zip format)
        let zip_package = crate::utils::create_lambda_package(binary)?;

        // Ensure resource group exists
        self.ensure_resource_group().await?;

        // Ensure function app exists
        self.ensure_function_app(config).await?;

        // Upload function code via ZipDeploy API
        self.upload_function_code(job_id, &zip_package).await?;

        // Create function.json for the function
        self.create_function_config(job_id, config).await?;

        // Wait for deployment to complete
        tokio::time::sleep(tokio::time::Duration::from_secs(5)).await;

        Ok(FaasDeployment {
            function_id: format!("{}/{}", self.function_app_name, function_name),
            job_id,
            endpoint: format!(
                "https://{}.azurewebsites.net/api/{}",
                self.function_app_name, function_name
            ),
            cold_start_ms: Some(600), // Typical Azure Functions cold start
            memory_mb: config.memory_mb,
            timeout_secs: config.timeout_secs,
        })
    }

    async fn health_check(&self, job_id: u32) -> Result<bool, FaasError> {
        let function_name = self.function_name(job_id);
        let token = self.get_access_token().await?;

        let url = self.arm_endpoint(&format!(
            "/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Web/sites/{}/functions/{}",
            self.subscription_id, self.resource_group, self.function_app_name, function_name
        ));

        self.client
            .get(&url)
            .bearer_auth(&token)
            .send()
            .await
            .map(|r| r.status().is_success())
            .map_err(|e| FaasError::InfrastructureError(format!("Health check failed: {}", e)))
    }

    async fn warm(&self, job_id: u32) -> Result<(), FaasError> {
        debug!(job_id, "Warming Azure Function");

        // Create a minimal JobCall for warming
        let warm_call = JobCall::new(job_id as u8, bytes::Bytes::from_static(b"{}"));

        // Invoke the function (ignore result)
        let _ = self.invoke(warm_call).await;

        Ok(())
    }

    async fn get_deployment(&self, job_id: u32) -> Result<FaasDeployment, FaasError> {
        let function_name = self.function_name(job_id);
        let token = self.get_access_token().await?;

        let url = self.arm_endpoint(&format!(
            "/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Web/sites/{}/functions/{}",
            self.subscription_id, self.resource_group, self.function_app_name, function_name
        ));

        let response = self
            .client
            .get(&url)
            .bearer_auth(&token)
            .send()
            .await
            .map_err(|e| {
                FaasError::InfrastructureError(format!("Failed to get function: {}", e))
            })?;

        if !response.status().is_success() {
            return Err(FaasError::InfrastructureError(format!(
                "Function not found: {}",
                function_name
            )));
        }

        Ok(FaasDeployment {
            function_id: format!("{}/{}", self.function_app_name, function_name),
            job_id,
            endpoint: format!(
                "https://{}.azurewebsites.net/api/{}",
                self.function_app_name, function_name
            ),
            cold_start_ms: Some(600),
            memory_mb: 512,    // Default
            timeout_secs: 300, // Default
        })
    }

    async fn undeploy_job(&self, job_id: u32) -> Result<(), FaasError> {
        let function_name = self.function_name(job_id);

        info!(function = %function_name, "Deleting Azure Function");

        let token = self.get_access_token().await?;

        let url = self.arm_endpoint(&format!(
            "/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Web/sites/{}/functions/{}",
            self.subscription_id, self.resource_group, self.function_app_name, function_name
        ));

        self.client
            .delete(&url)
            .bearer_auth(&token)
            .send()
            .await
            .map_err(|e| {
                FaasError::InfrastructureError(format!("Failed to delete function: {}", e))
            })?;

        Ok(())
    }

    fn provider_name(&self) -> &'static str {
        "Azure Functions"
    }
}

impl AzureFunctionExecutor {
    /// Ensure the resource group exists, create if not
    async fn ensure_resource_group(&self) -> Result<(), FaasError> {
        let token = self.get_access_token().await?;

        let url = self.arm_endpoint(&format!(
            "/subscriptions/{}/resourceGroups/{}",
            self.subscription_id, self.resource_group
        ));

        // Check if exists
        let exists = self
            .client
            .get(&url)
            .bearer_auth(&token)
            .send()
            .await
            .map(|r| r.status().is_success())
            .unwrap_or(false);

        if !exists {
            debug!(
                resource_group = %self.resource_group,
                "Creating resource group"
            );

            // Create resource group
            let create_body = serde_json::json!({
                "location": self.region
            });

            self.client
                .put(&url)
                .bearer_auth(&token)
                .json(&create_body)
                .send()
                .await
                .map_err(|e| {
                    FaasError::InfrastructureError(format!(
                        "Failed to create resource group: {}",
                        e
                    ))
                })?;
        }

        Ok(())
    }

    /// Ensure the function app exists, create if not
    async fn ensure_function_app(&self, config: &FaasConfig) -> Result<(), FaasError> {
        let token = self.get_access_token().await?;

        let url = self.arm_endpoint(&format!(
            "/subscriptions/{}/resourceGroups/{}/providers/Microsoft.Web/sites/{}",
            self.subscription_id, self.resource_group, self.function_app_name
        ));

        // Check if exists
        let exists = self
            .client
            .get(&url)
            .bearer_auth(&token)
            .send()
            .await
            .map(|r| r.status().is_success())
            .unwrap_or(false);

        if !exists {
            debug!(
                function_app = %self.function_app_name,
                "Creating function app"
            );

            // Create function app
            let create_body = serde_json::json!({
                "location": self.region,
                "kind": "functionapp",
                "properties": {
                    "reserved": true, // Linux
                    "siteConfig": {
                        "linuxFxVersion": "CUSTOM",
                        "appSettings": config.env_vars.iter().map(|(k, v)| {
                            serde_json::json!({
                                "name": k,
                                "value": v
                            })
                        }).collect::<Vec<_>>()
                    }
                }
            });

            self.client
                .put(&url)
                .bearer_auth(&token)
                .json(&create_body)
                .send()
                .await
                .map_err(|e| {
                    FaasError::InfrastructureError(format!("Failed to create function app: {}", e))
                })?;

            // Wait for function app to be ready
            tokio::time::sleep(tokio::time::Duration::from_secs(30)).await;
        }

        Ok(())
    }

    /// Upload function code via ZipDeploy
    async fn upload_function_code(&self, _job_id: u32, zip_data: &[u8]) -> Result<(), FaasError> {
        let token = self.get_access_token().await?;

        let url = format!(
            "https://{}.scm.azurewebsites.net/api/zipdeploy",
            self.function_app_name
        );

        debug!(
            size_bytes = zip_data.len(),
            "Uploading function code via ZipDeploy"
        );

        self.client
            .post(&url)
            .bearer_auth(&token)
            .header("Content-Type", "application/zip")
            .body(zip_data.to_vec())
            .send()
            .await
            .map_err(|e| {
                FaasError::InfrastructureError(format!("Failed to upload function code: {}", e))
            })?;

        Ok(())
    }

    /// Create function.json configuration file
    async fn create_function_config(
        &self,
        job_id: u32,
        _config: &FaasConfig,
    ) -> Result<(), FaasError> {
        let function_name = self.function_name(job_id);
        let token = self.get_access_token().await?;

        // Create function.json for HTTP trigger
        let function_json = serde_json::json!({
            "bindings": [
                {
                    "authLevel": "function",
                    "type": "httpTrigger",
                    "direction": "in",
                    "name": "req",
                    "methods": ["post"]
                },
                {
                    "type": "http",
                    "direction": "out",
                    "name": "res"
                }
            ]
        });

        let url = format!(
            "https://{}.scm.azurewebsites.net/api/vfs/site/wwwroot/{}/function.json",
            self.function_app_name, function_name
        );

        self.client
            .put(&url)
            .bearer_auth(&token)
            .json(&function_json)
            .send()
            .await
            .map_err(|e| {
                FaasError::InfrastructureError(format!("Failed to create function config: {}", e))
            })?;

        Ok(())
    }
}

// Azure ARM API types

#[derive(Debug, Deserialize)]
struct FunctionKeysInfo {
    default: Option<String>,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    #[ignore = "Requires Azure credentials"]
    async fn test_azure_executor_creation() {
        let executor = AzureFunctionExecutor::new("test-subscription", "eastus").await;

        assert!(executor.is_ok());
        let exec = executor.unwrap();
        assert_eq!(exec.provider_name(), "Azure Functions");
    }

    #[test]
    fn test_function_naming() {
        // Create a credential without requiring actual authentication
        let credential = DefaultAzureCredential::create(TokenCredentialOptions::default())
            .expect("Failed to create test credential (this is expected in test environment)");

        let executor = AzureFunctionExecutor {
            subscription_id: "test-subscription".to_string(),
            region: "eastus".to_string(),
            resource_group: "test-rg".to_string(),
            function_app_name: "test-app".to_string(),
            client: Client::new(),
            credential: Arc::new(credential),
        };

        assert_eq!(executor.function_name(0), "job0");
        assert_eq!(executor.function_name(42), "job42");
    }
}