/
dataset_resource.go
193 lines (154 loc) · 5.42 KB
/
dataset_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"
"strings"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/tessellator/go-sanity/sanity"
)
var _ resource.Resource = &DatasetResource{}
var _ resource.ResourceWithImportState = &DatasetResource{}
func NewDatasetResource() resource.Resource {
return &DatasetResource{}
}
type DatasetResource struct {
client *sanity.Client
}
type DatasetResourceModel struct {
Project types.String `tfsdk:"project"`
Name types.String `tfsdk:"name"`
AclMode types.String `tfsdk:"acl_mode"`
}
func (r *DatasetResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_dataset"
}
func (r *DatasetResource) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
MarkdownDescription: "Provides a dataset to a Sanity project. A dataset is like a database for your content, and you manage its contents with a studio and query it with GROQ or GraphQL.",
Attributes: map[string]tfsdk.Attribute{
"project": {
Required: true,
Type: types.StringType,
MarkdownDescription: "The ID of the project that the dataset belongs to.",
PlanModifiers: tfsdk.AttributePlanModifiers{
resource.RequiresReplace(),
},
},
"name": {
Required: true,
Type: types.StringType,
MarkdownDescription: "The name of the dataset.",
PlanModifiers: tfsdk.AttributePlanModifiers{
resource.RequiresReplace(),
},
},
"acl_mode": {
Optional: true,
Computed: true,
Type: types.StringType,
MarkdownDescription: "The ACL mode for the data. Valid options are `public` and `private`.",
PlanModifiers: tfsdk.AttributePlanModifiers{
resource.RequiresReplace(),
},
},
},
}, nil
}
func (r *DatasetResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*sanity.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *DatasetResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data *DatasetResourceModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
_, err := r.client.Projects.CreateDataset(ctx, data.Project.Value, &sanity.CreateDatasetRequest{
Name: data.Name.Value,
AclMode: data.AclMode.Value,
})
if err != nil {
resp.Diagnostics.AddError("Client Error", err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *DatasetResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data *DatasetResourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
projectId := data.Project.Value
datasets, err := r.client.Projects.ListDatasets(ctx, projectId)
if err != nil {
resp.Diagnostics.AddError("Client Error", err.Error())
return
}
var dataset sanity.Dataset
found := false
for _, d := range datasets {
if d.Name == data.Name.Value {
dataset = d
found = true
break
}
}
if !found {
resp.Diagnostics.AddError("dataset not found", "dataset not found")
return
}
data.AclMode = types.String{Value: dataset.AclMode}
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *DatasetResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
resp.Diagnostics.AddError("Provider Error", "Update is not supported on dataset")
}
func (r *DatasetResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data *DatasetResourceModel
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
if data.Name.Null {
resp.Diagnostics.AddError("Name is null", "Name is null")
return
}
if data.Project.Null {
resp.Diagnostics.AddError("Project is null", "Project is null")
return
}
_, err := r.client.Projects.DeleteDataset(ctx, data.Project.Value, data.Name.Value)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("dataset %s could not be deleted, got error: %s", data.Name.Value, err))
return
}
}
func (r *DatasetResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
parts := strings.Split(req.ID, "/")
if len(parts) != 2 {
resp.Diagnostics.AddError("Input Error", "The import identifier for a dataset should be in the form project-id/dataset-name")
return
}
reqProject := resource.ImportStateRequest{ID: parts[0]}
reqName := resource.ImportStateRequest{ID: parts[1]}
resource.ImportStatePassthroughID(ctx, path.Root("project"), reqProject, resp)
resource.ImportStatePassthroughID(ctx, path.Root("name"), reqName, resp)
}