@@ -54,3 +54,57 @@ func IsImageOffered(req ImageRequest) bool {
5454 }
5555 return true
5656}
57+
58+ func SkuG2Support (location string , publisher string , offer string , sku string ) (string , error ) {
59+ cred , err := azidentity .NewDefaultAzureCredential (nil )
60+ if err != nil {
61+ return "" , err
62+ }
63+ subscriptionId := os .Getenv ("AZURE_SUBSCRIPTION_ID" )
64+
65+ clientFactory , err := armcompute .NewClientFactory (subscriptionId , cred , nil )
66+ if err != nil {
67+ return "" , err
68+ }
69+ imagesClient := clientFactory .NewVirtualMachineImagesClient ()
70+ if ! verify_g2 (imagesClient , location , publisher , offer , sku ) {
71+ finalSKU , err := get_g2_sku (imagesClient , location , publisher , offer , sku )
72+ if err == nil && finalSKU != "" {
73+ if verify_g2 (imagesClient , location , publisher , offer , finalSKU ) {
74+ fmt .Printf ("%s is g1, Using SKU %s\n " , sku , finalSKU )
75+ return finalSKU , nil
76+ }
77+ }
78+ } else {
79+ return sku , nil
80+ }
81+ return "" , fmt .Errorf ("the SKU %s is not support for G2" , sku )
82+ }
83+
84+ func verify_g2 (imagesClient * armcompute.VirtualMachineImagesClient , location string , publisher string , offer string , sku string ) bool {
85+ // List available image versions
86+ resp , err := imagesClient .List (context .Background (),location , publisher , offer , sku , nil )
87+ if err != nil {
88+ return false
89+ }
90+
91+ image := resp .VirtualMachineImageResourceArray [0 ]
92+ version := * image .Name
93+ resps , _ := imagesClient .Get (context .Background (),location , publisher , offer , sku , version , nil )
94+ info := resps .VirtualMachineImage
95+ generation := * info .Properties .HyperVGeneration
96+ return generation == "V2"
97+ }
98+
99+ func get_g2_sku (imagesClient * armcompute.VirtualMachineImagesClient , location string , publisher string , offer string , originSKU string ) (string , error ) {
100+ resp , err := imagesClient .ListSKUs (context .Background (),location , publisher , offer , nil )
101+ if err != nil {
102+ return "" , err
103+ }
104+ for _ , sku := range resp .VirtualMachineImageResourceArray {
105+ if strings .HasPrefix (* sku .Name , originSKU + "-" ) {
106+ return * sku .Name , nil
107+ }
108+ }
109+ return "" , nil
110+ }
0 commit comments