|
| 1 | +package auth0 |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 9 | + |
| 10 | + "gopkg.in/auth0.v5/management" |
| 11 | +) |
| 12 | + |
| 13 | +func newLogStream() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + |
| 16 | + Create: createLogStream, |
| 17 | + Read: readLogStream, |
| 18 | + Update: updateLogStream, |
| 19 | + Delete: deleteLogStream, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + State: schema.ImportStatePassthrough, |
| 22 | + }, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "name": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Required: true, |
| 27 | + }, |
| 28 | + "type": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + ValidateFunc: validation.StringInSlice([]string{ |
| 32 | + "eventbridge", |
| 33 | + "eventgrid", |
| 34 | + "http", |
| 35 | + "datadog", |
| 36 | + "splunk", |
| 37 | + }, true), |
| 38 | + ForceNew: true, |
| 39 | + Description: "Type of the log stream, which indicates the sink provider", |
| 40 | + }, |
| 41 | + "status": { |
| 42 | + Type: schema.TypeString, |
| 43 | + Optional: true, |
| 44 | + Computed: true, |
| 45 | + ValidateFunc: validation.StringInSlice([]string{ |
| 46 | + "active", |
| 47 | + "paused", |
| 48 | + "suspended", |
| 49 | + }, false), |
| 50 | + Description: "Status of the LogStream", |
| 51 | + }, |
| 52 | + "sink": { |
| 53 | + Type: schema.TypeList, |
| 54 | + MaxItems: 1, |
| 55 | + Required: true, |
| 56 | + Elem: &schema.Resource{ |
| 57 | + Schema: map[string]*schema.Schema{ |
| 58 | + "aws_account_id": { |
| 59 | + Type: schema.TypeString, |
| 60 | + Optional: true, |
| 61 | + ForceNew: true, |
| 62 | + RequiredWith: []string{"sink.0.aws_region"}, |
| 63 | + }, |
| 64 | + "aws_region": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Optional: true, |
| 67 | + ForceNew: true, |
| 68 | + RequiredWith: []string{"sink.0.aws_account_id"}, |
| 69 | + }, |
| 70 | + "aws_partner_event_source": { |
| 71 | + Type: schema.TypeString, |
| 72 | + Computed: true, |
| 73 | + Optional: true, |
| 74 | + Description: "Name of the Partner Event Source to be used with AWS, if the type is 'eventbridge'", |
| 75 | + }, |
| 76 | + "azure_subscription_id": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Optional: true, |
| 79 | + ForceNew: true, |
| 80 | + RequiredWith: []string{"sink.0.azure_resource_group", "sink.0.azure_region"}, |
| 81 | + }, |
| 82 | + "azure_resource_group": { |
| 83 | + Type: schema.TypeString, |
| 84 | + Optional: true, |
| 85 | + ForceNew: true, |
| 86 | + RequiredWith: []string{"sink.0.azure_subscription_id", "sink.0.azure_region"}, |
| 87 | + }, |
| 88 | + "azure_region": { |
| 89 | + Type: schema.TypeString, |
| 90 | + Optional: true, |
| 91 | + ForceNew: true, |
| 92 | + RequiredWith: []string{"sink.0.azure_subscription_id", "sink.0.azure_resource_group"}, |
| 93 | + }, |
| 94 | + "azure_partner_topic": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Optional: true, |
| 98 | + Description: "Name of the Partner Topic to be used with Azure, if the type is 'eventgrid'", |
| 99 | + }, |
| 100 | + "http_content_format": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Optional: true, |
| 103 | + RequiredWith: []string{"sink.0.http_endpoint", "sink.0.http_authorization", "sink.0.http_content_type"}, |
| 104 | + Description: "HTTP Content Format can be JSONLINES or JSONARRAY", |
| 105 | + ValidateFunc: validation.StringInSlice([]string{ |
| 106 | + "JSONLINES", |
| 107 | + "JSONARRAY", |
| 108 | + }, false), |
| 109 | + }, |
| 110 | + "http_content_type": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Optional: true, |
| 113 | + Description: "HTTP Content Type", |
| 114 | + RequiredWith: []string{"sink.0.http_endpoint", "sink.0.http_authorization", "sink.0.http_content_format"}, |
| 115 | + }, |
| 116 | + "http_endpoint": { |
| 117 | + Type: schema.TypeString, |
| 118 | + Optional: true, |
| 119 | + Description: "HTTP endpoint", |
| 120 | + RequiredWith: []string{"sink.0.http_content_format", "sink.0.http_authorization", "sink.0.http_content_type"}, |
| 121 | + }, |
| 122 | + "http_authorization": { |
| 123 | + Type: schema.TypeString, |
| 124 | + Optional: true, |
| 125 | + Sensitive: true, |
| 126 | + RequiredWith: []string{"sink.0.http_content_format", "sink.0.http_endpoint", "sink.0.http_content_type"}, |
| 127 | + }, |
| 128 | + "http_custom_headers": { |
| 129 | + Type: schema.TypeSet, |
| 130 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 131 | + Optional: true, |
| 132 | + Default: nil, |
| 133 | + Description: "Custom HTTP headers", |
| 134 | + }, |
| 135 | + |
| 136 | + "datadog_region": { |
| 137 | + Type: schema.TypeString, |
| 138 | + Sensitive: true, |
| 139 | + Optional: true, |
| 140 | + RequiredWith: []string{"sink.0.datadog_api_key"}, |
| 141 | + }, |
| 142 | + "datadog_api_key": { |
| 143 | + Type: schema.TypeString, |
| 144 | + Optional: true, |
| 145 | + Sensitive: true, |
| 146 | + RequiredWith: []string{"sink.0.datadog_region"}, |
| 147 | + }, |
| 148 | + "splunk_domain": { |
| 149 | + Type: schema.TypeString, |
| 150 | + Optional: true, |
| 151 | + RequiredWith: []string{"sink.0.splunk_token", "sink.0.splunk_port", "sink.0.splunk_secure"}, |
| 152 | + }, |
| 153 | + "splunk_token": { |
| 154 | + Type: schema.TypeString, |
| 155 | + Optional: true, |
| 156 | + Sensitive: true, |
| 157 | + RequiredWith: []string{"sink.0.splunk_domain", "sink.0.splunk_port", "sink.0.splunk_secure"}, |
| 158 | + }, |
| 159 | + "splunk_port": { |
| 160 | + Type: schema.TypeString, |
| 161 | + Optional: true, |
| 162 | + RequiredWith: []string{"sink.0.splunk_domain", "sink.0.splunk_token", "sink.0.splunk_secure"}, |
| 163 | + }, |
| 164 | + "splunk_secure": { |
| 165 | + Type: schema.TypeBool, |
| 166 | + Optional: true, |
| 167 | + Default: nil, |
| 168 | + RequiredWith: []string{"sink.0.splunk_domain", "sink.0.splunk_port", "sink.0.splunk_token"}, |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +func createLogStream(d *schema.ResourceData, m interface{}) error { |
| 178 | + ls := expandLogStream(d) |
| 179 | + |
| 180 | + api := m.(*management.Management) |
| 181 | + if err := api.LogStream.Create(ls); err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + d.SetId(ls.GetID()) |
| 185 | + |
| 186 | + // The Management API only allows updating a log stream's status. Therefore |
| 187 | + // if the status field was present in the configuration, we perform an |
| 188 | + // additional operation to modify it. |
| 189 | + s := String(d, "status") |
| 190 | + if s != nil && s != ls.Status { |
| 191 | + err := api.LogStream.Update(ls.GetID(), &management.LogStream{Status: s}) |
| 192 | + if err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + return readLogStream(d, m) |
| 198 | +} |
| 199 | + |
| 200 | +func readLogStream(d *schema.ResourceData, m interface{}) error { |
| 201 | + api := m.(*management.Management) |
| 202 | + ls, err := api.LogStream.Read(d.Id()) |
| 203 | + if err != nil { |
| 204 | + if mErr, ok := err.(management.Error); ok { |
| 205 | + if mErr.Status() == http.StatusNotFound { |
| 206 | + d.SetId("") |
| 207 | + return nil |
| 208 | + } |
| 209 | + } |
| 210 | + return err |
| 211 | + } |
| 212 | + |
| 213 | + d.SetId(ls.GetID()) |
| 214 | + d.Set("name", ls.Name) |
| 215 | + d.Set("status", ls.Status) |
| 216 | + d.Set("type", ls.Type) |
| 217 | + d.Set("sink", flattenLogStreamSink(d, ls.Sink)) |
| 218 | + return nil |
| 219 | +} |
| 220 | + |
| 221 | +func updateLogStream(d *schema.ResourceData, m interface{}) error { |
| 222 | + ls := expandLogStream(d) |
| 223 | + |
| 224 | + api := m.(*management.Management) |
| 225 | + err := api.LogStream.Update(d.Id(), ls) |
| 226 | + if err != nil { |
| 227 | + return err |
| 228 | + } |
| 229 | + return readLogStream(d, m) |
| 230 | +} |
| 231 | + |
| 232 | +func deleteLogStream(d *schema.ResourceData, m interface{}) error { |
| 233 | + api := m.(*management.Management) |
| 234 | + err := api.LogStream.Delete(d.Id()) |
| 235 | + if err != nil { |
| 236 | + if mErr, ok := err.(management.Error); ok { |
| 237 | + if mErr.Status() == http.StatusNotFound { |
| 238 | + d.SetId("") |
| 239 | + return nil |
| 240 | + } |
| 241 | + } |
| 242 | + } |
| 243 | + return err |
| 244 | +} |
| 245 | + |
| 246 | +func flattenLogStreamSink(d ResourceData, sink interface{}) []interface{} { |
| 247 | + |
| 248 | + var m interface{} |
| 249 | + |
| 250 | + switch o := sink.(type) { |
| 251 | + case *management.LogStreamSinkAmazonEventBridge: |
| 252 | + m = flattenLogStreamSinkAmazonEventBridge(o) |
| 253 | + case *management.LogStreamSinkAzureEventGrid: |
| 254 | + m = flattenLogStreamSinkAzureEventGrid(o) |
| 255 | + case *management.LogStreamSinkHTTP: |
| 256 | + m = flattenLogStreamSinkHTTP(o) |
| 257 | + case *management.LogStreamSinkDatadog: |
| 258 | + m = flattenLogStreamSinkDatadog(o) |
| 259 | + case *management.LogStreamSinkSplunk: |
| 260 | + m = flattenLogStreamSinkSplunk(o) |
| 261 | + } |
| 262 | + return []interface{}{m} |
| 263 | +} |
| 264 | + |
| 265 | +func flattenLogStreamSinkAmazonEventBridge(o *management.LogStreamSinkAmazonEventBridge) interface{} { |
| 266 | + return map[string]interface{}{ |
| 267 | + "aws_account_id": o.GetAccountID(), |
| 268 | + "aws_region": o.GetRegion(), |
| 269 | + "aws_partner_event_source": o.GetPartnerEventSource(), |
| 270 | + } |
| 271 | +} |
| 272 | + |
| 273 | +func flattenLogStreamSinkAzureEventGrid(o *management.LogStreamSinkAzureEventGrid) interface{} { |
| 274 | + return map[string]interface{}{ |
| 275 | + "azure_subscription_id": o.GetSubscriptionID(), |
| 276 | + "azure_resource_group": o.GetResourceGroup(), |
| 277 | + "azure_region": o.GetRegion(), |
| 278 | + "azure_partner_topic": o.GetPartnerTopic(), |
| 279 | + } |
| 280 | +} |
| 281 | + |
| 282 | +func flattenLogStreamSinkHTTP(o *management.LogStreamSinkHTTP) interface{} { |
| 283 | + return map[string]interface{}{ |
| 284 | + "http_endpoint": o.GetEndpoint(), |
| 285 | + "http_contentFormat": o.GetContentFormat(), |
| 286 | + "http_contentType": o.GetContentType(), |
| 287 | + "http_authorization": o.GetAuthorization(), |
| 288 | + "http_custom_headers": o.CustomHeaders, |
| 289 | + } |
| 290 | +} |
| 291 | + |
| 292 | +func flattenLogStreamSinkDatadog(o *management.LogStreamSinkDatadog) interface{} { |
| 293 | + return map[string]interface{}{ |
| 294 | + "datadog_region": o.GetRegion(), |
| 295 | + "datadog_api_key": o.GetAPIKey(), |
| 296 | + } |
| 297 | +} |
| 298 | + |
| 299 | +func flattenLogStreamSinkSplunk(o *management.LogStreamSinkSplunk) interface{} { |
| 300 | + return map[string]interface{}{ |
| 301 | + "splunk_domain": o.GetDomain(), |
| 302 | + "splunk_token": o.GetToken(), |
| 303 | + "splunk_port": o.GetPort(), |
| 304 | + "splunk_secure": o.GetSecure(), |
| 305 | + } |
| 306 | +} |
| 307 | +func expandLogStream(d ResourceData) *management.LogStream { |
| 308 | + |
| 309 | + ls := &management.LogStream{ |
| 310 | + Name: String(d, "name"), |
| 311 | + Type: String(d, "type", IsNewResource()), |
| 312 | + Status: String(d, "status", Not(IsNewResource())), |
| 313 | + } |
| 314 | + |
| 315 | + s := d.Get("type").(string) |
| 316 | + |
| 317 | + List(d, "sink").Elem(func(d ResourceData) { |
| 318 | + switch s { |
| 319 | + case management.LogStreamTypeAmazonEventBridge: |
| 320 | + // LogStreamTypeAmazonEventBridge cannot be updated |
| 321 | + if d.IsNewResource() { |
| 322 | + ls.Sink = expandLogStreamSinkAmazonEventBridge(d) |
| 323 | + } |
| 324 | + case management.LogStreamTypeAzureEventGrid: |
| 325 | + // LogStreamTypeAzureEventGrid cannot be updated |
| 326 | + if d.IsNewResource() { |
| 327 | + ls.Sink = expandLogStreamSinkAzureEventGrid(d) |
| 328 | + } |
| 329 | + case management.LogStreamTypeHTTP: |
| 330 | + ls.Sink = expandLogStreamSinkHTTP(d) |
| 331 | + case management.LogStreamTypeDatadog: |
| 332 | + ls.Sink = expandLogStreamSinkDatadog(d) |
| 333 | + case management.LogStreamTypeSplunk: |
| 334 | + ls.Sink = expandLogStreamSinkSplunk(d) |
| 335 | + default: |
| 336 | + log.Printf("[WARN]: Unsupported log stream sink %s", s) |
| 337 | + log.Printf("[WARN]: Raise an issue with the auth0 provider in order to support it:") |
| 338 | + log.Printf("[WARN]: https://github.com/alexkappa/terraform-provider-auth0/issues/new") |
| 339 | + } |
| 340 | + }) |
| 341 | + |
| 342 | + return ls |
| 343 | +} |
| 344 | + |
| 345 | +func expandLogStreamSinkAmazonEventBridge(d ResourceData) *management.LogStreamSinkAmazonEventBridge { |
| 346 | + o := &management.LogStreamSinkAmazonEventBridge{ |
| 347 | + AccountID: String(d, "aws_account_id"), |
| 348 | + Region: String(d, "aws_region"), |
| 349 | + } |
| 350 | + return o |
| 351 | +} |
| 352 | + |
| 353 | +func expandLogStreamSinkAzureEventGrid(d ResourceData) *management.LogStreamSinkAzureEventGrid { |
| 354 | + o := &management.LogStreamSinkAzureEventGrid{ |
| 355 | + SubscriptionID: String(d, "azure_subscription_id"), |
| 356 | + ResourceGroup: String(d, "azure_resource_group"), |
| 357 | + Region: String(d, "azure_region"), |
| 358 | + PartnerTopic: String(d, "azure_partner_topic"), |
| 359 | + } |
| 360 | + return o |
| 361 | +} |
| 362 | + |
| 363 | +func expandLogStreamSinkHTTP(d ResourceData) *management.LogStreamSinkHTTP { |
| 364 | + o := &management.LogStreamSinkHTTP{ |
| 365 | + ContentFormat: String(d, "http_content_format"), |
| 366 | + ContentType: String(d, "http_content_type"), |
| 367 | + Endpoint: String(d, "http_endpoint"), |
| 368 | + Authorization: String(d, "http_authorization"), |
| 369 | + CustomHeaders: Set(d, "http_custom_headers").List(), |
| 370 | + } |
| 371 | + return o |
| 372 | +} |
| 373 | +func expandLogStreamSinkDatadog(d ResourceData) *management.LogStreamSinkDatadog { |
| 374 | + o := &management.LogStreamSinkDatadog{ |
| 375 | + Region: String(d, "datadog_region"), |
| 376 | + APIKey: String(d, "datadog_api_key"), |
| 377 | + } |
| 378 | + return o |
| 379 | +} |
| 380 | +func expandLogStreamSinkSplunk(d ResourceData) *management.LogStreamSinkSplunk { |
| 381 | + o := &management.LogStreamSinkSplunk{ |
| 382 | + Domain: String(d, "splunk_domain"), |
| 383 | + Token: String(d, "splunk_token"), |
| 384 | + Port: String(d, "splunk_port"), |
| 385 | + Secure: Bool(d, "splunk_secure"), |
| 386 | + } |
| 387 | + return o |
| 388 | +} |
0 commit comments