Skip to content

Commit

Permalink
strings.Cut everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
jharley committed Feb 17, 2024
1 parent 1fa9555 commit 1365236
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 20 deletions.
11 changes: 3 additions & 8 deletions honeycombio/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package honeycombio
import (
"context"
"errors"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -69,15 +68,11 @@ the column evaluation should consistently return nil, true, or false, as these a

func resourceSLOImport(ctx context.Context, d *schema.ResourceData, i interface{}) ([]*schema.ResourceData, error) {
// import ID is of the format <dataset>/<SLO ID>
// note that the dataset name can also contain '/'
idSegments := strings.Split(d.Id(), "/")
if len(idSegments) < 2 {
return nil, fmt.Errorf("invalid import ID, supplied ID must be written as <dataset>/<SLO ID>")
dataset, id, found := strings.Cut(d.Id(), "/")
if !found {
return nil, errors.New("invalid import ID, supplied ID must be written as <dataset>/<SLO ID>")
}

dataset := strings.Join(idSegments[0:len(idSegments)-1], "/")
id := idSegments[len(idSegments)-1]

d.Set("dataset", dataset)
d.SetId(id)

Expand Down
8 changes: 2 additions & 6 deletions internal/provider/burn_alert_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,15 @@ func (r *burnAlertResource) ValidateConfig(ctx context.Context, req resource.Val

func (r *burnAlertResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// import ID is of the format <dataset>/<BurnAlert ID>
// note that the dataset name can also contain '/'
idSegments := strings.Split(req.ID, "/")
if len(idSegments) < 2 {
dataset, id, found := strings.Cut(req.ID, "/")
if !found {
resp.Diagnostics.AddError(
"Invalid Import ID",
"The supplied ID must be written as <dataset>/<BurnAlert ID>.",
)
return
}

id := idSegments[len(idSegments)-1]
dataset := strings.Join(idSegments[0:len(idSegments)-1], "/")

resp.Diagnostics.Append(resp.State.Set(ctx, &models.BurnAlertResourceModel{
ID: types.StringValue(id),
Dataset: types.StringValue(dataset),
Expand Down
8 changes: 2 additions & 6 deletions internal/provider/trigger_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,19 +373,15 @@ func (r *triggerResource) Delete(ctx context.Context, req resource.DeleteRequest

func (r *triggerResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
// import ID is of the format <dataset>/<trigger ID>
// note that the dataset name can also contain '/'
idSegments := strings.Split(req.ID, "/")
if len(idSegments) < 2 {
dataset, id, found := strings.Cut(req.ID, "/")
if !found {
resp.Diagnostics.AddError(
"Invalid Import ID",
"The supplied ID must be wrtten as <dataset>/<trigger ID>.",
)
return
}

id := idSegments[len(idSegments)-1]
dataset := strings.Join(idSegments[0:len(idSegments)-1], "/")

resp.Diagnostics.Append(resp.State.Set(ctx, &models.TriggerResourceModel{
ID: types.StringValue(id),
Dataset: types.StringValue(dataset),
Expand Down

0 comments on commit 1365236

Please sign in to comment.