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

Add image_names to azurerm_shared_image_gallery data source #24176

Merged
merged 1 commit into from
Dec 11, 2023
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
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
}
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
Loading