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 - add hibernation_enabled updated data source #26975

Merged
merged 4 commits into from
Aug 20, 2024
Merged
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
67 changes: 67 additions & 0 deletions internal/services/compute/shared_image_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package compute

import (
"fmt"
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
Expand Down Expand Up @@ -126,6 +127,36 @@ func dataSourceSharedImage() *pluginsdk.Resource {
Computed: true,
},

"trusted_launch_supported": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"trusted_launch_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"confidential_vm_supported": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"confidential_vm_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"accelerated_network_support_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"hibernation_enabled": {
Type: pluginsdk.TypeBool,
Computed: true,
},

"tags": commonschema.TagsDataSource(),
},
}
Expand Down Expand Up @@ -174,6 +205,42 @@ func dataSourceSharedImageRead(d *pluginsdk.ResourceData, meta interface{}) erro
if err := d.Set("purchase_plan", flattenGalleryImageDataSourcePurchasePlan(props.PurchasePlan)); err != nil {
return fmt.Errorf("setting `purchase_plan`: %+v", err)
}

trustedLaunchSupported := false
trustedLaunchEnabled := false
cvmEnabled := false
cvmSupported := false
acceleratedNetworkSupportEnabled := false
hibernationEnabled := false
if model.Properties.Features != nil {
for _, feature := range *model.Properties.Features {
if feature.Name == nil || feature.Value == nil {
continue
}

if strings.EqualFold(*feature.Name, "SecurityType") {
trustedLaunchSupported = strings.EqualFold(*feature.Value, "TrustedLaunchSupported")
trustedLaunchEnabled = strings.EqualFold(*feature.Value, "TrustedLaunch")
cvmSupported = strings.EqualFold(*feature.Value, "ConfidentialVmSupported")
cvmEnabled = strings.EqualFold(*feature.Value, "ConfidentialVm")
}

if strings.EqualFold(*feature.Name, "IsAcceleratedNetworkSupported") {
acceleratedNetworkSupportEnabled = strings.EqualFold(*feature.Value, "true")
}

if strings.EqualFold(*feature.Name, "IsHibernateSupported") {
hibernationEnabled = strings.EqualFold(*feature.Value, "true")
}
}
}

d.Set("confidential_vm_supported", cvmSupported)
d.Set("confidential_vm_enabled", cvmEnabled)
d.Set("trusted_launch_supported", trustedLaunchSupported)
d.Set("trusted_launch_enabled", trustedLaunchEnabled)
d.Set("accelerated_network_support_enabled", acceleratedNetworkSupportEnabled)
d.Set("hibernation_enabled", hibernationEnabled)
}

return tags.FlattenAndSet(d, model.Tags)
Expand Down
157 changes: 157 additions & 0 deletions internal/services/compute/shared_image_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestAccDataSourceSharedImage_basic(t *testing.T) {
Config: r.basic(data, ""),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("hibernation_enabled").HasValue("false"),
),
},
})
Expand Down Expand Up @@ -57,6 +58,90 @@ func TestAccDataSourceSharedImage_complete(t *testing.T) {
})
}

func TestAccDataSourceSharedImage_hibernationEnabled(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.withHibernationEnabled(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("hibernation_enabled").HasValue("true"),
),
},
})
}

func TestAccDataSourceSharedImage_acceleratedNetworkSupportEnabled(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.withAcceleratedNetworkSupportEnabled(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("accelerated_network_support_enabled").HasValue("true"),
),
},
})
}

func TestAccDataSourceSharedImage_trustedLaunchEnabled(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.withTrustedLaunchEnabled(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("trusted_launch_enabled").HasValue("true"),
),
},
})
}

func TestAccDataSourceSharedImage_trustedLaunchSupported(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.withTrustedLaunchSupported(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("trusted_launch_supported").HasValue("true"),
),
},
})
}

func TestAccDataSourceSharedImage_confidentialVMEnabled(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.withConfidentialVM(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("confidential_vm_enabled").HasValue("true"),
),
},
})
}

func TestAccDataSourceSharedImage_confidentialVMSupported(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
{
Config: r.withConfidentialVMSupported(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).Key("tags.%").HasValue("0"),
check.That(data.ResourceName).Key("confidential_vm_supported").HasValue("true"),
),
},
})
}

func (SharedImageDataSource) basic(data acceptance.TestData, hyperVGen string) string {
return fmt.Sprintf(`
%s
Expand All @@ -80,3 +165,75 @@ data "azurerm_shared_image" "test" {
}
`, SharedImageResource{}.completeWithHyperVGen(data, hyperVGen))
}

