Skip to content

Commit b890da1

Browse files
committed
shared_image_version_resource - add uefi_settings
1 parent e5c7f99 commit b890da1

2 files changed

Lines changed: 346 additions & 6 deletions

File tree

internal/services/compute/shared_image_version_resource.go

Lines changed: 252 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,63 @@ func resourceSharedImageVersion() *pluginsdk.Resource {
190190
Default: false,
191191
},
192192

193+
"uefi_settings": {
194+
Type: pluginsdk.TypeList,
195+
Optional: true,
196+
ForceNew: true,
197+
MaxItems: 1,
198+
Elem: &pluginsdk.Resource{
199+
Schema: map[string]*pluginsdk.Schema{
200+
"signature_template_names": {
201+
Type: pluginsdk.TypeSet,
202+
Required: true,
203+
Elem: &pluginsdk.Schema{
204+
Type: pluginsdk.TypeString,
205+
ValidateFunc: validation.StringInSlice(galleryimageversions.PossibleValuesForUefiSignatureTemplateName(), false),
206+
},
207+
},
208+
"additional_signatures": {
209+
Type: pluginsdk.TypeList,
210+
Optional: true,
211+
MaxItems: 1,
212+
Elem: &pluginsdk.Resource{
213+
Schema: map[string]*pluginsdk.Schema{
214+
"db": {
215+
Type: pluginsdk.TypeList,
216+
Optional: true,
217+
Elem: uefiKeySchema(),
218+
},
219+
"dbx": {
220+
Type: pluginsdk.TypeList,
221+
Optional: true,
222+
Elem: uefiKeySchema(),
223+
},
224+
"kek": {
225+
Type: pluginsdk.TypeList,
226+
Optional: true,
227+
Elem: uefiKeySchema(),
228+
},
229+
"pk": {
230+
Type: pluginsdk.TypeList,
231+
Optional: true,
232+
MaxItems: 1,
233+
Elem: uefiKeySchema(),
234+
},
235+
},
236+
},
237+
},
238+
},
239+
},
240+
},
241+
193242
"tags": commonschema.Tags(),
194243
},
195244

196-
CustomizeDiff: pluginsdk.CustomDiffWithAll(
197-
pluginsdk.ForceNewIfChange("end_of_life_date", func(ctx context.Context, old, new, meta interface{}) bool {
198-
return old.(string) != "" && new.(string) == ""
199-
}),
200-
),
245+
CustomizeDiff: pluginsdk.CustomDiffWithAll(
246+
pluginsdk.ForceNewIfChange("end_of_life_date", func(ctx context.Context, old, new, meta interface{}) bool {
247+
return old.(string) != "" && new.(string) == ""
248+
}),
249+
),
201250
}
202251
}
203252

