From 0cbec105f528305d2749204c8eb71c2242df3989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20W=C3=BCrbach?= Date: Fri, 14 Jun 2024 09:23:36 +0200 Subject: [PATCH] chore: remove unused code and resolve linter warnings --- internal/provider/humanitec_data.go | 135 ------------------ .../provider/resource_application_user.go | 1 - .../resource_definition_criteria_resource.go | 2 +- .../provider/resource_definition_resource.go | 37 ++--- .../resource_environment_type_user.go | 1 - .../provider/resource_pipeline_criteria.go | 2 - 6 files changed, 9 insertions(+), 169 deletions(-) diff --git a/internal/provider/humanitec_data.go b/internal/provider/humanitec_data.go index bcce2c9..c665319 100644 --- a/internal/provider/humanitec_data.go +++ b/internal/provider/humanitec_data.go @@ -1,145 +1,10 @@ package provider import ( - "context" - "fmt" - "sync" - - "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/humanitec/humanitec-go-autogen" - "github.com/humanitec/humanitec-go-autogen/client" ) type HumanitecData struct { Client *humanitec.Client OrgID string - - fetchDriversMu sync.Mutex - driversByType map[string]*client.DriverDefinitionResponse - - fetchTypesMu sync.Mutex - typesByType map[string]*client.ResourceTypeResponse -} - -func (h *HumanitecData) fetchResourceDrivers(ctx context.Context) (map[string]*client.DriverDefinitionResponse, diag.Diagnostics) { - var diags diag.Diagnostics - - h.fetchDriversMu.Lock() - defer h.fetchDriversMu.Unlock() - - if h.driversByType != nil { - return h.driversByType, diags - } - - httpResp, err := h.Client.ListResourceDriversWithResponse(ctx, h.OrgID) - if err != nil { - diags.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to get resource drivers, got error: %s", err)) - return nil, diags - } - - if httpResp.StatusCode() != 200 { - diags.AddError(HUM_API_ERR, fmt.Sprintf("Unable to get resource drivers, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body)) - return nil, diags - } - - if httpResp.JSON200 == nil { - diags.AddError(HUM_API_ERR, fmt.Sprintf("Unable to get resource drivers, missing body, body: %s", httpResp.Body)) - return nil, diags - } - - driversByType := map[string]*client.DriverDefinitionResponse{} - for _, d := range *httpResp.JSON200 { - d := d - driversByType[fmt.Sprintf("%s/%s", d.OrgId, d.Id)] = &d - } - - h.driversByType = driversByType - - return driversByType, diags -} - -func (h *HumanitecData) fetchResourceTypes(ctx context.Context) (map[string]*client.ResourceTypeResponse, diag.Diagnostics) { - var diags diag.Diagnostics - - h.fetchTypesMu.Lock() - defer h.fetchTypesMu.Unlock() - - if h.typesByType != nil { - return h.typesByType, diags - } - - httpResp, err := h.Client.ListResourceTypesWithResponse(ctx, h.OrgID) - if err != nil { - diags.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to get resource types, got error: %s", err)) - return nil, diags - } - - if httpResp.StatusCode() != 200 { - diags.AddError(HUM_API_ERR, fmt.Sprintf("Unable to get resource types, unexpected status code: %d, body: %s", httpResp.StatusCode(), httpResp.Body)) - return nil, diags - } - - if httpResp.JSON200 == nil { - diags.AddError(HUM_API_ERR, fmt.Sprintf("Unable to get resource types, missing body, body: %s", httpResp.Body)) - return nil, diags - } - - typesByType := map[string]*client.ResourceTypeResponse{} - for _, d := range *httpResp.JSON200 { - d := d - typesByType[d.Type] = &d - } - - h.typesByType = typesByType - - return typesByType, diags -} - -func (h *HumanitecData) driverByDriverType(ctx context.Context, driverType string) (*client.DriverDefinitionResponse, diag.Diagnostics) { - driversByType, diags := h.fetchResourceDrivers(ctx) - if diags.HasError() { - return nil, diags - } - - driver, ok := driversByType[driverType] - if !ok { - diags.AddError(HUM_INPUT_ERR, fmt.Sprintf("No resource driver found for type: %s", driverType)) - return nil, diags - } - - return driver, diags -} - -func (h *HumanitecData) resourceByType(ctx context.Context, resourceType string) (*client.ResourceTypeResponse, diag.Diagnostics) { - resourcesByType, diags := h.fetchResourceTypes(ctx) - if diags.HasError() { - return nil, diags - } - - resource, ok := resourcesByType[resourceType] - if !ok { - diags.AddError(HUM_INPUT_ERR, fmt.Sprintf("No resource type found for type: %s", resourceType)) - return nil, diags - } - - return resource, diags -} - -func (h *HumanitecData) DriverInputSchemaByDriverTypeOrType(ctx context.Context, driverType, resourceType string) (map[string]interface{}, diag.Diagnostics) { - // The static driver has no input schema and matches the output schema of the resource type - if driverType == "humanitec/static" { - resource, diags := h.resourceByType(ctx, resourceType) - if diags.HasError() { - return nil, diags - } - - return resource.OutputsSchema, diags - } - - driver, diags := h.driverByDriverType(ctx, driverType) - if diags.HasError() { - return nil, diags - } - - return driver.InputsSchema, diags } diff --git a/internal/provider/resource_application_user.go b/internal/provider/resource_application_user.go index 463d0b2..c9ac874 100644 --- a/internal/provider/resource_application_user.go +++ b/internal/provider/resource_application_user.go @@ -25,7 +25,6 @@ var _ resource.ResourceWithImportState = &ResourceApplicationUser{} var ( defaultApplicationUserCreateTimeout = 30 * time.Second - defaultApplicationUserReadTimeout = 30 * time.Second ) func NewResourceApplicationUser() resource.Resource { diff --git a/internal/provider/resource_definition_criteria_resource.go b/internal/provider/resource_definition_criteria_resource.go index 432682d..1178596 100644 --- a/internal/provider/resource_definition_criteria_resource.go +++ b/internal/provider/resource_definition_criteria_resource.go @@ -204,7 +204,7 @@ func (r *ResourceDefinitionCriteriaResource) Read(ctx context.Context, req resou return } - httpResp, err := r.client().GetResourceDefinitionWithResponse(ctx, r.orgId(), data.ResourceDefinitionID.ValueString(), &client.GetResourceDefinitionParams{toPtr(false)}) + httpResp, err := r.client().GetResourceDefinitionWithResponse(ctx, r.orgId(), data.ResourceDefinitionID.ValueString(), &client.GetResourceDefinitionParams{Deleted: toPtr(false)}) if err != nil { resp.Diagnostics.AddError(HUM_CLIENT_ERR, fmt.Sprintf("Unable to read resource definition, got error: %s", err)) return diff --git a/internal/provider/resource_definition_resource.go b/internal/provider/resource_definition_resource.go index 41cffab..2b2f550 100644 --- a/internal/provider/resource_definition_resource.go +++ b/internal/provider/resource_definition_resource.go @@ -227,7 +227,7 @@ func defaultFalseBoolValuePointer(b *bool) types.Bool { return types.BoolValue(*b) } -func parseResourceDefinitionResponse(ctx context.Context, driverInputSchema map[string]interface{}, res *client.ResourceDefinitionResponse, data *DefinitionResourceModel) diag.Diagnostics { +func parseResourceDefinitionResponse(res *client.ResourceDefinitionResponse, data *DefinitionResourceModel) diag.Diagnostics { var diags diag.Diagnostics data.ID = types.StringValue(res.Id) @@ -386,7 +386,7 @@ func provisionFromModel(data *map[string]DefinitionResourceProvisionModel) *map[ return &provision } -func driverInputsFromModel(ctx context.Context, inputSchema map[string]interface{}, data *DefinitionResourceModel) (*client.ValuesSecretsRefsRequest, diag.Diagnostics) { +func driverInputsFromModel(data *DefinitionResourceModel) (*client.ValuesSecretsRefsRequest, diag.Diagnostics) { if data.DriverInputs == nil { return nil, nil } @@ -443,14 +443,7 @@ func (r *ResourceDefinitionResource) Create(ctx context.Context, req resource.Cr } provision := provisionFromModel(data.Provision) - driverType := data.DriverType.ValueString() - driverInputSchema, diag := r.data.DriverInputSchemaByDriverTypeOrType(ctx, driverType, data.Type.ValueString()) - resp.Diagnostics.Append(diag...) - if resp.Diagnostics.HasError() { - return - } - - driverInputs, diag := driverInputsFromModel(ctx, driverInputSchema, data) + driverInputs, diag := driverInputsFromModel(data) resp.Diagnostics.Append(diag...) if resp.Diagnostics.HasError() { return @@ -475,7 +468,7 @@ func (r *ResourceDefinitionResource) Create(ctx context.Context, req resource.Cr return } - resp.Diagnostics.Append(parseResourceDefinitionResponse(ctx, driverInputSchema, httpResp.JSON200, data)...) + resp.Diagnostics.Append(parseResourceDefinitionResponse(httpResp.JSON200, data)...) if resp.Diagnostics.HasError() { return } @@ -511,13 +504,7 @@ func (r *ResourceDefinitionResource) Read(ctx context.Context, req resource.Read return } - driverInputSchema, diag := r.data.DriverInputSchemaByDriverTypeOrType(ctx, httpResp.JSON200.DriverType, httpResp.JSON200.Type) - resp.Diagnostics.Append(diag...) - if resp.Diagnostics.HasError() { - return - } - - resp.Diagnostics.Append(parseResourceDefinitionResponse(ctx, driverInputSchema, httpResp.JSON200, data)...) + resp.Diagnostics.Append(parseResourceDefinitionResponse(httpResp.JSON200, data)...) if resp.Diagnostics.HasError() { return } @@ -537,15 +524,7 @@ func (r *ResourceDefinitionResource) Update(ctx context.Context, req resource.Up return } - name := data.Name.ValueString() - driverType := data.DriverType.ValueString() - driverInputSchema, diag := r.data.DriverInputSchemaByDriverTypeOrType(ctx, driverType, data.Type.ValueString()) - resp.Diagnostics.Append(diag...) - if resp.Diagnostics.HasError() { - return - } - - driverInputs, diag := driverInputsFromModel(ctx, driverInputSchema, data) + driverInputs, diag := driverInputsFromModel(data) resp.Diagnostics.Append(diag...) if resp.Diagnostics.HasError() { return @@ -559,7 +538,7 @@ func (r *ResourceDefinitionResource) Update(ctx context.Context, req resource.Up DriverType: data.DriverType.ValueStringPointer(), DriverAccount: data.DriverAccount.ValueStringPointer(), DriverInputs: driverInputs, - Name: name, + Name: data.Name.ValueString(), Provision: provision, }) if err != nil { @@ -572,7 +551,7 @@ func (r *ResourceDefinitionResource) Update(ctx context.Context, req resource.Up return } - resp.Diagnostics.Append(parseResourceDefinitionResponse(ctx, driverInputSchema, httpResp.JSON200, data)...) + resp.Diagnostics.Append(parseResourceDefinitionResponse(httpResp.JSON200, data)...) if resp.Diagnostics.HasError() { return } diff --git a/internal/provider/resource_environment_type_user.go b/internal/provider/resource_environment_type_user.go index 6ecdbbd..68986fd 100644 --- a/internal/provider/resource_environment_type_user.go +++ b/internal/provider/resource_environment_type_user.go @@ -25,7 +25,6 @@ var _ resource.ResourceWithImportState = &ResourceEnvironmentTypeUser{} var ( defaultEnvironmentTypeUserCreateTimeout = 30 * time.Second - defaultEnvironmentTypeUserReadTimeout = 30 * time.Second ) func NewResourceEnvironmentTypeUser() resource.Resource { diff --git a/internal/provider/resource_pipeline_criteria.go b/internal/provider/resource_pipeline_criteria.go index 6cb9ee2..71c3b7e 100644 --- a/internal/provider/resource_pipeline_criteria.go +++ b/internal/provider/resource_pipeline_criteria.go @@ -236,7 +236,6 @@ func (r *ResourcePipelineCriteria) Read(ctx context.Context, req resource.ReadRe func (r *ResourcePipelineCriteria) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { // you can't update criteria in place, all updates should be done with a replacement resp.Diagnostics.AddError(HUM_CLIENT_ERR, "Unable to update pipeline criteria") - return } func (r *ResourcePipelineCriteria) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { @@ -275,5 +274,4 @@ func (r *ResourcePipelineCriteria) ImportState(ctx context.Context, req resource resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("app_id"), appId)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("pipeline_id"), pipelineId)...) resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), criteriaId)...) - return }