|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 8 | +) |
| 9 | + |
| 10 | +func resourceSumologicMetricsSearch() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + Create: resourceSumologicMetricsSearchCreate, |
| 13 | + Read: resourceSumologicMetricsSearchRead, |
| 14 | + Update: resourceSumologicMetricsSearchUpdate, |
| 15 | + Delete: resourceSumologicMetricsSearchDelete, |
| 16 | + Importer: &schema.ResourceImporter{ |
| 17 | + State: schema.ImportStatePassthrough, |
| 18 | + }, |
| 19 | + |
| 20 | + Schema: map[string]*schema.Schema{ |
| 21 | + "title": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + ValidateFunc: validation.StringLenBetween(1, 255), |
| 25 | + }, |
| 26 | + "description": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ValidateFunc: validation.StringLenBetween(0, 8192), |
| 30 | + }, |
| 31 | + "parent_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + }, |
| 35 | + "log_query": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + ValidateFunc: validation.StringLenBetween(0, 1024), |
| 39 | + }, |
| 40 | + "desired_quantization_in_secs": { |
| 41 | + Type: schema.TypeInt, |
| 42 | + Optional: true, |
| 43 | + Default: 0, |
| 44 | + ValidateFunc: validation.IntAtLeast(0), |
| 45 | + }, |
| 46 | + "time_range": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Required: true, |
| 49 | + MaxItems: 1, |
| 50 | + Elem: &schema.Resource{ |
| 51 | + Schema: GetTimeRangeSchema(), |
| 52 | + }, |
| 53 | + }, |
| 54 | + "metrics_queries": { |
| 55 | + Type: schema.TypeList, |
| 56 | + Required: true, |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "row_id": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Required: true, |
| 62 | + }, |
| 63 | + "query": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Required: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func resourceSumologicMetricsSearchCreate(d *schema.ResourceData, meta interface{}) error { |
| 75 | + c := meta.(*Client) |
| 76 | + |
| 77 | + if d.Id() == "" { |
| 78 | + metricsSearch := resourceToMetricsSearch(d) |
| 79 | + log.Println("=====================================================================") |
| 80 | + log.Printf("creating metrics search - %+v", metricsSearch) |
| 81 | + log.Println("=====================================================================") |
| 82 | + id, err := c.CreateMetricsSearch(metricsSearch) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + d.SetId(id) |
| 88 | + } |
| 89 | + |
| 90 | + return resourceSumologicMetricsSearchRead(d, meta) |
| 91 | +} |
| 92 | + |
| 93 | +func resourceSumologicMetricsSearchRead(d *schema.ResourceData, meta interface{}) error { |
| 94 | + c := meta.(*Client) |
| 95 | + |
| 96 | + id := d.Id() |
| 97 | + metricsSearch, err := c.GetMetricsSearch(id) |
| 98 | + if err != nil { |
| 99 | + return err |
| 100 | + } |
| 101 | + |
| 102 | + if metricsSearch == nil { |
| 103 | + log.Printf("[WARN] MetricsSearch not found, removing from state: %v - %v", id, err) |
| 104 | + d.SetId("") |
| 105 | + return nil |
| 106 | + } |
| 107 | + log.Println("=====================================================================") |
| 108 | + log.Printf("read metrics search - %+v", metricsSearch) |
| 109 | + log.Println("=====================================================================") |
| 110 | + |
| 111 | + return setMetricsSearch(d, metricsSearch) |
| 112 | +} |
| 113 | + |
| 114 | +func resourceSumologicMetricsSearchUpdate(d *schema.ResourceData, meta interface{}) error { |
| 115 | + c := meta.(*Client) |
| 116 | + |
| 117 | + metricsSearch := resourceToMetricsSearch(d) |
| 118 | + log.Println("=====================================================================") |
| 119 | + log.Printf("updating metrics search - %+v", metricsSearch) |
| 120 | + log.Println("=====================================================================") |
| 121 | + err := c.UpdateMetricsSearch(metricsSearch) |
| 122 | + if err != nil { |
| 123 | + return err |
| 124 | + } |
| 125 | + |
| 126 | + return resourceSumologicMetricsSearchRead(d, meta) |
| 127 | +} |
| 128 | + |
| 129 | +func resourceSumologicMetricsSearchDelete(d *schema.ResourceData, meta interface{}) error { |
| 130 | + c := meta.(*Client) |
| 131 | + return c.DeleteMetricsSearch(d.Id()) |
| 132 | +} |
| 133 | + |
| 134 | +func setMetricsSearch(d *schema.ResourceData, metricsSearch *MetricsSearch) error { |
| 135 | + if err := d.Set("title", metricsSearch.Title); err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + if err := d.Set("description", metricsSearch.Description); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + if err := d.Set("parent_id", metricsSearch.ParentId); err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + if err := d.Set("log_query", metricsSearch.LogQuery); err != nil { |
| 145 | + return err |
| 146 | + } |
| 147 | + if err := d.Set("desired_quantization_in_secs", metricsSearch.DesiredQuantizationInSecs); err != nil { |
| 148 | + return err |
| 149 | + } |
| 150 | + |
| 151 | + metricsSearchQueries := make([]map[string]interface{}, len(metricsSearch.MetricsQueries)) |
| 152 | + for i, searchQuery := range metricsSearch.MetricsQueries { |
| 153 | + metricsSearchQueries[i] = getTerraformMetricsSearchQuery(searchQuery) |
| 154 | + } |
| 155 | + if err := d.Set("metrics_queries", metricsSearchQueries); err != nil { |
| 156 | + return err |
| 157 | + } |
| 158 | + |
| 159 | + timeRange := GetTerraformTimeRange(metricsSearch.TimeRange.(map[string]interface{})) |
| 160 | + if err := d.Set("time_range", timeRange); err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func getTerraformMetricsSearchQuery(metricsSearchQuery MetricsSearchQuery) map[string]interface{} { |
| 168 | + tfMetricsSearchQuery := map[string]interface{}{} |
| 169 | + |
| 170 | + tfMetricsSearchQuery["row_id"] = metricsSearchQuery.RowId |
| 171 | + tfMetricsSearchQuery["query"] = metricsSearchQuery.Query |
| 172 | + return tfMetricsSearchQuery |
| 173 | +} |
| 174 | + |
| 175 | +func resourceToMetricsSearch(d *schema.ResourceData) MetricsSearch { |
| 176 | + var timeRange interface{} |
| 177 | + if val, ok := d.GetOk("time_range"); ok { |
| 178 | + tfTimeRange := val.([]interface{})[0] |
| 179 | + timeRange = GetTimeRange(tfTimeRange.(map[string]interface{})) |
| 180 | + } |
| 181 | + |
| 182 | + var metricsQueries []MetricsSearchQuery |
| 183 | + if val, ok := d.GetOk("metrics_queries"); ok { |
| 184 | + metricsQueryData := val.([]interface{}) |
| 185 | + for _, data := range metricsQueryData { |
| 186 | + metricsQueries = append(metricsQueries, resourceToMetricsSearchMetricsQuery([]interface{}{data})) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + return MetricsSearch{ |
| 191 | + ID: d.Id(), |
| 192 | + Title: d.Get("title").(string), |
| 193 | + Description: d.Get("description").(string), |
| 194 | + ParentId: d.Get("parent_id").(string), |
| 195 | + LogQuery: d.Get("log_query").(string), |
| 196 | + DesiredQuantizationInSecs: d.Get("desired_quantization_in_secs").(int), |
| 197 | + TimeRange: timeRange, |
| 198 | + MetricsQueries: metricsQueries, |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +func resourceToMetricsSearchMetricsQuery(data interface{}) MetricsSearchQuery { |
| 203 | + metricsSearchQuery := MetricsSearchQuery{} |
| 204 | + |
| 205 | + metricsSearchQuerySlice := data.([]interface{}) |
| 206 | + if len(metricsSearchQuerySlice) > 0 { |
| 207 | + metricsSearchQueryObj := metricsSearchQuerySlice[0].(map[string]interface{}) |
| 208 | + |
| 209 | + metricsSearchQuery.RowId = metricsSearchQueryObj["row_id"].(string) |
| 210 | + metricsSearchQuery.Query = metricsSearchQueryObj["query"].(string) |
| 211 | + } |
| 212 | + |
| 213 | + return metricsSearchQuery |
| 214 | +} |
0 commit comments