From b8bf56bd0b7ebb07a7cfe9069138dee684505222 Mon Sep 17 00:00:00 2001
From: Alex Wilcox <alex.wilcox@cloudsecure.ltd>
Date: Mon, 11 Dec 2023 11:09:39 +0000
Subject: [PATCH] Add image names to gallery data source

---
 .../shared_image_gallery_data_source.go       | 25 ++++++++++++++++++
 .../shared_image_gallery_data_source_test.go  | 26 +++++++++++++++++++
 .../docs/d/shared_image_gallery.html.markdown |  2 ++
 3 files changed, 53 insertions(+)

diff --git a/internal/services/compute/shared_image_gallery_data_source.go b/internal/services/compute/shared_image_gallery_data_source.go
index 005d16a450e9..4eddeb1dbab1 100644
--- a/internal/services/compute/shared_image_gallery_data_source.go
+++ b/internal/services/compute/shared_image_gallery_data_source.go
@@ -43,6 +43,12 @@ func dataSourceSharedImageGallery() *pluginsdk.Resource {
 				Computed: true,
 			},
 
+			"image_names": {
+				Type:     pluginsdk.TypeList,
+				Computed: true,
+				Elem:     &pluginsdk.Schema{Type: pluginsdk.TypeString},
+			},
+
 			"unique_name": {
 				Type:     pluginsdk.TypeString,
 				Computed: true,
@@ -55,6 +61,7 @@ func dataSourceSharedImageGallery() *pluginsdk.Resource {
 
 func dataSourceSharedImageGalleryRead(d *pluginsdk.ResourceData, meta interface{}) error {
 	client := meta.(*clients.Client).Compute.GalleriesClient
+	imagesClient := meta.(*clients.Client).Compute.GalleryImagesClient
 	subscriptionId := meta.(*clients.Client).Account.SubscriptionId
 	ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
 	defer cancel()
@@ -90,5 +97,23 @@ func dataSourceSharedImageGalleryRead(d *pluginsdk.ResourceData, meta interface{
 		}
 	}
 
+	imagesResp, err := imagesClient.ListByGalleryComplete(ctx, id.ResourceGroupName, id.GalleryName)
+	if err != nil {
+		return fmt.Errorf("retrieving %s: %+v", id, err)
+	}
+
+	imageNames := make([]string, 0)
+	for imagesResp.NotDone() {
+		image := imagesResp.Value()
+		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)
+		}
+	}
+
+	d.Set("image_names", imageNames)
+
 	return nil
 }
diff --git a/internal/services/compute/shared_image_gallery_data_source_test.go b/internal/services/compute/shared_image_gallery_data_source_test.go
index 39650066382f..091ca2ee1d55 100644
--- a/internal/services/compute/shared_image_gallery_data_source_test.go
+++ b/internal/services/compute/shared_image_gallery_data_source_test.go
@@ -44,6 +44,21 @@ func TestAccDataSourceSharedImageGallery_complete(t *testing.T) {
 	})
 }
 
+func TestAccDataSourceSharedImageGallery_imageNames(t *testing.T) {
+	data := acceptance.BuildTestData(t, "data.azurerm_shared_image_gallery", "test")
+	r := SharedImageGalleryDataSource{}
+
+	data.DataSourceTest(t, []acceptance.TestStep{
+		{
+			Config: r.imageNames(data),
+			Check: acceptance.ComposeTestCheckFunc(
+				check.That(data.ResourceName).Key("tags.%").HasValue("0"),
+				check.That(data.ResourceName).Key("image_names.#").HasValue("1"),
+			),
+		},
+	})
+}
+
 func (SharedImageGalleryDataSource) basic(data acceptance.TestData) string {
 	return fmt.Sprintf(`
 %s
@@ -65,3 +80,14 @@ data "azurerm_shared_image_gallery" "test" {
 }
 `, SharedImageGalleryResource{}.complete(data))
 }
+
+func (SharedImageGalleryDataSource) imageNames(data acceptance.TestData) string {
+	return fmt.Sprintf(`
+%s
+
+data "azurerm_shared_image_gallery" "test" {
+  name                = azurerm_shared_image.test.gallery_name
+  resource_group_name = azurerm_shared_image.test.resource_group_name
+}
+`, SharedImageResource{}.basic(data))
+}
diff --git a/website/docs/d/shared_image_gallery.html.markdown b/website/docs/d/shared_image_gallery.html.markdown
index e2a399fe32e8..54aaf764d118 100644
--- a/website/docs/d/shared_image_gallery.html.markdown
+++ b/website/docs/d/shared_image_gallery.html.markdown
@@ -36,6 +36,8 @@ The following attributes are exported:
 
 * `description` - A description for the Shared Image Gallery.
 
+* `image_names` - A list of Shared Image names within this Shared Image Gallery.
+
 * `unique_name` - The unique name assigned to the Shared Image Gallery.
 
 * `tags` - A mapping of tags which are assigned to the Shared Image Gallery.