|
| 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 resourceSumologicKinesisLogSource() *schema.Resource { |
| 14 | + kinesisLogSource := resourceSumologicSource() |
| 15 | + kinesisLogSource.Create = resourceSumologicKinesisLogSourceCreate |
| 16 | + kinesisLogSource.Read = resourceSumologicKinesisLogSourceRead |
| 17 | + kinesisLogSource.Update = resourceSumologicKinesisLogSourceUpdate |
| 18 | + kinesisLogSource.Importer = &schema.ResourceImporter{ |
| 19 | + State: resourceSumologicSourceImport, |
| 20 | + } |
| 21 | + |
| 22 | + kinesisLogSource.Schema["content_type"] = &schema.Schema{ |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + ValidateFunc: validation.StringInSlice([]string{"KinesisLog"}, false), |
| 26 | + } |
| 27 | + |
| 28 | + kinesisLogSource.Schema["message_per_request"] = &schema.Schema{ |
| 29 | + Type: schema.TypeBool, |
| 30 | + Optional: true, |
| 31 | + Default: false, |
| 32 | + } |
| 33 | + kinesisLogSource.Schema["url"] = &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + Computed: true, |
| 36 | + } |
| 37 | + kinesisLogSource.Schema["authentication"] = &schema.Schema{ |
| 38 | + Type: schema.TypeList, |
| 39 | + Optional: true, |
| 40 | + MaxItems: 1, |
| 41 | + Elem: &schema.Resource{ |
| 42 | + Schema: map[string]*schema.Schema{ |
| 43 | + "type": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + ValidateFunc: validation.StringInSlice([]string{"S3BucketAuthentication", "AWSRoleBasedAuthentication", "NoAuthentication"}, false), |
| 47 | + }, |
| 48 | + "access_key": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + }, |
| 52 | + "secret_key": { |
| 53 | + Type: schema.TypeString, |
| 54 | + Optional: true, |
| 55 | + }, |
| 56 | + "role_arn": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + kinesisLogSource.Schema["path"] = &schema.Schema{ |
| 65 | + Type: schema.TypeList, |
| 66 | + Optional: true, |
| 67 | + MaxItems: 1, |
| 68 | + Elem: &schema.Resource{ |
| 69 | + Schema: map[string]*schema.Schema{ |
| 70 | + "type": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Required: true, |
| 73 | + ValidateFunc: validation.StringInSlice([]string{"KinesisLogPath", "NoPathExpression"}, false), |
| 74 | + }, |
| 75 | + "bucket_name": { |
| 76 | + Type: schema.TypeString, |
| 77 | + Optional: true, |
| 78 | + }, |
| 79 | + "path_expression": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Optional: true, |
| 82 | + }, |
| 83 | + "scan_interval": { |
| 84 | + Type: schema.TypeInt, |
| 85 | + Optional: true, |
| 86 | + Default: 300000, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + return kinesisLogSource |
| 93 | +} |
| 94 | + |
| 95 | +func resourceSumologicKinesisLogSourceCreate(d *schema.ResourceData, meta interface{}) error { |
| 96 | + c := meta.(*Client) |
| 97 | + |
| 98 | + if d.Id() == "" { |
| 99 | + source, err := resourceToKinesisLogSource(d) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + id, err := c.CreateKinesisLogSource(source, d.Get("collector_id").(int)) |
| 105 | + |
| 106 | + if err != nil { |
| 107 | + return err |
| 108 | + } |
| 109 | + |
| 110 | + d.SetId(strconv.Itoa(id)) |
| 111 | + } |
| 112 | + |
| 113 | + return resourceSumologicKinesisLogSourceRead(d, meta) |
| 114 | +} |
| 115 | + |
| 116 | +func resourceSumologicKinesisLogSourceUpdate(d *schema.ResourceData, meta interface{}) error { |
| 117 | + c := meta.(*Client) |
| 118 | + |
| 119 | + source, err := resourceToKinesisLogSource(d) |
| 120 | + if err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + |
| 124 | + err = c.UpdateKinesisLogSource(source, d.Get("collector_id").(int)) |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + return resourceSumologicKinesisLogSourceRead(d, meta) |
| 131 | +} |
| 132 | + |
| 133 | +func resourceSumologicKinesisLogSourceRead(d *schema.ResourceData, meta interface{}) error { |
| 134 | + c := meta.(*Client) |
| 135 | + |
| 136 | + id, _ := strconv.Atoi(d.Id()) |
| 137 | + source, err := c.GetKinesisLogSource(d.Get("collector_id").(int), id) |
| 138 | + |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + if source == nil { |
| 144 | + log.Printf("[WARN] KinesisLog source not found, removing from state: %v - %v", id, err) |
| 145 | + d.SetId("") |
| 146 | + |
| 147 | + return nil |
| 148 | + } |
| 149 | + |
| 150 | + kinesisLogResources := source.ThirdPartyRef.Resources |
| 151 | + path := getKinesisLogThirdPartyPathAttributes(kinesisLogResources) |
| 152 | + |
| 153 | + if err := d.Set("path", path); err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + if err := resourceSumologicSourceRead(d, source.Source); err != nil { |
| 158 | + return fmt.Errorf("%s", err) |
| 159 | + } |
| 160 | + d.Set("content_type", source.ContentType) |
| 161 | + d.Set("message_per_request", source.MessagePerRequest) |
| 162 | + d.Set("url", source.URL) |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
| 166 | + |
| 167 | +func resourceToKinesisLogSource(d *schema.ResourceData) (KinesisLogSource, error) { |
| 168 | + source := resourceToSource(d) |
| 169 | + source.Type = "HTTP" |
| 170 | + |
| 171 | + kinesisLogSource := KinesisLogSource{ |
| 172 | + Source: source, |
| 173 | + MessagePerRequest: d.Get("message_per_request").(bool), |
| 174 | + URL: d.Get("url").(string), |
| 175 | + } |
| 176 | + |
| 177 | + authSettings, errAuthSettings := getKinesisLogAuthentication(d) |
| 178 | + if errAuthSettings != nil { |
| 179 | + return kinesisLogSource, errAuthSettings |
| 180 | + } |
| 181 | + |
| 182 | + pathSettings, errPathSettings := getKinesisLogPathSettings(d) |
| 183 | + if errPathSettings != nil { |
| 184 | + return kinesisLogSource, errPathSettings |
| 185 | + } |
| 186 | + |
| 187 | + kinesisLogResource := KinesisLogResource{ |
| 188 | + ServiceType: d.Get("content_type").(string), |
| 189 | + Authentication: authSettings, |
| 190 | + Path: pathSettings, |
| 191 | + } |
| 192 | + |
| 193 | + kinesisLogSource.ThirdPartyRef.Resources = append(kinesisLogSource.ThirdPartyRef.Resources, kinesisLogResource) |
| 194 | + |
| 195 | + return kinesisLogSource, nil |
| 196 | +} |
| 197 | + |
| 198 | +func getKinesisLogPathSettings(d *schema.ResourceData) (KinesisLogPath, error) { |
| 199 | + pathSettings := KinesisLogPath{} |
| 200 | + paths := d.Get("path").([]interface{}) |
| 201 | + if len(paths) > 0 { |
| 202 | + path := paths[0].(map[string]interface{}) |
| 203 | + switch pathType := path["type"].(string); pathType { |
| 204 | + case "KinesisLogPath": |
| 205 | + pathSettings.Type = pathType |
| 206 | + pathSettings.BucketName = path["bucket_name"].(string) |
| 207 | + pathSettings.PathExpression = path["path_expression"].(string) |
| 208 | + pathSettings.ScanInterval = path["scan_interval"].(int) |
| 209 | + case "NoPathExpression": |
| 210 | + pathSettings.Type = pathType |
| 211 | + default: |
| 212 | + errorMessage := fmt.Sprintf("[ERROR] Unknown resourceType in path: %v", pathType) |
| 213 | + log.Print(errorMessage) |
| 214 | + return pathSettings, errors.New(errorMessage) |
| 215 | + } |
| 216 | + } else { |
| 217 | + return pathSettings, errors.New(fmt.Sprintf("[ERROR] no path specification in kinesis log Soruce")) |
| 218 | + } |
| 219 | + return pathSettings, nil |
| 220 | +} |
| 221 | + |
| 222 | +func getKinesisLogAuthentication(d *schema.ResourceData) (PollingAuthentication, error) { |
| 223 | + auths := d.Get("authentication").([]interface{}) |
| 224 | + authSettings := PollingAuthentication{} |
| 225 | + |
| 226 | + if len(auths) > 0 { |
| 227 | + auth := auths[0].(map[string]interface{}) |
| 228 | + switch authType := auth["type"].(string); authType { |
| 229 | + case "S3BucketAuthentication": |
| 230 | + authSettings.Type = "S3BucketAuthentication" |
| 231 | + authSettings.AwsID = auth["access_key"].(string) |
| 232 | + authSettings.AwsKey = auth["secret_key"].(string) |
| 233 | + if auth["region"] != nil { |
| 234 | + authSettings.Region = auth["region"].(string) |
| 235 | + } |
| 236 | + case "AWSRoleBasedAuthentication": |
| 237 | + authSettings.Type = "AWSRoleBasedAuthentication" |
| 238 | + authSettings.RoleARN = auth["role_arn"].(string) |
| 239 | + if auth["region"] != nil { |
| 240 | + authSettings.Region = auth["region"].(string) |
| 241 | + } |
| 242 | + case "NoAuthentication": |
| 243 | + authSettings.Type = "NoAuthentication" |
| 244 | + default: |
| 245 | + errorMessage := fmt.Sprintf("[ERROR] Unknown authType: %v", authType) |
| 246 | + log.Print(errorMessage) |
| 247 | + return authSettings, errors.New(errorMessage) |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + return authSettings, nil |
| 252 | +} |
| 253 | + |
| 254 | +func getKinesisLogThirdPartyPathAttributes(kinesisLogResource []KinesisLogResource) []map[string]interface{} { |
| 255 | + |
| 256 | + var s []map[string]interface{} |
| 257 | + |
| 258 | + for _, t := range kinesisLogResource { |
| 259 | + mapping := map[string]interface{}{ |
| 260 | + "type": t.Path.Type, |
| 261 | + "bucket_name": t.Path.BucketName, |
| 262 | + "path_expression": t.Path.PathExpression, |
| 263 | + "scan_interval": t.Path.ScanInterval, |
| 264 | + } |
| 265 | + s = append(s, mapping) |
| 266 | + } |
| 267 | + return s |
| 268 | +} |
0 commit comments