Skip to content

Commit

Permalink
Add mixpanel log stream support (#408)
Browse files Browse the repository at this point in the history
  • Loading branch information
inhumantsar authored Dec 14, 2022
1 parent 3c8b8b9 commit f7d4f43
Show file tree
Hide file tree
Showing 4 changed files with 759 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/resources/log_stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Optional:
- `http_content_type` (String) The "Content-Type" header to send over HTTP. Common value is "application/json".
- `http_custom_headers` (List of Map of String) Additional HTTP headers to be included as part of the HTTP request.
- `http_endpoint` (String) The HTTP endpoint to send streaming logs.
- `mixpanel_project_id` (String) The Mixpanel project ID, found on the Project Settings page.
- `mixpanel_region` (String) The Mixpanel region. Options are ["us", "eu"]. EU is required for customers with EU data residency requirements.
- `mixpanel_service_account_password` (String, Sensitive) The Mixpanel Service Account password.
- `mixpanel_service_account_username` (String) The Mixpanel Service Account username. Services Accounts can be created in the Project Settings page.
- `splunk_domain` (String) The Splunk domain name.
- `splunk_port` (String) The Splunk port.
- `splunk_secure` (Boolean) This toggle should be turned off when using self-signed certificates.
Expand Down
52 changes: 50 additions & 2 deletions internal/provider/resource_auth0_log_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func newLogStream() *schema.Resource {
"datadog",
"splunk",
"sumo",
"mixpanel",
}, true),
ForceNew: true,
Description: "Type of the log stream, which indicates the sink provider.",
Expand Down Expand Up @@ -218,6 +219,32 @@ func newLogStream() *schema.Resource {
Description: "Generated URL for your defined HTTP source in " +
"Sumo Logic for collecting streaming data from Auth0.",
},
"mixpanel_region": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"sink.0.mixpanel_service_account_password", "sink.0.mixpanel_project_id", "sink.0.mixpanel_service_account_username"},
Description: "The Mixpanel region. Options are [\"us\", \"eu\"]. " +
"EU is required for customers with EU data residency requirements.",
},
"mixpanel_project_id": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"sink.0.mixpanel_region", "sink.0.mixpanel_service_account_username", "sink.0.mixpanel_service_account_password"},
Description: "The Mixpanel project ID, found on the Project Settings page.",
},
"mixpanel_service_account_username": {
Type: schema.TypeString,
Optional: true,
RequiredWith: []string{"sink.0.mixpanel_region", "sink.0.mixpanel_project_id", "sink.0.mixpanel_service_account_password"},
Description: "The Mixpanel Service Account username. Services Accounts can be created in the Project Settings page.",
},
"mixpanel_service_account_password": {
Type: schema.TypeString,
Optional: true,
Sensitive: true,
RequiredWith: []string{"sink.0.mixpanel_region", "sink.0.mixpanel_project_id", "sink.0.mixpanel_service_account_username"},
Description: "The Mixpanel Service Account password.",
},
},
},
},
Expand Down Expand Up @@ -265,7 +292,7 @@ func readLogStream(ctx context.Context, d *schema.ResourceData, m interface{}) d
d.Set("status", logStream.GetStatus()),
d.Set("type", logStream.GetType()),
d.Set("filters", logStream.Filters),
d.Set("sink", flattenLogStreamSink(logStream.Sink)),
d.Set("sink", flattenLogStreamSink(d, logStream.Sink)),
)

return diag.FromErr(result.ErrorOrNil())
Expand Down Expand Up @@ -296,7 +323,7 @@ func deleteLogStream(ctx context.Context, d *schema.ResourceData, m interface{})
return nil
}

