This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
122 lines (104 loc) · 3.69 KB
/
api.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package zerosdk
import (
"context"
"fmt"
"github.com/pomerium/zero-sdk/apierror"
cluster_api "github.com/pomerium/zero-sdk/cluster"
connect_api "github.com/pomerium/zero-sdk/connect"
connect_mux "github.com/pomerium/zero-sdk/connect-mux"
"github.com/pomerium/zero-sdk/fanout"
token_api "github.com/pomerium/zero-sdk/token"
)
// API is a Pomerium Zero Cluster API client
type API struct {
cfg *config
cluster cluster_api.ClientWithResponsesInterface
mux *connect_mux.Mux
downloadURLCache *cluster_api.URLCache
}
// WatchOption defines which events to watch for
type WatchOption = connect_mux.WatchOption
// NewAPI creates a new API client
func NewAPI(ctx context.Context, opts ...Option) (*API, error) {
cfg, err := newConfig(opts...)
if err != nil {
return nil, err
}
fetcher, err := cluster_api.NewTokenFetcher(cfg.clusterAPIEndpoint,
cluster_api.WithHTTPClient(cfg.httpClient),
)
if err != nil {
return nil, fmt.Errorf("error creating token fetcher: %w", err)
}
tokenCache := token_api.NewCache(fetcher, cfg.apiToken)
clusterClient, err := cluster_api.NewAuthorizedClient(cfg.clusterAPIEndpoint, tokenCache.GetToken, cfg.httpClient)
if err != nil {
return nil, fmt.Errorf("error creating cluster client: %w", err)
}
connectClient, err := connect_api.NewAuthorizedConnectClient(ctx, cfg.connectAPIEndpoint, tokenCache.GetToken)
if err != nil {
return nil, fmt.Errorf("error creating connect client: %w", err)
}
return &API{
cfg: cfg,
cluster: clusterClient,
mux: connect_mux.New(connectClient),
downloadURLCache: cluster_api.NewURLCache(),
}, nil
}
// Connect connects to the connect API and allows watching for changes
func (api *API) Connect(ctx context.Context, opts ...fanout.Option) error {
return api.mux.Run(ctx, opts...)
}
// Watch dispatches API updates
func (api *API) Watch(ctx context.Context, opts ...WatchOption) error {
return api.mux.Watch(ctx, opts...)
}
// GetClusterBootstrapConfig fetches the bootstrap configuration from the cluster API
func (api *API) GetClusterBootstrapConfig(ctx context.Context) (*cluster_api.BootstrapConfig, error) {
return apierror.CheckResponse[cluster_api.BootstrapConfig](
api.cluster.GetClusterBootstrapConfigWithResponse(ctx),
)
}
// GetClusterResourceBundles fetches the resource bundles from the cluster API
func (api *API) GetClusterResourceBundles(ctx context.Context) (*cluster_api.GetBundlesResponse, error) {
return apierror.CheckResponse[cluster_api.GetBundlesResponse](
api.cluster.GetClusterResourceBundlesWithResponse(ctx),
)
}
// ReportBundleAppliedSuccess reports a successful bundle application
func (api *API) ReportBundleAppliedSuccess(ctx context.Context, bundleID string, metadata map[string]string) error {
status := cluster_api.BundleStatus{
Success: &cluster_api.BundleStatusSuccess{
Metadata: metadata,
},
}
_, err := apierror.CheckResponse[cluster_api.EmptyResponse](
api.cluster.ReportClusterResourceBundleStatusWithResponse(ctx, bundleID, status),
)
if err != nil {
return fmt.Errorf("error reporting bundle status: %w", err)
}
return err
}
// ReportBundleAppliedFailure reports a failed bundle application
func (api *API) ReportBundleAppliedFailure(
ctx context.Context,
bundleID string,
source cluster_api.BundleStatusFailureSource,
err error,
) error {
status := cluster_api.BundleStatus{
Failure: &cluster_api.BundleStatusFailure{
Message: err.Error(),
Source: source,
},
}
_, err = apierror.CheckResponse[cluster_api.EmptyResponse](
api.cluster.ReportClusterResourceBundleStatusWithResponse(ctx, bundleID, status),
)
if err != nil {
return fmt.Errorf("error reporting bundle status: %w", err)
}
return err
}