azure-lite-rs 0.1.1

Lightweight HTTP client for Azure APIs
Documentation
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
//! Azure Compute API client.
//!
//! Thin wrapper over generated ops. All URL construction and HTTP methods
//! are in `ops::compute::ComputeOps`. This layer adds:
//! - Ergonomic method signatures (auto-injects subscription_id from client)

use crate::{
    AzureHttpClient, Result,
    ops::compute::ComputeOps,
    types::compute::{
        AccessUri, Disk, DiskCreateRequest, DiskListResult, DiskSku, DiskUpdateRequest,
        GrantAccessData, VirtualMachine, VirtualMachineCreateRequest,
        VirtualMachineInstanceViewResult, VirtualMachineListResult, VirtualMachineScaleSet,
        VirtualMachineScaleSetCreateRequest, VirtualMachineScaleSetListResult,
        VirtualMachineScaleSetVMInstanceIDs, VirtualMachineScaleSetVMListResult,
    },
};

/// Client for the Azure Compute API.
///
/// Wraps the raw [`ComputeOps`] with ergonomic signatures that
/// auto-inject `subscription_id` from the parent [`AzureHttpClient`].
pub struct ComputeClient<'a> {
    ops: ComputeOps<'a>,
    client: &'a AzureHttpClient,
}

impl<'a> ComputeClient<'a> {
    /// Create a new Azure Compute API client.
    pub(crate) fn new(client: &'a AzureHttpClient) -> Self {
        Self {
            ops: ComputeOps::new(client),
            client,
        }
    }

    /// List virtual machines in a resource group.
    pub async fn list_vms(&self, resource_group_name: &str) -> Result<VirtualMachineListResult> {
        self.ops
            .list_vms(self.client.subscription_id(), resource_group_name)
            .await
    }

    /// Get a virtual machine.
    pub async fn get_vm(&self, resource_group_name: &str, vm_name: &str) -> Result<VirtualMachine> {
        self.ops
            .get_vm(
                self.client.subscription_id(),
                resource_group_name,
                vm_name,
                "",
            )
            .await
    }

    /// Get a virtual machine with expanded properties.
    pub async fn get_vm_expanded(
        &self,
        resource_group_name: &str,
        vm_name: &str,
        expand: &str,
    ) -> Result<VirtualMachine> {
        self.ops
            .get_vm(
                self.client.subscription_id(),
                resource_group_name,
                vm_name,
                expand,
            )
            .await
    }

    /// Create or update a virtual machine.
    pub async fn create_vm(
        &self,
        resource_group_name: &str,
        vm_name: &str,
        body: &VirtualMachineCreateRequest,
    ) -> Result<VirtualMachine> {
        self.ops
            .create_vm(
                self.client.subscription_id(),
                resource_group_name,
                vm_name,
                body,
            )
            .await
    }

    /// Delete a virtual machine.
    pub async fn delete_vm(&self, resource_group_name: &str, vm_name: &str) -> Result<()> {
        self.ops
            .delete_vm(self.client.subscription_id(), resource_group_name, vm_name)
            .await
    }

    /// Start a virtual machine.
    pub async fn start_vm(&self, resource_group_name: &str, vm_name: &str) -> Result<()> {
        self.ops
            .start_vm(self.client.subscription_id(), resource_group_name, vm_name)
            .await
    }

    /// Power off (stop) a virtual machine. The VM continues to be billed.
    pub async fn stop_vm(&self, resource_group_name: &str, vm_name: &str) -> Result<()> {
        self.ops
            .stop_vm(self.client.subscription_id(), resource_group_name, vm_name)
            .await
    }

    /// Deallocate a virtual machine. Stops billing.
    pub async fn deallocate_vm(&self, resource_group_name: &str, vm_name: &str) -> Result<()> {
        self.ops
            .deallocate_vm(self.client.subscription_id(), resource_group_name, vm_name)
            .await
    }

    /// Restart a virtual machine.
    pub async fn restart_vm(&self, resource_group_name: &str, vm_name: &str) -> Result<()> {
        self.ops
            .restart_vm(self.client.subscription_id(), resource_group_name, vm_name)
            .await
    }

