Skip to content

Commit 7385e93

Browse files
authored
Move diag utilities from utils to dedicated diagutil package (elastic#1317)
* Move diag utilities from utils to dedicated diagutil package * Fix tests
1 parent add9bdd commit 7385e93

File tree

49 files changed

+359
-360
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+359
-360
lines changed

internal/apm/agent_configuration/create.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66

77
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
8-
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
8+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
99
"github.com/hashicorp/terraform-plugin-framework/resource"
1010
"github.com/hashicorp/terraform-plugin-log/tflog"
1111
)
@@ -53,7 +53,7 @@ func (r *resourceAgentConfiguration) Create(ctx context.Context, req resource.Cr
5353
}
5454
defer apiResp.Body.Close()
5555

56-
if diags := utils.CheckHttpErrorFromFW(apiResp, "Failed to create APM agent configuration"); diags.HasError() {
56+
if diags := diagutil.CheckHttpErrorFromFW(apiResp, "Failed to create APM agent configuration"); diags.HasError() {
5757
resp.Diagnostics.Append(diags...)
5858
return
5959
}

internal/apm/agent_configuration/delete.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77

88
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
9-
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
9+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
1010
"github.com/hashicorp/terraform-plugin-framework/resource"
1111
"github.com/hashicorp/terraform-plugin-log/tflog"
1212
)
@@ -50,7 +50,7 @@ func (r *resourceAgentConfiguration) Delete(ctx context.Context, req resource.De
5050
}
5151
defer apiResp.Body.Close()
5252

