Skip to content

Commit 61c2c26

Browse files
converted the client to an interface and added some implementations for testing and development (unfinished)
1 parent 2fe3e80 commit 61c2c26

5 files changed

Lines changed: 867 additions & 178 deletions

File tree

client/client.go

Lines changed: 79 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -5,235 +5,136 @@ package client
55

66
import (
77
"context"
8-
"errors"
9-
"log"
108
"time"
11-
12-
"github.com/toeirei/keymaster/config"
13-
"github.com/toeirei/keymaster/core"
14-
"github.com/toeirei/keymaster/core/db"
159
)
1610

17-
type Client struct {
18-
//lint:ignore U1000 Placeholder for future configuration wiring.
19-
config config.Config
20-
//lint:ignore U1000 Placeholder for future store wiring.
21-
store core.Store
22-
// NOTE:
23-
// log != audit_log
24-
// log is not meant for cli out
25-
//lint:ignore U1000 Placeholder for future logging wiring.
26-
log *log.Logger
27-
}
11+
type Client interface {
12+
// --- Lifecycle & Initialization ---
2813

29-
// --- Mock types that will later be imported or defined seperately ---
14+
// Close cleans up resources held by the client and closes any open
15+
// connections. Calls should pass a context for cancellation/timeouts.
16+
Close(ctx context.Context) error
3017

31-
// --- Mock types that will later be imported or defined seperately ---
32-
// ID is a local identifier type used by the client API.
33-
type ID int
18+
// --- PublicKey Management ---
3419

35-
// PublicKey represents a public key record.
36-
type PublicKey struct {
37-
// ...
38-
}
20+
CreatePublicKey(ctx context.Context, identity string, tags []string) (PublicKey, error)
3921

40-
// Target represents a remote host or endpoint to deploy keys to.
41-
type Target struct {
42-
// ...
43-
}
22+
GetPublicKey(ctx context.Context, id ID) (PublicKey, error)
4423

45-
// Account represents an account on a target host.
46-
type Account struct {
47-
// ...
48-
}
24+
GetPublicKeys(ctx context.Context, ids ...ID) ([]PublicKey, error)
4925

50-
// DeployProgress reports incremental progress for a deployment operation.
51-
type DeployProgress struct {
52-
Done bool
53-
// ...
54-
}
26+
ListPublicKeys(ctx context.Context, tagFilter string) ([]PublicKey, error)
5527

56-
// OnboardHostProgress reports progress during host onboarding.
57-
type OnboardHostProgress struct {
58-
Done bool
59-
// ...
60-
}
28+
UpdatePublicKeyTags(ctx context.Context, id ID, tags []string) error
6129

62-
// DecommisionTargetProgress reports progress for target decommissioning.
63-
type DecommisionTargetProgress struct {
64-
Done bool
65-
// ...
66-
}
30+
DeletePublicKeys(ctx context.Context, ids ...ID) error
6731

68-
// DecommisionAccountProgress reports progress for account decommissioning.
69-
type DecommisionAccountProgress struct {
70-
Done bool
71-
// ...
72-
}
32+
// --- Target Management ---
7333

74-
// --- Lifecycle & Initialization ---
75-
76-
// New creates and initializes a new `Client` from the provided `Config` and
77-
// `logger`. The implementation should connect to the backing store, run any
78-
// migrations and return a ready-to-use client. Currently unimplemented.
79-
func New(config config.Config, logger *log.Logger) (*Client, error) {
80-
// db.New(config.Database.Type, config.Database.Dsn)
81-
store, err := db.NewStoreFromDSN(config.Database.Type, config.Database.Dsn)
82-
_ = store // TODO can't use store yet, as it does not implement core.Store (wich it shouldn't but hey)
83-
if err != nil {
84-
return nil, err
85-
}
86-
87-
return &Client{
88-
config: config,
89-
log: logger,
90-
// store: core.Store(store),
91-
}, nil
92-
}
34+
CreateTarget(ctx context.Context, host string, port int /* , gateway string, plugin string */) (Target, error)
9335

94-
// Close cleans up resources held by the client and closes any open
95-
// connections. Calls should pass a context for cancellation/timeouts.
96-
func (c *Client) Close(ctx context.Context) error {
97-
return errors.New("client.Close not implemented")
98-
}
36+
GetTarget(ctx context.Context, id ID) (Target, error)
9937

100-
// --- PublicKey Management ---
38+
GetTargets(ctx context.Context, ids ...ID) ([]Target, error)
10139

102-
func (c *Client) CreatePublicKey(ctx context.Context, identity string, tags []string) (ID, error) {
103-
return 0, errors.New("client.CreatePublicKey not implemented")
104-
}
40+
ListTargets(ctx context.Context) ([]Target, error)
10541

106-
func (c *Client) GetPublicKey(ctx context.Context, id ID) (PublicKey, error) {
107-
return PublicKey{}, errors.New("client.GetPublicKey not implemented")
108-
}
42+
UpdateTarget(ctx context.Context, id ID, target Target) error
10943

110-
func (c *Client) GetPublicKeys(ctx context.Context, id ...ID) ([]PublicKey, error) {
111-
return nil, errors.New("client.GetPublicKeys not implemented")
112-
}
44+
DeleteTargets(ctx context.Context, ids ...ID) error
11345

114-
func (c *Client) ListPublicKeys(ctx context.Context, tagFilter string) ([]PublicKey, error) {
115-
return nil, errors.New("client.ListPublicKeys not implemented")
116-
}
46+
// --- Account Management ---
11747

118-
func (c *Client) UpdatePublicKeyTags(ctx context.Context, id ID, tags []string) error {
119-
return errors.New("client.UpdatePublicKeyTags not implemented")
120-
}
48+
CreateAccount(ctx context.Context, targetID ID, name string, deploymentKey string) (Account, error)
12149

122-
func (c *Client) DeletePublicKeys(ctx context.Context, id ...ID) error {
123-
return errors.New("client.DeletePublicKeys not implemented")
124-
}
50+
GetAccount(ctx context.Context, id ID) (Account, error)
12551

126-
// --- Target Management ---
52+
GetAccounts(ctx context.Context, ids ...ID) ([]Account, error)
12753

128-
func (c *Client) CreateTarget(ctx context.Context, host string, port int /* , gateway string, plugin string */) (ID, error) {
129-
return 0, errors.New("client.CreateTarget not implemented")
130-
}
54+
ListAccountsByTarget(ctx context.Context, targetID ID) ([]Account, error)
13155

132-
func (c *Client) GetTarget(ctx context.Context, id ID) (Target, error) {
133-
return Target{}, errors.New("client.GetTarget not implemented")
134-
}
56+
DeleteAccounts(ctx context.Context, ids ...ID) error
13557

136-
func (c *Client) GetTargets(ctx context.Context, id ...ID) ([]Target, error) {
137-
return nil, errors.New("client.GetTargets not implemented")
138-
}
58+
GetDirtyAccounts(ctx context.Context) ([]Account, error)
13959

140-
func (c *Client) ListTargets(ctx context.Context) ([]Target, error) {
141-
// ListTargets returns all known deployment targets.
142-
return nil, errors.New("client.ListTargets not implemented")
143-
}
60+
// --- Tag to Account Management ---
14461

145-
func (c *Client) UpdateTarget(ctx context.Context, id ID, target Target) error {
146-
// UpdateTarget updates the target record identified by `id`.
147-
return errors.New("client.UpdateTarget not implemented")
148-
}
62+
LinkTagAccount(ctx context.Context, accountID ID, filter string, expiresAt time.Time) (ID, error)
14963

150-
func (c *Client) DeleteTargets(ctx context.Context, id ...ID) error {
151-
// DeleteTargets removes targets identified by the given ids.
152-
return errors.New("client.DeleteTargets not implemented")
153-
}
64+
UnLinkTagAccount(ctx context.Context, linkIDs ...ID) error
15465

155-
// --- Account Management ---
66+
ResolvePublicKeysForAccount(ctx context.Context, accountID ID) ([]PublicKey, error)
15667

157-
// CreateAccount creates an account on `targetID` with the given name and
158-
// deployment key.
159-
func (c *Client) CreateAccount(ctx context.Context, targetID ID, name string, deploymentKey string) (ID, error) {
160-
return 0, errors.New("client.CreateAccount not implemented")
161-
}
68+
ResolveAccountsForPublicKey(ctx context.Context, publicKeyID ID) ([]Account, error)
16269

163-
// GetAccount returns account metadata for `id`.
164-
func (c *Client) GetAccount(ctx context.Context, id ID) (Account, error) {
165-
return Account{}, errors.New("client.GetAccount not implemented")
166-
}
70+
// --- Onboarding & Decommision ---
16771

168-
// ListAccountsByTarget lists accounts associated with `targetID`.
169-
func (c *Client) ListAccountsByTarget(ctx context.Context, targetID ID) ([]Account, error) {
170-
return nil, errors.New("client.ListAccountsByTarget not implemented")
171-
}
72+
OnboardHost(ctx context.Context, host string, port int /* , gateway string, plugin string */, accountName string, deploymentKey string) (chan OnboardHostProgress, error)
17273

173-
// GetDirtyAccounts returns accounts that require reconciliation or deployment.
174-
func (c *Client) GetDirtyAccounts(ctx context.Context) ([]Account, error) {
175-
return nil, errors.New("client.GetDirtyAccounts not implemented")
176-
}
74+
DecommisionTarget(ctx context.Context, id ID) (chan DecommisionTargetProgress, error)
17775

178-
// --- Tag to Account Management ---
76+
DecommisionAccount(ctx context.Context, id ID) (chan DecommisionAccountProgress, error)
17977

180-
// LinkTagAccount associates a tag filter (e.g. "device:mobile&company:telekom") with
181-
// an `accountID` until `expiresAt`.
182-
func (c *Client) LinkTagAccount(ctx context.Context, accountID ID, filter string, expiresAt time.Time) (ID, error) {
183-
return 0, errors.New("client.LinkTagAccount not implemented")
184-
}
78+
// --- Deploy stuff ---
18579

186-
// UnLinkTagAccount removes previously created tag-account links.
187-
func (c *Client) UnLinkTagAccount(ctx context.Context, linkIDs ...ID) error {
188-
return errors.New("client.UnLinkTagAccount not implemented")
189-
}
80+
DeployPublicKeys(ctx context.Context, publicKeyID ...ID) (chan DeployProgress, error)
19081

191-
// ResolvePublicKeysForAccount returns public keys applicable to `accountID`.
192-
func (c *Client) ResolvePublicKeysForAccount(ctx context.Context, accountID ID) ([]PublicKey, error) {
193-
return nil, errors.New("client.ResolvePublicKeysForAccount not implemented")
194-
}
82+
DeployTargets(ctx context.Context, targetID ...ID) (chan DeployProgress, error)
83+
84+
DeployAccounts(ctx context.Context, accountID ...ID) (chan DeployProgress, error)
19585

196-
// ResolveAccountsForPublicKey returns accounts that a public key applies to.
197-
func (c *Client) ResolveAccountsForPublicKey(ctx context.Context, publicKeyID ID) ([]Account, error) {
198-
return nil, errors.New("client.ResolveAccountsForPublicKey not implemented")
86+
DeployAll(ctx context.Context) (chan DeployProgress, error)
19987
}
20088

201-
// --- Onboarding & Decommision ---
89+
// ID is a local identifier type used by the client API.
90+
type ID int
20291

203-
// OnboardHost starts onboarding of a host and returns a progress channel.
204-
func (c *Client) OnboardHost(ctx context.Context, host string, port int /* , gateway string, plugin string */, accountName string, deploymentKey string) (chan OnboardHostProgress, error) {
205-
return nil, errors.New("client.OnboardHost not implemented")
92+
// PublicKey represents a public key record.
93+
type PublicKey struct {
94+
id ID
95+
identity string
96+
tags []string
97+
// ...
20698
}
20799

208-
// DecommisionTarget decommissions a deployment target and streams progress.
209-
func (c *Client) DecommisionTarget(ctx context.Context, id ID) (chan DecommisionTargetProgress, error) {
210-
return nil, errors.New("client.DecommisionTarget not implemented")
100+
// Target represents a remote host or endpoint to deploy keys to.
101+
type Target struct {
102+
id ID
103+
host string
104+
port int
105+
// ...
211106
}
212107

213-
// DecommisionAccount decommissions an account and streams progress.
214-
func (c *Client) DecommisionAccount(ctx context.Context, id ID) (chan DecommisionAccountProgress, error) {
215-
return nil, errors.New("client.DecommisionAccount not implemented")
108+
// Account represents an account on a target host.
109+
type Account struct {
110+
id ID
111+
targetID ID
112+
name string
113+
deploymentKey string
114+
// ...
216115
}
217116

218-
// --- Deploy stuff ---
219-
220-
// DeployPublicKeys deploys public keys to their target accounts and reports
221-
// progress on the returned channel.
222-
func (c *Client) DeployPublicKeys(ctx context.Context, publicKeyID ...ID) (chan DeployProgress, error) {
223-
return nil, errors.New("client.DeployPublicKeys not implemented")
117+
// DeployProgress reports incremental progress for a deployment operation.
118+
type DeployProgress struct {
119+
Percent float32
120+
Targets map[Target]float32
121+
// ...
224122
}
225123

226-
// DeployTargets deploys to the specified target ids and streams progress.
227-
func (c *Client) DeployTargets(ctx context.Context, targetID ...ID) (chan DeployProgress, error) {
228-
return nil, errors.New("client.DeployTargets not implemented")
124+
// OnboardHostProgress reports progress during host onboarding.
125+
type OnboardHostProgress struct {
126+
Percent float32
127+
// ...
229128
}
230129

231-
// DeployAccounts deploys to the specified account ids and streams progress.
232-
func (c *Client) DeployAccounts(ctx context.Context, accountID ...ID) (chan DeployProgress, error) {
233-
return nil, errors.New("client.DeployAccounts not implemented")
130+
// DecommisionTargetProgress reports progress for target decommissioning.
131+
type DecommisionTargetProgress struct {
132+
Percent float32
133+
// ...
234134
}
235135

236-
// DeployAll triggers deployment for all pending targets/accounts.
237-
func (c *Client) DeployAll(ctx context.Context) (chan DeployProgress, error) {
238-
return nil, errors.New("client.DeployAll not implemented")
136+
// DecommisionAccountProgress reports progress for account decommissioning.
137+
type DecommisionAccountProgress struct {
138+
Percent float32
139+
// ...
239140
}

0 commit comments

Comments
 (0)