|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/patientsknowbest/terraform-provider-aidbox/aidbox" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceSearch() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Description: "Search https://docs.aidbox.app/api/rest-api/aidbox-search#search-resource", |
| 14 | + CreateContext: resourceSearchCreate, |
| 15 | + ReadContext: resourceSearchRead, |
| 16 | + UpdateContext: resourceSearchUpdate, |
| 17 | + DeleteContext: resourceSearchDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + StateContext: resourceSearchImport, |
| 20 | + }, |
| 21 | + Schema: resourceFullSchema(resourceSchemaSearch()), |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func resourceSchemaSearch() map[string]*schema.Schema { |
| 26 | + return map[string]*schema.Schema{ |
| 27 | + "name": { |
| 28 | + Description: "Name of search, used in search query string", |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + }, |
| 32 | + "module": { |
| 33 | + Description: "Module name", |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + }, |
| 37 | + "where": { |
| 38 | + Description: "SQL of search", |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + }, |
| 42 | + "reference": { |
| 43 | + Description: "Reference to resource this search param attached to", |
| 44 | + // not a TypeMap because "using the Elem block to define specific keys for the map is currently not possible" |
| 45 | + Type: schema.TypeList, |
| 46 | + Required: true, |
| 47 | + MinItems: 1, |
| 48 | + MaxItems: 1, |
| 49 | + Elem: &schema.Resource{ |
| 50 | + Schema: map[string]*schema.Schema{ |
| 51 | + "resource_id": { |
| 52 | + Description: "The ID of the referenced resource", |
| 53 | + Type: schema.TypeString, |
| 54 | + Required: true, |
| 55 | + }, |
| 56 | + "resource_type": { |
| 57 | + Description: "The type of the referenced resource", |
| 58 | + Type: schema.TypeString, |
| 59 | + Required: true, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func mapSearchFromData(data *schema.ResourceData) (*aidbox.Search, error) { |
| 68 | + res := &aidbox.Search{} |
| 69 | + res.Name = data.Get("name").(string) |
| 70 | + res.Module = data.Get("module").(string) |
| 71 | + res.Where = data.Get("where").(string) |
| 72 | + |
| 73 | + // resource |
| 74 | + ref := data.Get("reference").([]interface{})[0].(map[string]interface{}) |
| 75 | + r := aidbox.Reference{ |
| 76 | + ResourceId: ref["resource_id"].(string), |
| 77 | + ResourceType: ref["resource_type"].(string), |
| 78 | + } |
| 79 | + res.Resource = r |
| 80 | + |
| 81 | + res.ID = res.Resource.ResourceId + "." + res.Name |
| 82 | + |
| 83 | + return res, nil |
| 84 | +} |
| 85 | + |
| 86 | +func mapSearchToData(res *aidbox.Search, data *schema.ResourceData) { |
| 87 | + data.SetId(res.ID) |
| 88 | + data.Set("name", res.Name) |
| 89 | + data.Set("module", res.Module) |
| 90 | + data.Set("where", res.Where) |
| 91 | + |
| 92 | + // resource |
| 93 | + var ref []interface{} |
| 94 | + r := map[string]string{ |
| 95 | + "resource_id": res.Resource.ResourceId, |
| 96 | + "resource_type": res.Resource.ResourceType, |
| 97 | + } |
| 98 | + data.Set("reference", append(ref, r)) |
| 99 | +} |
| 100 | + |
| 101 | +func resourceSearchCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 102 | + apiClient := meta.(*aidbox.ApiClient) |
| 103 | + q, err := mapSearchFromData(d) |
| 104 | + if err != nil { |
| 105 | + return diag.FromErr(err) |
| 106 | + } |
| 107 | + res, err := apiClient.CreateSearch(ctx, q) |
| 108 | + if err != nil { |
| 109 | + return diag.FromErr(err) |
| 110 | + } |
| 111 | + mapSearchToData(res, d) |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func resourceSearchRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 116 | + apiClient := meta.(*aidbox.ApiClient) |
| 117 | + res, err := apiClient.GetSearch(ctx, d.Id()) |
| 118 | + if err != nil { |
| 119 | + if handleNotFoundError(err, d) { |
| 120 | + return nil |
| 121 | + } |
| 122 | + return diag.FromErr(err) |
| 123 | + } |
| 124 | + mapSearchToData(res, d) |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func resourceSearchUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 129 | + apiClient := meta.(*aidbox.ApiClient) |
| 130 | + q, err := mapSearchFromData(d) |
| 131 | + if err != nil { |
| 132 | + return diag.FromErr(err) |
| 133 | + } |
| 134 | + ac, err := apiClient.UpdateSearch(ctx, q) |
| 135 | + if err != nil { |
| 136 | + return diag.FromErr(err) |
| 137 | + } |
| 138 | + mapSearchToData(ac, d) |
| 139 | + return nil |
| 140 | +} |
| 141 | + |
| 142 | +func resourceSearchDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 143 | + apiClient := meta.(*aidbox.ApiClient) |
| 144 | + err := apiClient.DeleteSearch(ctx, d.Id()) |
| 145 | + if err != nil { |
| 146 | + return diag.FromErr(err) |
| 147 | + } |
| 148 | + return nil |
| 149 | +} |
| 150 | + |
| 151 | +func resourceSearchImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 152 | + apiClient := meta.(*aidbox.ApiClient) |
| 153 | + res, err := apiClient.GetSearch(ctx, d.Id()) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + mapSearchToData(res, d) |
| 158 | + return []*schema.ResourceData{d}, nil |
| 159 | +} |
0 commit comments