Skip to content

Commit 0f68012

Browse files
authored
refactor: move ClientOptions to environment/types (#255)
* Preparation for Converged Client: - Go and toolchain versions bump up - Linter version bump up - ClientOptions and related structure moved to types - Fix linter errors * Fix lint errors * Add more unit-test to pass coverage requirements * More tests * Fix lint errors * Fix hash is equal comment * Fix review comments
1 parent 0b0288b commit 0f68012

19 files changed

+1413
-141
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
/.work
33
/_output
44
cover.out
5+
coverage.out
56
/vendor
67
/.vendor-new
78
.DS_Store

.golangci.yaml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
version: 2
2+
13
linters:
24
enable:
35
- errcheck
4-
- gofmt
5-
- gofumpt
6-
- gosimple
76
- govet
87
- ineffassign
98
- nolintlint

devbox.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -438,50 +438,50 @@
438438
}
439439
},
440440
"golangci-lint@latest": {
441-
"last_modified": "2024-07-31T08:48:38Z",
442-
"resolved": "github:NixOS/nixpkgs/c3392ad349a5227f4a3464dce87bcc5046692fce#golangci-lint",
441+
"last_modified": "2025-08-03T19:18:05Z",
442+
"resolved": "github:NixOS/nixpkgs/bf9fa86a9b1005d932f842edf2c38eeecc98eef3#golangci-lint",
443443
"source": "devbox-search",
444-
"version": "1.59.1",
444+
"version": "2.3.1",
445445
"systems": {
446446
"aarch64-darwin": {
447447
"outputs": [
448448
{
449449
"name": "out",
450-
"path": "/nix/store/k3rnfx753gnx5v8l3i4sx1g1al2ds0di-golangci-lint-1.59.1",
450+
"path": "/nix/store/ahn380skfkdg3ynimjpqakc1wp26mpsi-golangci-lint-2.3.1",
451451
"default": true
452452
}
453453
],
454-
"store_path": "/nix/store/k3rnfx753gnx5v8l3i4sx1g1al2ds0di-golangci-lint-1.59.1"
454+
"store_path": "/nix/store/ahn380skfkdg3ynimjpqakc1wp26mpsi-golangci-lint-2.3.1"
455455
},
456456
"aarch64-linux": {
457457
"outputs": [
458458
{
459459
"name": "out",
460-
"path": "/nix/store/29908ar68rrr9jzdhzcrlkbfk30gnihf-golangci-lint-1.59.1",
460+
"path": "/nix/store/5wf61l5pnqlna9mwllllq334rkh1i2a6-golangci-lint-2.3.1",
461461
"default": true
462462
}
463463
],
464-
"store_path": "/nix/store/29908ar68rrr9jzdhzcrlkbfk30gnihf-golangci-lint-1.59.1"
464+
"store_path": "/nix/store/5wf61l5pnqlna9mwllllq334rkh1i2a6-golangci-lint-2.3.1"
465465
},
466466
"x86_64-darwin": {
467467
"outputs": [
468468
{
469469
"name": "out",
470-
"path": "/nix/store/v7882fqgdy46a7l432q11f3xmbln80rc-golangci-lint-1.59.1",
470+
"path": "/nix/store/sbqfd50kb6g4gka3w1vixjrnacspdwq5-golangci-lint-2.3.1",
471471
"default": true
472472
}
473473
],
474-
"store_path": "/nix/store/v7882fqgdy46a7l432q11f3xmbln80rc-golangci-lint-1.59.1"
474+
"store_path": "/nix/store/sbqfd50kb6g4gka3w1vixjrnacspdwq5-golangci-lint-2.3.1"
475475
},
476476
"x86_64-linux": {
477477
"outputs": [
478478
{
479479
"name": "out",
480-
"path": "/nix/store/1m1i9zhrgdw2192803n6wf2k7g86y6d5-golangci-lint-1.59.1",
480+
"path": "/nix/store/kxbsvkixvkn0ss81rbc1g814sizd1jwj-golangci-lint-2.3.1",
481481
"default": true
482482
}
483483
],
484-
"store_path": "/nix/store/1m1i9zhrgdw2192803n6wf2k7g86y6d5-golangci-lint-1.59.1"
484+
"store_path": "/nix/store/kxbsvkixvkn0ss81rbc1g814sizd1jwj-golangci-lint-2.3.1"
485485
}
486486
}
487487
},

