Skip to content

Commit 2e53bf4

Browse files
added mock deployments and transaction support to testui client
1 parent d33cdf6 commit 2e53bf4

6 files changed

Lines changed: 187 additions & 76 deletions

File tree

client/client.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ package client
66
import (
77
"context"
88
"time"
9+
10+
"github.com/toeirei/keymaster/util/slicest"
911
)
1012

1113
type Client interface {
1214
// --- Lifecycle & Initialization ---
1315

1416
Close(ctx context.Context) error
1517

18+
WithTransaction(ctx context.Context, fn func(c Client) error) error
19+
1620
// --- PublicKey Management ---
1721

1822
CreatePublicKey(ctx context.Context, key string, comment string, tags []string) (PublicKey, error)
@@ -47,19 +51,19 @@ type Client interface {
4751

4852
// --- Link Management ---
4953

50-
CreateLink(ctx context.Context, accountID AccountId, tagFilter string, expiresAt time.Time) (Link, error)
54+
CreateLink(ctx context.Context, accountId AccountId, tagFilter string, expiresAt time.Time) (Link, error)
5155

5256
GetLink(ctx context.Context, id LinkId) (Link, error)
5357

5458
GetLinks(ctx context.Context, ids ...LinkId) ([]Link, error)
5559

56-
ListLinksAccount(ctx context.Context, accountID AccountId) ([]Link, error)
60+
ListLinksAccount(ctx context.Context, accountId AccountId) ([]Link, error)
5761

58-
ListLinksPublicKey(ctx context.Context, publicKeyID PublicKeyId) ([]Link, error)
62+
ListLinksPublicKey(ctx context.Context, publicKeyId PublicKeyId) ([]Link, error)
5963

60-
ListPublicKeysForAccount(ctx context.Context, accountID AccountId) ([]PublicKey, error)
64+
ListPublicKeysForAccount(ctx context.Context, accountId AccountId) ([]PublicKey, error)
6165

62-
ListAccountsForPublicKey(ctx context.Context, publicKeyID PublicKeyId) ([]Account, error)
66+
ListAccountsForPublicKey(ctx context.Context, publicKeyId PublicKeyId) ([]Account, error)
6367

6468
UpdateLink(ctx context.Context, id LinkId, accountId AccountId, tagFilter string, expiresAt time.Time) error
6569

@@ -73,14 +77,14 @@ type Client interface {
7377

7478
DecommisionAccount(ctx context.Context, id AccountId) (chan DecommisionAccountProgress, error)
7579

76-
DeployPublicKeys(ctx context.Context, publicKeyID ...PublicKeyId) (chan DeployProgress, error)
80+
DeployPublicKeys(ctx context.Context, publicKeyId ...PublicKeyId) (chan DeployProgress, error)
7781

78-
DeployAccounts(ctx context.Context, accountID ...AccountId) (chan DeployProgress, error)
82+
DeployAccounts(ctx context.Context, accountId ...AccountId) (chan DeployProgress, error)
7983

8084
DeployAll(ctx context.Context) (chan DeployProgress, error)
8185
}
8286

83-
// ID is a local identifier type used by the client API.
87+
// id is a local identifier type used by the client API.
8488
type id = int
8589

8690
// PublicKey represents a public key record.
@@ -116,11 +120,22 @@ type Link struct {
116120
// ...
117121
}
118122

123+
type DeployAccountProgress struct {
124+
Progress float64
125+
Status string
126+
Err error
127+
}
128+
119129
// DeployProgress reports incremental progress for a deployment operation.
120130
type DeployProgress struct {
121-
Percent float64
122-
Targets map[*Account]float64
123-
// ...
131+
Accounts map[AccountId]*DeployAccountProgress
132+
}
133+
134+
func (dp DeployProgress) TotalProgress() float64 {
135+
return slicest.Reduce(
136+
slicest.MapValues(dp.Accounts),
137+
func(dap *DeployAccountProgress, total float64) float64 { return total + dap.Progress },
138+
) / float64(len(dp.Accounts))
124139
}
125140

126141
// OnboardHostProgress reports progress during host onboarding.

client/mock/client.go

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ type Client struct {
1818

1919
type ClientOverwrites struct {
2020
// --- Lifecycle & Initialization ---
21-
Close func(ctx context.Context) error
21+
Close func(ctx context.Context) error
22+
WithTransaction func(ctx context.Context, fn func(c client.Client) error) error
2223

2324
// --- PublicKey Management ---
2425
CreatePublicKey func(ctx context.Context, key string, comment string, tags []string) (client.PublicKey, error)
@@ -39,22 +40,22 @@ type ClientOverwrites struct {
3940
IsAccountDirty func(ctx context.Context, account client.Account) (bool, error)
4041

4142
// --- Link Management ---
42-
CreateLink func(ctx context.Context, accountID client.AccountId, tagFilter string, expiresAt time.Time) (client.Link, error)
43+
CreateLink func(ctx context.Context, accountId client.AccountId, tagFilter string, expiresAt time.Time) (client.Link, error)
4344
GetLink func(ctx context.Context, id client.LinkId) (client.Link, error)
4445
GetLinks func(ctx context.Context, ids ...client.LinkId) ([]client.Link, error)
45-
ListLinksAccount func(ctx context.Context, accountID client.AccountId) ([]client.Link, error)
46-
ListLinksPublicKey func(ctx context.Context, publicKeyID client.PublicKeyId) ([]client.Link, error)
47-
ListPublicKeysForAccount func(ctx context.Context, accountID client.AccountId) ([]client.PublicKey, error)
48-
ListAccountsForPublicKey func(ctx context.Context, publicKeyID client.PublicKeyId) ([]client.Account, error)
46+
ListLinksAccount func(ctx context.Context, accountId client.AccountId) ([]client.Link, error)
47+
ListLinksPublicKey func(ctx context.Context, publicKeyId client.PublicKeyId) ([]client.Link, error)
48+
ListPublicKeysForAccount func(ctx context.Context, accountId client.AccountId) ([]client.PublicKey, error)
49+
ListAccountsForPublicKey func(ctx context.Context, publicKeyId client.PublicKeyId) ([]client.Account, error)
4950
UpdateLink func(ctx context.Context, id client.LinkId, accountId client.AccountId, tagFilter string, expiresAt time.Time) error
5051
DeleteLinks func(ctx context.Context, ids ...client.LinkId) error
5152

5253
// --- Other ---
5354
ListExistingTags func(ctx context.Context) []string
5455
OnboardHost func(ctx context.Context, host string, port int /* , gateway string, plugin string */, accountName string, deploymentKey string) (chan client.OnboardHostProgress, error)
5556
DecommisionAccount func(ctx context.Context, id client.AccountId) (chan client.DecommisionAccountProgress, error)
56-
DeployPublicKeys func(ctx context.Context, publicKeyID ...client.PublicKeyId) (chan client.DeployProgress, error)
57-
DeployAccounts func(ctx context.Context, accountID ...client.AccountId) (chan client.DeployProgress, error)
57+
DeployPublicKeys func(ctx context.Context, publicKeyId ...client.PublicKeyId) (chan client.DeployProgress, error)
58+
DeployAccounts func(ctx context.Context, accountId ...client.AccountId) (chan client.DeployProgress, error)
5859
DeployAll func(ctx context.Context) (chan client.DeployProgress, error)
5960
}
6061

@@ -112,6 +113,18 @@ func (m *Client) Close(ctx context.Context) error {
112113
panic("Client.Close not implemented")
113114
}
114115

116+
func (m *Client) WithTransaction(ctx context.Context, fn func(c client.Client) error) error {
117+
if m.Pre != nil {
118+
m.Pre("WithTransaction", map[string]any{"ctx": ctx, "fn": fn})
119+
}
120+
if m.Overwrites.WithTransaction != nil {
121+
return m.Overwrites.WithTransaction(ctx, fn)
122+
} else if m.BaseClient != nil {
123+
return m.BaseClient.WithTransaction(ctx, fn)
124+
}
125+
panic("Client.WithTransaction not implemented")
126+
}
127+
115128
// --- PublicKey Management ---
116129

117130
func (m *Client) CreatePublicKey(ctx context.Context, key string, comment string, tags []string) (client.PublicKey, error) {
@@ -292,14 +305,14 @@ func (m *Client) IsAccountDirty(ctx context.Context, account client.Account) (bo
292305

293306
// --- Link Management ---
294307

295-
func (m *Client) CreateLink(ctx context.Context, accountID client.AccountId, tagFilter string, expiresAt time.Time) (client.Link, error) {
308+
func (m *Client) CreateLink(ctx context.Context, accountId client.AccountId, tagFilter string, expiresAt time.Time) (client.Link, error) {
296309
if m.Pre != nil {
297-
m.Pre("CreateLink", map[string]any{"ctx": ctx, "accountID": accountID, "tagFilter": tagFilter, "expiresAt": expiresAt})
310+
m.Pre("CreateLink", map[string]any{"ctx": ctx, "accountId": accountId, "tagFilter": tagFilter, "expiresAt": expiresAt})
298311
}
299312
if m.Overwrites.CreateLink != nil {
300-
return m.Overwrites.CreateLink(ctx, accountID, tagFilter, expiresAt)
313+
return m.Overwrites.CreateLink(ctx, accountId, tagFilter, expiresAt)
301314
} else if m.BaseClient != nil {
302-
return m.BaseClient.CreateLink(ctx, accountID, tagFilter, expiresAt)
315+
return m.BaseClient.CreateLink(ctx, accountId, tagFilter, expiresAt)
303316
}
304317
panic("Client.CreateLink not implemented")
305318
}
@@ -328,50 +341,50 @@ func (m *Client) GetLinks(ctx context.Context, ids ...client.LinkId) ([]client.L
328341
panic("Client.GetLinks not implemented")
329342
}
330343

331-
func (m *Client) ListLinksAccount(ctx context.Context, accountID client.AccountId) ([]client.Link, error) {
344+
func (m *Client) ListLinksAccount(ctx context.Context, accountId client.AccountId) ([]client.Link, error) {
332345
if m.Pre != nil {
333-
m.Pre("ListLinksAccount", map[string]any{"ctx": ctx, "accountID": accountID})
346+
m.Pre("ListLinksAccount", map[string]any{"ctx": ctx, "accountId": accountId})
334347
}
335348
if m.Overwrites.ListLinksAccount != nil {
336-
return m.Overwrites.ListLinksAccount(ctx, accountID)
349+
return m.Overwrites.ListLinksAccount(ctx, accountId)
337350
} else if m.BaseClient != nil {
338-
return m.BaseClient.ListLinksAccount(ctx, accountID)
351+
return m.BaseClient.ListLinksAccount(ctx, accountId)
339352
}
340353
panic("Client.ListLinksAccount not implemented")
341354
}
342355

343-
func (m *Client) ListLinksPublicKey(ctx context.Context, publicKeyID client.PublicKeyId) ([]client.Link, error) {
356+
func (m *Client) ListLinksPublicKey(ctx context.Context, publicKeyId client.PublicKeyId) ([]client.Link, error) {
344357
if m.Pre != nil {
345-
m.Pre("ListLinksPublicKey", map[string]any{"ctx": ctx, "publicKeyID": publicKeyID})
358+
m.Pre("ListLinksPublicKey", map[string]any{"ctx": ctx, "publicKeyId": publicKeyId})
346359
}
347360
if m.Overwrites.ListLinksPublicKey != nil {
348-
return m.Overwrites.ListLinksPublicKey(ctx, publicKeyID)
361+
return m.Overwrites.ListLinksPublicKey(ctx, publicKeyId)
349362
} else if m.BaseClient != nil {
350-
return m.BaseClient.ListLinksPublicKey(ctx, publicKeyID)
363+
return m.BaseClient.ListLinksPublicKey(ctx, publicKeyId)
351364
}
352365
panic("Client.ListLinksPublicKey not implemented")
353366
}
354367

355-
func (m *Client) ListPublicKeysForAccount(ctx context.Context, accountID client.AccountId) ([]client.PublicKey, error) {
368+
func (m *Client) ListPublicKeysForAccount(ctx context.Context, accountId client.AccountId) ([]client.PublicKey, error) {
356369
if m.Pre != nil {
357-
m.Pre("ListPublicKeysForAccount", map[string]any{"ctx": ctx, "accountID": accountID})
370+
m.Pre("ListPublicKeysForAccount", map[string]any{"ctx": ctx, "accountId": accountId})
358371
}
359372
if m.Overwrites.ListPublicKeysForAccount != nil {
360-
return m.Overwrites.ListPublicKeysForAccount(ctx, accountID)
373+
return m.Overwrites.ListPublicKeysForAccount(ctx, accountId)
361374
} else if m.BaseClient != nil {
362-
return m.BaseClient.ListPublicKeysForAccount(ctx, accountID)
375+
return m.BaseClient.ListPublicKeysForAccount(ctx, accountId)
363376
}
364377
panic("Client.ListPublicKeysForAccount not implemented")
365378
}
366379

367-
func (m *Client) ListAccountsForPublicKey(ctx context.Context, publicKeyID client.PublicKeyId) ([]client.Account, error) {
380+
func (m *Client) ListAccountsForPublicKey(ctx context.Context, publicKeyId client.PublicKeyId) ([]client.Account, error) {
368381
if m.Pre != nil {
369-
m.Pre("ListAccountsForPublicKey", map[string]any{"ctx": ctx, "publicKeyID": publicKeyID})
382+
m.Pre("ListAccountsForPublicKey", map[string]any{"ctx": ctx, "publicKeyId": publicKeyId})
370383
}
371384
if m.Overwrites.ListAccountsForPublicKey != nil {
372-
return m.Overwrites.ListAccountsForPublicKey(ctx, publicKeyID)
385+
return m.Overwrites.ListAccountsForPublicKey(ctx, publicKeyId)
373386
} else if m.BaseClient != nil {
374-
return m.BaseClient.ListAccountsForPublicKey(ctx, publicKeyID)
387+
return m.BaseClient.ListAccountsForPublicKey(ctx, publicKeyId)
375388
}
376389
panic("Client.ListAccountsForPublicKey not implemented")
377390
}
@@ -438,26 +451,26 @@ func (m *Client) DecommisionAccount(ctx context.Context, id client.AccountId) (c
438451
panic("Client.DecommisionAccount not implemented")
439452
}
440453

441-
func (m *Client) DeployPublicKeys(ctx context.Context, publicKeyID ...client.PublicKeyId) (chan client.DeployProgress, error) {
454+
func (m *Client) DeployPublicKeys(ctx context.Context, publicKeyId ...client.PublicKeyId) (chan client.DeployProgress, error) {
442455
if m.Pre != nil {
443-
m.Pre("DeployPublicKeys", map[string]any{"ctx": ctx, "publicKeyID": publicKeyID})
456+
m.Pre("DeployPublicKeys", map[string]any{"ctx": ctx, "publicKeyId": publicKeyId})
444457
}
445458
if m.Overwrites.DeployPublicKeys != nil {
446-
return m.Overwrites.DeployPublicKeys(ctx, publicKeyID...)
459+
return m.Overwrites.DeployPublicKeys(ctx, publicKeyId...)
447460
} else if m.BaseClient != nil {
448-
return m.BaseClient.DeployPublicKeys(ctx, publicKeyID...)
461+
return m.BaseClient.DeployPublicKeys(ctx, publicKeyId...)
449462
}
450463
panic("Client.DeployPublicKeys not implemented")
451464
}
452465

453-
func (m *Client) DeployAccounts(ctx context.Context, accountID ...client.AccountId) (chan client.DeployProgress, error) {
466+
func (m *Client) DeployAccounts(ctx context.Context, accountId ...client.AccountId) (chan client.DeployProgress, error) {
454467
if m.Pre != nil {
455-
m.Pre("DeployAccounts", map[string]any{"ctx": ctx, "accountID": accountID})
468+
m.Pre("DeployAccounts", map[string]any{"ctx": ctx, "accountId": accountId})
456469
}
457470
if m.Overwrites.DeployAccounts != nil {
458-
return m.Overwrites.DeployAccounts(ctx, accountID...)
471+
return m.Overwrites.DeployAccounts(ctx, accountId...)
459472
} else if m.BaseClient != nil {
460-
return m.BaseClient.DeployAccounts(ctx, accountID...)
473+
return m.BaseClient.DeployAccounts(ctx, accountId...)
461474
}
462475
panic("Client.DeployAccounts not implemented")
463476
}

0 commit comments

Comments
 (0)