Skip to content

Commit 920e625

Browse files
Added datasource/resource for Image Share Group
1 parent 4436bca commit 920e625

17 files changed

+1228
-6
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
page_title: "Linode: linode_producer_image_share_group"
3+
description: |-
4+
Provides details about an Image Share Group created by a producer.
5+
---
6+
7+
# Data Source: linode\producer\_image\_share\_group
8+
9+
`linode_producer_image_share_group` provides details about an Image Share Group.
10+
For more information, see the [Linode APIv4 docs](TODO).
11+
12+
13+
## Example Usage
14+
15+
The following example shows how the datasource might be used to obtain additional information about an Image Share Group.
16+
17+
```hcl
18+
data "linode_producer_image_share_group" "sg" {
19+
id = 12345
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
* `id` - (Required) The ID of the Image Share Group.
26+
27+
## Attributes Reference
28+
29+
In addition to all arguments above, the following attributes are exported:
30+
31+
* `uuid` - The UUID of the Image Share Group.
32+
33+
* `label` - The label of the Image Share Group.
34+
35+
* `description` - The description of the Image Share Group.
36+
37+
* `is_suspended` - Whether the Image Share Group is suspended.
38+
39+
* `images_count` - The number of images in the Image Share Group.
40+
41+
* `members_count` - The number of members in the Image Share Group.
42+
43+
* `created` - The date and time the Image Share Group was created.
44+
45+
* `updated` - The date and time the Image Share Group was last updated.
46+
47+
* `expiry` - The date and time the Image Share Group will expire.
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
page_title: "Linode: linode_producer_image_share_group"
3+
description: |-
4+
Manages an Image Share Group.
5+
---
6+
7+
# linode\producer\_image\_share\_group
8+
9+
Manages an Image Share Group.
10+
For more information, see the [Linode APIv4 docs](TODO).
11+
12+
## Example Usage
13+
14+
Create an Image Share Group without any Images:
15+
16+
```terraform
17+
resource "linode_producer_image_share_group" "test-empty" {
18+
label = "my-image-share-group"
19+
description = "My description."
20+
}
21+
```
22+
23+
Create an Image Share Group with one Image:
24+
25+
```terraform
26+
resource "linode_producer_image_share_group" "test-images" {
27+
label = "my-image-share-group"
28+
description = "My description."
29+
images = [
30+
{
31+
id = "private/12345"
32+
label = "my-image"
33+
description = "My image description."
34+
},
35+
]
36+
}
37+
```
38+
39+
## Argument Reference
40+
41+
The following arguments are supported:
42+
43+
* `label` - (Required) The label of the Image Share Group.
44+
45+
* `description` - (Optional) The description of the Image Share Group
46+
47+
* [`images`](#images) - (Optional) A list of Images to include in the Image Share Group.
48+
49+
## Attributes Reference
50+
51+
In addition to all the arguments above, the following attributes are exported.
52+
53+
* `id` - The ID of the Image Share Group.
54+
55+
* `uuid` - The UUID of the Image Share Group.
56+
57+
* `label` - The label of the Image Share Group.
58+
59+
* `description` - The description of the Image Share Group.
60+
61+
* `is_suspended` - Whether the Image Share Group is suspended.
62+
63+
* `images_count` - The number of images in the Image Share Group.
64+
65+
* `members_count` - The number of members in the Image Share Group.
66+
67+
* `created` - The date and time the Image Share Group was created.
68+
69+
* `updated` - The date and time the Image Share Group was last updated.
70+
71+
* `expiry` - The date and time the Image Share Group will expire.
72+
73+
### Images
74+
75+
Represents a single Image shared in an Image Share Group.
76+
77+
* `id` - (Required) The ID of the Image to share. This must be in the format `private/<image_id>`.
78+
79+
* `label` - (Optional) The label of the Image Share.
80+
81+
* `description` - (Optional) The description of the Image Share.

linode/framework_provider.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ import (
7575
"github.com/linode/terraform-provider-linode/v3/linode/placementgroup"
7676
"github.com/linode/terraform-provider-linode/v3/linode/placementgroupassignment"
7777
"github.com/linode/terraform-provider-linode/v3/linode/placementgroups"
78+
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroup"
7879
"github.com/linode/terraform-provider-linode/v3/linode/profile"
7980
"github.com/linode/terraform-provider-linode/v3/linode/rdns"
8081
"github.com/linode/terraform-provider-linode/v3/linode/region"
@@ -252,6 +253,7 @@ func (p *FrameworkProvider) Resources(ctx context.Context) []func() resource.Res
252253
networkingipassignment.NewResource,
253254
obj.NewResource,
254255
databasemysqlv2.NewResource,
256+
producerimagesharegroup.NewResource,
255257
}
256258
}
257259

@@ -332,5 +334,6 @@ func (p *FrameworkProvider) DataSources(ctx context.Context) []func() datasource
332334
objendpoints.NewDataSource,
333335
objquota.NewDataSource,
334336
objquotas.NewDataSource,
337+
producerimagesharegroup.NewDataSource,
335338
}
336339
}

linode/image/resource_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func init() {
7070
return !ok || !isDisallowed
7171
})
7272

73-
testRegion = testRegions[0]
73+
testRegion = testRegions[1]
7474
}
7575

7676
func sweep(prefix string) error {

linode/images/datasource_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestAccDataSourceImages_basic_smoke(t *testing.T) {
4444
resource.TestCheckResourceAttr(resourceName, "images.0.is_shared", "false"),
4545
resource.TestCheckResourceAttr(resourceName, "images.0.image_sharing.shared_with.sharegroup_count", "0"),
4646
resource.TestCheckResourceAttrSet(resourceName, "images.0.image_sharing.shared_with.sharegroup_list_url"),
47-
resource.TestCheckNoResourceAttr(resourceName, "image_sharing.shared_by"),
47+
resource.TestCheckNoResourceAttr(resourceName, "images.0.image_sharing.shared_by"),
4848
resource.TestCheckResourceAttr(resourceName, "images.0.type", "manual"),
4949
acceptance.CheckListContains(resourceName, "images.0.tags", "test"),
5050
resource.TestCheckResourceAttrSet(resourceName, "images.0.created"),
@@ -57,10 +57,10 @@ func TestAccDataSourceImages_basic_smoke(t *testing.T) {
5757
resource.TestCheckResourceAttr(resourceName, "images.1.label", imageName),
5858
resource.TestCheckResourceAttr(resourceName, "images.1.description", "descriptive text"),
5959
resource.TestCheckResourceAttr(resourceName, "images.1.is_public", "false"),
60-
resource.TestCheckResourceAttr(resourceName, "images.0.is_shared", "false"),
61-
resource.TestCheckResourceAttr(resourceName, "images.0.image_sharing.shared_with.sharegroup_count", "0"),
62-
resource.TestCheckResourceAttrSet(resourceName, "images.0.image_sharing.shared_with.sharegroup_list_url"),
63-
resource.TestCheckNoResourceAttr(resourceName, "image_sharing.shared_by"),
60+
resource.TestCheckResourceAttr(resourceName, "images.1.is_shared", "false"),
61+
resource.TestCheckResourceAttr(resourceName, "images.1.image_sharing.shared_with.sharegroup_count", "0"),
62+
resource.TestCheckResourceAttrSet(resourceName, "images.1.image_sharing.shared_with.sharegroup_list_url"),
63+
resource.TestCheckNoResourceAttr(resourceName, "images.1.image_sharing.shared_by"),
6464
resource.TestCheckResourceAttr(resourceName, "images.1.type", "manual"),
6565
acceptance.CheckListContains(resourceName, "images.1.tags", "test"),
6666
resource.TestCheckResourceAttrSet(resourceName, "images.1.created"),
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//go:build integration || producerimagesharegroup
2+
3+
package producerimagesharegroup_test
4+
5+
import (
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
10+
"github.com/linode/terraform-provider-linode/v3/linode/acceptance"
11+
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroup/tmpl"
12+
)
13+
14+
func TestAccDataSourceImageShareGroup_basic(t *testing.T) {
15+
t.Parallel()
16+
17+
resourceName := "data.linode_producer_image_share_group.foobar"
18+
19+
label := acctest.RandomWithPrefix("tf-test")
20+
description := "A cool description."
21+
22+
resource.Test(t, resource.TestCase{
23+
PreCheck: func() { acceptance.PreCheck(t) },
24+
ProtoV6ProviderFactories: acceptance.ProtoV6ProviderFactories,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: tmpl.DataBasic(t, label, description),
28+
Check: resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttrSet(resourceName, "id"),
30+
resource.TestCheckResourceAttrSet(resourceName, "uuid"),
31+
resource.TestCheckResourceAttr(resourceName, "label", label),
32+
resource.TestCheckResourceAttr(resourceName, "description", description),
33+
resource.TestCheckResourceAttr(resourceName, "is_suspended", "false"),
34+
resource.TestCheckResourceAttr(resourceName, "images_count", "0"),
35+
resource.TestCheckResourceAttr(resourceName, "members_count", "0"),
36+
resource.TestCheckResourceAttrSet(resourceName, "created"),
37+
resource.TestCheckNoResourceAttr(resourceName, "updated"),
38+
resource.TestCheckNoResourceAttr(resourceName, "expiry"),
39+
),
40+
},
41+
},
42+
})
43+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package producerimagesharegroup
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-log/tflog"
9+
"github.com/linode/terraform-provider-linode/v3/linode/helper"
10+
)
11+
12+
func NewDataSource() datasource.DataSource {
13+
return &DataSource{
14+
BaseDataSource: helper.NewBaseDataSource(
15+
helper.BaseDataSourceConfig{
16+
Name: "linode_producer_image_share_group",
17+
Schema: &frameworkDataSourceSchema,
18+
},
19+
),
20+
}
21+
}
22+
23+
type DataSource struct {
24+
helper.BaseDataSource
25+
}
26+
27+
func (d *DataSource) Read(
28+
ctx context.Context,
29+
req datasource.ReadRequest,
30+
resp *datasource.ReadResponse,
31+
) {
32+
tflog.Debug(ctx, "Read data."+d.Config.Name)
33+
34+
client := d.Meta.Client
35+
36+
var data DataSourceModel
37+
38+
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
39+
if resp.Diagnostics.HasError() {
40+
return
41+
}
42+
43+
id := helper.FrameworkSafeInt64ToInt(
44+
data.ID.ValueInt64(),
45+
&resp.Diagnostics,
46+
)
47+
if resp.Diagnostics.HasError() {
48+
return
49+
}
50+
51+
tflog.Trace(ctx, "client.GetImageShareGroup(...)")
52+
sg, err := client.GetImageShareGroup(ctx, id)
53+
if err != nil {
54+
resp.Diagnostics.AddError(
55+
fmt.Sprintf("Unable to get Image Share Group %v", id), err.Error(),
56+
)
57+
return
58+
}
59+
60+
data.ParseImageShareGroup(sg)
61+
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
62+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package producerimagesharegroup
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
5+
"github.com/hashicorp/terraform-plugin-framework/diag"
6+
"github.com/hashicorp/terraform-plugin-framework/types"
7+
"github.com/linode/linodego"
8+
"github.com/linode/terraform-provider-linode/v3/linode/helper"
9+
)
10+
11+
type ResourceModel struct {
12+
ID types.Int64 `tfsdk:"id"`
13+
UUID types.String `tfsdk:"uuid"`
14+
Label types.String `tfsdk:"label"`
15+
Description types.String `tfsdk:"description"`
16+
IsSuspended types.Bool `tfsdk:"is_suspended"`
17+
ImagesCount types.Int64 `tfsdk:"images_count"`
18+
MembersCount types.Int64 `tfsdk:"members_count"`
19+
Created timetypes.RFC3339 `tfsdk:"created"`
20+
Updated timetypes.RFC3339 `tfsdk:"updated"`
21+
Expiry timetypes.RFC3339 `tfsdk:"expiry"`
22+
Images types.List `tfsdk:"images"`
23+
}
24+
25+
type ImageShareAttributesModel struct {
26+
ID types.String `tfsdk:"id"`
27+
Label types.String `tfsdk:"label"`
28+
Description types.String `tfsdk:"description"`
29+
}
30+
31+
func (data *ResourceModel) FlattenImageShareGroup(
32+
imageShareGroup *linodego.ProducerImageShareGroup,
33+
preserveKnown bool,
34+
) {
35+
data.ID = helper.KeepOrUpdateInt64(data.ID, int64(imageShareGroup.ID), preserveKnown)
36+
data.UUID = helper.KeepOrUpdateString(data.UUID, imageShareGroup.UUID, preserveKnown)
37+
data.Label = helper.KeepOrUpdateString(data.Label, imageShareGroup.Label, preserveKnown)
38+
data.Description = helper.KeepOrUpdateString(
39+
data.Description, imageShareGroup.Description, preserveKnown,
40+
)
41+
data.IsSuspended = helper.KeepOrUpdateBool(data.IsSuspended, imageShareGroup.IsSuspended, preserveKnown)
42+
data.ImagesCount = helper.KeepOrUpdateInt64(data.ImagesCount, int64(imageShareGroup.ImagesCount), preserveKnown)
43+
data.MembersCount = helper.KeepOrUpdateInt64(data.MembersCount, int64(imageShareGroup.MembersCount), preserveKnown)
44+
data.Created = helper.KeepOrUpdateValue(
45+
data.Created, timetypes.NewRFC3339TimePointerValue(imageShareGroup.Created), preserveKnown,
46+
)
47+
data.Updated = helper.KeepOrUpdateValue(
48+
data.Updated, timetypes.NewRFC3339TimePointerValue(imageShareGroup.Updated), preserveKnown,
49+
)
50+
data.Expiry = helper.KeepOrUpdateValue(
51+
data.Expiry, timetypes.NewRFC3339TimePointerValue(imageShareGroup.Expiry), preserveKnown,
52+
)
53+
54+
// Images must persist in state across CRUD operations but is not returned by the API. It will be maintained
55+
// manually as a part of Create, Update, and Read, so we only need to set it here if it is null so that it is
56+
// properly typed.
57+
if data.Images.IsNull() {
58+
data.Images = types.ListNull(imageShareGroupImage.Type())
59+
}
60+
}
61+
62+
type DataSourceModel struct {
63+
ID types.Int64 `tfsdk:"id"`
64+
UUID types.String `tfsdk:"uuid"`
65+
Label types.String `tfsdk:"label"`
66+
Description types.String `tfsdk:"description"`
67+
IsSuspended types.Bool `tfsdk:"is_suspended"`
68+
ImagesCount types.Int64 `tfsdk:"images_count"`
69+
MembersCount types.Int64 `tfsdk:"members_count"`
70+
Created timetypes.RFC3339 `tfsdk:"created"`
71+
Updated timetypes.RFC3339 `tfsdk:"updated"`
72+
Expiry timetypes.RFC3339 `tfsdk:"expiry"`
73+
}
74+
75+
func (data *DataSourceModel) ParseImageShareGroup(isg *linodego.ProducerImageShareGroup,
76+
) diag.Diagnostics {
77+
data.ID = types.Int64Value(int64(isg.ID))
78+
data.UUID = types.StringValue(isg.UUID)
79+
data.Label = types.StringValue(isg.Label)
80+
data.Description = types.StringValue(isg.Description)
81+
data.IsSuspended = types.BoolValue(isg.IsSuspended)
82+
data.ImagesCount = types.Int64Value(int64(isg.ImagesCount))
83+
data.MembersCount = types.Int64Value(int64(isg.MembersCount))
84+
data.Created = timetypes.NewRFC3339TimePointerValue(isg.Created)
85+
data.Updated = timetypes.NewRFC3339TimePointerValue(isg.Updated)
86+
data.Expiry = timetypes.NewRFC3339TimePointerValue(isg.Expiry)
87+
88+
return nil
89+
}

0 commit comments

Comments
 (0)