environment/providers/secretdir/secretdir_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ func TestBasicAuth(t *testing.T) {
3434
g.Expect(err).To(Succeed())
3535
defer func() {
3636
if !t.Failed() {
37-
os.RemoveAll(path)
37+
_ = os.RemoveAll(path)
3838
}
3939
}()
4040
t.Logf("Temporary directory %s", path)
41-
os.Setenv(envSecretDir, path)
41+
_ = os.Setenv(envSecretDir, path)
4242
ip := rand.String(10)
4343
user := rand.String(10)
4444
passwd := rand.String(10)
@@ -61,13 +61,13 @@ func TestTLSAuth(t *testing.T) {
6161
g.Expect(err).To(Succeed())
6262
defer func() {
6363
if !t.Failed() {
64-
os.RemoveAll(path)
64+
_ = os.RemoveAll(path)
6565
}
6666
}()
6767
ip := rand.String(10)
6868
cert := rand.String(512)
6969
t.Logf("Temporary directory %s", path)
70-
os.Setenv(envSecretDir, path)
70+
_ = os.Setenv(envSecretDir, path)
7171
g.Expect(writeParam(t, path, secretKeyCertName, cert)).To(Succeed())
7272
g.Expect(writeParam(t, path, secretKeyEndpoint,
7373
fmt.Sprintf("%s:9440", ip))).To(Succeed())
@@ -87,7 +87,7 @@ func TestNutanixAuth(t *testing.T) {
8787
g.Expect(err).To(Succeed())
8888
defer func() {
8989
if !t.Failed() {
90-
os.RemoveAll(path)
90+
_ = os.RemoveAll(path)
9191
}
9292
}()
9393
ip := rand.String(10)
@@ -109,7 +109,7 @@ func TestNutanixAuth(t *testing.T) {
109109
`, user, passwd)
110110
endpoint := fmt.Sprintf("%s:9440", ip)
111111
t.Logf("Temporary directory %s", path)
112-
os.Setenv(envSecretDir, path)
112+
_ = os.Setenv(envSecretDir, path)
113113

114114
// Write three keys
115115
g.Expect(writeParam(t, path, secretKeyCredentials, creds)).To(Succeed())

environment/types/types.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package types
22

33
import (
4+
"crypto/sha256"
5+
"encoding/hex"
6+
"encoding/json"
47
"fmt"
58
"net/url"
69
)
@@ -62,3 +65,34 @@ type Environment interface {
6265
type Provider interface {
6366
Environment
6467
}
68+
69+
// CachedClientParams define the interface that needs to be implemented by an object that will be used to create
70+
// a cached client.
71+
type CachedClientParams interface {
72+
// ManagementEndpoint returns the struct containing all information needed to construct a new client
73+
// and is used to calculate the validation hash for the client for the purpose of cache invalidation.
74+
// The validation hash is calculated based on the serialized version of the ManagementEndpoint.
75+
ManagementEndpoint() ManagementEndpoint
76+
// Key returns a unique key for the client that is used to store the client in the cache
77+
Key() string
78+
}
79+
80+
type CacheOpts[T any] func(*T)
81+
type ClientOption[T any] func(*T) error
82+
83+
func (ep *ManagementEndpoint) GetHash() (string, error) {
84+
// Note: this will only work reliably as long as types.ManagementEndpoint is predictably serializable i.e. does
85+
// not contain a map. Due to randomized ordering of map keys in Go, we would constantly invalidate caches
86+
// if the ManagementEndpoint has a map.
87+
serializedEndpoint, err := json.Marshal(*ep)
88+
if err != nil {
89+
return "", err
90+
}
91+
92+
hasher := sha256.New()
93+
hasher.Write(serializedEndpoint)
94+
hashedBytes := hasher.Sum(nil)
95+
currentValidationHash := hex.EncodeToString(hashedBytes)
96+
97+
return currentValidationHash, nil
98+
}

0 commit comments

Comments
 (0)