Skip to content

Commit f041149

Browse files
Added datasource for Image Shares
1 parent 44d3e91 commit f041149

File tree

11 files changed

+633
-1
lines changed

11 files changed

+633
-1
lines changed

docs/data-sources/image.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ The Linode Image resource exports the following attributes:
4141

4242
* `is_public` - True if the Image is public.
4343

44+
* `is_shared` - True if the Image is shared.
45+
4446
* `image_sharing` - Details about image sharing, including who the image is shared with and by.
4547
* `shared_with` - Details about who the image is shared with.
4648
* `sharegroup_count` - The number of sharegroups the private image is present in.

docs/data-sources/images.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ Each Linode image will be stored in the `images` attribute and will export the f
7979

8080
* `is_public` - True if the Image is public.
8181

82+
* `is_shared` - True if the Image is shared.
83+
8284
* `image_sharing` - Details about image sharing, including who the image is shared with and by.
8385
* `shared_with` - Details about who the image is shared with.
8486
* `sharegroup_count` - The number of sharegroups the private image is present in.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
page_title: "Linode: linode_producer_image_share_group_image_shares"
3+
description: |-
4+
Lists Images shared in the specified Image Share Group on your account.
5+
---
6+
7+
# Data Source: linode\producer\_image\_share\_group\_image\_shares
8+
9+
Provides information about a list of Images shared in the specified Image Share Group that match a set of filters.
10+
For more information, see the [Linode APIv4 docs](TODO).
11+
12+
## Example Usage
13+
14+
The following example shows how one might use this data source to list Images shared in an Image Share Group.
15+
16+
```hcl
17+
data "linode_producer_image_share_group_image_shares" "all" {}
18+
19+
data "linode_producer_image_share_group_image_shares" "filtered" {
20+
filter {
21+
name = "label"
22+
values = ["my-label"]
23+
}
24+
}
25+
26+
output "all-shared-images" {
27+
value = data.linode_producer_image_share_group_image_shares.all.image_shares
28+
}
29+
30+
output "filtered-shared-images" {
31+
value = data.linode_producer_image_share_group_image_shares.filtered.image_shares
32+
}
33+
```
34+
35+
## Argument Reference
36+
37+
The following arguments are supported:
38+
39+
* `sharegroup_id` - (Required) The ID of the Image Share Group to list shared Images from.
40+
41+
* [`filter`](#filter) - (Optional) A set of filters used to select Image Share Groups that meet certain requirements.
42+
43+
### Filter
44+
45+
* `name` - (Required) The name of the field to filter by. See the [Filterable Fields section](#filterable-fields) for a complete list of filterable fields.
46+
47+
* `values` - (Required) A list of values for the filter to allow. These values should all be in string form.
48+
49+
* `match_by` - (Optional) The method to match the field by. (`exact`, `regex`, `substring`; default `exact`)
50+
51+
## Attributes Reference
52+
53+
Each Image Share will be stored in the `images_shares` attribute and will export the following attributes:
54+
55+
* `id` - The unique ID assigned to this Image Share.
56+
57+
* `label` - The label of the Image Share.
58+
59+
* `capabilities` - The capabilities of the Image represented by the Image Share.
60+
61+
* `created` - When this Image Share was created.
62+
63+
* `deprecated` - Whether this Image is deprecated.
64+
65+
* `description` - A description of the Image Share.
66+
67+
* `is_public` - True if the Image is public.
68+
69+
* `image_sharing` - Details about image sharing, including who the image is shared with and by.
70+
* `shared_with` - Details about who the image is shared with.
71+
* `sharegroup_count` - The number of sharegroups the private image is present in.
72+
* `sharegroup_list_url` - The GET api url to view the sharegroups in which the image is shared.
73+
* `shared_by` - Details about who the image is shared by.
74+
* `sharegroup_id` - The sharegroup_id from the im_ImageShare row.
75+
* `sharegroup_uuid` - The sharegroup_uuid from the im_ImageShare row.
76+
* `sharegroup_label` - The label from the associated im_ImageShareGroup row.
77+
* `source_image_id` - The image id of the base image (will only be shown to producers, will be null for consumers).
78+
79+
* `size` - The minimum size this Image needs to deploy. Size is in MB. example: 2500
80+
81+
* `status` - The current status of this image. (`creating`, `pending_upload`, `available`)
82+
83+
* `type` - How the Image was created. Manual Images can be created at any time. "Automatic" Images are created automatically from a deleted Linode. (`manual`, `automatic`)
84+
85+
* `tags` - A list of customized tags.
86+
87+
* `total_size` - The total size of the image in all available regions.
88+
89+
## Filterable Fields
90+
91+
* `id`
92+
93+
* `label`

linode/framework_provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import (
7676
"github.com/linode/terraform-provider-linode/v3/linode/placementgroupassignment"
7777
"github.com/linode/terraform-provider-linode/v3/linode/placementgroups"
7878
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroup"
79+
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroupimageshares"
7980
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroupmember"
8081
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroups"
8182
"github.com/linode/terraform-provider-linode/v3/linode/profile"
@@ -339,5 +340,6 @@ func (p *FrameworkProvider) DataSources(ctx context.Context) []func() datasource
339340
objquotas.NewDataSource,
340341
producerimagesharegroup.NewDataSource,
341342
producerimagesharegroups.NewDataSource,
343+
producerimagesharegroupimageshares.NewDataSource,
342344
}
343345
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//go:build integration || producerimagesharegroupimageshares
2+
3+
package producerimagesharegroupimageshares_test
4+
5+
import (
6+
"log"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
11+
"github.com/linode/terraform-provider-linode/v3/linode/acceptance"
12+
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroupimageshares/tmpl"
13+
)
14+
15+
func TestAccDataSourceImageShareGroupImageShares_basic(t *testing.T) {
16+
t.Parallel()
17+
18+
const dsAll = "data.linode_producer_image_share_group_image_shares.all"
19+
const dsByID = "data.linode_producer_image_share_group_image_shares.by_id"
20+
const dsByLabel = "data.linode_producer_image_share_group_image_shares.by_label"
21+
22+
label := acctest.RandomWithPrefix("tf_test")
23+
instanceLabel := acctest.RandomWithPrefix("tf_test")
24+
25+
instanceRegion, err := acceptance.GetRandomRegionWithCaps([]string{}, "core")
26+
if err != nil {
27+
log.Fatal(err)
28+
}
29+
30+
imageLabel1 := acctest.RandomWithPrefix("tf-test")
31+
imageLabel2 := acctest.RandomWithPrefix("tf-test")
32+
shareGroupLabel := acctest.RandomWithPrefix("tf-test")
33+
34+
resource.Test(t, resource.TestCase{
35+
PreCheck: func() { acceptance.PreCheck(t) },
36+
ProtoV6ProviderFactories: acceptance.ProtoV6ProviderFactories,
37+
Steps: []resource.TestStep{
38+
{
39+
Config: tmpl.DataBasic(t, label, instanceLabel, instanceRegion, imageLabel1, imageLabel2, shareGroupLabel),
40+
Check: resource.ComposeTestCheckFunc(
41+
resource.TestCheckResourceAttr(dsAll, "image_shares.#", "2"),
42+
resource.TestCheckResourceAttr(dsAll, "image_shares.0.label", "image_one_label"),
43+
resource.TestCheckResourceAttr(dsAll, "image_shares.0.description", "image one description"),
44+
resource.TestCheckResourceAttr(dsAll, "image_shares.1.label", "image_two_label"),
45+
resource.TestCheckResourceAttr(dsAll, "image_shares.1.description", "image two description"),
46+
47+
resource.TestCheckResourceAttr(dsByID, "image_shares.#", "1"),
48+
resource.TestCheckResourceAttr(dsByID, "image_shares.0.label", "image_one_label"),
49+
resource.TestCheckResourceAttr(dsByID, "image_shares.0.description", "image one description"),
50+
51+
resource.TestCheckResourceAttr(dsByLabel, "image_shares.#", "1"),
52+
resource.TestCheckResourceAttr(dsByLabel, "image_shares.0.label", "image_two_label"),
53+
resource.TestCheckResourceAttr(dsByLabel, "image_shares.0.description", "image two description"),
54+
),
55+
},
56+
},
57+
})
58+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package producerimagesharegroupimageshares
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/datasource"
7+
"github.com/hashicorp/terraform-plugin-log/tflog"
8+
"github.com/linode/linodego"
9+
"github.com/linode/terraform-provider-linode/v3/linode/helper"
10+
"github.com/linode/terraform-provider-linode/v3/linode/helper/frameworkfilter"
11+
)
12+
13+
func NewDataSource() datasource.DataSource {
14+
return &DataSource{
15+
BaseDataSource: helper.NewBaseDataSource(
16+
helper.BaseDataSourceConfig{
17+
Name: "linode_producer_image_share_group_image_shares",
18+
Schema: &frameworkDataSourceSchema,
19+
},
20+
),
21+
}
22+
}
23+
24+
type DataSource struct {
25+
helper.BaseDataSource
26+
}
27+
28+
func (r *DataSource) Read(
29+
ctx context.Context,
30+
req datasource.ReadRequest,
31+
resp *datasource.ReadResponse,
32+
) {
33+
tflog.Debug(ctx, "Read data."+r.Config.Name)
34+
35+
var data ImageShareGroupImageShareFilterModel
36+
37+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
38+
if resp.Diagnostics.HasError() {
39+
return
40+
}
41+
42+
id, d := filterConfig.GenerateID(data.Filters)
43+
if d != nil {
44+
resp.Diagnostics.Append(d)
45+
return
46+
}
47+
data.ID = id
48+
49+
shareGroupID := helper.FrameworkSafeInt64ToInt(data.ShareGroupID.ValueInt64(), &resp.Diagnostics)
50+
if resp.Diagnostics.HasError() {
51+
return
52+
}
53+
54+
result, d := filterConfig.GetAndFilter(
55+
ctx,
56+
r.Meta.Client,
57+
data.Filters,
58+
listWrapper(shareGroupID),
59+
data.Order,
60+
data.OrderBy,
61+
)
62+
if d != nil {
63+
resp.Diagnostics.Append(d)
64+
return
65+
}
66+
67+
data.parseImageShares(ctx, helper.AnySliceToTyped[linodego.ImageShareEntry](result))
68+
if resp.Diagnostics.HasError() {
69+
return
70+
}
71+
72+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
73+
}
74+
75+
func listWrapper(
76+
shareGroupID int,
77+
) frameworkfilter.ListFunc {
78+
return func(
79+
ctx context.Context,
80+
client *linodego.Client,
81+
filter string,
82+
) ([]any, error) {
83+
tflog.Trace(ctx, "client.ImageShareGroupListImageShareEntries(...)")
84+
85+
nbs, err := client.ImageShareGroupListImageShareEntries(
86+
ctx,
87+
shareGroupID,
88+
&linodego.ListOptions{
89+
Filter: filter,
90+
},
91+
)
92+
if err != nil {
93+
return nil, err
94+
}
95+
96+
return helper.TypedSliceToAny(nbs), nil
97+
}
98+
}

0 commit comments

Comments
 (0)