|
| 1 | +package consumerimagesharegrouptoken |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 8 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 9 | + "github.com/linode/linodego" |
| 10 | + "github.com/linode/terraform-provider-linode/v3/linode/helper" |
| 11 | +) |
| 12 | + |
| 13 | +func NewResource() resource.Resource { |
| 14 | + return &Resource{ |
| 15 | + BaseResource: helper.NewBaseResource( |
| 16 | + helper.BaseResourceConfig{ |
| 17 | + Name: "linode_consumer_image_share_group_token", |
| 18 | + IDType: types.Int64Type, |
| 19 | + Schema: &frameworkResourceSchema, |
| 20 | + }, |
| 21 | + ), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +type Resource struct { |
| 26 | + helper.BaseResource |
| 27 | +} |
| 28 | + |
| 29 | +func (r *Resource) Create( |
| 30 | + ctx context.Context, |
| 31 | + req resource.CreateRequest, |
| 32 | + resp *resource.CreateResponse, |
| 33 | +) { |
| 34 | + tflog.Debug(ctx, "Create "+r.Config.Name) |
| 35 | + |
| 36 | + var plan ResourceModel |
| 37 | + client := r.Meta.Client |
| 38 | + |
| 39 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) |
| 40 | + if resp.Diagnostics.HasError() { |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + createOpts := linodego.ImageShareGroupCreateTokenOptions{ |
| 45 | + ValidForShareGroupUUID: plan.ValidForShareGroupUUID.ValueString(), |
| 46 | + Label: plan.Label.ValueStringPointer(), |
| 47 | + } |
| 48 | + |
| 49 | + tflog.Debug(ctx, "client.ImageShareGroupCreateToken(...)", map[string]any{ |
| 50 | + "options": createOpts, |
| 51 | + }) |
| 52 | + |
| 53 | + token, err := client.ImageShareGroupCreateToken(ctx, createOpts) |
| 54 | + if err != nil { |
| 55 | + resp.Diagnostics.AddError( |
| 56 | + "Failed to create Image Share Group Token.", |
| 57 | + err.Error(), |
| 58 | + ) |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + plan.FlattenImageShareGroupCreateToken(token) |
| 63 | + resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) |
| 64 | +} |
| 65 | + |
| 66 | +func (r *Resource) Read( |
| 67 | + ctx context.Context, |
| 68 | + req resource.ReadRequest, |
| 69 | + resp *resource.ReadResponse, |
| 70 | +) { |
| 71 | + tflog.Debug(ctx, "Read "+r.Config.Name) |
| 72 | + |
| 73 | + client := r.Meta.Client |
| 74 | + |
| 75 | + var state ResourceModel |
| 76 | + |
| 77 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 78 | + if resp.Diagnostics.HasError() { |
| 79 | + return |
| 80 | + } |
| 81 | + |
| 82 | + tokenUUID := state.TokenUUID.ValueString() |
| 83 | + |
| 84 | + token, err := client.ImageShareGroupGetToken(ctx, tokenUUID) |
| 85 | + if err != nil { |
| 86 | + resp.Diagnostics.AddError( |
| 87 | + "Failed to read Image Share Group Token.", |
| 88 | + err.Error(), |
| 89 | + ) |
| 90 | + return |
| 91 | + } |
| 92 | + |
| 93 | + state.FlattenImageShareGroupToken(token, true) |
| 94 | + resp.Diagnostics.Append(resp.State.Set(ctx, &state)...) |
| 95 | +} |
| 96 | + |
| 97 | +func (r *Resource) Update( |
| 98 | + ctx context.Context, |
| 99 | + req resource.UpdateRequest, |
| 100 | + resp *resource.UpdateResponse, |
| 101 | +) { |
| 102 | + tflog.Debug(ctx, "Update "+r.Config.Name) |
| 103 | + |
| 104 | + client := r.Meta.Client |
| 105 | + |
| 106 | + var plan ResourceModel |
| 107 | + var state ResourceModel |
| 108 | + |
| 109 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...) |
| 110 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 111 | + if resp.Diagnostics.HasError() { |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + tokenUUID := state.TokenUUID.ValueString() |
| 116 | + |
| 117 | + var updateOpts linodego.ImageShareGroupUpdateTokenOptions |
| 118 | + shouldUpdate := false |
| 119 | + |
| 120 | + if !state.Label.Equal(plan.Label) { |
| 121 | + shouldUpdate = true |
| 122 | + updateOpts.Label = plan.Label.ValueString() |
| 123 | + } |
| 124 | + |
| 125 | + if shouldUpdate { |
| 126 | + tflog.Debug(ctx, "client.ImageShareGroupUpdateToken(...)", map[string]any{ |
| 127 | + "options": updateOpts, |
| 128 | + }) |
| 129 | + |
| 130 | + token, err := client.ImageShareGroupUpdateToken(ctx, tokenUUID, updateOpts) |
| 131 | + if err != nil { |
| 132 | + resp.Diagnostics.AddError( |
| 133 | + fmt.Sprintf("Failed to update Image Share Group Token (%d).", tokenUUID), |
| 134 | + err.Error(), |
| 135 | + ) |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + plan.FlattenImageShareGroupToken(token, false) |
| 140 | + if resp.Diagnostics.HasError() { |
| 141 | + return |
| 142 | + } |
| 143 | + } |
| 144 | + plan.CopyFrom(state, true) |
| 145 | + |
| 146 | + resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...) |
| 147 | +} |
| 148 | + |
| 149 | +func (r *Resource) Delete( |
| 150 | + ctx context.Context, |
| 151 | + req resource.DeleteRequest, |
| 152 | + resp *resource.DeleteResponse, |
| 153 | +) { |
| 154 | + tflog.Debug(ctx, "Delete "+r.Config.Name) |
| 155 | + |
| 156 | + var state ResourceModel |
| 157 | + |
| 158 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 159 | + if resp.Diagnostics.HasError() { |
| 160 | + return |
| 161 | + } |
| 162 | + |
| 163 | + tokenUUID := state.TokenUUID.ValueString() |
| 164 | + |
| 165 | + client := r.Meta.Client |
| 166 | + |
| 167 | + err := client.ImageShareGroupRemoveToken(ctx, tokenUUID) |
| 168 | + if err != nil { |
| 169 | + resp.Diagnostics.AddError("Failed to Delete Image Share Group Token.", err.Error()) |
| 170 | + return |
| 171 | + } |
| 172 | +} |
0 commit comments