* move config unions to common code The configuration union handlers in Storage and Account overlapped significantly in behavior. Moving those helpers into a common code folder was requested. Although the behavior was similar across the files, the types were not, requiring the addition of generics to solve the need.
60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package account
|
|
|
|
import (
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/alcionai/corso/pkg/credentials"
|
|
)
|
|
|
|
type M365Config struct {
|
|
credentials.M365 // requires: ClientID, ClientSecret, TenantID
|
|
|
|
// (todo) TenantID string
|
|
}
|
|
|
|
// config key consts
|
|
const (
|
|
keyM365ClientID = "m365_clientID"
|
|
keyM365ClientSecret = "m365_clientSecret"
|
|
keyM365TenantID = "m365_tenantID"
|
|
)
|
|
|
|
// config exported name consts
|
|
const (
|
|
// (todo) TenantID = "TENANT_ID"
|
|
)
|
|
|
|
func (c M365Config) StringConfig() (map[string]string, error) {
|
|
cfg := map[string]string{
|
|
keyM365ClientID: c.ClientID,
|
|
keyM365ClientSecret: c.ClientSecret,
|
|
keyM365TenantID: c.TenantID,
|
|
}
|
|
return cfg, c.validate()
|
|
}
|
|
|
|
// M365Config retrieves the M365Config details from the Account config.
|
|
func (a Account) M365Config() (M365Config, error) {
|
|
c := M365Config{}
|
|
if len(a.Config) > 0 {
|
|
c.ClientID = a.Config[keyM365ClientID]
|
|
c.ClientSecret = a.Config[keyM365ClientSecret]
|
|
c.TenantID = a.Config[keyM365TenantID]
|
|
}
|
|
return c, c.validate()
|
|
}
|
|
|
|
func (c M365Config) validate() error {
|
|
check := map[string]string{
|
|
credentials.ClientID: c.ClientID,
|
|
credentials.ClientSecret: c.ClientSecret,
|
|
credentials.TenantID: c.TenantID,
|
|
}
|
|
for k, v := range check {
|
|
if len(v) == 0 {
|
|
return errors.Wrap(errMissingRequired, k)
|
|
}
|
|
}
|
|
return nil
|
|
}
|