|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceSumologicKinesisMetricsSource() *schema.Resource { |
| 14 | + kinesisMetricsSource := resourceSumologicSource() |
| 15 | + kinesisMetricsSource.Create = resourceSumologicKinesisMetricsSourceCreate |
| 16 | + kinesisMetricsSource.Read = resourceSumologicKinesisMetricsSourceRead |
| 17 | + kinesisMetricsSource.Update = resourceSumologicKinesisMetricsSourceUpdate |
| 18 | + kinesisMetricsSource.Importer = &schema.ResourceImporter{ |
| 19 | + State: resourceSumologicSourceImport, |
| 20 | + } |
| 21 | + |
| 22 | + kinesisMetricsSource.Schema["content_type"] = &schema.Schema{ |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + ValidateFunc: validation.StringInSlice([]string{"KinesisMetric"}, false), |
| 26 | + } |
| 27 | + |
| 28 | + kinesisMetricsSource.Schema["message_per_request"] = &schema.Schema{ |
| 29 | + Type: schema.TypeBool, |
| 30 | + Optional: true, |
| 31 | + Default: false, |
| 32 | + } |
| 33 | + kinesisMetricsSource.Schema["url"] = &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + } |
| 37 | + kinesisMetricsSource.Schema["authentication"] = &schema.Schema{ |
| 38 | + Type: schema.TypeList, |
| 39 | + Required: true, |
| 40 | + ForceNew: true, |
| 41 | + MinItems: 1, |
| 42 | + MaxItems: 1, |
| 43 | + Elem: &schema.Resource{ |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "type": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + ValidateFunc: validation.StringInSlice([]string{"S3BucketAuthentication", "AWSRoleBasedAuthentication"}, false), |
| 49 | + }, |
| 50 | + "access_key": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + }, |
| 54 | + "secret_key": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Optional: true, |
| 57 | + }, |
| 58 | + "role_arn": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Optional: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + |
| 66 | + kinesisMetricsSource.Schema["path"] = &schema.Schema{ |
| 67 | + Type: schema.TypeList, |
| 68 | + Required: true, |
| 69 | + ForceNew: true, |
| 70 | + MinItems: 1, |
| 71 | + MaxItems: 1, |
| 72 | + Elem: &schema.Resource{ |
| 73 | + Schema: map[string]*schema.Schema{ |
| 74 | + "type": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Required: true, |
| 77 | + ValidateFunc: validation.StringInSlice([]string{"KinesisMetricPath"}, false), |
| 78 | + }, |
| 79 | + |
| 80 | + "tag_filters": { |
| 81 | + Type: schema.TypeList, |
| 82 | + Optional: true, |
| 83 | + Elem: &schema.Resource{ |
| 84 | + Schema: map[string]*schema.Schema{ |
| 85 | + "type": { |
| 86 | + Type: schema.TypeString, |
| 87 | + Optional: true, |
| 88 | + }, |
| 89 | + "namespace": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Optional: true, |
| 92 | + }, |
| 93 | + "tags": { |
| 94 | + Type: schema.TypeList, |
| 95 | + Optional: true, |
| 96 | + Elem: &schema.Schema{ |
| 97 | + Type: schema.TypeString, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + return kinesisMetricsSource |
| 108 | +} |
| 109 | + |
| 110 | +func resourceSumologicKinesisMetricsSourceCreate(d *schema.ResourceData, meta interface{}) error { |
| 111 | + c := meta.(*Client) |
| 112 | + |
| 113 | + if d.Id() == "" { |
| 114 | + source, err := resourceToKinesisMetricsSource(d) |
| 115 | + if err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + id, err := c.CreateKinesisMetricsSource(source, d.Get("collector_id").(int)) |
| 120 | + |
| 121 | + if err != nil { |
| 122 | + return err |
| 123 | + } |
| 124 | + |
| 125 | + d.SetId(strconv.Itoa(id)) |
| 126 | + } |
| 127 | + |
| 128 | + return resourceSumologicKinesisMetricsSourceRead(d, meta) |
| 129 | +} |
| 130 | + |
| 131 | +func resourceSumologicKinesisMetricsSourceUpdate(d *schema.ResourceData, meta interface{}) error { |
| 132 | + c := meta.(*Client) |
| 133 | + |
| 134 | + source, err := resourceToKinesisMetricsSource(d) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + |
| 139 | + err = c.UpdateKinesisMetricsSource(source, d.Get("collector_id").(int)) |
| 140 | + |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + return resourceSumologicKinesisMetricsSourceRead(d, meta) |
| 146 | +} |
| 147 | + |
| 148 | +func resourceSumologicKinesisMetricsSourceRead(d *schema.ResourceData, meta interface{}) error { |
| 149 | + c := meta.(*Client) |
| 150 | + |
| 151 | + id, _ := strconv.Atoi(d.Id()) |
| 152 | + source, err := c.GetKinesisMetricsSource(d.Get("collector_id").(int), id) |
| 153 | + |
| 154 | + if err != nil { |
| 155 | + return err |
| 156 | + } |
| 157 | + |
| 158 | + if source == nil { |
| 159 | + log.Printf("[WARN] KinesisMetric source not found, removing from state: %v - %v", id, err) |
| 160 | + d.SetId("") |
| 161 | + |
| 162 | + return nil |
| 163 | + } |
| 164 | + |
| 165 | + kinesisMetricsResources := source.ThirdPartyRef.Resources |
| 166 | + path := getKinesisMetricsThirdPartyPathAttributes(kinesisMetricsResources) |
| 167 | + |
| 168 | + if err := d.Set("path", path); err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + |
| 172 | + if err := resourceSumologicSourceRead(d, source.Source); err != nil { |
| 173 | + return fmt.Errorf("%s", err) |
| 174 | + } |
| 175 | + d.Set("content_type", source.ContentType) |
| 176 | + d.Set("message_per_request", source.MessagePerRequest) |
| 177 | + d.Set("url", source.URL) |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +func resourceToKinesisMetricsSource(d *schema.ResourceData) (KinesisMetricsSource, error) { |
| 183 | + source := resourceToSource(d) |
| 184 | + source.Type = "HTTP" |
| 185 | + |
| 186 | + kinesisMetricsSource := KinesisMetricsSource{ |
| 187 | + Source: source, |
| 188 | + MessagePerRequest: d.Get("message_per_request").(bool), |
| 189 | + URL: d.Get("url").(string), |
| 190 | + } |
| 191 | + |
| 192 | + authSettings, errAuthSettings := getPollingAuthentication(d) |
| 193 | + if errAuthSettings != nil { |
| 194 | + return kinesisMetricsSource, errAuthSettings |
| 195 | + } |
| 196 | + |
| 197 | + pathSettings, errPathSettings := getKinesisMetricsPathSettings(d) |
| 198 | + if errPathSettings != nil { |
| 199 | + return kinesisMetricsSource, errPathSettings |
| 200 | + } |
| 201 | + |
| 202 | + kinesisMetricsResource := PollingResource{ |
| 203 | + ServiceType: d.Get("content_type").(string), |
| 204 | + Authentication: authSettings, |
| 205 | + Path: pathSettings, |
| 206 | + } |
| 207 | + |
| 208 | + kinesisMetricsSource.ThirdPartyRef.Resources = append(kinesisMetricsSource.ThirdPartyRef.Resources, kinesisMetricsResource) |
| 209 | + |
| 210 | + return kinesisMetricsSource, nil |
| 211 | +} |
| 212 | + |
| 213 | +func getKinesisMetricsPathSettings(d *schema.ResourceData) (PollingPath, error) { |
| 214 | + pathSettings := PollingPath{} |
| 215 | + paths := d.Get("path").([]interface{}) |
| 216 | + if len(paths) > 0 { |
| 217 | + path := paths[0].(map[string]interface{}) |
| 218 | + pathType := path["type"].(string) |
| 219 | + pathSettings.Type = pathType |
| 220 | + pathSettings.TagFilters = getPollingTagFilters(d) |
| 221 | + } else { |
| 222 | + return pathSettings, errors.New(fmt.Sprintf("[ERROR] no path specification in kinesis metric Soruce")) |
| 223 | + } |
| 224 | + return pathSettings, nil |
| 225 | +} |
| 226 | + |
| 227 | +func getKinesisMetricsThirdPartyPathAttributes(pollingResource []PollingResource) []map[string]interface{} { |
| 228 | + |
| 229 | + var s []map[string]interface{} |
| 230 | + |
| 231 | + for _, t := range pollingResource { |
| 232 | + mapping := map[string]interface{}{ |
| 233 | + "type": t.Path.Type, |
| 234 | + "tag_filters": flattenPollingTagFilters(t.Path.TagFilters), |
| 235 | + } |
| 236 | + s = append(s, mapping) |
| 237 | + } |
| 238 | + return s |
| 239 | +} |
0 commit comments