Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use options-pattern for cloudian sdk client #48

Merged
merged 9 commits into from
Dec 6, 2024
6 changes: 5 additions & 1 deletion internal/controller/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ type NoOpService struct{}
var (
newCloudianService = func(providerConfig *apisv1alpha1.ProviderConfig, authHeader string) (*cloudian.Client, error) {
// FIXME: Don't require InsecureSkipVerify
return cloudian.NewClient(providerConfig.Spec.Endpoint, true, authHeader), nil
return cloudian.NewClient(
providerConfig.Spec.Endpoint,
authHeader,
cloudian.WithInsecureTLSVerify(true),
), nil
}
)

Expand Down
23 changes: 17 additions & 6 deletions internal/sdk/cloudian/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,25 @@ type User struct {

var ErrNotFound = errors.New("not found")

func NewClient(baseUrl string, tlsInsecureSkipVerify bool, authHeader string) *Client {
return &Client{
baseURL: baseUrl,
httpClient: &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: tlsInsecureSkipVerify}, // nolint:gosec
}},
// WithInsecureTLSVerify skips the TLS validation of the server certificate when `insecure` is true.
func WithInsecureTLSVerify(insecure bool) func(*Client) {
return func(c *Client) {
c.httpClient = &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure}, // nolint:gosec
}}
}
}

func NewClient(baseURL string, authHeader string, opts ...func(*Client)) *Client {
c := &Client{
baseURL: baseURL,
httpClient: http.DefaultClient,
authHeader: authHeader,
}
for _, opt := range opts {
opt(c)
}
return c
}

// List all users of a group.
Expand Down
Loading