Files

48 lines
1.4 KiB
Go

package judgement
import (
"context"
"fmt"
"github.com/samcharles93/archie-core/modules/connectors"
)
// FactsSource answers what the connectors' systems of record know about a
// target, at the moment it is asked.
type FactsSource interface {
TargetFacts(ctx context.Context, t connectors.Target) (connectors.TargetFacts, error)
}
// Identities says which account holds an identity, such as "servicenow:jdoe".
type Identities interface {
AccountOf(ctx context.Context, identity string) (id string, ok bool, err error)
}
// RecordedFacts looks a target's facts up in the connectors each time and
// keeps none. The owner a connector names counts only when the identity is
// linked to an account, so the request can be put to them.
type RecordedFacts struct {
Source FactsSource
Accounts Identities
}
// Facts asks the connectors about t and maps the owner they name to an account.
func (r RecordedFacts) Facts(ctx context.Context, t connectors.Target) (TargetFacts, error) {
got, err := r.Source.TargetFacts(ctx, t)
if err != nil {
return TargetFacts{}, fmt.Errorf("ask connectors: %w", err)
}
facts := TargetFacts{Count: got.Count, DataClass: got.DataClass, OwnerIdentity: got.OwnerIdentity}
if got.OwnerIdentity == "" {
return facts, nil
}
id, ok, err := r.Accounts.AccountOf(ctx, got.OwnerIdentity)
if err != nil {
return TargetFacts{}, fmt.Errorf("match owner %s: %w", got.OwnerIdentity, err)
}
if ok {
facts.Owner = id
}
return facts, nil
}