53-
if diags := utils.CheckHttpErrorFromFW(apiResp, "Failed to delete APM agent configuration"); diags.HasError() {
53+
if diags := diagutil.CheckHttpErrorFromFW(apiResp, "Failed to delete APM agent configuration"); diags.HasError() {
5454
resp.Diagnostics.Append(diags...)
5555
return
5656
}

internal/apm/agent_configuration/read.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strings"
77

88
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
9-
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
9+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
1010
"github.com/hashicorp/terraform-plugin-framework/diag"
1111
"github.com/hashicorp/terraform-plugin-framework/resource"
1212
"github.com/hashicorp/terraform-plugin-framework/types"
@@ -53,7 +53,7 @@ func (r *resourceAgentConfiguration) read(ctx context.Context, state *AgentConfi
5353
return nil, diags
5454
}
5555

56-
if httpDiags := utils.CheckHttpErrorFromFW(apiResp.HTTPResponse, "Failed to get APM agent configurations"); httpDiags.HasError() {
56+
if httpDiags := diagutil.CheckHttpErrorFromFW(apiResp.HTTPResponse, "Failed to get APM agent configurations"); httpDiags.HasError() {
5757
diags.Append(httpDiags...)
5858
return nil, diags
5959
}

internal/apm/agent_configuration/update.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66

77
"github.com/elastic/terraform-provider-elasticstack/generated/kbapi"
8-
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
8+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
99
"github.com/hashicorp/terraform-plugin-framework/resource"
1010
"github.com/hashicorp/terraform-plugin-log/tflog"
1111
)
@@ -51,7 +51,7 @@ func (r *resourceAgentConfiguration) Update(ctx context.Context, req resource.Up
5151
}
5252
defer apiResp.Body.Close()
5353

54-
if diags := utils.CheckHttpErrorFromFW(apiResp, "Failed to update APM agent configuration"); diags.HasError() {
54+
if diags := diagutil.CheckHttpErrorFromFW(apiResp, "Failed to update APM agent configuration"); diags.HasError() {
5555
resp.Diagnostics.Append(diags...)
5656
return
5757
}

internal/clients/api_client.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/elastic/terraform-provider-elasticstack/internal/clients/config"
1616
"github.com/elastic/terraform-provider-elasticstack/internal/clients/fleet"
1717
"github.com/elastic/terraform-provider-elasticstack/internal/clients/kibana_oapi"
18+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
1819
"github.com/elastic/terraform-provider-elasticstack/internal/models"
1920
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
2021
"github.com/hashicorp/go-version"
@@ -53,7 +54,7 @@ func CompositeIdFromStr(id string) (*CompositeId, diag.Diagnostics) {
5354

5455
func CompositeIdFromStrFw(id string) (*CompositeId, fwdiags.Diagnostics) {
5556
composite, diags := CompositeIdFromStr(id)
56-
return composite, utils.FrameworkDiagsFromSDK(diags)
57+
return composite, diagutil.FrameworkDiagsFromSDK(diags)
5758
}
5859

5960
func ResourceIDFromStr(id string) (string, diag.Diagnostics) {
@@ -323,7 +324,7 @@ func (a *ApiClient) serverInfo(ctx context.Context) (*models.ClusterInfo, diag.D
323324
return nil, diag.FromErr(err)
324325
}
325326
defer res.Body.Close()
326-
if diags := utils.CheckError(res, "Unable to connect to the Elasticsearch cluster"); diags.HasError() {
327+
if diags := diagutil.CheckError(res, "Unable to connect to the Elasticsearch cluster"); diags.HasError() {
327328
return nil, diags
328329
}
329330

internal/clients/elasticsearch/cluster.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"net/http"
99

1010
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
11+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
1112
"github.com/elastic/terraform-provider-elasticstack/internal/models"
12-
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1414
)
1515

@@ -24,7 +24,7 @@ func GetClusterInfo(ctx context.Context, apiClient *clients.ApiClient) (*models.
2424
return nil, diag.FromErr(err)
2525
}
2626
defer res.Body.Close()
27-
if diags := utils.CheckError(res, "Unable to connect to the Elasticsearch cluster"); diags.HasError() {
27+
if diags := diagutil.CheckError(res, "Unable to connect to the Elasticsearch cluster"); diags.HasError() {
2828
return nil, diags
2929
}
3030

@@ -50,7 +50,7 @@ func PutSnapshotRepository(ctx context.Context, apiClient *clients.ApiClient, re
5050
return diag.FromErr(err)
5151
}
5252
defer res.Body.Close()
53-
if diags := utils.CheckError(res, "Unable to create or update the snapshot repository"); diags.HasError() {
53+
if diags := diagutil.CheckError(res, "Unable to create or update the snapshot repository"); diags.HasError() {
5454
return diags
5555
}
5656

@@ -72,7 +72,7 @@ func GetSnapshotRepository(ctx context.Context, apiClient *clients.ApiClient, na
7272
if res.StatusCode == http.StatusNotFound {
7373
return nil, nil
7474
}
75-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get the information about snapshot repository: %s", name)); diags.HasError() {
75+
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to get the information about snapshot repository: %s", name)); diags.HasError() {
7676
return nil, diags
7777
}
7878
snapRepoResponse := make(map[string]models.SnapshotRepository)
@@ -106,7 +106,7 @@ func DeleteSnapshotRepository(ctx context.Context, apiClient *clients.ApiClient,
106106
return diag.FromErr(err)
107107
}
108108
defer res.Body.Close()
109-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to delete snapshot repository: %s", name)); diags.HasError() {
109+
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to delete snapshot repository: %s", name)); diags.HasError() {
110110
return diags
111111
}
112112
return diags
@@ -129,7 +129,7 @@ func PutSlm(ctx context.Context, apiClient *clients.ApiClient, slm *models.Snaps
129129
return diag.FromErr(err)
130130
}
131131
defer res.Body.Close()
132-
if diags := utils.CheckError(res, "Unable to create or update the SLM"); diags.HasError() {
132+
if diags := diagutil.CheckError(res, "Unable to create or update the SLM"); diags.HasError() {
133133
return diags
134134
}
135135

@@ -151,7 +151,7 @@ func GetSlm(ctx context.Context, apiClient *clients.ApiClient, slmName string) (
151151
if res.StatusCode == http.StatusNotFound {
152152
return nil, nil
153153
}
154-
if diags := utils.CheckError(res, "Unable to get SLM policy from ES API"); diags.HasError() {
154+
if diags := diagutil.CheckError(res, "Unable to get SLM policy from ES API"); diags.HasError() {
155155
return nil, diags
156156
}
157157
type SlmResponse = map[string]struct {
@@ -183,7 +183,7 @@ func DeleteSlm(ctx context.Context, apiClient *clients.ApiClient, slmName string
183183
return diag.FromErr(err)
184184
}
185185
defer res.Body.Close()
186-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to delete SLM policy: %s", slmName)); diags.HasError() {
186+
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to delete SLM policy: %s", slmName)); diags.HasError() {
187187
return diags
188188
}
189189

@@ -205,7 +205,7 @@ func PutSettings(ctx context.Context, apiClient *clients.ApiClient, settings map
205205
return diag.FromErr(err)
206206
}
207207
defer res.Body.Close()
208-
if diags := utils.CheckError(res, "Unable to update cluster settings."); diags.HasError() {
208+
if diags := diagutil.CheckError(res, "Unable to update cluster settings."); diags.HasError() {
209209
return diags
210210
}
211211
return diags
@@ -223,7 +223,7 @@ func GetSettings(ctx context.Context, apiClient *clients.ApiClient) (map[string]
223223
return nil, diag.FromErr(err)
224224
}
225225
defer res.Body.Close()
226-
if diags := utils.CheckError(res, "Unable to read cluster settings."); diags.HasError() {
226+
if diags := diagutil.CheckError(res, "Unable to read cluster settings."); diags.HasError() {
227227
return nil, diags
228228
}
229229

@@ -247,7 +247,7 @@ func GetScript(ctx context.Context, apiClient *clients.ApiClient, id string) (*m
247247
if res.StatusCode == http.StatusNotFound {
248248
return nil, nil
249249
}
250-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get stored script: %s", id)); diags.HasError() {
250+
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to get stored script: %s", id)); diags.HasError() {
251251
return nil, diags
252252
}
253253
var scriptResponse struct {
@@ -279,7 +279,7 @@ func PutScript(ctx context.Context, apiClient *clients.ApiClient, script *models
279279
return diag.FromErr(err)
280280
}
281281
defer res.Body.Close()
282-
if diags := utils.CheckError(res, "Unable to put stored script"); diags.HasError() {
282+
if diags := diagutil.CheckError(res, "Unable to put stored script"); diags.HasError() {
283283
return diags
284284
}
285285
return nil
@@ -295,7 +295,7 @@ func DeleteScript(ctx context.Context, apiClient *clients.ApiClient, id string)
295295
return diag.FromErr(err)
296296
}
297297
defer res.Body.Close()
298-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to delete script: %s", id)); diags.HasError() {
298+
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to delete script: %s", id)); diags.HasError() {
299299
return diags
300300
}
301301
return nil

internal/clients/elasticsearch/enrich.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"net/http"
99

1010
"github.com/elastic/terraform-provider-elasticstack/internal/clients"
11+
"github.com/elastic/terraform-provider-elasticstack/internal/diagutil"
1112
"github.com/elastic/terraform-provider-elasticstack/internal/models"
12-
"github.com/elastic/terraform-provider-elasticstack/internal/utils"
1313
"github.com/hashicorp/terraform-plugin-log/tflog"
1414
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1515
)
@@ -54,7 +54,7 @@ func GetEnrichPolicy(ctx context.Context, apiClient *clients.ApiClient, policyNa
5454
if res.StatusCode == http.StatusNotFound {
5555
return nil, nil
5656
}
57-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to get requested EnrichPolicy: %s", policyName)); diags.HasError() {
57+
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to get requested EnrichPolicy: %s", policyName)); diags.HasError() {
5858
return nil, diags
5959
}
6060

@@ -131,7 +131,7 @@ func PutEnrichPolicy(ctx context.Context, apiClient *clients.ApiClient, policy *
131131
}
132132
defer res.Body.Close()
133133

134-
if diags := utils.CheckError(res, "Unable to create enrich policy"); diags.HasError() {
134+
if diags := diagutil.CheckError(res, "Unable to create enrich policy"); diags.HasError() {
135135
return diags
136136
}
137137
return diags
@@ -149,7 +149,7 @@ func DeleteEnrichPolicy(ctx context.Context, apiClient *clients.ApiClient, polic
149149
return diag.FromErr(err)
150150
}
151151
defer res.Body.Close()
152-
if diags := utils.CheckError(res, fmt.Sprintf("Unable to delete enrich policy: %s", policyName)); diags.HasError() {
152+
if diags := diagutil.CheckError(res, fmt.Sprintf("Unable to delete enrich policy: %s", policyName)); diags.HasError() {
153153
return diags
154154
}
155155

0 commit comments

Comments
 (0)