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

compute - switch share image resource/data sources to use go-azure-sdk #25503

Merged
merged 5 commits into from Apr 3, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions internal/services/compute/client/client.go
Expand Up @@ -21,6 +21,8 @@ import (
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleries"
"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/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 Expand Up @@ -49,8 +51,8 @@ type Client struct {
GalleriesClient *galleries.GalleriesClient
GalleryApplicationsClient *galleryapplications.GalleryApplicationsClient
GalleryApplicationVersionsClient *galleryapplicationversions.GalleryApplicationVersionsClient
GalleryImagesClient *compute.GalleryImagesClient
GalleryImageVersionsClient *compute.GalleryImageVersionsClient
GalleryImagesClient *galleryimages.GalleryImagesClient
GalleryImageVersionsClient *galleryimageversions.GalleryImageVersionsClient
GallerySharingUpdateClient *gallerysharingupdate.GallerySharingUpdateClient
ImagesClient *images.ImagesClient
MarketplaceAgreementsClient *agreements.AgreementsClient
Expand Down Expand Up @@ -137,11 +139,17 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
}
o.Configure(galleryApplicationVersionsClient.Client, o.Authorizers.ResourceManager)

galleryImagesClient := compute.NewGalleryImagesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&galleryImagesClient.Client, o.ResourceManagerAuthorizer)
galleryImagesClient, err := galleryimages.NewGalleryImagesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building GalleryImages client: %+v", err)
}
o.Configure(galleryImagesClient.Client, o.Authorizers.ResourceManager)

galleryImageVersionsClient := compute.NewGalleryImageVersionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&galleryImageVersionsClient.Client, o.ResourceManagerAuthorizer)
galleryImageVersionsClient, err := galleryimageversions.NewGalleryImageVersionsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building GalleryImageVersions client: %+v", err)
}
o.Configure(galleryImageVersionsClient.Client, o.Authorizers.ResourceManager)