func (SharedImageDataSource) withHibernationEnabled(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_shared_image" "test" {
name = azurerm_shared_image.test.name
gallery_name = azurerm_shared_image.test.gallery_name
resource_group_name = azurerm_shared_image.test.resource_group_name
}
`, SharedImageResource{}.withHibernationEnabled(data))
}

func (SharedImageDataSource) withAcceleratedNetworkSupportEnabled(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_shared_image" "test" {
name = azurerm_shared_image.test.name
gallery_name = azurerm_shared_image.test.gallery_name
resource_group_name = azurerm_shared_image.test.resource_group_name
}
`, SharedImageResource{}.withAcceleratedNetworkSupportEnabled(data))
}

func (SharedImageDataSource) withTrustedLaunchEnabled(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_shared_image" "test" {
name = azurerm_shared_image.test.name
gallery_name = azurerm_shared_image.test.gallery_name
resource_group_name = azurerm_shared_image.test.resource_group_name
}
`, SharedImageResource{}.withTrustedLaunchEnabled(data))
}

func (SharedImageDataSource) withTrustedLaunchSupported(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_shared_image" "test" {
name = azurerm_shared_image.test.name
gallery_name = azurerm_shared_image.test.gallery_name
resource_group_name = azurerm_shared_image.test.resource_group_name
}
`, SharedImageResource{}.withTrustedLaunchSupported(data))
}

func (SharedImageDataSource) withConfidentialVM(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_shared_image" "test" {
name = azurerm_shared_image.test.name
gallery_name = azurerm_shared_image.test.gallery_name
resource_group_name = azurerm_shared_image.test.resource_group_name
}
`, SharedImageResource{}.withConfidentialVM(data))
}

func (SharedImageDataSource) withConfidentialVMSupported(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

data "azurerm_shared_image" "test" {
name = azurerm_shared_image.test.name
gallery_name = azurerm_shared_image.test.gallery_name
resource_group_name = azurerm_shared_image.test.resource_group_name
}
`, SharedImageResource{}.withConfidentialVmSupported(data))
}
20 changes: 20 additions & 0 deletions internal/services/compute/shared_image_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"log"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -258,6 +259,12 @@ func resourceSharedImage() *pluginsdk.Resource {
ForceNew: true,
},

"hibernation_enabled": {
Type: pluginsdk.TypeBool,
Optional: true,
ForceNew: true,
},

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

Expand Down Expand Up @@ -516,6 +523,7 @@ func resourceSharedImageRead(d *pluginsdk.ResourceData, meta interface{}) error
cvmEnabled := false
cvmSupported := false
acceleratedNetworkSupportEnabled := false
hibernationEnabled := false
if features := props.Features; features != nil {
for _, feature := range *features {
if feature.Name == nil || feature.Value == nil {
Expand All @@ -532,13 +540,18 @@ func resourceSharedImageRead(d *pluginsdk.ResourceData, meta interface{}) error
if strings.EqualFold(*feature.Name, "IsAcceleratedNetworkSupported") {
acceleratedNetworkSupportEnabled = strings.EqualFold(*feature.Value, "true")
}

if strings.EqualFold(*feature.Name, "IsHibernateSupported") {
hibernationEnabled = strings.EqualFold(*feature.Value, "true")
}
}
}
d.Set("confidential_vm_supported", cvmSupported)
d.Set("confidential_vm_enabled", cvmEnabled)
d.Set("trusted_launch_supported", trustedLaunchSupported)
d.Set("trusted_launch_enabled", trustedLaunchEnabled)
d.Set("accelerated_network_support_enabled", acceleratedNetworkSupportEnabled)
d.Set("hibernation_enabled", hibernationEnabled)
}

return tags.FlattenAndSet(d, model.Tags)
Expand Down Expand Up @@ -759,5 +772,12 @@ func expandSharedImageFeatures(d *pluginsdk.ResourceData) *[]galleryimages.Galle
})
}

if hibernationEnabled := d.Get("hibernation_enabled").(bool); hibernationEnabled {
features = append(features, galleryimages.GalleryImageFeature{
Name: pointer.To("IsHibernateSupported"),
Value: pointer.To(strconv.FormatBool(hibernationEnabled)),
})
}

return &features
}
49 changes: 49 additions & 0 deletions internal/services/compute/shared_image_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ func TestAccSharedImage_withAcceleratedNetworkSupportEnabled(t *testing.T) {
})
}

func TestAccSharedImage_withHibernationEnabled(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_shared_image", "test")
r := SharedImageResource{}
data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.withHibernationEnabled(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func TestAccSharedImage_description(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_shared_image", "test")
r := SharedImageResource{}
Expand Down Expand Up @@ -721,6 +735,41 @@ resource "azurerm_shared_image" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (SharedImageResource) withHibernationEnabled(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_shared_image_gallery" "test" {
name = "acctestsig%d"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
}

resource "azurerm_shared_image" "test" {
name = "acctestimg%d"
gallery_name = azurerm_shared_image_gallery.test.name
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
os_type = "Linux"

hibernation_enabled = true

identifier {
publisher = "AccTesPublisher%d"
offer = "AccTesOffer%d"
sku = "AccTesSku%d"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (SharedImageResource) description(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
Loading
Loading