Skip to content

Commit

Permalink
shared_image - add hibernation_enabled updated data source (#26975)
Browse files Browse the repository at this point in the history
* Add hibernation_enabled to share_image_resource

Signed-off-by: Aidan Jensen <[email protected]>

* Add tests and datasource

Signed-off-by: Aidan Jensen <[email protected]>

* remove optional specifiers

Co-authored-by: stephybun <[email protected]>

* inline shared_image_data_source feature flattening

Signed-off-by: Aidan Jensen <[email protected]>

---------

Signed-off-by: Aidan Jensen <[email protected]>
Co-authored-by: stephybun <[email protected]>
  • Loading branch information
artificial-aidan and stephybun authored Aug 20, 2024
1 parent e7dddab commit 6015f48
Show file tree
Hide file tree
Showing 6 changed files with 307 additions and 0 deletions.
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

0 comments on commit 6015f48

Please sign in to comment.