Skip to content

Commit

Permalink
Add image names to gallery data source
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwilcox9 committed Dec 11, 2023
1 parent 428f6a8 commit b8bf56b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
25 changes: 25 additions & 0 deletions internal/services/compute/shared_image_gallery_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down Expand Up @@ -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
}
26 changes: 26 additions & 0 deletions internal/services/compute/shared_image_gallery_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
2 changes: 2 additions & 0 deletions website/docs/d/shared_image_gallery.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit b8bf56b

Please sign in to comment.