@@ -237,6 +286,9 @@ func resourceSharedImageVersionCreate(d *pluginsdk.ResourceData, meta interface{
237286
AllowDeletionOfReplicatedLocations: utils.Bool(d.Get("deletion_of_replicated_locations_enabled").(bool)),
238287
},
239288
StorageProfile: galleryimageversions.GalleryImageVersionStorageProfile{},
289+
SecurityProfile: &galleryimageversions.ImageVersionSecurityProfile{
290+
UefiSettings: expandUefiSettings(d.GetOk("uefi_settings")),
291+
},
240292
},
241293
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
242294
}
@@ -292,7 +344,7 @@ func resourceSharedImageVersionUpdate(d *pluginsdk.ResourceData, meta interface{
292344

293345
existing, err := client.Get(ctx, *id, galleryimageversions.DefaultGetOperationOptions())
294346
if err != nil {
295-
if !response.WasNotFound(existing.HttpResponse) {
347+
if response.WasNotFound(existing.HttpResponse) {
296348
return fmt.Errorf("checking for presence of existing %s: %+v", id, err)
297349
}
298350
}
@@ -330,6 +382,12 @@ func resourceSharedImageVersionUpdate(d *pluginsdk.ResourceData, meta interface{
330382
payload.Properties.PublishingProfile.ExcludeFromLatest = pointer.To(d.Get("exclude_from_latest").(bool))
331383
}
332384

385+
if d.HasChange("uefi_settings") {
386+
payload.Properties.SecurityProfile = &galleryimageversions.ImageVersionSecurityProfile{
387+
UefiSettings: expandUefiSettings(d.GetOk("uefi_settings")),
388+
}
389+
}
390+
333391
if d.HasChange("tags") {
334392
payload.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
335393
}
@@ -416,6 +474,17 @@ func resourceSharedImageVersionRead(d *pluginsdk.ResourceData, meta interface{})
416474
if safetyProfile := props.SafetyProfile; safetyProfile != nil {
417475
d.Set("deletion_of_replicated_locations_enabled", pointer.From(safetyProfile.AllowDeletionOfReplicatedLocations))
418476
}
477+
478+
if securityProfile := props.SecurityProfile; securityProfile != nil {
479+
if uefiSettings := securityProfile.UefiSettings; uefiSettings != nil {
480+
d.Set("uefi_settings", []interface{}{
481+
map[string]interface{}{
482+
"signature_template_names": uefiSettings.SignatureTemplateNames,
483+
"additional_signatures": flattenAdditionalSignatures(uefiSettings.AdditionalSignatures),
484+
},
485+
})
486+
}
487+
}
419488
}
420489
return tags.FlattenAndSet(d, model.Tags)
421490
}
@@ -511,6 +580,183 @@ func expandSharedImageVersionTargetRegions(d *pluginsdk.ResourceData) (*[]galler
511580
return &results, nil
512581
}
513582

583+
func uefiKeySchema() *pluginsdk.Resource {
584+
possibleKeyTypes := galleryimageversions.PossibleValuesForUefiKeyType()
585+
586+
return &pluginsdk.Resource{
587+
Schema: map[string]*pluginsdk.Schema{
588+
"certificate_base64": {
589+
Type: pluginsdk.TypeList,
590+
Required: true,
591+
Elem: &pluginsdk.Schema{
592+
Type: pluginsdk.TypeString,
593+
},
594+
},
595+
"type": {
596+
Type: pluginsdk.TypeString,
597+
Required: true,
598+
ValidateFunc: validation.StringInSlice(possibleKeyTypes, false),
599+
},
600+
},
601+
}
602+
}
603+
604+
func expandUefiSettings(input interface{}, ok bool) *galleryimageversions.GalleryImageVersionUefiSettings {
605+
if !ok || input == nil {
606+
return nil
607+
}
608+
609+
settings := input.([]interface{})
610+
if len(settings) == 0 {
611+
return nil
612+
}
613+
614+
us := settings[0].(map[string]interface{})
615+
616+
// Convert Set to slice of interfaces
617+
templateNamesSet := us["signature_template_names"].(*pluginsdk.Set)
618+
templateNames := templateNamesSet.List()
619+
620+
return &galleryimageversions.GalleryImageVersionUefiSettings{
621+
SignatureTemplateNames: expandSignatureTemplateNames(templateNames),
622+
AdditionalSignatures: expandAdditionalSignatures(us["additional_signatures"].([]interface{})),
623+
}
624+
}
625+
626+
func expandSignatureTemplateNames(input []interface{}) *[]galleryimageversions.UefiSignatureTemplateName {
627+
result := make([]galleryimageversions.UefiSignatureTemplateName, len(input))
628+
for i, v := range input {
629+
result[i] = galleryimageversions.UefiSignatureTemplateName(v.(string))
630+
}
631+
return &result
632+
}
633+
634+
func expandAdditionalSignatures(input []interface{}) *galleryimageversions.UefiKeySignatures {
635+
if input == nil || len(input) == 0 {
636+
return nil
637+
}
638+
639+
data := input[0].(map[string]interface{})
640+
return &galleryimageversions.UefiKeySignatures{
641+
Db: expandUefiKeyList(data["db"].([]interface{})),
642+
Dbx: expandUefiKeyList(data["dbx"].([]interface{})),
643+
Kek: expandUefiKeyList(data["kek"].([]interface{})),
644+
Pk: expandSingleUefiKey(data["pk"].([]interface{})),
645+
}
646+
}
647+
648+
func expandUefiKeyList(input []interface{}) *[]galleryimageversions.UefiKey {
649+
if input == nil {
650+
return nil
651+
}
652+
653+
result := make([]galleryimageversions.UefiKey, len(input))
654+
for i, v := range input {
655+
result[i] = expandUefiKey(v.(map[string]interface{}))
656+
}
657+
return &result
658+
}
659+
660+
func expandSingleUefiKey(input []interface{}) *galleryimageversions.UefiKey {
661+
if input == nil || len(input) == 0 {
662+
return nil
663+
}
664+
665+
data := input[0].(map[string]interface{})
666+
return &galleryimageversions.UefiKey{
667+
Type: pointer.To(galleryimageversions.UefiKeyType(data["type"].(string))),
668+
Value: &[]string{data["certificate_base64"].(string)},
669+
}
670+
}
671+
672+
func expandUefiKey(data map[string]interface{}) galleryimageversions.UefiKey {
673+
certData := make([]string, 0)
674+
if v, ok := data["certificate_base64"].([]interface{}); ok {
675+
for _, item := range v {
676+
if s, ok := item.(string); ok {
677+
certData = append(certData, s)
678+
}
679+
}
680+
}
681+
682+
return galleryimageversions.UefiKey{
683+
Type: pointer.To(galleryimageversions.UefiKeyType(data["type"].(string))),
684+
Value: &certData,
685+
}
686+
}
687+
688+
func flattenUefiSettings(input *galleryimageversions.GalleryImageVersionUefiSettings) []interface{} {
689+
if input == nil || input.SignatureTemplateNames == nil {
690+
return make([]interface{}, 0)
691+
}
692+
693+
results := make([]interface{}, 0)
694+
results = append(results, map[string]interface{}{
695+
"signature_template_names": *input.SignatureTemplateNames,
696+
"additional_signatures": flattenAdditionalSignatures(input.AdditionalSignatures),
697+
})
698+
699+
return results
700+
}
701+
702+
func flattenAdditionalSignatures(input *galleryimageversions.UefiKeySignatures) []interface{} {
703+
if input == nil {
704+
return make([]interface{}, 0)
705+
}
706+
707+
results := make([]interface{}, 0)
708+
results = append(results, map[string]interface{}{
709+
"db": flattenUefiKeyList(input.Db),
710+
"dbx": flattenUefiKeyList(input.Dbx),
711+
"kek": flattenUefiKeyList(input.Kek),
712+
"pk": flattenSingleUefiKey(input.Pk),
713+
})
714+
715+
return results
716+
}
717+
718+
func flattenSingleUefiKey(input *galleryimageversions.UefiKey) []interface{} {
719+
if input == nil || input.Type == nil || input.Value == nil || len(*input.Value) == 0 {
720+
return make([]interface{}, 0)
721+
}
722+
723+
results := make([]interface{}, 0)
724+
results = append(results, map[string]interface{}{
725+
"certificate_base64": (*input.Value)[0],
726+
"type": string(*input.Type),
727+
})
728+
729+
return results
730+
}
731+
732+
func flattenUefiKeyList(input *[]galleryimageversions.UefiKey) []interface{} {
733+
if input == nil {
734+
return make([]interface{}, 0)
735+
}
736+
737+
results := make([]interface{}, 0)
738+
for _, v := range *input {
739+
if item := flattenUefiKey(&v); len(item) > 0 {
740+
results = append(results, item[0])
741+
}
742+
}
743+
return results
744+
}
745+
746+
func flattenUefiKey(input *galleryimageversions.UefiKey) []interface{} {
747+
if input == nil || input.Value == nil || len(*input.Value) == 0 {
748+
return make([]interface{}, 0)
749+
}
750+
751+
results := make([]interface{}, 0)
752+
results = append(results, map[string]interface{}{
753+
"certificate_base64": (*input.Value)[0],
754+
"type": string(*input.Type),
755+
})
756+
757+
return results
758+
}
759+
514760
func flattenSharedImageVersionTargetRegions(input *[]galleryimageversions.TargetRegion) []interface{} {
515761
results := make([]interface{}, 0)
516762

0 commit comments

Comments
 (0)