Simplify Zendesk driver and share time parsing
Drop the zendeskRole helper and use the raw API role, matching the other drivers. Factor the duplicated RFC3339 timestamp parsing (parseZendeskTime and parseDatadogTime were identical) into a shared parseRFC3339Ptr in driver.go. Signed-off-by: Aurélien Sibiril <81782+aureliensibiril@users.noreply.github.com>
This commit is contained in:
@@ -22,7 +22,6 @@ import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
)
|
||||
@@ -148,7 +147,7 @@ func (d *DatadogDriver) ListAccounts(ctx context.Context) ([]AccountRecord, erro
|
||||
AuthMethod: coredata.AccessEntryAuthMethodUnknown,
|
||||
AccountType: accountType,
|
||||
ExternalID: u.ID,
|
||||
CreatedAt: parseDatadogTime(u.Attributes.CreatedAt),
|
||||
CreatedAt: parseRFC3339Ptr(u.Attributes.CreatedAt),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -197,16 +196,3 @@ func (d *DatadogDriver) queryUsers(ctx context.Context, page int) (*datadogUsers
|
||||
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func parseDatadogTime(s string) *time.Time {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
t, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &t
|
||||
}
|
||||
|
||||
@@ -67,3 +67,19 @@ type Driver interface {
|
||||
// ListAccounts returns all accounts from the source system.
|
||||
ListAccounts(ctx context.Context) ([]AccountRecord, error)
|
||||
}
|
||||
|
||||
// parseRFC3339Ptr parses an RFC 3339 timestamp into a *time.Time, returning
|
||||
// nil for an empty or unparseable value. Drivers use it for best-effort
|
||||
// timestamp fields (created_at, last_login_at) that an API may omit.
|
||||
func parseRFC3339Ptr(s string) *time.Time {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
t, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &t
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"go.probo.inc/probo/pkg/coredata"
|
||||
)
|
||||
@@ -133,7 +132,7 @@ func zendeskRecord(u zendeskUser) AccountRecord {
|
||||
return AccountRecord{
|
||||
Email: u.Email,
|
||||
FullName: u.Name,
|
||||
Role: zendeskRole(u.Role),
|
||||
Role: u.Role,
|
||||
Active: &active,
|
||||
IsAdmin: isAdmin,
|
||||
MFAStatus: mfaStatus,
|
||||
@@ -142,21 +141,8 @@ func zendeskRecord(u zendeskUser) AccountRecord {
|
||||
AuthMethod: coredata.AccessEntryAuthMethodUnknown,
|
||||
AccountType: coredata.AccessEntryAccountTypeUser,
|
||||
ExternalID: strconv.FormatInt(u.ID, 10),
|
||||
LastLogin: parseZendeskTime(lastLogin),
|
||||
CreatedAt: parseZendeskTime(u.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
// zendeskRole title-cases the two staff roles for display; any other value
|
||||
// (custom role names) passes through unchanged.
|
||||
func zendeskRole(role string) string {
|
||||
switch role {
|
||||
case "admin":
|
||||
return "Admin"
|
||||
case "agent":
|
||||
return "Agent"
|
||||
default:
|
||||
return role
|
||||
LastLogin: parseRFC3339Ptr(lastLogin),
|
||||
CreatedAt: parseRFC3339Ptr(u.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -203,16 +189,3 @@ func (d *ZendeskDriver) queryUsers(ctx context.Context, afterCursor string) (*ze
|
||||
|
||||
return &out, nil
|
||||
}
|
||||
|
||||
func parseZendeskTime(s string) *time.Time {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
t, err := time.Parse(time.RFC3339, s)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &t
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ func TestZendeskDriver(t *testing.T) {
|
||||
require.NotNil(t, r.Active)
|
||||
assert.True(t, *r.Active)
|
||||
assert.True(t, r.IsAdmin)
|
||||
assert.Equal(t, "Admin", r.Role)
|
||||
assert.Equal(t, "admin", r.Role)
|
||||
assert.Equal(t, coredata.AccessEntryAccountTypeUser, r.AccountType)
|
||||
assert.Equal(t, coredata.MFAStatusEnabled, r.MFAStatus)
|
||||
assert.Equal(t, coredata.AccessEntryAuthMethodUnknown, r.AuthMethod)
|
||||
@@ -59,7 +59,7 @@ func TestZendeskDriver(t *testing.T) {
|
||||
require.NotNil(t, r2.Active)
|
||||
assert.True(t, *r2.Active)
|
||||
assert.False(t, r2.IsAdmin)
|
||||
assert.Equal(t, "Agent", r2.Role)
|
||||
assert.Equal(t, "agent", r2.Role)
|
||||
assert.Equal(t, coredata.MFAStatusDisabled, r2.MFAStatus)
|
||||
assert.Nil(t, r2.LastLogin)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user