1// Copyright 2019 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 gapic-generator. DO NOT EDIT.
16
17package debugger
18
19import (
20	"context"
21	"fmt"
22	"time"
23
24	gax "github.com/googleapis/gax-go/v2"
25	"google.golang.org/api/option"
26	"google.golang.org/api/transport"
27	clouddebuggerpb "google.golang.org/genproto/googleapis/devtools/clouddebugger/v2"
28	"google.golang.org/grpc"
29	"google.golang.org/grpc/codes"
30	"google.golang.org/grpc/metadata"
31)
32
33// Controller2CallOptions contains the retry settings for each method of Controller2Client.
34type Controller2CallOptions struct {
35	RegisterDebuggee       []gax.CallOption
36	ListActiveBreakpoints  []gax.CallOption
37	UpdateActiveBreakpoint []gax.CallOption
38}
39
40func defaultController2ClientOptions() []option.ClientOption {
41	return []option.ClientOption{
42		option.WithEndpoint("clouddebugger.googleapis.com:443"),
43		option.WithScopes(DefaultAuthScopes()...),
44	}
45}
46
47func defaultController2CallOptions() *Controller2CallOptions {
48	retry := map[[2]string][]gax.CallOption{
49		{"default", "idempotent"}: {
50			gax.WithRetry(func() gax.Retryer {
51				return gax.OnCodes([]codes.Code{
52					codes.DeadlineExceeded,
53					codes.Unavailable,
54				}, gax.Backoff{
55					Initial:    100 * time.Millisecond,
56					Max:        60000 * time.Millisecond,
57					Multiplier: 1.3,
58				})
59			}),
60		},
61	}
62	return &Controller2CallOptions{
63		RegisterDebuggee:       retry[[2]string{"default", "non_idempotent"}],
64		ListActiveBreakpoints:  retry[[2]string{"default", "idempotent"}],
65		UpdateActiveBreakpoint: retry[[2]string{"default", "idempotent"}],
66	}
67}
68
69// Controller2Client is a client for interacting with Stackdriver Debugger API.
70//
71// Methods, except Close, may be called concurrently. However, fields must not be modified concurrently with method calls.
72type Controller2Client struct {
73	// The connection to the service.
74	conn *grpc.ClientConn
75
76	// The gRPC API client.
77	controller2Client clouddebuggerpb.Controller2Client
78
79	// The call options for this service.
80	CallOptions *Controller2CallOptions
81
82	// The x-goog-* metadata to be sent with each request.
83	xGoogMetadata metadata.MD
84}
85
86// NewController2Client creates a new controller2 client.
87//
88// The Controller service provides the API for orchestrating a collection of
89// debugger agents to perform debugging tasks. These agents are each attached
90// to a process of an application which may include one or more replicas.
91//
92// The debugger agents register with the Controller to identify the application
93// being debugged, the Debuggee. All agents that register with the same data,
94// represent the same Debuggee, and are assigned the same debuggee_id.
95//
96// The debugger agents call the Controller to retrieve  the list of active
97// Breakpoints. Agents with the same debuggee_id get the same breakpoints
98// list. An agent that can fulfill the breakpoint request updates the
99// Controller with the breakpoint result. The controller selects the first
100// result received and discards the rest of the results.
101// Agents that poll again for active breakpoints will no longer have
102// the completed breakpoint in the list and should remove that breakpoint from
103// their attached process.
104//
105// The Controller service does not provide a way to retrieve the results of
106// a completed breakpoint. This functionality is available using the Debugger
107// service.
108func NewController2Client(ctx context.Context, opts ...option.ClientOption) (*Controller2Client, error) {
109	conn, err := transport.DialGRPC(ctx, append(defaultController2ClientOptions(), opts...)...)
110	if err != nil {
111		return nil, err
112	}
113	c := &Controller2Client{
114		conn:        conn,
115		CallOptions: defaultController2CallOptions(),
116
117		controller2Client: clouddebuggerpb.NewController2Client(conn),
118	}
119	c.SetGoogleClientInfo()
120	return c, nil
121}
122
123// Connection returns the client's connection to the API service.
124func (c *Controller2Client) Connection() *grpc.ClientConn {
125	return c.conn
126}
127
128// Close closes the connection to the API service. The user should invoke this when
129// the client is no longer required.
130func (c *Controller2Client) Close() error {
131	return c.conn.Close()
132}
133
134// SetGoogleClientInfo sets the name and version of the application in
135// the `x-goog-api-client` header passed on each request. Intended for
136// use by Google-written clients.
137func (c *Controller2Client) SetGoogleClientInfo(keyval ...string) {
138	kv := append([]string{"gl-go", versionGo()}, keyval...)
139	kv = append(kv, "gapic", versionClient, "gax", gax.Version, "grpc", grpc.Version)
140	c.xGoogMetadata = metadata.Pairs("x-goog-api-client", gax.XGoogHeader(kv...))
141}
142
143// RegisterDebuggee registers the debuggee with the controller service.
144//
145// All agents attached to the same application must call this method with
146// exactly the same request content to get back the same stable debuggee_id.
147// Agents should call this method again whenever google.rpc.Code.NOT_FOUND
148// is returned from any controller method.
149//
150// This protocol allows the controller service to disable debuggees, recover
151// from data loss, or change the debuggee_id format. Agents must handle
152// debuggee_id value changing upon re-registration.
153func (c *Controller2Client) RegisterDebuggee(ctx context.Context, req *clouddebuggerpb.RegisterDebuggeeRequest, opts ...gax.CallOption) (*clouddebuggerpb.RegisterDebuggeeResponse, error) {
154	ctx = insertMetadata(ctx, c.xGoogMetadata)
155	opts = append(c.CallOptions.RegisterDebuggee[0:len(c.CallOptions.RegisterDebuggee):len(c.CallOptions.RegisterDebuggee)], opts...)
156	var resp *clouddebuggerpb.RegisterDebuggeeResponse
157	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
158		var err error
159		resp, err = c.controller2Client.RegisterDebuggee(ctx, req, settings.GRPC...)
160		return err
161	}, opts...)
162	if err != nil {
163		return nil, err
164	}
165	return resp, nil
166}
167
168// ListActiveBreakpoints returns the list of all active breakpoints for the debuggee.
169//
170// The breakpoint specification (location, condition, and expressions
171// fields) is semantically immutable, although the field values may
172// change. For example, an agent may update the location line number
173// to reflect the actual line where the breakpoint was set, but this
174// doesn't change the breakpoint semantics.
175//
176// This means that an agent does not need to check if a breakpoint has changed
177// when it encounters the same breakpoint on a successive call.
178// Moreover, an agent should remember the breakpoints that are completed
179// until the controller removes them from the active list to avoid
180// setting those breakpoints again.
181func (c *Controller2Client) ListActiveBreakpoints(ctx context.Context, req *clouddebuggerpb.ListActiveBreakpointsRequest, opts ...gax.CallOption) (*clouddebuggerpb.ListActiveBreakpointsResponse, error) {
182	md := metadata.Pairs("x-goog-request-params", fmt.Sprintf("%s=%v", "debuggee_id", req.GetDebuggeeId()))
183	ctx = insertMetadata(ctx, c.xGoogMetadata, md)
184	opts = append(c.CallOptions.ListActiveBreakpoints[0:len(c.CallOptions.ListActiveBreakpoints):len(c.CallOptions.ListActiveBreakpoints)], opts...)
185	var resp *clouddebuggerpb.ListActiveBreakpointsResponse
186	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
187		var err error
188		resp, err = c.controller2Client.ListActiveBreakpoints(ctx, req, settings.GRPC...)
189		return err
190	}, opts...)
191	if err != nil {
192		return nil, err
193	}
194	return resp, nil
195}
196
197// UpdateActiveBreakpoint updates the breakpoint state or mutable fields.
198// The entire Breakpoint message must be sent back to the controller service.
199//
200// Updates to active breakpoint fields are only allowed if the new value
201// does not change the breakpoint specification. Updates to the location,
202// condition and expressions fields should not alter the breakpoint
203// semantics. These may only make changes such as canonicalizing a value
204// or snapping the location to the correct line of code.
205func (c *Controller2Client) UpdateActiveBreakpoint(ctx context.Context, req *clouddebuggerpb.UpdateActiveBreakpointRequest, opts ...gax.CallOption) (*clouddebuggerpb.UpdateActiveBreakpointResponse, error) {
206	ctx = insertMetadata(ctx, c.xGoogMetadata)
207	opts = append(c.CallOptions.UpdateActiveBreakpoint[0:len(c.CallOptions.UpdateActiveBreakpoint):len(c.CallOptions.UpdateActiveBreakpoint)], opts...)
208	var resp *clouddebuggerpb.UpdateActiveBreakpointResponse
209	err := gax.Invoke(ctx, func(ctx context.Context, settings gax.CallSettings) error {
210		var err error
211		resp, err = c.controller2Client.UpdateActiveBreakpoint(ctx, req, settings.GRPC...)
212		return err
213	}, opts...)
214	if err != nil {
215		return nil, err
216	}
217	return resp, nil
218}
219