    /// Get the instance view of a virtual machine.
    pub async fn get_instance_view(
        &self,
        resource_group_name: &str,
        vm_name: &str,
    ) -> Result<VirtualMachineInstanceViewResult> {
        self.ops
            .get_instance_view(self.client.subscription_id(), resource_group_name, vm_name)
            .await
    }

    // --- VMSS operations ---

    /// List virtual machine scale sets in a resource group.
    pub async fn list_vmss(
        &self,
        resource_group_name: &str,
    ) -> Result<VirtualMachineScaleSetListResult> {
        self.ops
            .list_vmss(self.client.subscription_id(), resource_group_name)
            .await
    }

    /// Get a virtual machine scale set.
    pub async fn get_vmss(
        &self,
        resource_group_name: &str,
        vmss_name: &str,
    ) -> Result<VirtualMachineScaleSet> {
        self.ops
            .get_vmss(
                self.client.subscription_id(),
                resource_group_name,
                vmss_name,
            )
            .await
    }

    /// Create or update a virtual machine scale set.
    pub async fn create_vmss(
        &self,
        resource_group_name: &str,
        vmss_name: &str,
        body: &VirtualMachineScaleSetCreateRequest,
    ) -> Result<VirtualMachineScaleSet> {
        self.ops
            .create_vmss(
                self.client.subscription_id(),
                resource_group_name,
                vmss_name,
                body,
            )
            .await
    }

    /// Delete a virtual machine scale set.
    pub async fn delete_vmss(&self, resource_group_name: &str, vmss_name: &str) -> Result<()> {
        self.ops
            .delete_vmss(
                self.client.subscription_id(),
                resource_group_name,
                vmss_name,
            )
            .await
    }

    /// List virtual machines in a VM scale set.
    pub async fn list_vmss_instances(
        &self,
        resource_group_name: &str,
        vmss_name: &str,
    ) -> Result<VirtualMachineScaleSetVMListResult> {
        self.ops
            .list_vmss_instances(
                self.client.subscription_id(),
                resource_group_name,
                vmss_name,
            )
            .await
    }

    /// Start one or more virtual machines in a VM scale set.
    pub async fn start_vmss_instances(
        &self,
        resource_group_name: &str,
        vmss_name: &str,
        instance_ids: &VirtualMachineScaleSetVMInstanceIDs,
    ) -> Result<()> {
        self.ops
            .start_vmss_instances(
                self.client.subscription_id(),
                resource_group_name,
                vmss_name,
                instance_ids,
            )
            .await
    }

    /// Power off one or more virtual machines in a VM scale set.
    pub async fn stop_vmss_instances(
        &self,
        resource_group_name: &str,
        vmss_name: &str,
        instance_ids: &VirtualMachineScaleSetVMInstanceIDs,
    ) -> Result<()> {
        self.ops
            .stop_vmss_instances(
                self.client.subscription_id(),
                resource_group_name,
                vmss_name,
                instance_ids,
            )
            .await
    }

    // --- Managed Disk operations ---

    /// List all managed disks in a resource group.
    pub async fn list_disks(&self, resource_group_name: &str) -> Result<DiskListResult> {
        self.ops
            .list_disks(self.client.subscription_id(), resource_group_name)
            .await
    }

    /// List all managed disks in the subscription.
    pub async fn list_disks_in_subscription(&self) -> Result<DiskListResult> {
        self.ops
            .list_disks_in_subscription(self.client.subscription_id())
            .await
    }

    /// Get information about a managed disk.
    pub async fn get_disk(&self, resource_group_name: &str, disk_name: &str) -> Result<Disk> {
        self.ops
            .get_disk(
                self.client.subscription_id(),
                resource_group_name,
                disk_name,
            )
            .await
    }

    /// Create or update a managed disk.
    pub async fn create_disk(
        &self,
        resource_group_name: &str,
        disk_name: &str,
        body: &DiskCreateRequest,
    ) -> Result<Disk> {
        self.ops
            .create_disk(
                self.client.subscription_id(),
                resource_group_name,
                disk_name,
                body,
            )
            .await
    }

