-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathclient.go
39 lines (34 loc) · 1.78 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package onepassword
import (
"context"
"errors"
"github.com/1Password/connect-sdk-go/onepassword"
"github.com/1Password/terraform-provider-onepassword/internal/onepassword/cli"
"github.com/1Password/terraform-provider-onepassword/internal/onepassword/connect"
)
// Client is a subset of connect.Client with context added.
type Client interface {
GetVault(ctx context.Context, uuid string) (*onepassword.Vault, error)
GetVaultsByTitle(ctx context.Context, title string) ([]onepassword.Vault, error)
GetItem(ctx context.Context, itemUuid, vaultUuid string) (*onepassword.Item, error)
GetItemByTitle(ctx context.Context, title string, vaultUuid string) (*onepassword.Item, error)
CreateItem(ctx context.Context, item *onepassword.Item, vaultUuid string) (*onepassword.Item, error)
UpdateItem(ctx context.Context, item *onepassword.Item, vaultUuid string) (*onepassword.Item, error)
DeleteItem(ctx context.Context, item *onepassword.Item, vaultUuid string) error
GetFileContent(ctx context.Context, file *onepassword.File, itemUUid, vaultUuid string) ([]byte, error)
}
type ClientConfig struct {
ConnectHost string
ConnectToken string
ServiceAccountToken string
Account string
OpCLIPath string
}
func NewClient(config ClientConfig) (Client, error) {
if config.ServiceAccountToken != "" || config.Account != "" {
return cli.NewClient(config.ServiceAccountToken, config.Account, config.OpCLIPath), nil
} else if config.ConnectHost != "" && config.ConnectToken != "" {
return connect.NewClient(config.ConnectHost, config.ConnectToken, config.OpCLIPath), nil
}
return nil, errors.New("Invalid provider configuration. Either Connect credentials (\"token\" and \"url\") or Service Account (\"service_account_token\" or \"account\") credentials should be set.")
}