Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

shared_image_version - support for new block uefi_settings #28076

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/services/compute/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryapplications"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryapplicationversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimages"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/gallerysharingupdate"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-03-01/virtualmachineruncommands"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-04-02/disks"
Expand Down
2 changes: 1 addition & 1 deletion internal/services/compute/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"sort"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2024-07-01/virtualmachinescalesets"
"github.com/hashicorp/go-version"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/services/compute/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
)

func TestSortVersions_valid(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/services/compute/shared_image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimages"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down
282 changes: 280 additions & 2 deletions internal/services/compute/shared_image_version_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-01/images"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2023-07-03/galleryimageversions"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
Expand Down Expand Up @@ -190,6 +190,55 @@ func resourceSharedImageVersion() *pluginsdk.Resource {
Default: false,
},

"uefi_settings": {
Type: pluginsdk.TypeList,
Optional: true,
ForceNew: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"signature_template_names": {
Type: pluginsdk.TypeSet,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
ValidateFunc: validation.StringInSlice(galleryimageversions.PossibleValuesForUefiSignatureTemplateName(), false),
},
},
"additional_signatures": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"db": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: uefiKeySchema(),
},
"dbx": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: uefiKeySchema(),
},
"kek": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: uefiKeySchema(),
},
"pk": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: uefiKeySchema(),
},
},
},
},
},
},
},

"tags": commonschema.Tags(),
},

Expand Down Expand Up @@ -236,7 +285,8 @@ func resourceSharedImageVersionCreate(d *pluginsdk.ResourceData, meta interface{
SafetyProfile: &galleryimageversions.GalleryImageVersionSafetyProfile{
AllowDeletionOfReplicatedLocations: utils.Bool(d.Get("deletion_of_replicated_locations_enabled").(bool)),
},
StorageProfile: galleryimageversions.GalleryImageVersionStorageProfile{},
StorageProfile: galleryimageversions.GalleryImageVersionStorageProfile{},
SecurityProfile: &galleryimageversions.ImageVersionSecurityProfile{},
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}
Expand Down Expand Up @@ -271,6 +321,10 @@ func resourceSharedImageVersionCreate(d *pluginsdk.ResourceData, meta interface{
}
}

if v, ok := d.GetOk("uefi_settings"); ok {
version.Properties.SecurityProfile.UefiSettings = expandUefiSettings(v.([]interface{}))
}

if err := client.CreateOrUpdateThenPoll(ctx, id, version); err != nil {
return fmt.Errorf("creating %s: %+v", id, err)
}
Expand Down Expand Up @@ -330,6 +384,17 @@ func resourceSharedImageVersionUpdate(d *pluginsdk.ResourceData, meta interface{
payload.Properties.PublishingProfile.ExcludeFromLatest = pointer.To(d.Get("exclude_from_latest").(bool))
}

if d.HasChange("uefi_settings") {
if payload.Properties.SecurityProfile == nil {
payload.Properties.SecurityProfile = &galleryimageversions.ImageVersionSecurityProfile{}
}
if v, ok := d.GetOk("uefi_settings"); ok {
payload.Properties.SecurityProfile.UefiSettings = expandUefiSettings(v.([]interface{}))
} else {
payload.Properties.SecurityProfile.UefiSettings = nil
}
}

if d.HasChange("tags") {
payload.Tags = tags.Expand(d.Get("tags").(map[string]interface{}))
}
Expand Down Expand Up @@ -416,6 +481,16 @@ func resourceSharedImageVersionRead(d *pluginsdk.ResourceData, meta interface{})
if safetyProfile := props.SafetyProfile; safetyProfile != nil {
d.Set("deletion_of_replicated_locations_enabled", pointer.From(safetyProfile.AllowDeletionOfReplicatedLocations))
}

if securityProfile := props.SecurityProfile; securityProfile != nil {
if uefiSettings := securityProfile.UefiSettings; uefiSettings != nil {
d.Set("uefi_settings", flattenUefiSettings(uefiSettings))
} else {
d.Set("uefi_settings", nil)
}
} else {
d.Set("uefi_settings", nil)
}
Comment on lines +485 to +493
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if securityProfile := props.SecurityProfile; securityProfile != nil {
if uefiSettings := securityProfile.UefiSettings; uefiSettings != nil {
d.Set("uefi_settings", flattenUefiSettings(uefiSettings))
} else {
d.Set("uefi_settings", nil)
}
} else {
d.Set("uefi_settings", nil)
}
if securityProfile := props.SecurityProfile; securityProfile != nil {
d.Set("uefi_settings", flattenUefiSettings(securityProfile.UefiSettings;))
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I guess I thought to complicated here.

}
return tags.FlattenAndSet(d, model.Tags)
}
Expand Down Expand Up @@ -511,6 +586,209 @@ func expandSharedImageVersionTargetRegions(d *pluginsdk.ResourceData) (*[]galler
return &results, nil
}

func uefiKeySchema() *pluginsdk.Resource {
return &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"certificate_base64": {
Type: pluginsdk.TypeList,
Required: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
"type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(galleryimageversions.PossibleValuesForUefiKeyType(), false),
},
},
}
}

