/
resource_ably_namespace.go
313 lines (283 loc) · 10.2 KB
/
resource_ably_namespace.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package ably_control
import (
"context"
ably_control_go "github.com/ably/ably-control-go"
"github.com/hashicorp/terraform-plugin-framework/diag"
tfsdk_resource "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
)
type resourceNamespace struct {
p *provider
}
// Get Namespace Resource schema
func (r resourceNamespace) GetSchema(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"app_id": {
Type: types.StringType,
Required: true,
Description: "The application ID.",
PlanModifiers: []tfsdk.AttributePlanModifier{
tfsdk_resource.RequiresReplace(),
},
},
"id": {
Type: types.StringType,
Required: true,
Description: "The namespace or channel name that the channel rule will apply to.",
PlanModifiers: []tfsdk.AttributePlanModifier{
tfsdk_resource.RequiresReplace(),
},
},
"authenticated": {
Type: types.BoolType,
Optional: true,
Computed: true,
Description: "Require clients to be authenticated to use channels in this namespace.",
PlanModifiers: []tfsdk.AttributePlanModifier{
DefaultAttribute(types.BoolValue(false)),
},
},
"persisted": {
Type: types.BoolType,
Optional: true,
Computed: true,
Description: "If true, messages will be stored for 24 hours.",
PlanModifiers: []tfsdk.AttributePlanModifier{
DefaultAttribute(types.BoolValue(false)),
},
},
"persist_last": {
Type: types.BoolType,
Optional: true,
Computed: true,
Description: "If true, the last message on each channel will persist for 365 days.",
PlanModifiers: []tfsdk.AttributePlanModifier{
DefaultAttribute(types.BoolValue(false)),
},
},
"push_enabled": {
Type: types.BoolType,
Optional: true,
Computed: true,
Description: "If true, publishing messages with a push payload in the extras field is permitted.",
PlanModifiers: []tfsdk.AttributePlanModifier{
DefaultAttribute(types.BoolValue(false)),
},
},
"tls_only": {
Type: types.BoolType,
Optional: true,
Computed: true,
Description: "If true, only clients that are connected using TLS will be permitted to subscribe.",
PlanModifiers: []tfsdk.AttributePlanModifier{
DefaultAttribute(types.BoolValue(false)),
},
},
"expose_timeserial": {
Type: types.BoolType,
Optional: true,
Computed: true,
Description: "If true, messages received on a channel will contain a unique timeserial that can be referenced by later messages for use with message interactions.",
PlanModifiers: []tfsdk.AttributePlanModifier{
DefaultAttribute(types.BoolValue(false)),
},
},
},
MarkdownDescription: "The ably_namespace resource allows you to manage namespaces for channel rules in Ably. Read more in the Ably documentation: https://ably.com/docs/general/channel-rules-namespaces.",
}, nil
}
// Create a new resource
func (r resourceNamespace) Create(ctx context.Context, req tfsdk_resource.CreateRequest, resp *tfsdk_resource.CreateResponse) {
// Checks whether the provider and API Client are configured. If they are not, the provider responds with an error.
if !r.p.configured {
resp.Diagnostics.AddError(
"Provider not configured",
"The provider hasn't been configured before apply",
)
return
}
// Gets plan values
var plan AblyNamespace
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Generates an API request body from the plan values
namespace_values := ably_control_go.Namespace{
ID: plan.ID.ValueString(),
Authenticated: plan.Authenticated.ValueBool(),
Persisted: plan.Persisted.ValueBool(),
PersistLast: plan.PersistLast.ValueBool(),
PushEnabled: plan.PushEnabled.ValueBool(),
TlsOnly: plan.TlsOnly.ValueBool(),
ExposeTimeserial: plan.ExposeTimeserial.ValueBool(),
}
// Creates a new Ably namespace by invoking the CreateNamespace function from the Client Library
ably_namespace, err := r.p.client.CreateNamespace(plan.AppID.ValueString(), &namespace_values)
if err != nil {
resp.Diagnostics.AddError(
"Error creating Resource",
"Could not create resource, unexpected error: "+err.Error(),
)
return
}
// Maps response body to resource schema attributes.
resp_apps := AblyNamespace{
AppID: types.StringValue(plan.AppID.ValueString()),
ID: types.StringValue(ably_namespace.ID),
Authenticated: types.BoolValue(ably_namespace.Authenticated),
Persisted: types.BoolValue(ably_namespace.Persisted),
PersistLast: types.BoolValue(ably_namespace.PersistLast),
PushEnabled: types.BoolValue(ably_namespace.PushEnabled),
TlsOnly: types.BoolValue(ably_namespace.TlsOnly),
ExposeTimeserial: types.BoolValue(namespace_values.ExposeTimeserial),
}
// Sets state for the new Ably App.
diags = resp.State.Set(ctx, resp_apps)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
func (r resourceNamespace) Metadata(ctx context.Context, req tfsdk_resource.MetadataRequest, resp *tfsdk_resource.MetadataResponse) {
resp.TypeName = "ably_namespace"
}
// Read resource
func (r resourceNamespace) Read(ctx context.Context, req tfsdk_resource.ReadRequest, resp *tfsdk_resource.ReadResponse) {
// Gets the current state. If it is unable to, the provider responds with an error.
var state AblyNamespace
found := false
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Gets the Ably App ID and namespace ID value for the resource
app_id := state.AppID.ValueString()
namespace_id := state.ID.ValueString()
// Fetches all Ably Namespaces in the app. The function invokes the Client Library Namespaces() method.
// NOTE: Control API & Client Lib do not currently support fetching single namespace given namespace id
namespaces, err := r.p.client.Namespaces(app_id)
if err != nil {
if is_404(err) {
resp.State.RemoveResource(ctx)
return
}
resp.Diagnostics.AddError(
"Error updating Resource",
"Could not update resource, unexpected error: "+err.Error(),
)
return
}
// Loops through namespaces and if id matches, sets state.
for _, v := range namespaces {
if v.ID == namespace_id {
resp_namespaces := AblyNamespace{
AppID: types.StringValue(app_id),
ID: types.StringValue(namespace_id),
Authenticated: types.BoolValue(v.Authenticated),
Persisted: types.BoolValue(v.Persisted),
PersistLast: types.BoolValue(v.PersistLast),
PushEnabled: types.BoolValue(v.PushEnabled),
TlsOnly: types.BoolValue(v.TlsOnly),
ExposeTimeserial: types.BoolValue(v.ExposeTimeserial),
}
// Sets state to namespace values.
diags = resp.State.Set(ctx, &resp_namespaces)
found = true
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
break
}
}
if !found {
resp.State.RemoveResource(ctx)
}
}
// Update resource
func (r resourceNamespace) Update(ctx context.Context, req tfsdk_resource.UpdateRequest, resp *tfsdk_resource.UpdateResponse) {
// Get plan values
var plan AblyNamespace
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Gets the app ID and ID
app_id := plan.AppID.ValueString()
namespace_id := plan.ID.ValueString()
// Instantiates struct of type ably_control_go.Namespace and sets values to output of plan
namespace_values := ably_control_go.Namespace{
ID: namespace_id,
Authenticated: plan.Authenticated.ValueBool(),
Persisted: plan.Persisted.ValueBool(),
PersistLast: plan.PersistLast.ValueBool(),
PushEnabled: plan.PushEnabled.ValueBool(),
TlsOnly: plan.TlsOnly.ValueBool(),
ExposeTimeserial: plan.ExposeTimeserial.ValueBool(),
}
// Updates an Ably Namespace. The function invokes the Client Library UpdateNamespace method.
ably_namespace, err := r.p.client.UpdateNamespace(app_id, &namespace_values)
if err != nil {
resp.Diagnostics.AddError(
"Error updating Resource",
"Could not update resource, unexpected error: "+err.Error(),
)
return
}
resp_namespaces := AblyNamespace{
AppID: types.StringValue(app_id),
ID: types.StringValue(ably_namespace.ID),
Authenticated: types.BoolValue(ably_namespace.Authenticated),
Persisted: types.BoolValue(ably_namespace.Persisted),
PersistLast: types.BoolValue(ably_namespace.PersistLast),
PushEnabled: types.BoolValue(ably_namespace.PushEnabled),
TlsOnly: types.BoolValue(ably_namespace.TlsOnly),
ExposeTimeserial: types.BoolValue(ably_namespace.ExposeTimeserial),
}
// Sets state to new namespace.
diags = resp.State.Set(ctx, resp_namespaces)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}
// Delete resource
func (r resourceNamespace) Delete(ctx context.Context, req tfsdk_resource.DeleteRequest, resp *tfsdk_resource.DeleteResponse) {
// Get current state
var state AblyNamespace
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Gets the current state. If it is unable to, the provider responds with an error.
app_id := state.AppID.ValueString()
namespace_id := state.ID.ValueString()
err := r.p.client.DeleteNamespace(app_id, namespace_id)
if err != nil {
if is_404(err) {
resp.Diagnostics.AddWarning(
"Resource does not exist",
"Resource does not exist, it may have already been deleted: "+err.Error(),
)
} else {
resp.Diagnostics.AddError(
"Error deleting Resource",
"Could not delete resource, unexpected error: "+err.Error(),
)
return
}
}
// Remove resource from state
resp.State.RemoveResource(ctx)
}
// Import resource
func (r resourceNamespace) ImportState(ctx context.Context, req tfsdk_resource.ImportStateRequest, resp *tfsdk_resource.ImportStateResponse) {
ImportResource(ctx, req, resp, "app_id", "id")
}