gallerySharingUpdateClient, err := gallerysharingupdate.NewGallerySharingUpdateClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
Expand Down Expand Up @@ -236,8 +244,8 @@ func NewClient(o *common.ClientOptions) (*Client, error) {
GalleriesClient: galleriesClient,
GalleryApplicationsClient: galleryApplicationsClient,
GalleryApplicationVersionsClient: galleryApplicationVersionsClient,
GalleryImagesClient: &galleryImagesClient,
GalleryImageVersionsClient: &galleryImageVersionsClient,
GalleryImagesClient: galleryImagesClient,
GalleryImageVersionsClient: galleryImageVersionsClient,
GallerySharingUpdateClient: gallerySharingUpdateClient,
ImagesClient: imagesClient,
MarketplaceAgreementsClient: marketplaceAgreementsClient,
Expand Down
3 changes: 2 additions & 1 deletion internal/services/compute/helpers.go
Expand Up @@ -6,6 +6,7 @@ package compute
import (
"sort"

"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/compute/2023-03-01/compute"
Expand Down Expand Up @@ -57,7 +58,7 @@ func flattenSubResourcesToStringIDs(input *[]compute.SubResource) []string {
return ids
}

func sortSharedImageVersions(values []compute.GalleryImageVersion) ([]compute.GalleryImageVersion, []error) {
func sortSharedImageVersions(values []galleryimageversions.GalleryImageVersion) ([]galleryimageversions.GalleryImageVersion, []error) {
errors := make([]error, 0)
sort.Slice(values, func(i, j int) bool {
if values[i].Name == nil || values[j].Name == nil {
Expand Down
72 changes: 36 additions & 36 deletions internal/services/compute/helpers_test.go
Expand Up @@ -7,31 +7,31 @@ import (
"reflect"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/compute/2023-03-01/compute"
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-sdk/resource-manager/compute/2022-03-03/galleryimageversions"
)

func TestSortVersions_valid(t *testing.T) {
testData := []struct {
input []compute.GalleryImageVersion
expected []compute.GalleryImageVersion
input []galleryimageversions.GalleryImageVersion
expected []galleryimageversions.GalleryImageVersion
}{
{
input: []compute.GalleryImageVersion{
{Name: utils.String("1.0.1")},
{Name: utils.String("1.2.15.0")},
{Name: utils.String("1.0.8")},
{Name: utils.String("1.0.9")},
{Name: utils.String("1.0.1.1")},
{Name: utils.String("1.0.10")},
input: []galleryimageversions.GalleryImageVersion{
{Name: pointer.To("1.0.1")},
{Name: pointer.To("1.2.15.0")},
{Name: pointer.To("1.0.8")},
{Name: pointer.To("1.0.9")},
{Name: pointer.To("1.0.1.1")},
{Name: pointer.To("1.0.10")},
},
expected: []compute.GalleryImageVersion{
{Name: utils.String("1.0.1")},
{Name: utils.String("1.0.1.1")},
{Name: utils.String("1.0.8")},
{Name: utils.String("1.0.9")},
{Name: utils.String("1.0.10")},
{Name: utils.String("1.2.15.0")},
expected: []galleryimageversions.GalleryImageVersion{
{Name: pointer.To("1.0.1")},
{Name: pointer.To("1.0.1.1")},
{Name: pointer.To("1.0.8")},
{Name: pointer.To("1.0.9")},
{Name: pointer.To("1.0.10")},
{Name: pointer.To("1.2.15.0")},
},
},
}
Expand All @@ -51,27 +51,27 @@ func TestSortVersions_valid(t *testing.T) {

func TestSortVersions_invalid(t *testing.T) {
testData := []struct {
input []compute.GalleryImageVersion
expected []compute.GalleryImageVersion
input []galleryimageversions.GalleryImageVersion
expected []galleryimageversions.GalleryImageVersion
}{
{
input: []compute.GalleryImageVersion{
{Name: utils.String("1.0.1")},
{Name: utils.String("1.2.15.0")},
{Name: utils.String("1.0.8")},
{Name: utils.String("1.0.9")},
{Name: utils.String("1.0.1.1")},
{Name: utils.String("1.0.10")},
{Name: utils.String("latest")},
input: []galleryimageversions.GalleryImageVersion{
{Name: pointer.To("1.0.1")},
{Name: pointer.To("1.2.15.0")},
{Name: pointer.To("1.0.8")},
{Name: pointer.To("1.0.9")},
{Name: pointer.To("1.0.1.1")},
{Name: pointer.To("1.0.10")},
{Name: pointer.To("latest")},
},
expected: []compute.GalleryImageVersion{
{Name: utils.String("1.0.1")},
{Name: utils.String("1.2.15.0")},
{Name: utils.String("1.0.8")},
{Name: utils.String("1.0.9")},
{Name: utils.String("1.0.1.1")},
{Name: utils.String("1.0.10")},
{Name: utils.String("latest")},
expected: []galleryimageversions.GalleryImageVersion{
{Name: pointer.To("1.0.1")},
{Name: pointer.To("1.2.15.0")},
{Name: pointer.To("1.0.8")},
{Name: pointer.To("1.0.9")},
{Name: pointer.To("1.0.1.1")},
{Name: pointer.To("1.0.10")},
{Name: pointer.To("latest")},
},
},
}
Expand Down
88 changes: 42 additions & 46 deletions internal/services/compute/shared_image_data_source.go
Expand Up @@ -7,16 +7,16 @@ import (
"fmt"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"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/galleryimages"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/compute/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
"github.com/tombuildsstuff/kermit/sdk/compute/2023-03-01/compute"
)

func dataSourceSharedImage() *pluginsdk.Resource {
Expand Down Expand Up @@ -126,7 +126,7 @@ func dataSourceSharedImage() *pluginsdk.Resource {
Computed: true,
},

"tags": tags.SchemaDataSource(),
"tags": commonschema.TagsDataSource(),
},
}
}
Expand All @@ -137,70 +137,66 @@ func dataSourceSharedImageRead(d *pluginsdk.ResourceData, meta interface{}) erro
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id := parse.NewSharedImageID(subscriptionId, d.Get("resource_group_name").(string), d.Get("gallery_name").(string), d.Get("name").(string))
id := galleryimages.NewGalleryImageID(subscriptionId, d.Get("resource_group_name").(string), d.Get("gallery_name").(string), d.Get("name").(string))

resp, err := client.Get(ctx, id.ResourceGroup, id.GalleryName, id.ImageName)
resp, err := client.Get(ctx, id)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}

return fmt.Errorf("making Read request on %s: %+v", id, err)
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.SetId(id.ID())

d.Set("name", id.ImageName)
d.Set("gallery_name", id.GalleryName)
d.Set("resource_group_name", id.ResourceGroup)

d.Set("location", location.NormalizeNilable(resp.Location))

if props := resp.GalleryImageProperties; props != nil {
d.Set("description", props.Description)
d.Set("eula", props.Eula)
d.Set("os_type", string(props.OsType))
d.Set("architecture", string(props.Architecture))
d.Set("specialized", props.OsState == compute.OperatingSystemStateTypesSpecialized)
d.Set("hyper_v_generation", string(props.HyperVGeneration))
d.Set("privacy_statement_uri", props.PrivacyStatementURI)
d.Set("release_note_uri", props.ReleaseNoteURI)

if err := d.Set("identifier", flattenGalleryImageDataSourceIdentifier(props.Identifier)); err != nil {
return fmt.Errorf("setting `identifier`: %+v", err)
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
d.Set("location", location.Normalize(model.Location))

if props := model.Properties; props != nil {
d.Set("description", props.Description)
d.Set("eula", props.Eula)
d.Set("os_type", string(props.OsType))
d.Set("architecture", pointer.From(props.Architecture))
d.Set("specialized", props.OsState == galleryimages.OperatingSystemStateTypesSpecialized)
d.Set("hyper_v_generation", pointer.From(props.HyperVGeneration))
d.Set("privacy_statement_uri", props.PrivacyStatementUri)
d.Set("release_note_uri", props.ReleaseNoteUri)

if err := d.Set("identifier", flattenGalleryImageDataSourceIdentifier(&props.Identifier)); err != nil {
return fmt.Errorf("setting `identifier`: %+v", err)
}

if err := d.Set("purchase_plan", flattenGalleryImageDataSourcePurchasePlan(props.PurchasePlan)); err != nil {
return fmt.Errorf("setting `purchase_plan`: %+v", err)
}
}

if err := d.Set("purchase_plan", flattenGalleryImageDataSourcePurchasePlan(props.PurchasePlan)); err != nil {
return fmt.Errorf("setting `purchase_plan`: %+v", err)
}
}
return tags.FlattenAndSet(d, model.Tags)

return tags.FlattenAndSet(d, resp.Tags)
}
return nil
}

func flattenGalleryImageDataSourceIdentifier(input *compute.GalleryImageIdentifier) []interface{} {
func flattenGalleryImageDataSourceIdentifier(input *galleryimages.GalleryImageIdentifier) []interface{} {
if input == nil {
return []interface{}{}
}

result := make(map[string]interface{})

if input.Offer != nil {
result["offer"] = *input.Offer
}

if input.Publisher != nil {
result["publisher"] = *input.Publisher
}

if input.Sku != nil {
result["sku"] = *input.Sku
return []interface{}{
map[string]interface{}{
"offer": input.Offer,
"publisher": input.Publisher,
"sku": input.Sku,
},
}

return []interface{}{result}
}

func flattenGalleryImageDataSourcePurchasePlan(input *compute.ImagePurchasePlan) []interface{} {
func flattenGalleryImageDataSourcePurchasePlan(input *galleryimages.ImagePurchasePlan) []interface{} {
if input == nil {
return []interface{}{}
}
Expand Down
6 changes: 3 additions & 3 deletions internal/services/compute/shared_image_data_source_test.go
Expand Up @@ -13,7 +13,7 @@ import (

type SharedImageDataSource struct{}

func TestAccDataSourceAzureRMSharedImage_basic(t *testing.T) {
func TestAccDataSourceSharedImage_basic(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
Expand All @@ -26,7 +26,7 @@ func TestAccDataSourceAzureRMSharedImage_basic(t *testing.T) {
})
}

func TestAccDataSourceAzureRMSharedImage_basic_hyperVGeneration_V2(t *testing.T) {
func TestAccDataSourceSharedImage_basicHyperVGenerationV2(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
Expand All @@ -40,7 +40,7 @@ func TestAccDataSourceAzureRMSharedImage_basic_hyperVGeneration_V2(t *testing.T)
})
}

func TestAccDataSourceAzureRMSharedImage_complete(t *testing.T) {
func TestAccDataSourceSharedImage_complete(t *testing.T) {
data := acceptance.BuildTestData(t, "data.azurerm_shared_image", "test")
r := SharedImageDataSource{}
data.DataSourceTest(t, []acceptance.TestStep{
Expand Down
10 changes: 3 additions & 7 deletions internal/services/compute/shared_image_gallery_data_source.go
Expand Up @@ -97,19 +97,15 @@ func dataSourceSharedImageGalleryRead(d *pluginsdk.ResourceData, meta interface{
}
}

imagesResp, err := imagesClient.ListByGalleryComplete(ctx, id.ResourceGroupName, id.GalleryName)
imagesResp, err := imagesClient.ListByGalleryComplete(ctx, id)
if err != nil {
return fmt.Errorf("retrieving %s: %+v", id, err)
}

imageNames := make([]string, 0)
for imagesResp.NotDone() {
image := imagesResp.Value()
for _, image := range imagesResp.Items {
if image.Name != nil {
imageNames = append(imageNames, *imagesResp.Value().Name)
}
if err := imagesResp.NextWithContext(ctx); err != nil {
return fmt.Errorf("listing next page of shared images for %s: %+v", id, err)
imageNames = append(imageNames, *image.Name)
}
}

Expand Down