|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/patientsknowbest/terraform-provider-aidbox/aidbox" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceStructureDefinitionOverride() *schema.Resource { |
| 14 | + // Note there's no import functionality here. Since the initial create has to read the original spec, it's impossible |
| 15 | + // to import that as that would be already overwritten. This would be probably possible if we asked the user |
| 16 | + // to store the core SD in an attribute, but that adds extra complexity and handling. |
| 17 | + return &schema.Resource{ |
| 18 | + Description: "A specialization of StructureDefinition which allows you to override the default version of" + |
| 19 | + " StructureDefinitions that are specified inside the core FHIR IG used on the server. This means default " + |
| 20 | + "rules of resources can be changed without having the client specify a meta.profile in their request.", |
| 21 | + CreateContext: resourceStructureDefinitionOverrideCreate, |
| 22 | + ReadContext: resourceStructureDefinitionOverrideRead, |
| 23 | + UpdateContext: resourceStructureDefinitionOverrideUpdate, |
| 24 | + DeleteContext: resourceStructureDefinitionOverrideDelete, |
| 25 | + Schema: resourceFullSchema(resourceSchemaStructureDefinitionOverride()), |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +func resourceSchemaStructureDefinitionOverride() map[string]*schema.Schema { |
| 30 | + return map[string]*schema.Schema{ |
| 31 | + "url": { |
| 32 | + Description: "Canonical URL that's unique to this StructureDefinition", |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + // url is essentially the logical id, hence it's impossible to update it |
| 36 | + // if you change it, what you mean is: destroy the changed override, and add a new one under this url |
| 37 | + ForceNew: true, |
| 38 | + }, |
| 39 | + "structure_definition_override": { |
| 40 | + Description: "A customized StructureDefinition, based on the original one from the core FHIR spec", |
| 41 | + Type: schema.TypeString, |
| 42 | + Required: true, |
| 43 | + DiffSuppressOnRefresh: true, |
| 44 | + DiffSuppressFunc: jsonDiffSuppressFunc, |
| 45 | + }, |
| 46 | + "original_structure_definition": { |
| 47 | + Description: "Backup of the original StructureDefinition, which will be restored upon deleting the override", |
| 48 | + Type: schema.TypeString, |
| 49 | + Computed: true, |
| 50 | + Sensitive: true, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func resourceStructureDefinitionOverrideCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 56 | + apiClient := meta.(*aidbox.ApiClient) |
| 57 | + |
| 58 | + // look up the original, FHIR spec StructureDefinition by the canonical URL |
| 59 | + // this is required to create a backup in tf state so we can restore it if we want to delete the |
| 60 | + // customized one - we must never delete the original as it would break FHIR functionality |
| 61 | + canonicalUrl := d.Get("url").(string) |
| 62 | + |
| 63 | + originalSD, err := apiClient.GetStructureDefinitionByUrl(ctx, canonicalUrl) |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + return diag.FromErr(err) |
| 67 | + } |
| 68 | + |
| 69 | + originalSDBytes, err := json.Marshal(originalSD) |
| 70 | + d.Set("original_structure_definition", string(originalSDBytes)) |
| 71 | + |
| 72 | + overrideSDString := d.Get("structure_definition_override").(string) |
| 73 | + |
| 74 | + overrideSD := map[string]interface{}{} |
| 75 | + err = json.Unmarshal([]byte(overrideSDString), &overrideSD) |
| 76 | + if err != nil { |
| 77 | + return diag.FromErr(err) |
| 78 | + } |
| 79 | + |
| 80 | + // now update the SD to our customized version |
| 81 | + updatedSD, err := apiClient.UpdateStructureDefinitionByUrl(ctx, &overrideSD, canonicalUrl) |
| 82 | + if err != nil { |
| 83 | + return diag.FromErr(err) |
| 84 | + } |
| 85 | + var updatedUrl = (*updatedSD)["url"].(string) |
| 86 | + if updatedUrl != canonicalUrl { |
| 87 | + return diag.FromErr(fmt.Errorf("canonical url of resource unexpectedly changed after update, %s was set on the resource but server responded with %s", canonicalUrl, updatedUrl)) |
| 88 | + } |
| 89 | + d.Set("url", updatedUrl) |
| 90 | + // throw away the id we didn't know upfront, it just adds unnecessary complexity here when comparing states |
| 91 | + delete(*updatedSD, "id") |
| 92 | + // terraform mandates that we have an id though, so use the url for that |
| 93 | + d.SetId(updatedUrl) |
| 94 | + |
| 95 | + updatedSDBytes, err := json.Marshal(updatedSD) |
| 96 | + updatedSDString := string(updatedSDBytes) |
| 97 | + if err != nil { |
| 98 | + return diag.FromErr(err) |
| 99 | + } |
| 100 | + d.Set("structure_definition_override", updatedSDString) |
| 101 | + |
| 102 | + return nil |
| 103 | +} |
| 104 | + |
| 105 | +func resourceStructureDefinitionOverrideRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 106 | + apiClient := meta.(*aidbox.ApiClient) |
| 107 | + |
| 108 | + canonicalUrl := d.Get("url").(string) |
| 109 | + overrideSD, err := apiClient.GetStructureDefinitionByUrl(ctx, canonicalUrl) |
| 110 | + if err != nil { |
| 111 | + if handleNotFoundError(err, d) { |
| 112 | + return nil |
| 113 | + } |
| 114 | + return diag.FromErr(err) |
| 115 | + } |
| 116 | + |
| 117 | + var overrideSDUrl = (*overrideSD)["url"].(string) |
| 118 | + if overrideSDUrl != canonicalUrl { |
| 119 | + return diag.FromErr(fmt.Errorf("canonical url of resource unexpectedly changed during state refresh, %s was set on the resource but server responded with %s", canonicalUrl, overrideSDUrl)) |
| 120 | + } |
| 121 | + d.Set("url", overrideSDUrl) |
| 122 | + // throw away the id we didn't know upfront, it just adds unnecessary complexity here when comparing states |
| 123 | + delete(*overrideSD, "id") |
| 124 | + // terraform mandates that we have an id though, so use the url for that |
| 125 | + d.SetId(overrideSDUrl) |
| 126 | + |
| 127 | + overrideSDBytes, err := json.Marshal(overrideSD) |
| 128 | + overrideSDString := string(overrideSDBytes) |
| 129 | + if err != nil { |
| 130 | + return diag.FromErr(err) |
| 131 | + } |
| 132 | + d.Set("structure_definition_override", overrideSDString) |
| 133 | + |
| 134 | + return nil |
| 135 | +} |
| 136 | + |
| 137 | +func resourceStructureDefinitionOverrideUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 138 | + apiClient := meta.(*aidbox.ApiClient) |
| 139 | + |
| 140 | + canonicalUrl := d.Get("url").(string) |
| 141 | + overrideSDString := d.Get("structure_definition_override").(string) |
| 142 | + |
| 143 | + overrideSD := map[string]interface{}{} |
| 144 | + err := json.Unmarshal([]byte(overrideSDString), &overrideSD) |
| 145 | + if err != nil { |
| 146 | + return diag.FromErr(err) |
| 147 | + } |
| 148 | + |
| 149 | + // update the SD to our new customized version |
| 150 | + updatedSD, err := apiClient.UpdateStructureDefinitionByUrl(ctx, &overrideSD, canonicalUrl) |
| 151 | + if err != nil { |
| 152 | + return diag.FromErr(err) |
| 153 | + } |
| 154 | + // throw away the id we didn't know upfront, it just adds unnecessary complexity here when comparing states |
| 155 | + delete(*updatedSD, "id") |
| 156 | + |
| 157 | + updatedSDBytes, err := json.Marshal(updatedSD) |
| 158 | + if err != nil { |
| 159 | + return diag.FromErr(err) |
| 160 | + } |
| 161 | + d.Set("structure_definition_override", string(updatedSDBytes)) |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func resourceStructureDefinitionOverrideDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 167 | + apiClient := meta.(*aidbox.ApiClient) |
| 168 | + |
| 169 | + // no such thing as deleting from the core fhir spec, this just means we restore the original spec |
| 170 | + canonicalUrl := d.Get("url").(string) |
| 171 | + originalSDString := d.Get("original_structure_definition").(string) |
| 172 | + |
| 173 | + originalSD := map[string]interface{}{} |
| 174 | + err := json.Unmarshal([]byte(originalSDString), &originalSD) |
| 175 | + if err != nil { |
| 176 | + return diag.FromErr(err) |
| 177 | + } |
| 178 | + |
| 179 | + // restore the SD to the spec version |
| 180 | + if _, err := apiClient.UpdateStructureDefinitionByUrl(ctx, &originalSD, canonicalUrl); err != nil { |
| 181 | + return diag.FromErr(err) |
| 182 | + } |
| 183 | + |
| 184 | + return nil |
| 185 | +} |
0 commit comments