Skip to content

Commit

Permalink
Merge pull request #94 from humanitec/resdef-secret-refs
Browse files Browse the repository at this point in the history
fix: res def secret refs with null values
  • Loading branch information
johanneswuerbach authored Jun 14, 2024
2 parents f0e550f + 0cbec10 commit 03a4a11
Show file tree
Hide file tree
Showing 10 changed files with 529 additions and 213 deletions.
4 changes: 2 additions & 2 deletions docs/resources/resource_definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ resource "humanitec_resource_definition" "azure-blob" {

Optional:

- `secret_refs` (String) JSON encoded secrets section of the data set. They can hold sensitive information that will be stored in the primary organization secret store and replaced with the secret store paths when sent outside, or secret references stored in a defined secret store. Can't be used together with secrets.
- `secrets_string` (String) JSON encoded secret data set. Passed around as-is. Can't be used together with secret_refs.
- `secret_refs` (String, Sensitive) JSON encoded secrets section of the data set. They can hold sensitive information that will be stored in the primary organization secret store and replaced with the secret store paths when sent outside, or secret references stored in a defined secret store. Can't be used together with secrets.
- `secrets_string` (String, Sensitive) JSON encoded secret data set. Passed around as-is. Can't be used together with secret_refs.
- `values_string` (String) JSON encoded input data set. Passed around as-is.


Expand Down
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
Loading

0 comments on commit 03a4a11

Please sign in to comment.