This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
/
volume_resource.go
193 lines (161 loc) · 5.99 KB
/
volume_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package provider
import (
"context"
"fmt"
"regexp"
"strings"
basegql "github.com/Khan/genqlient/graphql"
"github.com/fly-apps/terraform-provider-fly/graphql"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
var _ resource.Resource = &flyVolumeResource{}
var _ resource.ResourceWithConfigure = &flyVolumeResource{}
var _ resource.ResourceWithImportState = &flyVolumeResource{}
type flyVolumeResource struct {
client *basegql.Client
}
func NewVolumeResource() resource.Resource {
return &flyVolumeResource{}
}
func (r *flyVolumeResource) Metadata(_ context.Context, _ resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = "fly_volume"
}
func (r *flyVolumeResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}
config := req.ProviderData.(ProviderConfig)
r.client = config.gqclient
}
type flyVolumeResourceData struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Size types.Int64 `tfsdk:"size"`
Appid types.String `tfsdk:"app"`
Region types.String `tfsdk:"region"`
}
func (r *flyVolumeResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Fly volume resource",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
MarkdownDescription: "ID of volume",
Computed: true,
// Optional: true,
},
"app": schema.StringAttribute{
MarkdownDescription: "Name of app to attach to",
Required: true,
},
"size": schema.Int64Attribute{
MarkdownDescription: "Size of volume in GB",
Required: true,
},
"name": schema.StringAttribute{
MarkdownDescription: "name",
Required: true,
Validators: []validator.String{
stringvalidator.RegexMatches(
regexp.MustCompile(`^[a-z0-9_]+$`),
"only allows alphanumeric characters and underscores",
),
},
},
"region": schema.StringAttribute{
MarkdownDescription: "region",
Required: true,
},
},
}
}
func (r *flyVolumeResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data flyVolumeResourceData
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
q, err := graphql.CreateVolume(ctx, *r.client, data.Appid.ValueString(), data.Name.ValueString(), data.Region.ValueString(), int(data.Size.ValueInt64()))
if err != nil {
resp.Diagnostics.AddError("Failed to create volume", err.Error())
return
}
data = flyVolumeResourceData{
Id: types.StringValue(q.CreateVolume.Volume.Id),
Name: types.StringValue(q.CreateVolume.Volume.Name),
Size: types.Int64Value(int64(q.CreateVolume.Volume.SizeGb)),
Appid: types.StringValue(data.Appid.ValueString()),
Region: types.StringValue(q.CreateVolume.Volume.Region),
}
tflog.Info(ctx, fmt.Sprintf("%+v", data))
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *flyVolumeResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data flyVolumeResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
// strip leading vol_ off name
internalId := data.Id.ValueString()[4:]
app := data.Appid.ValueString()
query, err := graphql.VolumeQuery(ctx, *r.client, app, internalId)
if err != nil {
resp.Diagnostics.AddError("Read: query failed", err.Error())
return
}
// this query will currently still return success if it finds nothing, so check it:
if query.App.Volume.Id == "" {
resp.Diagnostics.AddError("Query failed", "Could not find matching volume")
}
data = flyVolumeResourceData{
Id: types.StringValue(query.App.Volume.Id),
Name: types.StringValue(query.App.Volume.Name),
Size: types.Int64Value(int64(query.App.Volume.SizeGb)),
Appid: types.StringValue(data.Appid.ValueString()),
Region: types.StringValue(query.App.Volume.Region),
// Internalid: types.StringValue(query.App.Volume.InternalId),
}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r *flyVolumeResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError("The fly api does not allow updating volumes once created", "Try deleting and then recreating a volume with new options")
return
}
func (r *flyVolumeResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data flyVolumeResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if !data.Id.IsUnknown() && !data.Id.IsNull() && data.Id.ValueString() != "" {
_, err := graphql.DeleteVolume(ctx, *r.client, data.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError("Delete volume failed", err.Error())
return
}
}
resp.State.RemoveResource(ctx)
if resp.Diagnostics.HasError() {
return
}
}
func (vr flyVolumeResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
idParts := strings.Split(req.ID, ",")
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.AddError(
"Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: app_id,volume_internal_id. Got: %q", req.ID),
)
return
}
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("app"), idParts[0])...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("internalid"), idParts[1])...)
}