    /// Delete a managed disk.
    pub async fn delete_disk(&self, resource_group_name: &str, disk_name: &str) -> Result<()> {
        self.ops
            .delete_disk(
                self.client.subscription_id(),
                resource_group_name,
                disk_name,
            )
            .await
    }

    /// Change the SKU (performance tier) of a managed disk.
    ///
    /// The disk must be unattached OR the attached VM must be fully deallocated
    /// before this call will succeed. Returns an error if the VM is still running.
    ///
    /// # Arguments
    /// * `resource_group_name` - Resource group containing the disk
    /// * `disk_name` - Name of the disk to update
    /// * `sku_name` - Target SKU name: "StandardSSD_LRS", "StandardSSD_ZRS",
    ///   "Standard_LRS", "Premium_LRS", "Premium_ZRS"
    pub async fn update_disk_sku(
        &self,
        resource_group_name: &str,
        disk_name: &str,
        sku_name: &str,
    ) -> Result<Disk> {
        let body = DiskUpdateRequest {
            sku: Some(DiskSku {
                name: Some(sku_name.to_string()),
                ..Default::default()
            }),
            ..Default::default()
        };
        self.ops
            .update_disk(
                self.client.subscription_id(),
                resource_group_name,
                disk_name,
                &body,
            )
            .await
    }

    /// Delete a managed disk snapshot.
    pub async fn delete_snapshot(
        &self,
        subscription_id: &str,
        resource_group_name: &str,
        snapshot_name: &str,
    ) -> Result<()> {
        self.ops
            .delete_snapshot(subscription_id, resource_group_name, snapshot_name)
            .await
    }

    /// Grant SAS access to a managed disk.
    ///
    /// The Azure `beginGetAccess` endpoint returns HTTP 202 with a `Location` header
    /// pointing to an async operation URL. This method polls that URL until the
    /// operation completes and returns the SAS URI.
    pub async fn grant_access(
        &self,
        resource_group_name: &str,
        disk_name: &str,
        body: &GrantAccessData,
    ) -> Result<AccessUri> {
        let sub = self.client.subscription_id();
        let url = format!(
            "https://management.azure.com/subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Compute/disks/{disk}/beginGetAccess?api-version=2024-07-01",
            rg = urlencoding::encode(resource_group_name),
            disk = urlencoding::encode(disk_name),
        );

        let body_bytes =
            serde_json::to_vec(body).map_err(|e| crate::AzureError::InvalidResponse {
                message: format!("Failed to serialize grant_access request: {e}"),
                body: None,
            })?;

        let response = self.client.post(&url, &body_bytes).await?;
        let location = response.location();
        let response_bytes = response.error_for_status().await?.bytes().await?;

        // If the response body is non-empty, parse it directly (rare synchronous response)
        if !response_bytes.is_empty() {
            return serde_json::from_slice(&response_bytes).map_err(|e| {
                crate::AzureError::InvalidResponse {
                    message: format!("Failed to parse grant_access response: {e}"),
                    body: Some(String::from_utf8_lossy(&response_bytes).to_string()),
                }
            });
        }

        // 202 Accepted: poll the Location URL until the operation completes
        let poll_url = location.ok_or_else(|| crate::AzureError::InvalidResponse {
            message: "grant_access returned 202 with no Location header".into(),
            body: None,
        })?;

        for _ in 0..30 {
            tokio::time::sleep(std::time::Duration::from_secs(2)).await;
            let poll_response = self.client.get(&poll_url).await?;
            let poll_bytes = poll_response.error_for_status().await?.bytes().await?;
            if !poll_bytes.is_empty() {
                return serde_json::from_slice(&poll_bytes).map_err(|e| {
                    crate::AzureError::InvalidResponse {
                        message: format!("Failed to parse grant_access poll response: {e}"),
                        body: Some(String::from_utf8_lossy(&poll_bytes).to_string()),
                    }
                });
            }
        }

        Err(crate::AzureError::InvalidResponse {
            message: "grant_access polling timed out after 30 attempts".into(),
            body: None,
        })
    }

