generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy patherrors.go
167 lines (153 loc) · 6.31 KB
/
errors.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
package helpers
import (
"encoding/json"
"github.com/harness/harness-go-sdk/harness/policymgmt"
"net/http"
"github.com/harness/harness-go-sdk/harness/nextgen"
openapi_client_nextgen "github.com/harness/harness-openapi-go-client/nextgen"
"google.golang.org/grpc/codes"
"github.com/harness/harness-go-sdk/harness/dbops"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func HandleApiError(err error, d *schema.ResourceData, httpResp *http.Response) diag.Diagnostics {
return handleApiError(err, d, httpResp, false)
}
func HandleDBOpsApiError(err error, d *schema.ResourceData, httpResp *http.Response) diag.Diagnostics {
erro, ok := err.(dbops.GenericSwaggerError)
if ok && httpResp != nil {
if httpResp.StatusCode == 401 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if token has expired or is wrong.\n" +
"2) Harness Provider is misconfigured. For firstgen resources please give the correct api_key and for nextgen resources please give the correct platform_api_key.")
}
if httpResp.StatusCode == 403 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if the token has required permission for this operation.\n" +
"2) Please check if the token has expired or is wrong.")
}
if httpResp.StatusCode == 404 {
return diag.Errorf("resource with ID %s not found: %v", d.Id(), erro.Error())
}
}
return diag.Errorf(err.Error())
}
func HandlePolicyApiError(err error, d *schema.ResourceData, httpResp *http.Response) diag.Diagnostics {
_, ok := err.(policymgmt.GenericSwaggerError)
if ok && httpResp != nil {
if httpResp.StatusCode == 401 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if token has expired or is wrong.\n" +
"2) Harness Provider is misconfigured. For firstgen resources please give the correct api_key and for nextgen resources please give the correct platform_api_key.")
}
if httpResp.StatusCode == 403 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if the token has required permission for this operation.\n" +
"2) Please check if the token has expired or is wrong.")
}
if httpResp.StatusCode == 404 {
d.SetId("")
return nil
}
}
return diag.Errorf(err.Error())
}
func handleApiError(err error, d *schema.ResourceData, httpResp *http.Response, read bool) diag.Diagnostics {
erro, ok := err.(nextgen.GenericSwaggerError)
if ok {
errMessage := erro.Error()
gitopsErr, gitopsErrOk := erro.Model().(nextgen.GatewayruntimeError)
if gitopsErrOk {
errMessage = gitopsErr.Message
}
if httpResp != nil && httpResp.StatusCode == 401 {
return diag.Errorf("%s", httpResp.Status+"\n"+"Hint:\n"+
"1) Please check if token has expired or is wrong.\n"+
"2) Harness Provider is misconfigured. For firstgen resources please give the correct api_key and for nextgen resources please give the correct platform_api_key.")
}
if gitopsErrOk && httpResp != nil && httpResp.StatusCode == 403 {
if len(errMessage) > 0 {
return diag.Errorf("%s", httpResp.Status+"\n"+"Hint:\n"+
"1) Please check if the token has required permission for this operation.\n"+
"2) Please check if the token has expired or is wrong.\n"+
"3) "+errMessage)
}
}
if httpResp != nil && httpResp.StatusCode == 403 {
return diag.Errorf("%s", httpResp.Status+"\n"+"Hint:\n"+
"1) Please check if the token has required permission for this operation.\n"+
"2) Please check if the token has expired or is wrong.")
}
if httpResp != nil && httpResp.StatusCode == 404 {
// GitOps handling for NotFound
if gitopsErrOk && read {
if codes.Code(gitopsErr.Code) == codes.NotFound {
d.SetId("")
d.MarkNewResource()
return nil
}
}
if read && !gitopsErrOk && (erro.Code() == nextgen.ErrorCodes.EntityNotFound) {
d.SetId("")
d.MarkNewResource()
return nil
}
return diag.Errorf("resource with ID %s not found: %v", d.Id(), errMessage)
}
if read && !gitopsErrOk {
if erro.Model() != nil && (erro.Code() == nextgen.ErrorCodes.ResourceNotFound || erro.Code() == nextgen.ErrorCodes.EntityNotFound) {
d.SetId("")
d.MarkNewResource()
return nil
}
}
return diag.Errorf(errMessage)
}
err_openapi_client, ok := err.(openapi_client_nextgen.GenericSwaggerError)
if ok {
if httpResp != nil && httpResp.StatusCode == 401 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if token has expired or is wrong.\n" +
"2) Harness Provider is misconfigured. For firstgen resources please give the correct api_key and for nextgen resources please give the correct platform_api_key.")
}
if httpResp != nil && httpResp.StatusCode == 403 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if the token has required permission for this operation.\n" +
"2) Please check if the token has expired or is wrong.")
}
if httpResp != nil && httpResp.StatusCode == 404 {
return diag.Errorf("resource with ID %s not found: %v", d.Id(), erro.Error())
}
var jsonMap map[string]interface{}
err := json.Unmarshal(err_openapi_client.Body(), &jsonMap)
if err == nil {
return diag.Errorf(jsonMap["message"].(string))
}
return diag.Errorf(err_openapi_client.Error())
}
return diag.Errorf(err.Error())
}
func HandleReadApiError(err error, d *schema.ResourceData, httpResp *http.Response) diag.Diagnostics {
return handleApiError(err, d, httpResp, true)
}
func HandleDBOpsReadApiError(err error, d *schema.ResourceData, httpResp *http.Response) diag.Diagnostics {
_, ok := err.(dbops.GenericSwaggerError)
if ok && httpResp != nil {
if httpResp.StatusCode == 401 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if token has expired or is wrong.\n" +
"2) Harness Provider is misconfigured. For firstgen resources please give the correct api_key and for nextgen resources please give the correct platform_api_key.")
}
if httpResp.StatusCode == 403 {
return diag.Errorf(httpResp.Status + "\n" + "Hint:\n" +
"1) Please check if the token has required permission for this operation.\n" +
"2) Please check if the token has expired or is wrong.")
}
if httpResp.StatusCode == 404 {
d.SetId("")
d.MarkNewResource()
return nil
}
}
return diag.Errorf(err.Error())
}