Skip to content

Commit 44d3e91

Browse files
Added Image Share Groups datasource and Image Share Group Member resource
1 parent e44d976 commit 44d3e91

File tree

12 files changed

+657
-1
lines changed

12 files changed

+657
-1
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
---
2+
page_title: "Linode: linode_producer_image_share_groups"
3+
description: |-
4+
Lists Image Share Groups on your account.
5+
---
6+
7+
# Data Source: linode\producer\_image\_share\_groups
8+
9+
Provides information about a list of Image Share Groups 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 Image Share Groups.
15+
16+
```hcl
17+
data "linode_producer_image_share_groups" "all" {}
18+
19+
data "linode_producer_image_share_groups" "filtered" {
20+
filter {
21+
name = "label"
22+
values = ["my-label"]
23+
}
24+
}
25+
26+
output "all-share-groups" {
27+
value = data.linode_producer_image_share_groups.all.image_share_groups
28+
}
29+
30+
output "filtered-share-groups" {
31+
value = data.linode_producer_image_share_groups.filtered.image_share_groups
32+
}
33+
```
34+
35+
## Argument Reference
36+
37+
The following arguments are supported:
38+
39+
* [`filter`](#filter) - (Optional) A set of filters used to select Image Share Groups that meet certain requirements.
40+
41+
### Filter
42+
43+
* `name` - (Required) The name of the field to filter by. See the [Filterable Fields section](#filterable-fields) for a complete list of filterable fields.
44+
45+
* `values` - (Required) A list of values for the filter to allow. These values should all be in string form.
46+
47+
* `match_by` - (Optional) The method to match the field by. (`exact`, `regex`, `substring`; default `exact`)
48+
49+
## Attributes Reference
50+
51+
In addition to all 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+
## Filterable Fields
74+
75+
* `id`
76+
77+
* `label`
78+
79+
* `is_suspended`

linode/framework_provider.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ 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/producerimagesharegroupmember"
80+
"github.com/linode/terraform-provider-linode/v3/linode/producerimagesharegroups"
7981
"github.com/linode/terraform-provider-linode/v3/linode/profile"
8082
"github.com/linode/terraform-provider-linode/v3/linode/rdns"
8183
"github.com/linode/terraform-provider-linode/v3/linode/region"
@@ -254,6 +256,7 @@ func (p *FrameworkProvider) Resources(ctx context.Context) []func() resource.Res
254256
obj.NewResource,
255257
databasemysqlv2.NewResource,
256258
producerimagesharegroup.NewResource,
259+
producerimagesharegroupmember.NewResource,
257260
}
258261
}
259262

@@ -335,5 +338,6 @@ func (p *FrameworkProvider) DataSources(ctx context.Context) []func() datasource
335338
objquota.NewDataSource,
336339
objquotas.NewDataSource,
337340
producerimagesharegroup.NewDataSource,
341+
producerimagesharegroups.NewDataSource,
338342
}
339343
}