    /// Revoke SAS access to a managed disk.
    pub async fn revoke_access(&self, resource_group_name: &str, disk_name: &str) -> Result<()> {
        self.ops
            .revoke_access(
                self.client.subscription_id(),
                resource_group_name,
                disk_name,
            )
            .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::compute::VirtualMachineProperties;

    #[tokio::test]
    async fn list_vms_returns_empty_list() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines")
            .returning_json(serde_json::json!({
                "value": []
            }));

        let client = AzureHttpClient::from_mock(mock);
        let compute = client.compute();
        let result = compute.list_vms("my-rg").await.expect("list_vms failed");
        assert!(result.value.is_empty());
        assert!(result.next_link.is_none());
    }

    #[tokio::test]
    async fn list_vms_returns_vm_with_fields() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines")
            .returning_json(serde_json::json!({
                "value": [{
                    "id": "/subscriptions/sub/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachines/vm1",
                    "name": "vm1",
                    "type": "Microsoft.Compute/virtualMachines",
                    "location": "eastus",
                    "properties": {
                        "vmId": "abc-123",
                        "provisioningState": "Succeeded",
                        "hardwareProfile": { "vmSize": "Standard_B1s" }
                    }
                }]
            }));

        let client = AzureHttpClient::from_mock(mock);
        let compute = client.compute();
        let result = compute.list_vms("my-rg").await.expect("list_vms failed");
        assert_eq!(result.value.len(), 1);
        let vm = &result.value[0];
        assert_eq!(vm.name.as_deref(), Some("vm1"));
        assert_eq!(vm.location.as_deref(), Some("eastus"));
        assert_eq!(
            vm.properties.as_ref().and_then(|p| p.vm_id.as_deref()),
            Some("abc-123"),
        );
        assert_eq!(
            vm.properties
                .as_ref()
                .and_then(|p| p.provisioning_state.as_deref()),
            Some("Succeeded"),
        );
    }

    #[tokio::test]
    async fn get_vm_injects_subscription_id() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/my-vm")
            .returning_json(serde_json::json!({
                "id": "/subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/my-vm",
                "name": "my-vm",
                "type": "Microsoft.Compute/virtualMachines",
                "location": "westus2",
                "properties": {
                    "vmId": "vm-uuid",
                    "provisioningState": "Succeeded"
                }
            }));

        let client = AzureHttpClient::from_mock(mock);
        let compute = client.compute();
        let vm = compute.get_vm("rg1", "my-vm").await.expect("get_vm failed");
        assert_eq!(vm.name.as_deref(), Some("my-vm"));
        assert_eq!(vm.location.as_deref(), Some("westus2"));
        assert_eq!(
            vm.properties.as_ref().and_then(|p| p.vm_id.as_deref()),
            Some("vm-uuid"),
        );
    }

    #[tokio::test]
    async fn create_vm_sends_put_and_returns_vm() {
        let mut mock = crate::MockClient::new();
        mock.expect_put("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/new-vm")
            .returning_json(serde_json::json!({
                "name": "new-vm",
                "location": "eastus",
                "properties": {
                    "provisioningState": "Creating",
                    "hardwareProfile": { "vmSize": "Standard_B1s" }
                }
            }));

        let client = AzureHttpClient::from_mock(mock);
        let compute = client.compute();
        let request = VirtualMachineCreateRequest {
            location: "eastus".into(),
            properties: Some(VirtualMachineProperties {
                hardware_profile: Some(crate::types::compute::HardwareProfile {
                    vm_size: Some("Standard_B1s".into()),
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let vm = compute
            .create_vm("rg1", "new-vm", &request)
            .await
            .expect("create_vm failed");
        assert_eq!(vm.name.as_deref(), Some("new-vm"));
        assert_eq!(
            vm.properties
                .as_ref()
                .and_then(|p| p.provisioning_state.as_deref()),
            Some("Creating"),
        );
    }

    #[tokio::test]
    async fn delete_vm_sends_delete() {
        let mut mock = crate::MockClient::new();
        mock.expect_delete("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/dead-vm")
            .returning_json(serde_json::json!(null));

        let client = AzureHttpClient::from_mock(mock);
        let compute = client.compute();
        compute
            .delete_vm("rg1", "dead-vm")
            .await
            .expect("delete_vm failed");
    }

    #[tokio::test]
    async fn deallocate_vm_sends_post() {
        let mut mock = crate::MockClient::new();
        mock.expect_post("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/my-vm/deallocate")
            .returning_json(serde_json::json!(null));

        let client = AzureHttpClient::from_mock(mock);
        let compute = client.compute();
        compute
            .deallocate_vm("rg1", "my-vm")
            .await
            .expect("deallocate_vm failed");
    }

    #[tokio::test]
    async fn get_instance_view_returns_statuses() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachines/my-vm/instanceView")
            .returning_json(serde_json::json!({
                "statuses": [
                    {
                        "code": "ProvisioningState/succeeded",
                        "displayStatus": "Provisioning succeeded",
                        "level": "Info"
                    },
                    {
                        "code": "PowerState/running",
                        "displayStatus": "VM running",
                        "level": "Info"
                    }
                ]
            }));

        let client = AzureHttpClient::from_mock(mock);
        let compute = client.compute();
        let iv = compute
            .get_instance_view("rg1", "my-vm")
            .await
            .expect("get_instance_view failed");
        assert_eq!(iv.statuses.len(), 2);
        assert_eq!(
            iv.statuses[0].code.as_deref(),
            Some("ProvisioningState/succeeded")
        );
        assert_eq!(
            iv.statuses[0].display_status.as_deref(),
            Some("Provisioning succeeded")
        );
        assert_eq!(iv.statuses[1].code.as_deref(), Some("PowerState/running"));
    }

    // --- VMSS unit tests ---

    #[tokio::test]
    async fn list_vmss_returns_empty_list() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachineScaleSets")
            .returning_json(serde_json::json!({ "value": [] }));

        let client = AzureHttpClient::from_mock(mock);
        let result = client
            .compute()
            .list_vmss("my-rg")
            .await
            .expect("list_vmss failed");
        assert!(result.value.is_empty());
        assert!(result.next_link.is_none());
    }

    #[tokio::test]
    async fn list_vmss_returns_vmss_with_fields() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachineScaleSets")
            .returning_json(serde_json::json!({
                "value": [{
                    "id": "/subscriptions/sub/resourceGroups/my-rg/providers/Microsoft.Compute/virtualMachineScaleSets/my-vmss",
                    "name": "my-vmss",
                    "type": "Microsoft.Compute/virtualMachineScaleSets",
                    "location": "eastus",
                    "sku": { "name": "Standard_B1s", "tier": "Standard", "capacity": 1 },
                    "properties": {
                        "provisioningState": "Succeeded",
                        "uniqueId": "abc-123"
                    }
                }]
            }));

        let client = AzureHttpClient::from_mock(mock);
        let result = client
            .compute()
            .list_vmss("my-rg")
            .await
            .expect("list_vmss failed");
        assert_eq!(result.value.len(), 1);
        let vmss = &result.value[0];
        assert_eq!(vmss.name.as_deref(), Some("my-vmss"));
        assert_eq!(vmss.location.as_deref(), Some("eastus"));
        assert_eq!(
            vmss.sku.as_ref().and_then(|s| s.name.as_deref()),
            Some("Standard_B1s")
        );
        assert_eq!(vmss.sku.as_ref().and_then(|s| s.capacity), Some(1));
        assert_eq!(
            vmss.properties
                .as_ref()
                .and_then(|p| p.provisioning_state.as_deref()),
            Some("Succeeded"),
        );
        assert_eq!(
            vmss.properties
                .as_ref()
                .and_then(|p| p.unique_id.as_deref()),
            Some("abc-123"),
        );
    }

    #[tokio::test]
    async fn get_vmss_injects_subscription_id() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/my-vmss")
            .returning_json(serde_json::json!({
                "name": "my-vmss",
                "type": "Microsoft.Compute/virtualMachineScaleSets",
                "location": "eastus",
                "sku": { "name": "Standard_B1s", "tier": "Standard", "capacity": 2 },
                "properties": {
                    "provisioningState": "Succeeded",
                    "uniqueId": "vmss-uuid"
                }
            }));

        let client = AzureHttpClient::from_mock(mock);
        let vmss = client
            .compute()
            .get_vmss("rg1", "my-vmss")
            .await
            .expect("get_vmss failed");
        assert_eq!(vmss.name.as_deref(), Some("my-vmss"));
        assert_eq!(vmss.sku.as_ref().and_then(|s| s.capacity), Some(2));
        assert_eq!(
            vmss.properties
                .as_ref()
                .and_then(|p| p.unique_id.as_deref()),
            Some("vmss-uuid"),
        );
    }

    #[tokio::test]
    async fn create_vmss_sends_put_and_returns_vmss() {
        let mut mock = crate::MockClient::new();
        mock.expect_put("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/new-vmss")
            .returning_json(serde_json::json!({
                "name": "new-vmss",
                "location": "eastus",
                "sku": { "name": "Standard_B1s", "tier": "Standard", "capacity": 1 },
                "properties": {
                    "provisioningState": "Creating"
                }
            }));

        let client = AzureHttpClient::from_mock(mock);
        let request = VirtualMachineScaleSetCreateRequest {
            location: "eastus".into(),
            sku: Some(crate::types::compute::Sku {
                name: Some("Standard_B1s".into()),
                capacity: Some(1),
                ..Default::default()
            }),
            ..Default::default()
        };
        let vmss = client
            .compute()
            .create_vmss("rg1", "new-vmss", &request)
            .await
            .expect("create_vmss failed");
        assert_eq!(vmss.name.as_deref(), Some("new-vmss"));
        assert_eq!(
            vmss.properties
                .as_ref()
                .and_then(|p| p.provisioning_state.as_deref()),
            Some("Creating"),
        );
    }

    #[tokio::test]
    async fn delete_vmss_sends_delete() {
        let mut mock = crate::MockClient::new();
        mock.expect_delete("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/dead-vmss")
            .returning_json(serde_json::json!(null));

        let client = AzureHttpClient::from_mock(mock);
        client
            .compute()
            .delete_vmss("rg1", "dead-vmss")
            .await
            .expect("delete_vmss failed");
    }

    #[tokio::test]
    async fn list_vmss_instances_returns_instances() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/my-vmss/virtualMachines")
            .returning_json(serde_json::json!({
                "value": [{
                    "name": "my-vmss_0",
                    "instanceId": "0",
                    "location": "eastus",
                    "properties": {
                        "latestModelApplied": true,
                        "provisioningState": "Succeeded",
                        "vmId": "instance-uuid"
                    }
                }]
            }));

        let client = AzureHttpClient::from_mock(mock);
        let result = client
            .compute()
            .list_vmss_instances("rg1", "my-vmss")
            .await
            .expect("list_vmss_instances failed");
        assert_eq!(result.value.len(), 1);
        let inst = &result.value[0];
        assert_eq!(inst.name.as_deref(), Some("my-vmss_0"));
        assert_eq!(inst.instance_id.as_deref(), Some("0"));
        assert_eq!(
            inst.properties
                .as_ref()
                .and_then(|p| p.provisioning_state.as_deref()),
            Some("Succeeded"),
        );
        assert_eq!(
            inst.properties
                .as_ref()
                .and_then(|p| p.latest_model_applied),
            Some(true),
        );
    }

    #[tokio::test]
    async fn stop_vmss_instances_sends_post() {
        let mut mock = crate::MockClient::new();
        mock.expect_post("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/my-vmss/poweroff")
            .returning_json(serde_json::json!(null));

        let client = AzureHttpClient::from_mock(mock);
        let ids = VirtualMachineScaleSetVMInstanceIDs {
            instance_ids: vec!["0".into()],
        };
        client
            .compute()
            .stop_vmss_instances("rg1", "my-vmss", &ids)
            .await
            .expect("stop_vmss_instances failed");
    }

    #[tokio::test]
    async fn start_vmss_instances_sends_post() {
        let mut mock = crate::MockClient::new();
        mock.expect_post("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/virtualMachineScaleSets/my-vmss/start")
            .returning_json(serde_json::json!(null));

        let client = AzureHttpClient::from_mock(mock);
        let ids = VirtualMachineScaleSetVMInstanceIDs {
            instance_ids: vec!["0".into(), "1".into()],
        };
        client
            .compute()
            .start_vmss_instances("rg1", "my-vmss", &ids)
            .await
            .expect("start_vmss_instances failed");
    }

    // --- Managed Disk unit tests ---

    #[tokio::test]
    async fn list_disks_returns_empty_list() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/my-rg/providers/Microsoft.Compute/disks")
            .returning_json(serde_json::json!({ "value": [] }));

        let client = AzureHttpClient::from_mock(mock);
        let result = client
            .compute()
            .list_disks("my-rg")
            .await
            .expect("list_disks failed");
        assert!(result.value.is_empty());
        assert!(result.next_link.is_none());
    }

    #[tokio::test]
    async fn list_disks_returns_disk_with_fields() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/my-rg/providers/Microsoft.Compute/disks")
            .returning_json(serde_json::json!({
                "value": [{
                    "id": "/subscriptions/sub/resourceGroups/my-rg/providers/Microsoft.Compute/disks/my-disk",
                    "name": "my-disk",
                    "type": "Microsoft.Compute/disks",
                    "location": "eastus",
                    "sku": { "name": "Standard_LRS" },
                    "properties": {
                        "diskSizeGB": 32,
                        "diskState": "Unattached",
                        "provisioningState": "Succeeded"
                    }
                }]
            }));

        let client = AzureHttpClient::from_mock(mock);
        let result = client
            .compute()
            .list_disks("my-rg")
            .await
            .expect("list_disks failed");
        assert_eq!(result.value.len(), 1);
        let disk = &result.value[0];
        assert_eq!(disk.name.as_deref(), Some("my-disk"));
        assert_eq!(disk.location.as_str(), "eastus");
        assert_eq!(
            disk.sku.as_ref().and_then(|s| s.name.as_deref()),
            Some("Standard_LRS")
        );
        assert_eq!(
            disk.properties.as_ref().and_then(|p| p.disk_size_gb),
            Some(32)
        );
        assert_eq!(
            disk.properties
                .as_ref()
                .and_then(|p| p.disk_state.as_deref()),
            Some("Unattached"),
        );
    }

    #[tokio::test]
    async fn list_disks_in_subscription_returns_list() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/providers/Microsoft.Compute/disks")
            .returning_json(serde_json::json!({
                "value": [{
                    "name": "disk-a",
                    "location": "eastus",
                    "properties": {
                        "diskSizeGB": 64,
                        "diskState": "Unattached"
                    }
                }]
            }));

        let client = AzureHttpClient::from_mock(mock);
        let result = client
            .compute()
            .list_disks_in_subscription()
            .await
            .expect("list_disks_in_subscription failed");
        assert_eq!(result.value.len(), 1);
        assert_eq!(result.value[0].name.as_deref(), Some("disk-a"));
    }

    #[tokio::test]
    async fn get_disk_returns_disk_fields() {
        let mut mock = crate::MockClient::new();
        mock.expect_get("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/disks/my-disk")
            .returning_json(serde_json::json!({
                "id": "/subscriptions/sub/resourceGroups/rg1/providers/Microsoft.Compute/disks/my-disk",
                "name": "my-disk",
                "location": "westus2",
                "sku": { "name": "Premium_LRS", "tier": "Premium" },
                "properties": {
                    "diskSizeGB": 128,
                    "diskState": "Unattached",
                    "provisioningState": "Succeeded",
                    "uniqueId": "disk-uuid-abc"
                }
            }));

        let client = AzureHttpClient::from_mock(mock);
        let disk = client
            .compute()
            .get_disk("rg1", "my-disk")
            .await
            .expect("get_disk failed");
        assert_eq!(disk.name.as_deref(), Some("my-disk"));
        assert_eq!(disk.location.as_str(), "westus2");
        assert_eq!(
            disk.sku.as_ref().and_then(|s| s.name.as_deref()),
            Some("Premium_LRS")
        );
        assert_eq!(
            disk.properties.as_ref().and_then(|p| p.disk_size_gb),
            Some(128)
        );
        assert_eq!(
            disk.properties
                .as_ref()
                .and_then(|p| p.unique_id.as_deref()),
            Some("disk-uuid-abc"),
        );
    }

    #[tokio::test]
    async fn create_disk_sends_put_and_returns_disk() {
        use crate::types::compute::{DiskCreateRequest, DiskCreationData, DiskProperties, DiskSku};
        let mut mock = crate::MockClient::new();
        mock.expect_put("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/disks/new-disk")
            .returning_json(serde_json::json!({
                "name": "new-disk",
                "location": "eastus",
                "sku": { "name": "Standard_LRS" },
                "properties": {
                    "diskSizeGB": 4,
                    "diskState": "Unattached",
                    "provisioningState": "Updating"
                }
            }));

        let client = AzureHttpClient::from_mock(mock);
        let request = DiskCreateRequest {
            location: "eastus".into(),
            sku: Some(DiskSku {
                name: Some("Standard_LRS".into()),
                ..Default::default()
            }),
            properties: Some(DiskProperties {
                disk_size_gb: Some(4),
                creation_data: Some(DiskCreationData {
                    create_option: "Empty".into(),
                    ..Default::default()
                }),
                ..Default::default()
            }),
            ..Default::default()
        };
        let disk = client
            .compute()
            .create_disk("rg1", "new-disk", &request)
            .await
            .expect("create_disk failed");
        assert_eq!(disk.name.as_deref(), Some("new-disk"));
        assert_eq!(
            disk.properties.as_ref().and_then(|p| p.disk_size_gb),
            Some(4)
        );
        assert_eq!(
            disk.properties
                .as_ref()
                .and_then(|p| p.provisioning_state.as_deref()),
            Some("Updating"),
        );
    }

    #[tokio::test]
    async fn delete_disk_sends_delete() {
        let mut mock = crate::MockClient::new();
        mock.expect_delete("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/disks/old-disk")
            .returning_json(serde_json::json!(null));

        let client = AzureHttpClient::from_mock(mock);
        client
            .compute()
            .delete_disk("rg1", "old-disk")
            .await
            .expect("delete_disk failed");
    }

    #[tokio::test]
    async fn update_disk_sku_sends_patch_and_returns_disk() {
        let mut mock = crate::MockClient::new();
        mock.expect_patch("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/disks/my-disk")
            .returning_json(serde_json::json!({
                "name": "my-disk",
                "location": "eastus",
                "sku": { "name": "StandardSSD_LRS", "tier": "StandardSSD" },
                "properties": {
                    "diskSizeGB": 128,
                    "diskState": "Unattached",
                    "provisioningState": "Succeeded"
                }
            }));

        let client = AzureHttpClient::from_mock(mock);
        let disk = client
            .compute()
            .update_disk_sku("rg1", "my-disk", "StandardSSD_LRS")
            .await
            .expect("update_disk_sku failed");
        assert_eq!(disk.name.as_deref(), Some("my-disk"));
        assert_eq!(
            disk.sku.as_ref().and_then(|s| s.name.as_deref()),
            Some("StandardSSD_LRS")
        );
        assert_eq!(
            disk.properties.as_ref().and_then(|p| p.disk_size_gb),
            Some(128)
        );
        assert_eq!(
            disk.properties
                .as_ref()
                .and_then(|p| p.disk_state.as_deref()),
            Some("Unattached"),
        );
        assert_eq!(
            disk.properties
                .as_ref()
                .and_then(|p| p.provisioning_state.as_deref()),
            Some("Succeeded"),
        );
    }

    #[tokio::test]
    async fn revoke_access_sends_post() {
        let mut mock = crate::MockClient::new();
        mock.expect_post("/subscriptions/test-subscription-id/resourceGroups/rg1/providers/Microsoft.Compute/disks/my-disk/endGetAccess")
            .returning_json(serde_json::json!(null));

        let client = AzureHttpClient::from_mock(mock);
        client
            .compute()
            .revoke_access("rg1", "my-disk")
            .await
            .expect("revoke_access failed");
    }
}