From c80d3ed1509d29371a026e59e1e865a8146facea Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 13 Dec 2024 14:50:10 +0000 Subject: [PATCH] Add dataservices sub-client This client will be used by terraform providers or data sources to access the dataservices API, eg to read the `/secrets` endpoint. --- internal/client/client.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/internal/client/client.go b/internal/client/client.go index baf9ccf..0130d90 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -8,6 +8,7 @@ import ( "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/debug" "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/defaults" "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/sdk/async" + "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/sdk/dataservices" "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/sdk/systems" "github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/sdk/virt" "github.com/microsoft/kiota-abstractions-go/authentication" @@ -121,6 +122,38 @@ func (c *PCBeClient) NewSysClient( return sysClient, headerOpts, nil } +func (c *PCBeClient) NewDataServicesClient( + ctx context.Context, +) (*dataservices.ApiClient, *nethttplibrary.HeadersInspectionOptions, error) { + var middlewares []nethttplibrary.Middleware + + tp := auth.NewPcbeAccessTokenProvider(c.Config.Token) + authProvider := authentication.NewBaseBearerTokenAuthenticationProvider(tp) + observeOpts := nethttplibrary.ObservabilityOptions{} + if c.Config.HTTPDump { + debugOpts := debug.NewInspectionOptions() + debugOpts.Enabled = true + debugOptsHandler := debug.NewInspectionHandlerWithOptions(*debugOpts) + middlewares = append(middlewares, debugOptsHandler) + } + headerOpts := nethttplibrary.NewHeadersInspectionOptions() + headerOpts.InspectResponseHeaders = true + headerOptsHandler := nethttplibrary.NewHeadersInspectionHandlerWithOptions(*headerOpts) + middlewares = append(middlewares, headerOptsHandler) + httpclient := nethttplibrary.GetDefaultClient(middlewares...) + httpclient.Timeout = c.Config.HTTPTimeout + + adapter, err := nethttplibrary.NewNetHttpRequestAdapterWithParseNodeFactoryAndSerializationWriterFactoryAndHttpClientAndObservabilityOptions(authProvider, nil, nil, httpclient, observeOpts) // nolint: lll + if err != nil { + return nil, nil, err + } + + adapter.SetBaseUrl(c.Config.Host) + sysClient := dataservices.NewApiClient(adapter) + + return sysClient, headerOpts, nil +} + func NewPCBeClient(ctx context.Context, cfg Config) (*PCBeClient, error) { if cfg.MaxPolls == 0 { cfg.MaxPolls = defaults.MaxPolls