linode/producerimagesharegroup/framework_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package producerimagesharegroup
33
import (
44
"context"
55
"fmt"
6-
"github.com/hashicorp/terraform-plugin-framework/attr"
76

7+
"github.com/hashicorp/terraform-plugin-framework/attr"
88
"github.com/hashicorp/terraform-plugin-framework/resource"
99
"github.com/hashicorp/terraform-plugin-framework/types"
1010
"github.com/hashicorp/terraform-plugin-log/tflog"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package producerimagesharegroupmember
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework-timetypes/timetypes"
5+
"github.com/hashicorp/terraform-plugin-framework/types"
6+
"github.com/linode/linodego"
7+
"github.com/linode/terraform-provider-linode/v3/linode/helper"
8+
)
9+
10+
type ResourceModel struct {
11+
ShareGroupID types.Int64 `tfsdk:"sharegroup_id"`
12+
Token types.String `tfsdk:"token"`
13+
Label types.String `tfsdk:"label"`
14+
TokenUUID types.String `tfsdk:"token_uuid"`
15+
Status types.String `tfsdk:"status"`
16+
Created timetypes.RFC3339 `tfsdk:"created"`
17+
Updated timetypes.RFC3339 `tfsdk:"updated"`
18+
Expiry timetypes.RFC3339 `tfsdk:"expiry"`
19+
}
20+
21+
func (data *ResourceModel) FlattenImageShareGroupMember(
22+
member *linodego.ImageShareGroupMember,
23+
preserveKnown bool,
24+
) {
25+
// We do not touch ShareGroupID Token as they are not returned by the API and must be preserved as-is.
26+
27+
data.Label = helper.KeepOrUpdateString(data.Label, member.Label, preserveKnown)
28+
data.TokenUUID = helper.KeepOrUpdateString(data.TokenUUID, member.TokenUUID, preserveKnown)
29+
data.Status = helper.KeepOrUpdateString(data.Status, member.Status, preserveKnown)
30+
data.Created = helper.KeepOrUpdateValue(
31+
data.Created, timetypes.NewRFC3339TimePointerValue(member.Created), preserveKnown,
32+
)
33+
data.Updated = helper.KeepOrUpdateValue(
34+
data.Updated, timetypes.NewRFC3339TimePointerValue(member.Updated), preserveKnown,
35+
)
36+
data.Expiry = helper.KeepOrUpdateValue(
37+
data.Expiry, timetypes.NewRFC3339TimePointerValue(member.Expiry), preserveKnown,
38+
)
39+
}
40+
41+
func (m *ResourceModel) CopyFrom(other ResourceModel, preserveKnown bool) {
42+
m.ShareGroupID = helper.KeepOrUpdateValue(m.ShareGroupID, other.ShareGroupID, preserveKnown)
43+
m.Token = helper.KeepOrUpdateValue(m.Token, other.Token, preserveKnown)
44+
m.Label = helper.KeepOrUpdateValue(m.Label, other.Label, preserveKnown)
45+
m.TokenUUID = helper.KeepOrUpdateValue(m.TokenUUID, other.TokenUUID, preserveKnown)
46+
m.Status = helper.KeepOrUpdateValue(m.Status, other.Status, preserveKnown)
47+
m.Created = helper.KeepOrUpdateValue(m.Created, other.Created, preserveKnown)
48+
m.Updated = helper.KeepOrUpdateValue(m.Updated, other.Updated, preserveKnown)
49+
m.Expiry = helper.KeepOrUpdateValue(m.Expiry, other.Expiry, preserveKnown)
50+
}
Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package producerimagesharegroupmember
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/resource"
8+
"github.com/hashicorp/terraform-plugin-framework/types"
9+
"github.com/hashicorp/terraform-plugin-log/tflog"
10+
"github.com/linode/linodego"
11+
"github.com/linode/terraform-provider-linode/v3/linode/helper"
12+
)
13+
14+
func NewResource() resource.Resource {
15+
return &Resource{
16+
BaseResource: helper.NewBaseResource(
17+
helper.BaseResourceConfig{
18+
Name: "linode_producer_image_share_group_member",
19+
IDType: types.Int64Type,
20+
Schema: &frameworkResourceSchema,
21+
},
22+
),
23+
}
24+
}
25+
26+
type Resource struct {
27+
helper.BaseResource
28+
}
29+
30+
func (r *Resource) Create(
31+
ctx context.Context,
32+
req resource.CreateRequest,
33+
resp *resource.CreateResponse,
34+
) {
35+
tflog.Debug(ctx, "Create "+r.Config.Name)
36+
37+
var plan ResourceModel
38+
client := r.Meta.Client
39+
40+
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
41+
if resp.Diagnostics.HasError() {
42+
return
43+
}
44+
45+
createOpts := linodego.ImageShareGroupAddMemberOptions{
46+
Token: plan.Token.ValueString(),
47+
Label: plan.Label.ValueString(),
48+
}
49+
50+
tflog.Debug(ctx, "client.ImageShareGroupAddMember(...)", map[string]any{
51+
"options": createOpts,
52+
})
53+
54+
shareGroupID := helper.FrameworkSafeInt64ToInt(plan.ShareGroupID.ValueInt64(), &resp.Diagnostics)
55+
56+
member, err := client.ImageShareGroupAddMember(ctx, shareGroupID, createOpts)
57+
if err != nil {
58+
resp.Diagnostics.AddError(
59+
"Failed to create Image Share Group Member.",
60+
err.Error(),
61+
)
62+
return
63+
}
64+
65+
plan.FlattenImageShareGroupMember(member, true)
66+
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
67+
}
68+
69+
func (r *Resource) Read(
70+
ctx context.Context,
71+
req resource.ReadRequest,
72+
resp *resource.ReadResponse,
73+
) {
74+
tflog.Debug(ctx, "Read "+r.Config.Name)
75+
76+
client := r.Meta.Client
77+
78+
var state ResourceModel
79+
80+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
81+
if resp.Diagnostics.HasError() {
82+
return
83+
}
84+
85+
shareGroupID := helper.FrameworkSafeInt64ToInt(state.ShareGroupID.ValueInt64(), &resp.Diagnostics)
86+
if resp.Diagnostics.HasError() {
87+
return
88+
}
89+
90+
tokenUUID := state.TokenUUID.ValueString()
91+
92+
member, err := client.ImageShareGroupGetMember(ctx, shareGroupID, tokenUUID)
93+
if err != nil {
94+
resp.Diagnostics.AddError(
95+
"Failed to read Image Share Group Member.",
96+
err.Error(),
97+
)
98+
return
99+
}
100+
101+
state.FlattenImageShareGroupMember(member, true)
102+
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
103+
}
104+
105+
func (r *Resource) Update(
106+
ctx context.Context,
107+
req resource.UpdateRequest,
108+
resp *resource.UpdateResponse,
109+
) {
110+
tflog.Debug(ctx, "Update "+r.Config.Name)
111+
112+
client := r.Meta.Client
113+
114+
var plan ResourceModel
115+
var state ResourceModel
116+
117+
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
118+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
119+
if resp.Diagnostics.HasError() {
120+
return
121+
}
122+
123+
shareGroupID := helper.FrameworkSafeInt64ToInt(state.ShareGroupID.ValueInt64(), &resp.Diagnostics)
124+
if resp.Diagnostics.HasError() {
125+
return
126+
}
127+
128+
tokenUUID := state.TokenUUID.ValueString()
129+
130+
var updateOpts linodego.ImageShareGroupUpdateMemberOptions
131+
shouldUpdate := false
132+
133+
if !state.Label.Equal(plan.Label) {
134+
shouldUpdate = true
135+
updateOpts.Label = plan.Label.ValueString()
136+
}
137+
138+
if shouldUpdate {
139+
tflog.Debug(ctx, "client.ImageShareGroupUpdateMember(...)", map[string]any{
140+
"options": updateOpts,
141+
})
142+
143+
member, err := client.ImageShareGroupUpdateMember(ctx, shareGroupID, tokenUUID, updateOpts)
144+
if err != nil {
145+
resp.Diagnostics.AddError(
146+
fmt.Sprintf("Failed to update Image Share Group Member (%d).", shareGroupID),
147+
err.Error(),
148+
)
149+
return
150+
}
151+
152+
plan.FlattenImageShareGroupMember(member, false)
153+
if resp.Diagnostics.HasError() {
154+
return
155+
}
156+
}
157+
plan.CopyFrom(state, true)
158+
159+
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
160+
}
161+
162+
func (r *Resource) Delete(
163+
ctx context.Context,
164+
req resource.DeleteRequest,
165+
resp *resource.DeleteResponse,
166+
) {
167+
tflog.Debug(ctx, "Delete "+r.Config.Name)
168+
169+
var state ResourceModel
170+
171+
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
172+
if resp.Diagnostics.HasError() {
173+
return
174+
}
175+
176+
shareGroupID := helper.FrameworkSafeInt64ToInt(state.ShareGroupID.ValueInt64(), &resp.Diagnostics)
177+
if resp.Diagnostics.HasError() {
178+
return
179+
}
180+
181+
tokenUUID := state.TokenUUID.ValueString()
182+
183+
client := r.Meta.Client
184+
185+
err := client.ImageShareGroupRemoveMember(ctx, shareGroupID, tokenUUID)
186+
if err != nil {
187+
resp.Diagnostics.AddError("Failed to Delete Image Share Group Member.", err.Error())
188+
return
189+
}
190+
}

0 commit comments

Comments
 (0)