func flattenLogStreamSink(sink interface{}) []interface{} {
func flattenLogStreamSink(d *schema.ResourceData, sink interface{}) []interface{} {
var m interface{}

switch sinkType := sink.(type) {
Expand All @@ -312,6 +339,8 @@ func flattenLogStreamSink(sink interface{}) []interface{} {
m = flattenLogStreamSinkSplunk(sinkType)
case *management.LogStreamSinkSumo:
m = flattenLogStreamSinkSumo(sinkType)
case *management.LogStreamSinkMixpanel:
m = flattenLogStreamSinkMixpanel(d, sinkType)
}

return []interface{}{m}
Expand Down Expand Up @@ -366,6 +395,15 @@ func flattenLogStreamSinkSumo(o *management.LogStreamSinkSumo) interface{} {
}
}

func flattenLogStreamSinkMixpanel(d *schema.ResourceData, o *management.LogStreamSinkMixpanel) interface{} {
return map[string]interface{}{
"mixpanel_region": o.GetRegion(),
"mixpanel_project_id": o.GetProjectID(),
"mixpanel_service_account_username": o.GetServiceAccountUsername(),
"mixpanel_service_account_password": d.Get("sink.0.mixpanel_service_account_password").(string), // Value does not get read back.
}
}

func expandLogStream(d *schema.ResourceData) *management.LogStream {
config := d.GetRawConfig()

Expand Down Expand Up @@ -414,6 +452,8 @@ func expandLogStream(d *schema.ResourceData) *management.LogStream {
logStream.Sink = expandLogStreamSinkSplunk(sink)
case management.LogStreamTypeSumo:
logStream.Sink = expandLogStreamSinkSumo(sink)
case management.LogStreamTypeMixpanel:
logStream.Sink = expandLogStreamSinkMixpanel(sink)
default:
log.Printf("[WARN]: Unsupported log stream sink %s", logStream.GetType())
log.Printf("[WARN]: Raise an issue with the auth0 provider in order to support it:")
Expand Down Expand Up @@ -483,3 +523,11 @@ func expandLogStreamSinkSumo(config cty.Value) *management.LogStreamSinkSumo {
SourceAddress: value.String(config.GetAttr("sumo_source_address")),
}
}
func expandLogStreamSinkMixpanel(config cty.Value) *management.LogStreamSinkMixpanel {
return &management.LogStreamSinkMixpanel{
Region: value.String(config.GetAttr("mixpanel_region")),
ProjectID: value.String(config.GetAttr("mixpanel_project_id")),
ServiceAccountUsername: value.String(config.GetAttr("mixpanel_service_account_username")),
ServiceAccountPassword: value.String(config.GetAttr("mixpanel_service_account_password")),
}
}
127 changes: 127 additions & 0 deletions internal/provider/resource_auth0_log_stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,130 @@ resource "auth0_log_stream" "my_log_stream" {
}
}
`

func TestAccLogStreamMixpanel(t *testing.T) {
httpRecorder := recorder.New(t)

resource.Test(t, resource.TestCase{
ProviderFactories: testProviders(httpRecorder),
Steps: []resource.TestStep{
{
Config: template.ParseTestName(logStreamMixpanelConfig, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "name", fmt.Sprintf("Acceptance-Test-LogStream-mixpanel-%s", t.Name())),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "type", "mixpanel"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_region", "us"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_project_id", "123456789"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_service_account_username", "fake-account.123abc.mp-service-account"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_service_account_password", "8iwyKSzwV2brfakepassGGKhsZ3INozo"),
),
},
{
Config: template.ParseTestName(logStreamMixpanelConfigUpdate, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "name", fmt.Sprintf("Acceptance-Test-LogStream-mixpanel-%s", t.Name())),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "type", "mixpanel"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "filters.#", "0"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_region", "us"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_project_id", "987654321"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_service_account_username", "fake-account.123abc.mp-service-account"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_service_account_password", "8iwyKSzwV2brfakepassGGKhsZ3INozo"),
),
},
{
Config: template.ParseTestName(logStreamMixpanelConfigUpdateWithFilters, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "name", fmt.Sprintf("Acceptance-Test-LogStream-mixpanel-%s", t.Name())),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "type", "mixpanel"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "filters.#", "2"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "filters.0.type", "category"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "filters.0.name", "auth.login.fail"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "filters.1.type", "category"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "filters.1.name", "auth.signup.fail"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_region", "us"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_project_id", "987654321"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_service_account_username", "fake-account.123abc.mp-service-account"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_service_account_password", "8iwyKSzwV2brfakepassGGKhsZ3INozo"),
),
},
{
Config: template.ParseTestName(logStreamMixpanelConfigUpdateWithEmptyFilters, t.Name()),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "name", fmt.Sprintf("Acceptance-Test-LogStream-mixpanel-%s", t.Name())),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "type", "mixpanel"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "filters.#", "0"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_region", "us"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_project_id", "987654321"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_service_account_username", "fake-account.123abc.mp-service-account"),
resource.TestCheckResourceAttr("auth0_log_stream.my_log_stream", "sink.0.mixpanel_service_account_password", "8iwyKSzwV2brfakepassGGKhsZ3INozo"),
),
},
},
})
}

const logStreamMixpanelConfig = `
resource "auth0_log_stream" "my_log_stream" {
name = "Acceptance-Test-LogStream-mixpanel-{{.testName}}"
type = "mixpanel"
sink {
mixpanel_region = "us"
mixpanel_project_id = "123456789"
mixpanel_service_account_username = "fake-account.123abc.mp-service-account"
mixpanel_service_account_password = "8iwyKSzwV2brfakepassGGKhsZ3INozo"
}
}
`
const logStreamMixpanelConfigUpdate = `
resource "auth0_log_stream" "my_log_stream" {
name = "Acceptance-Test-LogStream-mixpanel-{{.testName}}"
type = "mixpanel"
sink {
mixpanel_region = "us"
mixpanel_project_id = "987654321"
mixpanel_service_account_username = "fake-account.123abc.mp-service-account"
mixpanel_service_account_password = "8iwyKSzwV2brfakepassGGKhsZ3INozo"
}
}
`

const logStreamMixpanelConfigUpdateWithFilters = `
resource "auth0_log_stream" "my_log_stream" {
name = "Acceptance-Test-LogStream-mixpanel-{{.testName}}"
type = "mixpanel"
filters = [
{
type = "category"
name = "auth.login.fail"
},
{
type = "category"
name = "auth.signup.fail"
}
]
sink {
mixpanel_region = "us"
mixpanel_project_id = "987654321"
mixpanel_service_account_username = "fake-account.123abc.mp-service-account"
mixpanel_service_account_password = "8iwyKSzwV2brfakepassGGKhsZ3INozo"
}
}
`

const logStreamMixpanelConfigUpdateWithEmptyFilters = `
resource "auth0_log_stream" "my_log_stream" {
name = "Acceptance-Test-LogStream-mixpanel-{{.testName}}"
type = "mixpanel"
filters = [ ]
sink {
mixpanel_region = "us"
mixpanel_project_id = "987654321"
mixpanel_service_account_username = "fake-account.123abc.mp-service-account"
mixpanel_service_account_password = "8iwyKSzwV2brfakepassGGKhsZ3INozo"
}
}
`
Loading

0 comments on commit f7d4f43

Please sign in to comment.