Debian Codegraph @

gkeconnect/gateway/apiv1/gateway_control_client.go

raw
1 // Copyright 2025 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Code generated by protoc-gen-go_gapic. DO NOT EDIT.
16
17 package gateway
18
19 import (
20 "context"
21 "fmt"
22 "log/slog"
23 "net/http"
24 "net/url"
25 "time"
26
27 gatewaypb "cloud.google.com/go/gkeconnect/gateway/apiv1/gatewaypb"
28 gax "github.com/googleapis/gax-go/v2"
29 "google.golang.org/api/option"
30 "google.golang.org/api/option/internaloption"
31 httptransport "google.golang.org/api/transport/http"
32 "google.golang.org/grpc"
33 "google.golang.org/protobuf/encoding/protojson"
34 )
35
36 var newGatewayControlClientHook clientHook
37
38 // GatewayControlCallOptions contains the retry settings for each method of GatewayControlClient.
39 type GatewayControlCallOptions struct {
40 GenerateCredentials []gax.CallOption
41 }
42
43 func defaultGatewayControlRESTCallOptions() *GatewayControlCallOptions {
44 return &GatewayControlCallOptions{
45 GenerateCredentials: []gax.CallOption{
46 gax.WithTimeout(60000 * time.Millisecond),
47 gax.WithRetry(func() gax.Retryer {
48 return gax.OnHTTPCodes(gax.Backoff{
49 Initial: 1000 * time.Millisecond,
50 Max: 10000 * time.Millisecond,
51 Multiplier: 1.30,
52 },
53 http.StatusServiceUnavailable)
54 }),
55 },
56 }
57 }
58
59 // internalGatewayControlClient is an interface that defines the methods available from Connect Gateway API.
60 type internalGatewayControlClient interface {
61 Close() error
62 setGoogleClientInfo(...string)
63 Connection() *grpc.ClientConn
64 GenerateCredentials(context.Context, *gatewaypb.GenerateCredentialsRequest, ...gax.CallOption) (*gatewaypb.GenerateCredentialsResponse, error)
65 }
66
67 // GatewayControlClient is a client for interacting with Connect Gateway API.
68 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
69 //
70 // GatewayControl is the control plane API for Connect Gateway.
71 type GatewayControlClient struct {
72 // The internal transport-dependent client.
73 internalClient internalGatewayControlClient
74
75 // The call options for this service.
76 CallOptions *GatewayControlCallOptions
77 }
78
79 // Wrapper methods routed to the internal client.
80
81 // Close closes the connection to the API service. The user should invoke this when
82 // the client is no longer required.
83 func (c *GatewayControlClient) Close() error {
84 return c.internalClient.Close()
85 }
86
87 // setGoogleClientInfo sets the name and version of the application in
88 // the `x-goog-api-client` header passed on each request. Intended for
89 // use by Google-written clients.
90 func (c *GatewayControlClient) setGoogleClientInfo(keyval ...string) {
91 c.internalClient.setGoogleClientInfo(keyval...)
92 }
93
94 // Connection returns a connection to the API service.
95 //
96 // Deprecated: Connections are now pooled so this method does not always
97 // return the same resource.
98 func (c *GatewayControlClient) Connection() *grpc.ClientConn {
99 return c.internalClient.Connection()
100 }
101
102 // GenerateCredentials generateCredentials provides connection information that allows a user to
103 // access the specified membership using Connect Gateway.
104 func (c *GatewayControlClient) GenerateCredentials(ctx context.Context, req *gatewaypb.GenerateCredentialsRequest, opts ...gax.CallOption) (*gatewaypb.GenerateCredentialsResponse, error) {
105 return c.internalClient.GenerateCredentials(ctx, req, opts...)
106 }
107
108 // Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
109 type gatewayControlRESTClient struct {
110 // The http endpoint to connect to.
111 endpoint string
112
113 // The http client.
114 httpClient *http.Client
115
116 // The x-goog-* headers to be sent with each request.
117 xGoogHeaders []string
118
119 // Points back to the CallOptions field of the containing GatewayControlClient
120 CallOptions **GatewayControlCallOptions
121
122 logger *slog.Logger
123 }
124
125 // NewGatewayControlRESTClient creates a new gateway control rest client.
126 //
127 // GatewayControl is the control plane API for Connect Gateway.
128 func NewGatewayControlRESTClient(ctx context.Context, opts ...option.ClientOption) (*GatewayControlClient, error) {
129 clientOpts := append(defaultGatewayControlRESTClientOptions(), opts...)
130 httpClient, endpoint, err := httptransport.NewClient(ctx, clientOpts...)
131 if err != nil {
132 return nil, err
133 }
134
135 callOpts := defaultGatewayControlRESTCallOptions()
136 c := &gatewayControlRESTClient{
137 endpoint: endpoint,
138 httpClient: httpClient,
139 CallOptions: &callOpts,
140 logger: internaloption.GetLogger(opts),
141 }
142 c.setGoogleClientInfo()
143
144 return &GatewayControlClient{internalClient: c, CallOptions: callOpts}, nil
145 }
146
147 func defaultGatewayControlRESTClientOptions() []option.ClientOption {
148 return []option.ClientOption{
149 internaloption.WithDefaultEndpoint("https://connectgateway.googleapis.com"),
150 internaloption.WithDefaultEndpointTemplate("https://connectgateway.UNIVERSE_DOMAIN"),
151 internaloption.WithDefaultMTLSEndpoint("https://connectgateway.mtls.googleapis.com"),
152 internaloption.WithDefaultUniverseDomain("googleapis.com"),
153 internaloption.WithDefaultAudience("https://connectgateway.googleapis.com/"),
154 internaloption.WithDefaultScopes(DefaultAuthScopes()...),
155 internaloption.EnableNewAuthLibrary(),
156 }
157 }
158
159 // setGoogleClientInfo sets the name and version of the application in
160 // the `x-goog-api-client` header passed on each request. Intended for
161 // use by Google-written clients.
162 func (c *gatewayControlRESTClient) setGoogleClientInfo(keyval ...string) {
163 kv := append([]string{"gl-go", gax.GoVersion}, keyval...)
164 kv = append(kv, "gapic", getVersionClient(), "gax", gax.Version, "rest", "UNKNOWN", "pb", protoVersion)
165 c.xGoogHeaders = []string{
166 "x-goog-api-client", gax.XGoogHeader(kv...),
167 }
168 }
169
170 // Close closes the connection to the API service. The user should invoke this when
171 // the client is no longer required.
172 func (c *gatewayControlRESTClient) Close() error {
173 // Replace httpClient with nil to force cleanup.
174 c.httpClient = nil
175 return nil
176 }
177
178 // Connection returns a connection to the API service.
179 //
180 // Deprecated: This method always returns nil.
181 func (c *gatewayControlRESTClient) Connection() *grpc.ClientConn {
182 return nil
183 }
184
185 // GenerateCredentials generateCredentials provides connection information that allows a user to
186 // access the specified membership using Connect Gateway.
187 func (c *gatewayControlRESTClient) GenerateCredentials(ctx context.Context, req *gatewaypb.GenerateCredentialsRequest, opts ...gax.CallOption) (*gatewaypb.GenerateCredentialsResponse, error) {
188 baseUrl, err := url.Parse(c.endpoint)
189 if err != nil {
190 return nil, err
191 }
192 baseUrl.Path += fmt.Sprintf("/v1/%v:generateCredentials", req.GetName())
193
194 params := url.Values{}
195 params.Add("$alt", "json;enum-encoding=int")
196 if req.GetForceUseAgent() {
197 params.Add("forceUseAgent", fmt.Sprintf("%v", req.GetForceUseAgent()))
198 }
199 if req.GetKubernetesNamespace() != "" {
200 params.Add("kubernetesNamespace", fmt.Sprintf("%v", req.GetKubernetesNamespace()))
201 }
202 if req.GetOperatingSystem() != 0 {
203 params.Add("operatingSystem", fmt.Sprintf("%v", req.GetOperatingSystem()))
204 }
205 if req.GetVersion() != "" {
206 params.Add("version", fmt.Sprintf("%v", req.GetVersion()))
207 }
208
209 baseUrl.RawQuery = params.Encode()
210
211 // Build HTTP headers from client and context metadata.
212 hds := []string{"x-goog-request-params", fmt.Sprintf("%s=%v", "name", url.QueryEscape(req.GetName()))}
213
214 hds = append(c.xGoogHeaders, hds...)
215 hds = append(hds, "Content-Type", "application/json")
216 headers := gax.BuildHeaders(ctx, hds...)
217 opts = append((*c.CallOptions).GenerateCredentials[0:len((*c.CallOptions).GenerateCredentials):len((*c.CallOptions).GenerateCredentials)], opts...)
218 unm := protojson.UnmarshalOptions{AllowPartial: true, DiscardUnknown: true}
219 resp := &gatewaypb.GenerateCredentialsResponse{}
220 e := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
221 if settings.Path != "" {
222 baseUrl.Path = settings.Path
223 }
224 httpReq, err := http.NewRequest("GET", baseUrl.String(), nil)
225 if err != nil {
226 return err
227 }
228 httpReq = httpReq.WithContext(ctx)
229 httpReq.Header = headers
230
231 buf, err := executeHTTPRequest(ctx, c.httpClient, httpReq, c.logger, nil, "GenerateCredentials")
232 if err != nil {
233 return err
234 }
235
236 if err := unm.Unmarshal(buf, resp); err != nil {
237 return err
238 }
239
240 return nil
241 }, opts...)
242 if e != nil {
243 return nil, e
244 }
245 return resp, nil
246 }