|
| 1 | +package ingest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/elastic/terraform-provider-elasticstack/internal/models" |
| 9 | + "github.com/elastic/terraform-provider-elasticstack/internal/utils" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 13 | +) |
| 14 | + |
| 15 | +func DataSourceProcessorReroute() *schema.Resource { |
| 16 | + processorSchema := map[string]*schema.Schema{ |
| 17 | + "id": { |
| 18 | + Description: "Internal identifier of the resource.", |
| 19 | + Type: schema.TypeString, |
| 20 | + Computed: true, |
| 21 | + }, |
| 22 | + "destination": { |
| 23 | + Description: "The destination data stream, index, or index alias to route the document to.", |
| 24 | + Type: schema.TypeString, |
| 25 | + Optional: true, |
| 26 | + }, |
| 27 | + "dataset": { |
| 28 | + Description: "The destination dataset to route the document to.", |
| 29 | + Type: schema.TypeString, |
| 30 | + Optional: true, |
| 31 | + }, |
| 32 | + "namespace": { |
| 33 | + Description: "The destination namespace to route the document to.", |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + }, |
| 37 | + "description": { |
| 38 | + Description: "Description of the processor. ", |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + }, |
| 42 | + "if": { |
| 43 | + Description: "Conditionally execute the processor", |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + }, |
| 47 | + "ignore_failure": { |
| 48 | + Description: "Ignore failures for the processor. ", |
| 49 | + Type: schema.TypeBool, |
| 50 | + Optional: true, |
| 51 | + Default: false, |
| 52 | + }, |
| 53 | + "on_failure": { |
| 54 | + Description: "Handle failures for the processor.", |
| 55 | + Type: schema.TypeList, |
| 56 | + Optional: true, |
| 57 | + MinItems: 1, |
| 58 | + Elem: &schema.Schema{ |
| 59 | + Type: schema.TypeString, |
| 60 | + ValidateFunc: validation.StringIsJSON, |
| 61 | + DiffSuppressFunc: utils.DiffJsonSuppress, |
| 62 | + }, |
| 63 | + }, |
| 64 | + "tag": { |
| 65 | + Description: "Identifier for the processor.", |
| 66 | + Type: schema.TypeString, |
| 67 | + Optional: true, |
| 68 | + }, |
| 69 | + "json": { |
| 70 | + Description: "JSON representation of this data source.", |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + } |
| 75 | + |
| 76 | + return &schema.Resource{ |
| 77 | + Description: "Reroutes a document to a different data stream, index, or index alias. See: https://www.elastic.co/guide/en/elasticsearch/reference/current/reroute-processor.html", |
| 78 | + |
| 79 | + ReadContext: dataSourceProcessorRerouteRead, |
| 80 | + |
| 81 | + Schema: processorSchema, |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func dataSourceProcessorRerouteRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 86 | + var diags diag.Diagnostics |
| 87 | + |
| 88 | + processor := &models.ProcessorReroute{} |
| 89 | + |
| 90 | + processor.IgnoreFailure = d.Get("ignore_failure").(bool) |
| 91 | + |
| 92 | + if v, ok := d.GetOk("destination"); ok { |
| 93 | + processor.Destination = v.(string) |
| 94 | + } |
| 95 | + if v, ok := d.GetOk("dataset"); ok { |
| 96 | + processor.Dataset = v.(string) |
| 97 | + } |
| 98 | + if v, ok := d.GetOk("namespace"); ok { |
| 99 | + processor.Namespace = v.(string) |
| 100 | + } |
| 101 | + |
| 102 | + if v, ok := d.GetOk("description"); ok { |
| 103 | + processor.Description = v.(string) |
| 104 | + } |
| 105 | + if v, ok := d.GetOk("if"); ok { |
| 106 | + processor.If = v.(string) |
| 107 | + } |
| 108 | + if v, ok := d.GetOk("tag"); ok { |
| 109 | + processor.Tag = v.(string) |
| 110 | + } |
| 111 | + if v, ok := d.GetOk("on_failure"); ok { |
| 112 | + onFailure := make([]map[string]interface{}, len(v.([]interface{}))) |
| 113 | + for i, f := range v.([]interface{}) { |
| 114 | + item := make(map[string]interface{}) |
| 115 | + if err := json.NewDecoder(strings.NewReader(f.(string))).Decode(&item); err != nil { |
| 116 | + return diag.FromErr(err) |
| 117 | + } |
| 118 | + onFailure[i] = item |
| 119 | + } |
| 120 | + processor.OnFailure = onFailure |
| 121 | + } |
| 122 | + |
| 123 | + processorJson, err := json.MarshalIndent(map[string]*models.ProcessorReroute{"reroute": processor}, "", " ") |
| 124 | + if err != nil { |
| 125 | + return diag.FromErr(err) |
| 126 | + } |
| 127 | + if err := d.Set("json", string(processorJson)); err != nil { |
| 128 | + return diag.FromErr(err) |
| 129 | + } |
| 130 | + |
| 131 | + hash, err := utils.StringToHash(string(processorJson)) |
| 132 | + if err != nil { |
| 133 | + return diag.FromErr(err) |
| 134 | + } |
| 135 | + |
| 136 | + d.SetId(*hash) |
| 137 | + |
| 138 | + return diags |
| 139 | +} |
0 commit comments