-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgreennode.go
More file actions
209 lines (178 loc) · 6.23 KB
/
Copy pathgreennode.go
File metadata and controls
209 lines (178 loc) · 6.23 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package greennode
import (
"context"
"strings"
"time"
"danny.vn/gnode/auth"
"danny.vn/gnode/client"
"danny.vn/gnode/option"
computev1 "danny.vn/gnode/services/compute/v1"
computev2 "danny.vn/gnode/services/compute/v2"
dnsv1 "danny.vn/gnode/services/dns/v1"
glbv1 "danny.vn/gnode/services/glb/v1"
identityv2 "danny.vn/gnode/services/identity/v2"
lbv2 "danny.vn/gnode/services/loadbalancer/v2"
networkv1 "danny.vn/gnode/services/network/v1"
networkv2 "danny.vn/gnode/services/network/v2"
portalv1 "danny.vn/gnode/services/portal/v1"
portalv2 "danny.vn/gnode/services/portal/v2"
volumev1 "danny.vn/gnode/services/volume/v1"
volumev2 "danny.vn/gnode/services/volume/v2"
)
// Config holds all configuration needed to create an SDK client.
type Config struct {
Region string // e.g. "hcm-3", "han-1" — derives default endpoint URLs
ClientID string
ClientSecret string
ProjectID string
UserID string
ZoneID string
UserAgent string
IAMAuth *auth.IAMUserAuth // optional — enables IAM user auth instead of client credentials
RetryCount int
SleepDuration time.Duration
IAMEndpoint string
VServerEndpoint string
VLBEndpoint string
VNetworkEndpoint string
GLBEndpoint string
DNSEndpoint string
}
// Client provides flat access to all service APIs.
type Client struct {
// Primary services (latest API version)
LoadBalancer *lbv2.LoadBalancerServiceV2
Compute *computev2.ComputeServiceV2
Network *networkv2.NetworkServiceV2
Volume *volumev2.VolumeServiceV2
DNS *dnsv1.VDnsServiceV1
GLB *glbv1.GLBServiceV1
Portal *portalv2.PortalServiceV2
Identity *identityv2.IdentityServiceV2
// Legacy API versions
ComputeV1 *computev1.ComputeServiceV1
NetworkV1 *networkv1.NetworkServiceV1
VolumeV1 *volumev1.VolumeServiceV1
PortalV1 *portalv1.PortalServiceV1
}
// NewClient creates a fully-wired SDK client from the given configuration.
func NewClient(ctx context.Context, cfg Config, opts ...option.ClientOption) (*Client, error) {
resolveEndpoints(&cfg)
settings := option.Apply(opts)
hc := client.NewHTTPClient()
if settings.HTTPClient != nil {
hc.WithUnderlyingClient(settings.HTTPClient)
} else if settings.Transport != nil {
hc.WithTransport(settings.Transport)
}
if cfg.RetryCount > 0 {
hc.WithRetryCount(cfg.RetryCount)
}
if cfg.SleepDuration > 0 {
hc.WithRetryInterval(cfg.SleepDuration)
}
hc.WithDefaultHeaders("Content-Type", "application/json")
ua := cfg.UserAgent
if settings.UserAgent != "" {
ua = settings.UserAgent
}
if ua != "" {
hc.WithDefaultHeaders("User-Agent", ua)
}
c := &Client{}
// IAM / Identity
if cfg.IAMEndpoint != "" {
iamSvc := newServiceClient(cfg.IAMEndpoint+"v2", cfg.ProjectID, "", hc)
c.Identity = &identityv2.IdentityServiceV2{Client: iamSvc}
}
// VServer services (compute, network, volume, portal)
if cfg.VServerEndpoint != "" {
ep := client.NormalizeURL(cfg.VServerEndpoint)
svcV2 := newServiceClient(ep+"v2", cfg.ProjectID, "", hc)
svcV1 := newServiceClient(ep+"v1", cfg.ProjectID, "", hc)
c.Compute = &computev2.ComputeServiceV2{Client: svcV2, V1Client: svcV1}
c.Network = &networkv2.NetworkServiceV2{Client: svcV2}
c.Volume = &volumev2.VolumeServiceV2{Client: svcV2}
c.Portal = &portalv2.PortalServiceV2{Client: svcV2}
c.ComputeV1 = &computev1.ComputeServiceV1{Client: svcV1}
c.VolumeV1 = &volumev1.VolumeServiceV1{Client: svcV1}
c.PortalV1 = &portalv1.PortalServiceV1{Client: svcV1}
}
// VLB (load balancer)
if cfg.VLBEndpoint != "" {
vlbEp := client.NormalizeURL(cfg.VLBEndpoint)
vlbSvcV2 := newServiceClient(vlbEp+"v2", cfg.ProjectID, "", hc)
var vserverSvcV2 *client.ServiceClient
if cfg.VServerEndpoint != "" {
vserverSvcV2 = newServiceClient(client.NormalizeURL(cfg.VServerEndpoint)+"v2", cfg.ProjectID, "", hc)
}
c.LoadBalancer = &lbv2.LoadBalancerServiceV2{Client: vlbSvcV2, ServerClient: vserverSvcV2}
}
// VNetwork
if cfg.VNetworkEndpoint != "" {
vnEp := client.NormalizeURL(cfg.VNetworkEndpoint)
vnetV1 := newServiceClient(vnEp+"vnetwork/v1", cfg.ProjectID, cfg.ZoneID, hc)
c.NetworkV1 = &networkv1.NetworkServiceV1{Client: vnetV1}
}
// GLB (global load balancer)
if cfg.GLBEndpoint != "" {
glbSvc := newServiceClient(cfg.GLBEndpoint+"v1", "", "", hc)
c.GLB = &glbv1.GLBServiceV1{Client: glbSvc}
}
// DNS
if cfg.DNSEndpoint != "" {
dnsEp := client.NormalizeURL(cfg.DNSEndpoint)
dnsSvc := newServiceClient(dnsEp+"v1", cfg.ProjectID, "", hc)
c.DNS = &dnsv1.VDnsServiceV1{Client: dnsSvc}
}
// Set up auth
if cfg.IAMAuth != nil {
hc.WithReauthFunc(client.IAMUserOauth2, iamUserReauthFunc(cfg.IAMAuth))
} else if c.Identity != nil && cfg.ClientID != "" && cfg.ClientSecret != "" {
hc.WithReauthFunc(client.IAMOauth2, reauthFunc(c.Identity, cfg.ClientID, cfg.ClientSecret))
}
// Auto-discover VNetwork ZoneID (regionId) if not explicitly set.
if c.NetworkV1 != nil && c.NetworkV1.Client.ZoneID == "" && cfg.Region != "" {
if regions, err := c.NetworkV1.ListVNetworkRegions(ctx, networkv1.NewListVNetworkRegionsRequest()); err == nil {
for _, r := range regions.Items {
if strings.Contains(r.DashboardURL, cfg.Region+"-vnetwork") {
c.NetworkV1.Client.ZoneID = r.ID
break
}
}
}
}
return c, nil
}
func reauthFunc(identity *identityv2.IdentityServiceV2, clientID, clientSecret string) func(ctx context.Context) (*client.Token, error) {
return func(ctx context.Context) (*client.Token, error) {
token, err := identity.GetAccessToken(ctx, identityv2.NewGetAccessTokenRequest(clientID, clientSecret))
if err != nil {
return nil, err
}
return &client.Token{
AccessToken: token.Token,
ExpiresAt: token.ExpiresAt,
}, nil
}
}
func iamUserReauthFunc(iamAuth *auth.IAMUserAuth) func(ctx context.Context) (*client.Token, error) {
return func(ctx context.Context) (*client.Token, error) {
token, expiresAt, err := iamAuth.Authenticate(ctx)
if err != nil {
return nil, err
}
return &client.Token{
AccessToken: token,
ExpiresAt: expiresAt,
}, nil
}
}
func newServiceClient(endpoint, projectID, zoneID string, hc *client.HTTPClient) *client.ServiceClient {
return &client.ServiceClient{
Endpoint: client.NormalizeURL(endpoint),
ProjectID: projectID,
ZoneID: zoneID,
HTTP: hc,
}
}