-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_resonse.go
148 lines (135 loc) · 5.02 KB
/
base_resonse.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
package clcv1
import (
"reflect"
"fmt"
)
// All requests will receive a response with at least these attributes:
type BaseResponse struct {
// True if the request was successful, otherwise False.
Success bool
// A description of the result. The contents of this field does not contain
// any actionable information, it is purely intended to provide a human
// readable description of the result.
Message string
// This value will help to identify any errors which were encountered while
// processing the request. The value of '0' indicates success, all non-zero
// StatusCodes indicate an error state.
StatusCode int
}
// Evaluate the StatusCode of @b according to cases listed in the v1 API
func (b *BaseResponse) Evaluate() (err error) {
switch b.StatusCode {
case 0: /* Success */
case 2:
err = fmt.Errorf("Unknown application error - contact support to resolve the issue (%s).", b.Message)
case 3:
err = fmt.Errorf("Invalid request format (%s).", b.Message)
case 5:
err = fmt.Errorf("Resource not found (%s).", b.Message)
case 6:
err = fmt.Errorf("Invalid operation (%s).", b.Message)
case 100:
// FIXME: re-authenticate here unless this is Logon request
err = fmt.Errorf("The APIKey / Password combination is valid. Verify your CenturyLink Cloud Credentials.")
case 101:
err = fmt.Errorf("Access denied (%s).", b.Message)
case 400:
err = fmt.Errorf("You have reached the SMTP Relay Alias limit set on your account (%s).", b.Message)
case 401:
err = fmt.Errorf("Relay alias attribute required (%s).", b.Message)
case 402:
err = fmt.Errorf("Relay alias was previously deleted (%s).", b.Message)
case 403:
err = fmt.Errorf("Relay alias has already been disabled (%s).", b.Message)
case 500:
err = fmt.Errorf("Invalid memory value (%s).", b.Message)
case 501:
err = fmt.Errorf("Invalid CPU value (%s).", b.Message)
case 502:
err = fmt.Errorf("Alias required (%s).", b.Message)
case 503:
err = fmt.Errorf("Alias length exceeded (%s).", b.Message)
case 506:
err = fmt.Errorf("Server name required (%s).", b.Message)
case 514:
err = fmt.Errorf("Server password required (%s).", b.Message)
case 541:
err = fmt.Errorf("Hardware Group ID required (%s).", b.Message)
case 900:
err = fmt.Errorf("Invalid RequestID (%s)", b.Message)
case 1000:
err = fmt.Errorf("The IP address provided is not configured on the server (%s)", b.Message)
case 1201:
err = fmt.Errorf("The visibility value is missing or invalid (%s).", b.Message)
case 1310:
err = fmt.Errorf("The Name attribute is missing (%s).", b.Message)
case 1410:
err = fmt.Errorf("Name required (%s).", b.Message)
case 1411:
err = fmt.Errorf("Password required (%s).", b.Message)
case 1413:
err = fmt.Errorf("Maximum size of additional storage exceeded (%s).", b.Message)
case 1414:
err = fmt.Errorf("Password does not meet strength requirements (%s).", b.Message)
case 1415:
err = fmt.Errorf("Unknown snapshot state (%s).", b.Message)
case 1510:
err = fmt.Errorf("Network required (%s).", b.Message)
case 1511:
err = fmt.Errorf("Public IP address required (%s).", b.Message)
case 1600:
err = fmt.Errorf("Account alias missing (%s).", b.Message)
case 1700:
err = fmt.Errorf("Email address required (%s).", b.Message)
case 1702:
err = fmt.Errorf("First name required (%s).", b.Message)
case 1703:
err = fmt.Errorf("Last name required (%s).", b.Message)
case 1704:
err = fmt.Errorf("Username required (%s).", b.Message)
case 1705:
err = fmt.Errorf("User not found (%s).", b.Message)
case 1706:
err = fmt.Errorf("Invalid user role(s) (%s).", b.Message)
case 1800:
err = fmt.Errorf("Account not found (%s).", b.Message)
case 1801:
err = fmt.Errorf("Invalid start date (%s).", b.Message)
case 1802:
err = fmt.Errorf("Invalid end date (%s).", b.Message)
default:
err = fmt.Errorf("%s (Status Code: %d).", b.Message, b.StatusCode)
}
return
}
// Extract (embedded) BaseResponse from @inModel
func ExtractBaseResponse(inModel interface{}) (*BaseResponse, error) {
var base reflect.Value
if inModel == nil {
return nil, fmt.Errorf("Result model can not be nil")
}
resType := reflect.TypeOf(inModel)
/* inModel must be a pointer type (call-by-value) */
if resType.Kind() != reflect.Ptr {
return nil, fmt.Errorf("Expecting pointer to model %T", inModel)
}
/* The following is sufficient if BaseResponse is passed in directly as pointer */
base = reflect.Indirect(reflect.ValueOf(inModel))
if base.Kind() != reflect.Struct {
return nil, fmt.Errorf("Result model %T is not a pointer to struct", inModel)
}
if resType.Elem().Name() != "BaseResponse" {
/* BaseResponse embedded within @inModel */
base = base.FieldByName("BaseResponse")
}
if base.Kind() == reflect.Invalid {
return nil, fmt.Errorf("Unable to extract base from result model %T", inModel)
} else if !base.CanInterface() {
return nil, fmt.Errorf("Unable to cast base part of %T as interface", inModel)
}
br, ok := base.Interface().(BaseResponse);
if !ok {
return nil, fmt.Errorf("Unable to cast %+v of %T as BaseResponse", base, inModel)
}
return &br, nil
}