func expandUefiSettings(input []interface{}) *galleryimageversions.GalleryImageVersionUefiSettings {
if len(input) == 0 || input[0] == nil {
return nil
}

v := input[0].(map[string]interface{})
result := &galleryimageversions.GalleryImageVersionUefiSettings{}

if templateNamesSet, ok := v["signature_template_names"].(*pluginsdk.Set); ok {
result.SignatureTemplateNames = expandSignatureTemplateNames(templateNamesSet.List())
}

if additionalSignatures, ok := v["additional_signatures"].([]interface{}); ok {
result.AdditionalSignatures = expandAdditionalSignatures(additionalSignatures)
}

return result
}

func expandSignatureTemplateNames(input []interface{}) *[]galleryimageversions.UefiSignatureTemplateName {
if len(input) == 0 {
return nil
}

result := make([]galleryimageversions.UefiSignatureTemplateName, 0)
for _, v := range input {
result = append(result, galleryimageversions.UefiSignatureTemplateName(v.(string)))
}
return &result
}

func expandAdditionalSignatures(input []interface{}) *galleryimageversions.UefiKeySignatures {
if len(input) == 0 || input[0] == nil {
return nil
}

v := input[0].(map[string]interface{})
result := &galleryimageversions.UefiKeySignatures{}

if db, ok := v["db"].([]interface{}); ok {
result.Db = expandUefiKeyList(db)
}

if dbx, ok := v["dbx"].([]interface{}); ok {
result.Dbx = expandUefiKeyList(dbx)
}

if kek, ok := v["kek"].([]interface{}); ok {
result.Kek = expandUefiKeyList(kek)
}

if pk, ok := v["pk"].([]interface{}); ok {
result.Pk = expandSingleUefiKey(pk)
}

return result
}

func expandUefiKeyList(input []interface{}) *[]galleryimageversions.UefiKey {
if len(input) == 0 {
return nil
}

result := make([]galleryimageversions.UefiKey, 0)
for _, v := range input {
result = append(result, expandUefiKey(v.(map[string]interface{})))
}
return &result
}

func expandSingleUefiKey(input []interface{}) *galleryimageversions.UefiKey {
if len(input) == 0 {
return nil
}

data := input[0].(map[string]interface{})
return &galleryimageversions.UefiKey{
Type: pointer.To(galleryimageversions.UefiKeyType(data["type"].(string))),
Value: &[]string{data["certificate_base64"].(string)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since certificate_base64 is a string list, why convert it to a string ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed both Single functions. Those were artifacts I missed in the refactoring.

}
}

func expandUefiKey(input map[string]interface{}) galleryimageversions.UefiKey {
result := galleryimageversions.UefiKey{
Value: &[]string{},
}

if certList, ok := input["certificate_base64"].([]interface{}); ok {
certData := make([]string, 0)
for _, item := range certList {
if str, ok := item.(string); ok {
certData = append(certData, str)
}
}
result.Value = &certData
}

if typeStr, ok := input["type"].(string); ok {
result.Type = pointer.To(galleryimageversions.UefiKeyType(typeStr))
}

return result
}

func flattenUefiSettings(input *galleryimageversions.GalleryImageVersionUefiSettings) []interface{} {
results := make([]interface{}, 0)

if input == nil {
return results
}

results = append(results, map[string]interface{}{
"signature_template_names": pointer.From(input.SignatureTemplateNames),
"additional_signatures": flattenAdditionalSignatures(input.AdditionalSignatures),
})

return results
}

func flattenAdditionalSignatures(input *galleryimageversions.UefiKeySignatures) []interface{} {
results := make([]interface{}, 0)

if input == nil {
return results
}

result := make(map[string]interface{})
result["db"] = flattenUefiKeyList(input.Db)
result["dbx"] = flattenUefiKeyList(input.Dbx)
result["kek"] = flattenUefiKeyList(input.Kek)
result["pk"] = flattenSingleUefiKey(input.Pk)

return append(results, result)
}

func flattenSingleUefiKey(input *galleryimageversions.UefiKey) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

result := make(map[string]interface{})
if input.Value != nil && len(*input.Value) > 0 {
result["certificate_base64"] = (*input.Value)[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

certificate_base64 is a string list, why assign the first element only?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed both Single functions. Those were artifacts I missed in the refactoring.

}
if input.Type != nil {
result["type"] = pointer.From(input.Type)
}

return append(results, result)
}

func flattenUefiKeyList(input *[]galleryimageversions.UefiKey) []interface{} {
results := make([]interface{}, 0)
if input == nil {
return results
}

for _, v := range *input {
if item := flattenUefiKey(&v); len(item) > 0 {
results = append(results, item[0])
}
}

return results
}

func flattenUefiKey(input *galleryimageversions.UefiKey) []interface{} {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's the difference between flattenSingleUefiKey and flattenUefiKey ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed both Single functions. Those were artifacts I missed in the refactoring.

results := make([]interface{}, 0)
if input == nil {
return results
}

result := make(map[string]interface{})
if input.Value != nil && len(*input.Value) > 0 {
result["certificate_base64"] = (*input.Value)[0]
}
if input.Type != nil {
result["type"] = pointer.From(input.Type)
}

return append(results, result)
}

func flattenSharedImageVersionTargetRegions(input *[]galleryimageversions.TargetRegion) []interface{} {
results := make([]interface{}, 0)

Expand Down
Loading