Skip to content

Commit

Permalink
feat: Adding substitions input to replace values on files
Browse files Browse the repository at this point in the history
  • Loading branch information
reiinoldo committed Sep 19, 2024
1 parent 270c0b7 commit 028dbd3
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
2 changes: 1 addition & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ The document generation tool looks for files in the following locations by defau

* **provider/provider.tf** example file for the provider index page
* **data-sources/`full data source name`/data-source.tf** example file for the named data source page
* **resources/`full resource name`/resource.tf** example file for the named data source page
* **resources/`full resource name`/resource.tf** examples files for the named data source page
8 changes: 8 additions & 0 deletions examples/resources/substitution_example/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
resource "fhirrest_fhir_resource" "patient" {
file_path = "${path.cwd}/patient.json"
file_sha256 = sha256(file("${path.cwd}/patient.json"))
substitutions = {
"name" = "John Doe",
"TENANT_PLACEHOLDER" = "my-tenant",
}
}
68 changes: 61 additions & 7 deletions internal/provider/fhir_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
)

Expand All @@ -40,13 +41,15 @@ type FhirResource struct {
type FhirResourceSettings struct {
FhirResourceFilePath string
FhirBaseUrl *string
Substitutions map[string]string
}

type FhirResourceModel struct {
// from model
FilePath types.String `tfsdk:"file_path"`
FileSha256 types.String `tfsdk:"file_sha256"`
FhirBaseUrl types.String `tfsdk:"fhir_base_url"`
FilePath types.String `tfsdk:"file_path"`
FileSha256 types.String `tfsdk:"file_sha256"`
FhirBaseUrl types.String `tfsdk:"fhir_base_url"`
Substitutions types.Map `tfsdk:"substitutions"`

//actual state
ResourceId types.String `tfsdk:"resource_id"`
Expand Down Expand Up @@ -83,6 +86,33 @@ func (r *FhirResource) Schema(ctx context.Context, req resource.SchemaRequest, r
MarkdownDescription: "The sha256 of the response of the fhir server.",
Computed: true,
},
"substitutions": schema.MapAttribute{
ElementType: basetypes.StringType{},
MarkdownDescription: `A map of substitutions to be applied to the file content before sending it to the server.
The key is the string to be replaced, and the value is the string to replace it with.
### Example:
Given a FHIR resource file with content:
{
"resourceType": "Questionnaire",
"url": "https://system.com/R4/Questionnaire/{{Tenant.GUID}}/DiagnosticTests"
}
And the following substitutions:
substitutions = {
"{{Tenant.GUID}}" = "12345"
}
The final content sent to the server will be:
{
"resourceType": "Questionnaire",
"url": "https://system.com/R4/Questionnaire/12345/DiagnosticTests"
}`,
Optional: true,
},
},
}
}
Expand Down Expand Up @@ -112,7 +142,7 @@ func (r *FhirResource) Create(ctx context.Context, req resource.CreateRequest, r
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)

r.fhirResourceSettings = FhirResourceSettings{FhirResourceFilePath: data.FilePath.ValueString(), FhirBaseUrl: data.FhirBaseUrl.ValueStringPointer()}
r.fhirResourceSettings = NewFhirResourceSettings(data, ctx)

if resp.Diagnostics.HasError() {
return
Expand All @@ -139,6 +169,8 @@ func persistFhirResource(ctx context.Context, fhirResource *FhirResource, resour
return nil, nil, nil
}

fileContent = replaceValues(fileContent, fhirResource.fhirResourceSettings.Substitutions)

var fileContentJson map[string]interface{}
if err := json.Unmarshal(fileContent, &fileContentJson); err != nil {
diag.AddError(fmt.Sprintf("failed to unmarshal JSON file %s", fhirResource.fhirResourceSettings.FhirResourceFilePath), err.Error())
Expand Down Expand Up @@ -223,7 +255,7 @@ func (r *FhirResource) Read(ctx context.Context, req resource.ReadRequest, resp
return
}

r.fhirResourceSettings = FhirResourceSettings{FhirResourceFilePath: data.FilePath.ValueString(), FhirBaseUrl: data.FhirBaseUrl.ValueStringPointer()}
r.fhirResourceSettings = NewFhirResourceSettings(data, ctx)

body, shouldReturn := ReadFhirResource(r.providerSettings, r.fhirResourceSettings.FhirBaseUrl, data.ResourceId.ValueString(), &resp.Diagnostics)
if shouldReturn {
Expand Down Expand Up @@ -262,7 +294,7 @@ func (r *FhirResource) Update(ctx context.Context, req resource.UpdateRequest, r
return
}

r.fhirResourceSettings = FhirResourceSettings{FhirResourceFilePath: data.FilePath.ValueString(), FhirBaseUrl: data.FhirBaseUrl.ValueStringPointer()}
r.fhirResourceSettings = NewFhirResourceSettings(data, ctx)

body, responseJson, resourceType := persistFhirResource(ctx, r, state.ResourceId.ValueStringPointer(), &resp.Diagnostics)
if responseJson == nil {
Expand All @@ -277,6 +309,7 @@ func (r *FhirResource) Update(ctx context.Context, req resource.UpdateRequest, r
state.ResponseSha256 = types.StringValue(hashString)
state.FilePath = data.FilePath
state.FileSha256 = data.FileSha256
state.Substitutions = data.Substitutions

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
Expand All @@ -292,7 +325,7 @@ func (r *FhirResource) Delete(ctx context.Context, req resource.DeleteRequest, r
return
}

r.fhirResourceSettings = FhirResourceSettings{FhirResourceFilePath: data.FilePath.ValueString(), FhirBaseUrl: data.FhirBaseUrl.ValueStringPointer()}
r.fhirResourceSettings = NewFhirResourceSettings(data, ctx)

baseUrl := r.providerSettings.FhirBaseUrl
if r.fhirResourceSettings.FhirBaseUrl != nil {
Expand Down Expand Up @@ -326,3 +359,24 @@ func (r *FhirResource) Delete(ctx context.Context, req resource.DeleteRequest, r
func (r *FhirResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("resource_id"), req, resp)
}

func NewFhirResourceSettings(data FhirResourceModel, ctx context.Context) FhirResourceSettings {
substitutions := make(map[string]string)
data.Substitutions.ElementsAs(ctx, &substitutions, true)

return FhirResourceSettings{
FhirResourceFilePath: data.FilePath.ValueString(),
FhirBaseUrl: data.FhirBaseUrl.ValueStringPointer(),
Substitutions: substitutions,
}
}

func replaceValues(content []byte, substitutions map[string]string) []byte {
contentStr := string(content)

for key, value := range substitutions {
contentStr = strings.ReplaceAll(contentStr, key, value)
}

return []byte(contentStr)
}

0 comments on commit 028dbd3

Please sign in to comment.