Skip to content

Commit

Permalink
chore: remove unused code and resolve linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Jun 14, 2024
1 parent f0b09f5 commit 0cbec10
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 169 deletions.
135 changes: 0 additions & 135 deletions internal/provider/humanitec_data.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 0 additions & 1 deletion internal/provider/resource_application_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var _ resource.ResourceWithImportState = &ResourceApplicationUser{}

var (
defaultApplicationUserCreateTimeout = 30 * time.Second
defaultApplicationUserReadTimeout = 30 * time.Second
)

func NewResourceApplicationUser() resource.Resource {
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/resource_definition_criteria_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
37 changes: 8 additions & 29 deletions internal/provider/resource_definition_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down
1 change: 0 additions & 1 deletion internal/provider/resource_environment_type_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ var _ resource.ResourceWithImportState = &ResourceEnvironmentTypeUser{}

var (
defaultEnvironmentTypeUserCreateTimeout = 30 * time.Second
defaultEnvironmentTypeUserReadTimeout = 30 * time.Second
)

func NewResourceEnvironmentTypeUser() resource.Resource {
Expand Down
2 changes: 0 additions & 2 deletions internal/provider/resource_pipeline_criteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}

0 comments on commit 0cbec10

Please sign in to comment.