-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
operations.go
executable file
·206 lines (184 loc) · 5.98 KB
/
operations.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
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
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Code generated by protoc-gen-go_gapic. DO NOT EDIT.
package compute
import (
"context"
"fmt"
"time"
computepb "cloud.google.com/go/compute/apiv1/computepb"
gax "github.com/googleapis/gax-go/v2"
"github.com/googleapis/gax-go/v2/apierror"
"google.golang.org/api/googleapi"
)
// Operation represents a long-running operation for this API.
type Operation struct {
operationHandle
}
// Done reports whether the long-running operation has completed.
func (o *Operation) Done() bool {
return o.Proto().GetStatus() == computepb.Operation_DONE
}
// Name returns the name of the long-running operation.
// The name is assigned by the server and is unique within the service from which the operation is created.
func (o *Operation) Name() string {
return o.Proto().GetName()
}
// Wait blocks until the operation is complete, polling regularly
// after an intial period of backing off between attempts.
func (o *Operation) Wait(ctx context.Context, opts ...gax.CallOption) error {
bo := gax.Backoff{
Initial: time.Second,
Max: time.Minute,
}
for {
if err := o.Poll(ctx, opts...); err != nil {
return err
}
if o.Done() {
return nil
}
if err := gax.Sleep(ctx, bo.Pause()); err != nil {
return err
}
}
}
type operationHandle interface {
// Poll retrieves the operation.
Poll(ctx context.Context, opts ...gax.CallOption) error
// Proto returns the long-running operation message.
Proto() *computepb.Operation
}
// Implements the operationHandle interface for RegionOperations.
type regionOperationsHandle struct {
c *RegionOperationsClient
proto *computepb.Operation
project string
region string
}
// Poll retrieves the latest data for the long-running operation.
func (h *regionOperationsHandle) Poll(ctx context.Context, opts ...gax.CallOption) error {
resp, err := h.c.Get(ctx, &computepb.GetRegionOperationRequest{
Operation: h.proto.GetName(),
Project: h.project,
Region: h.region,
}, opts...)
if err != nil {
return err
}
h.proto = resp
if resp.HttpErrorStatusCode != nil && (resp.GetHttpErrorStatusCode() < 200 || resp.GetHttpErrorStatusCode() > 299) {
aErr := &googleapi.Error{
Code: int(resp.GetHttpErrorStatusCode()),
Message: fmt.Sprintf("%s: %v", resp.GetHttpErrorMessage(), resp.GetError()),
}
err, _ := apierror.FromError(aErr)
return err
}
return nil
}
// Proto returns the raw type this wraps.
func (h *regionOperationsHandle) Proto() *computepb.Operation {
return h.proto
}
// Implements the operationHandle interface for ZoneOperations.
type zoneOperationsHandle struct {
c *ZoneOperationsClient
proto *computepb.Operation
project string
zone string
}
// Poll retrieves the latest data for the long-running operation.
func (h *zoneOperationsHandle) Poll(ctx context.Context, opts ...gax.CallOption) error {
resp, err := h.c.Get(ctx, &computepb.GetZoneOperationRequest{
Operation: h.proto.GetName(),
Project: h.project,
Zone: h.zone,
}, opts...)
if err != nil {
return err
}
h.proto = resp
if resp.HttpErrorStatusCode != nil && (resp.GetHttpErrorStatusCode() < 200 || resp.GetHttpErrorStatusCode() > 299) {
aErr := &googleapi.Error{
Code: int(resp.GetHttpErrorStatusCode()),
Message: fmt.Sprintf("%s: %v", resp.GetHttpErrorMessage(), resp.GetError()),
}
err, _ := apierror.FromError(aErr)
return err
}
return nil
}
// Proto returns the raw type this wraps.
func (h *zoneOperationsHandle) Proto() *computepb.Operation {
return h.proto
}
// Implements the operationHandle interface for GlobalOperations.
type globalOperationsHandle struct {
c *GlobalOperationsClient
proto *computepb.Operation
project string
}
// Poll retrieves the latest data for the long-running operation.
func (h *globalOperationsHandle) Poll(ctx context.Context, opts ...gax.CallOption) error {
resp, err := h.c.Get(ctx, &computepb.GetGlobalOperationRequest{
Operation: h.proto.GetName(),
Project: h.project,
}, opts...)
if err != nil {
return err
}
h.proto = resp
if resp.HttpErrorStatusCode != nil && (resp.GetHttpErrorStatusCode() < 200 || resp.GetHttpErrorStatusCode() > 299) {
aErr := &googleapi.Error{
Code: int(resp.GetHttpErrorStatusCode()),
Message: fmt.Sprintf("%s: %v", resp.GetHttpErrorMessage(), resp.GetError()),
}
err, _ := apierror.FromError(aErr)
return err
}
return nil
}
// Proto returns the raw type this wraps.
func (h *globalOperationsHandle) Proto() *computepb.Operation {
return h.proto
}
// Implements the operationHandle interface for GlobalOrganizationOperations.
type globalOrganizationOperationsHandle struct {
c *GlobalOrganizationOperationsClient
proto *computepb.Operation
}
// Poll retrieves the latest data for the long-running operation.
func (h *globalOrganizationOperationsHandle) Poll(ctx context.Context, opts ...gax.CallOption) error {
resp, err := h.c.Get(ctx, &computepb.GetGlobalOrganizationOperationRequest{
Operation: h.proto.GetName(),
}, opts...)
if err != nil {
return err
}
h.proto = resp
if resp.HttpErrorStatusCode != nil && (resp.GetHttpErrorStatusCode() < 200 || resp.GetHttpErrorStatusCode() > 299) {
aErr := &googleapi.Error{
Code: int(resp.GetHttpErrorStatusCode()),
Message: fmt.Sprintf("%s: %v", resp.GetHttpErrorMessage(), resp.GetError()),
}
err, _ := apierror.FromError(aErr)
return err
}
return nil
}
// Proto returns the raw type this wraps.
func (h *globalOrganizationOperationsHandle) Proto() *computepb.Operation {
return h.proto
}