-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
47 lines (41 loc) · 1.55 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package stackit
import (
"context"
"github.com/SchwarzIT/community-stackit-go-client/pkg/clients"
"github.com/SchwarzIT/community-stackit-go-client/pkg/services"
)
// NewClientWithKeyAuth creates a new client that authenticates itself with STACKIT APIs using a
// service account key and a private RSA key
// this is the recommended way of authenticating to STACKIT API
func NewClientWithKeyAuth(ctx context.Context, cfg ...clients.KeyFlowConfig) (*services.Services, error) {
client := &clients.KeyFlow{}
if err := client.Init(ctx, cfg...); err != nil {
return nil, err
}
return services.Init(client)
}
// MustNewClientWithKeyAuth panics if client initialization failed
func MustNewClientWithKeyAuth(ctx context.Context, cfg ...clients.KeyFlowConfig) *services.Services {
c, err := NewClientWithKeyAuth(ctx, cfg...)
if err != nil {
panic(err)
}
return c
}
// NewClientWithTokenAuth creates a new client that authenticates itself with STACKIT APIs using a service account token
// important: this approach is less secure, as the token has a long lifespan
func NewClientWithTokenAuth(ctx context.Context, cfg ...clients.TokenFlowConfig) (*services.Services, error) {
client := &clients.TokenFlow{}
if err := client.Init(ctx, cfg...); err != nil {
return nil, err
}
return services.Init(client)
}
// MustNewClientWithTokenAuth panics if client initialization failed
func MustNewClientWithTokenAuth(ctx context.Context, cfg ...clients.TokenFlowConfig) *services.Services {
c, err := NewClientWithTokenAuth(ctx, cfg...)
if err != nil {
panic(err)
}
return c
}