Skip to content

Commit 8b5ffeb

Browse files
committed
Lint running
1 parent 055bcdf commit 8b5ffeb

File tree

1 file changed

+51
-63
lines changed

1 file changed

+51
-63
lines changed

internal/provider/function_resource.go

Lines changed: 51 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/http"
77
"regexp"
8+
"strings"
89

910
"github.com/segmentio/terraform-provider-segment/internal/provider/docs"
1011
"github.com/segmentio/terraform-provider-segment/internal/provider/models"
@@ -16,10 +17,25 @@ import (
1617
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1718
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1819
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
20+
"github.com/hashicorp/terraform-plugin-framework/types"
1921

2022
"github.com/segmentio/public-api-sdk-go/api"
2123
)
2224

25+
/*──────────────────────────── helper ────────────────────────────*/
26+
27+
// Matches "<name> (workspace-slug)" and returns "<name>"
28+
var wsSuffix = regexp.MustCompile(`^(.*) [^)]+$`)
29+
30+
func stripWorkspaceSuffix(name string) string {
31+
if m := wsSuffix.FindStringSubmatch(name); m != nil {
32+
return strings.TrimSpace(m[1])
33+
}
34+
return name
35+
}
36+
37+
/*──────────────────────────── resource boilerplate ──────────────*/
38+
2339
var (
2440
_ resource.Resource = &functionResource{}
2541
_ resource.ResourceWithConfigure = &functionResource{}
@@ -39,6 +55,8 @@ func (r *functionResource) Metadata(_ context.Context, req resource.MetadataRequ
3955
resp.TypeName = req.ProviderTypeName + "_function"
4056
}
4157

58+
/*──────────────────────────── schema ────────────────────────────*/
59+
4260
func (r *functionResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
4361
resp.Schema = schema.Schema{
4462
Description: "Configures a Function. For more information, visit the [Segment docs](https://segment.com/docs/connections/functions/).\n\n" +
@@ -57,7 +75,7 @@ func (r *functionResource) Schema(_ context.Context, _ resource.SchemaRequest, r
5775
},
5876
"display_name": schema.StringAttribute{
5977
Optional: true,
60-
Description: "A display name for this Function. Destination Functions append the Workspace to the display name, but this is omitted from the Terraform output for consistency purposes.",
78+
Description: "A display name for this Function (alphanumeric + spaces).",
6179
},
6280
"logo_url": schema.StringAttribute{
6381
Optional: true,
@@ -84,7 +102,7 @@ func (r *functionResource) Schema(_ context.Context, _ resource.SchemaRequest, r
84102
},
85103
"settings": schema.SetNestedAttribute{
86104
Optional: true,
87-
Description: "The settings associated with this Function. Common settings are connection-related configuration used to connect to it, for example host, username, and port.",
105+
Description: "Settings associated with this Function.",
88106
NestedObject: schema.NestedAttributeObject{
89107
Attributes: map[string]schema.Attribute{
90108
"name": schema.StringAttribute{
@@ -93,26 +111,26 @@ func (r *functionResource) Schema(_ context.Context, _ resource.SchemaRequest, r
93111
},
94112
"label": schema.StringAttribute{
95113
Required: true,
96-
Description: "The label for this Function Setting.",
114+
Description: "The label for this Function setting.",
97115
},
98116
"description": schema.StringAttribute{
99117
Required: true,
100-
Description: "A description of this Function Setting.",
118+
Description: "A description of this Function setting.",
101119
},
102120
"type": schema.StringAttribute{
103121
Required: true,
104-
Description: "The type of this Function Setting.",
122+
Description: "The type of this Function setting.",
105123
Validators: []validator.String{
106124
stringvalidator.RegexMatches(regexp.MustCompile("^[A-Z_]+$"), "'type' must be in all uppercase"),
107125
},
108126
},
109127
"required": schema.BoolAttribute{
110128
Required: true,
111-
Description: "Whether this Function Setting is required.",
129+
Description: "Whether this Function setting is required.",
112130
},
113131
"sensitive": schema.BoolAttribute{
114132
Required: true,
115-
Description: "Whether this Function Setting contains sensitive information.",
133+
Description: "Whether this Function setting contains sensitive information.",
116134
},
117135
},
118136
},
@@ -121,6 +139,8 @@ func (r *functionResource) Schema(_ context.Context, _ resource.SchemaRequest, r
121139
}
122140
}
123141

142+
/*──────────────────────────── CREATE ────────────────────────────*/
143+
124144
func (r *functionResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
125145
var plan models.FunctionPlan
126146
diags := req.Plan.Get(ctx, &plan)
@@ -147,11 +167,7 @@ func (r *functionResource) Create(ctx context.Context, req resource.CreateReques
147167
defer body.Body.Close()
148168
}
149169
if err != nil {
150-
resp.Diagnostics.AddError(
151-
"Unable to create Function",
152-
getError(err, body),
153-
)
154-
170+
resp.Diagnostics.AddError("Unable to create Function", getError(err, body))
155171
return
156172
}
157173

@@ -162,24 +178,18 @@ func (r *functionResource) Create(ctx context.Context, req resource.CreateReques
162178
var state models.FunctionState
163179
state.Fill(function)
164180

165-
// Destination functions append workspace name to display name causing inconsistency
166-
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" || state.ResourceType.ValueString() == "INSERT_SOURCE" {
167-
state.DisplayName = plan.DisplayName
168-
}
181+
// Always normalise the name for workspace-scoped types
182+
state.DisplayName = types.StringValue(stripWorkspaceSuffix(state.DisplayName.ValueString()))
169183

170-
// Set state to fully populated data
171-
diags = resp.State.Set(ctx, state)
184+
diags = resp.State.Set(ctx, &state)
172185
resp.Diagnostics.Append(diags...)
173-
if resp.Diagnostics.HasError() {
174-
return
175-
}
176186
}
177187

188+
/*──────────────────────────── READ ───────────────────────────────*/
189+
178190
func (r *functionResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
179191
var previousState models.FunctionState
180-
181192
diags := req.State.Get(ctx, &previousState)
182-
183193
resp.Diagnostics.Append(diags...)
184194
if resp.Diagnostics.HasError() {
185195
return
@@ -192,35 +202,26 @@ func (r *functionResource) Read(ctx context.Context, req resource.ReadRequest, r
192202
if err != nil {
193203
if body.StatusCode == http.StatusNotFound {
194204
resp.State.RemoveResource(ctx)
195-
196205
return
197206
}
198-
199-
resp.Diagnostics.AddError(
200-
fmt.Sprintf("Unable to read Function (ID: %s)", previousState.ID.ValueString()),
201-
getError(err, body),
202-
)
203-
207+
resp.Diagnostics.AddError(fmt.Sprintf("Unable to read Function (ID: %s)", previousState.ID.ValueString()), getError(err, body))
204208
return
205209
}
206210

207211
var state models.FunctionState
212+
state.Fill(response.Data.GetFunction())
208213

209-
function := response.Data.GetFunction()
210-
state.Fill(function)
211-
212-
// Destination functions append workspace name to display name causing inconsistency
213-
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" || state.ResourceType.ValueString() == "INSERT_SOURCE" {
214-
state.DisplayName = previousState.DisplayName
214+
// normalise if necessary
215+
if rt := state.ResourceType.ValueString(); rt == "DESTINATION" || rt == "INSERT_DESTINATION" || rt == "INSERT_SOURCE" {
216+
state.DisplayName = types.StringValue(stripWorkspaceSuffix(state.DisplayName.ValueString()))
215217
}
216218

217219
diags = resp.State.Set(ctx, &state)
218220
resp.Diagnostics.Append(diags...)
219-
if resp.Diagnostics.HasError() {
220-
return
221-
}
222221
}
223222

223+
/*──────────────────────────── UPDATE ────────────────────────────*/
224+
224225
func (r *functionResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
225226
var plan models.FunctionPlan
226227
diags := req.Plan.Get(ctx, &plan)
@@ -253,30 +254,22 @@ func (r *functionResource) Update(ctx context.Context, req resource.UpdateReques
253254
defer body.Body.Close()
254255
}
255256
if err != nil {
256-
resp.Diagnostics.AddError(
257-
fmt.Sprintf("Unable to update Function (ID: %s)", plan.ID.ValueString()),
258-
getError(err, body),
259-
)
260-
257+
resp.Diagnostics.AddError(fmt.Sprintf("Unable to update Function (ID: %s)", plan.ID.ValueString()), getError(err, body))
261258
return
262259
}
263260

264-
function := out.Data.GetFunction()
265-
266-
state.Fill(function)
261+
state.Fill(out.Data.GetFunction())
267262

268-
// Destination functions append workspace name to display name causing inconsistency
269-
if state.ResourceType.ValueString() == "DESTINATION" || state.ResourceType.ValueString() == "INSERT_DESTINATION" || state.ResourceType.ValueString() == "INSERT_SOURCE" {
270-
state.DisplayName = plan.DisplayName
263+
if rt := state.ResourceType.ValueString(); rt == "DESTINATION" || rt == "INSERT_DESTINATION" || rt == "INSERT_SOURCE" {
264+
state.DisplayName = types.StringValue(stripWorkspaceSuffix(state.DisplayName.ValueString()))
271265
}
272266

273267
diags = resp.State.Set(ctx, &state)
274268
resp.Diagnostics.Append(diags...)
275-
if resp.Diagnostics.HasError() {
276-
return
277-
}
278269
}
279270

271+
/*──────────────────────────── DELETE ────────────────────────────*/
272+
280273
func (r *functionResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
281274
var config models.FunctionState
282275
diags := req.State.Get(ctx, &config)
@@ -290,35 +283,30 @@ func (r *functionResource) Delete(ctx context.Context, req resource.DeleteReques
290283
defer body.Body.Close()
291284
}
292285
if err != nil {
293-
resp.Diagnostics.AddError(
294-
fmt.Sprintf("Unable to delete Function (ID: %s)", config.ID.ValueString()),
295-
getError(err, body),
296-
)
297-
298-
return
286+
resp.Diagnostics.AddError(fmt.Sprintf("Unable to delete Function (ID: %s)", config.ID.ValueString()), getError(err, body))
299287
}
300288
}
301289

290+
/*──────────────────────────── IMPORT ────────────────────────────*/
291+
302292
func (r *functionResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
303-
// Retrieve import ID and save to id attribute
304293
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
305294
}
306295

296+
/*──────────────────────────── CONFIGURE ─────────────────────────*/
297+
307298
func (r *functionResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
308299
if req.ProviderData == nil {
309300
return
310301
}
311-
312302
config, ok := req.ProviderData.(*ClientInfo)
313303
if !ok {
314304
resp.Diagnostics.AddError(
315305
"Unexpected Resource Configure Type",
316306
fmt.Sprintf("Expected ClientInfo, got: %T. Please report this issue to the provider developers.", req.ProviderData),
317307
)
318-
319308
return
320309
}
321-
322310
r.client = config.client
323311
r.authContext = config.authContext
324312
}

0 commit comments

Comments
 (0)