-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
These clients will be used to access various APIs. For example the virt client will be used for creating Virtual Machines.
- Loading branch information
1 parent
e06bb55
commit 566275e
Showing
5 changed files
with
165 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/microsoft/kiota-abstractions-go/authentication" | ||
) | ||
|
||
type PcbeAccessTokenProvider struct { | ||
token string | ||
} | ||
|
||
func (p *PcbeAccessTokenProvider) GetAuthorizationToken( | ||
ctx context.Context, url *url.URL, | ||
additionalAuthenticationContext map[string]interface{}, | ||
) (string, error) { | ||
return p.token, nil | ||
} | ||
|
||
func (p *PcbeAccessTokenProvider) GetAllowedHostsValidator() *authentication.AllowedHostsValidator { | ||
return nil | ||
} | ||
|
||
func NewPcbeAccessTokenProvider(token string) *PcbeAccessTokenProvider { | ||
p := PcbeAccessTokenProvider{ | ||
token: token, | ||
} | ||
|
||
return &p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/auth" | ||
"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/systems" | ||
"github.com/HewlettPackard/hpegl-pcbe-terraform-resources/internal/sdk/virt" | ||
"github.com/microsoft/kiota-abstractions-go/authentication" | ||
"github.com/microsoft/kiota-http-go" | ||
) | ||
|
||
type PcbeClient struct { | ||
Config Config | ||
} | ||
|
||
type Config struct { | ||
Host string | ||
Token string | ||
HTTPDump bool | ||
MaxPolls int32 | ||
PollInterval float32 | ||
HTTPTimeout time.Duration | ||
} | ||
|
||
func (c *PcbeClient) NewAsyncClient(ctx context.Context) (*async.ApiClient, error) { | ||
var middlewares []nethttplibrary.Middleware | ||
|
||
tp := auth.NewPcbeAccessTokenProvider(c.Config.Token) | ||
authProvider := authentication.NewBaseBearerTokenAuthenticationProvider(tp) | ||
observeOpts := nethttplibrary.ObservabilityOptions{} | ||
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, err | ||
} | ||
|
||
adapter.SetBaseUrl(c.Config.Host) | ||
adapter.SetBaseUrl(c.Config.Host) | ||
asyncClient := async.NewApiClient(adapter) | ||
|
||
return asyncClient, nil | ||
} | ||
|
||
func (c *PcbeClient) NewVirtClient( | ||
ctx context.Context, | ||
) (*virt.ApiClient, *nethttplibrary.HeadersInspectionOptions, error) { | ||
var middlewares []nethttplibrary.Middleware | ||
|
||
tp := auth.NewPcbeAccessTokenProvider(c.Config.Token) | ||
authProvider := authentication.NewBaseBearerTokenAuthenticationProvider(tp) | ||
observeOpts := nethttplibrary.ObservabilityOptions{} | ||
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) | ||
virtClient := virt.NewApiClient(adapter) | ||
|
||
return virtClient, headerOpts, nil | ||
} | ||
|
||
func (c *PcbeClient) NewSysClient( | ||
ctx context.Context, | ||
) (*systems.ApiClient, *nethttplibrary.HeadersInspectionOptions, error) { | ||
var middlewares []nethttplibrary.Middleware | ||
|
||
tp := auth.NewPcbeAccessTokenProvider(c.Config.Token) | ||
authProvider := authentication.NewBaseBearerTokenAuthenticationProvider(tp) | ||
observeOpts := nethttplibrary.ObservabilityOptions{} | ||
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 := systems.NewApiClient(adapter) | ||
|
||
return sysClient, headerOpts, nil | ||
} | ||
|
||
func NewPCBEClient(ctx context.Context, cfg Config) (*PcbeClient, error) { | ||
if cfg.MaxPolls == 0 { | ||
cfg.MaxPolls = defaults.MaxPolls | ||
} | ||
if cfg.PollInterval == 0.0 { | ||
cfg.PollInterval = defaults.PollInterval | ||
} | ||
client := &PcbeClient{ | ||
Config: cfg, | ||
} | ||
|
||
return client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package defaults | ||
|
||
import "time" | ||
|
||
const ( | ||
// Time in seconds between polling for updates of resources | ||
PollInterval = float32(5.0) | ||
// Limit on number of polls for a resource | ||
MaxPolls = int32(100) | ||
// http client timeout | ||
HTTPTimeout = 60 * time.Second | ||
) |