* introduce repository api scaffold with s3 (#6) introduces the public repository package in /pkg, with a skeleton implementation for generating the s3 provider config and boilerplate for where that usage gets centralized. * condense init/connect to single action A double-pass NewRepo() && Connect() allows Repository structs to exist independent guarantees that their usage will be successful. This hides the struct generation within the Connect and Initialize funcs so that the construction and guarantees can occur as a single process.
85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/kopia/kopia/repo/blob"
|
|
)
|
|
|
|
type repoProvider int
|
|
|
|
//go:generate stringer -type=repoProvider
|
|
const (
|
|
ProviderUnknown repoProvider = iota // Unknown Provider
|
|
ProviderS3 // S3
|
|
)
|
|
|
|
// Repository contains storage provider information.
|
|
type Repository struct {
|
|
ID uuid.UUID
|
|
CreatedAt time.Time
|
|
Version string // in case of future breaking changes
|
|
|
|
Provider repoProvider // must be repository.S3Provider
|
|
Account Account // the user's m365 account connection details
|
|
Config Config // provider-based configuration details
|
|
}
|
|
|
|
// Account holds the user's m365 account details.
|
|
type Account struct {
|
|
TenantID string
|
|
ClientID string
|
|
ClientSecret string
|
|
}
|
|
|
|
type Config interface {
|
|
KopiaStorage(ctx context.Context, create bool) (blob.Storage, error)
|
|
}
|
|
|
|
// Initialize will:
|
|
// * validate the m365 account & secrets
|
|
// * connect to the m365 account to ensure communication capability
|
|
// * validate the provider config & secrets
|
|
// * initialize the kopia repo with the provider
|
|
// * store the configuration details
|
|
// * connect to the provider
|
|
// * return the connected repository
|
|
func Initialize(
|
|
ctx context.Context,
|
|
provider repoProvider,
|
|
acct Account,
|
|
cfg Config,
|
|
) (Repository, error) {
|
|
r := Repository{
|
|
ID: uuid.New(),
|
|
Version: "v1",
|
|
Provider: provider,
|
|
Account: acct,
|
|
Config: cfg,
|
|
}
|
|
return r, nil
|
|
}
|
|
|
|
// Connect will:
|
|
// * validate the m365 account details
|
|
// * connect to the m365 account to ensure communication capability
|
|
// * connect to the provider storage
|
|
// * return the connected repository
|
|
func Connect(
|
|
ctx context.Context,
|
|
provider repoProvider,
|
|
acct Account,
|
|
cfg Config,
|
|
) (Repository, error) {
|
|
// todo: ID and CreatedAt should get retrieved from a stored kopia config.
|
|
r := Repository{
|
|
Version: "v1",
|
|
Provider: provider,
|
|
Account: acct,
|
|
Config: cfg,
|
|
}
|
|
return r, nil
|
|
}
|