-
Notifications
You must be signed in to change notification settings - Fork 86
/
result.go
85 lines (71 loc) · 2.5 KB
/
result.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
/*
Copyright 2023 The Radius Authors.
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
http://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.
*/
package controller
import (
v1 "github.com/radius-project/radius/pkg/armrpc/api/v1"
)
// Result is the response of async operation controller.
type Result struct {
// Requeue tells the Controller to requeue the reconcile key. Defaults to false.
Requeue bool
// Error represents the error when status is Cancelled or Failed.
Error *v1.ErrorDetails
// state represents the provisioning status.
state *v1.ProvisioningState
}
// NewCanceledResult creates a new Result object with a canceled status and the given message.
func NewCanceledResult(message string) Result {
r := Result{}
r.SetCanceled(message)
return r
}
// NewFailedResult creates a new Result object with the given error details and sets the failed flag to true.
func NewFailedResult(err v1.ErrorDetails) Result {
r := Result{}
r.SetFailed(err, false)
return r
}
// SetFailed sets the error response with Failed status.
func (r *Result) SetFailed(err v1.ErrorDetails, requeue bool) {
if r == nil {
r = &Result{}
}
r.Requeue = requeue
r.SetProvisioningState(v1.ProvisioningStateFailed)
r.Error = &err
}
// SetCanceled sets the Result's Requeue field to false, sets the ProvisioningState to Canceled and sets the Error field
// to an ErrorDetails object with the given message.
func (r *Result) SetCanceled(message string) {
if r == nil {
r = &Result{}
}
r.Requeue = false
r.SetProvisioningState(v1.ProvisioningStateCanceled)
r.Error = &v1.ErrorDetails{
Code: v1.CodeOperationCanceled,
Message: message,
}
}
// SetProvisioningState sets the state of a Result object to the given ProvisioningState.
func (r *Result) SetProvisioningState(s v1.ProvisioningState) {
r.state = &s
}
// ProvisioningState returns the provisioning state of the request object, which is either v1.ProvisioningStateSucceeded
// or the value of the state field if it is not nil.
func (r *Result) ProvisioningState() v1.ProvisioningState {
if r.state == nil {
return v1.ProvisioningStateSucceeded
}
return *r.state
}