forked from ccin2p3/go-freeipa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
failed_responses.go
69 lines (55 loc) · 1.64 KB
/
failed_responses.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
package freeipa
import (
"encoding/json"
"fmt"
)
const (
FailedReasonNoSuchEntry = "no such entry"
FailedReasonAlreadyAMember = "This entry is already a member"
)
type FailedOperations map[string]map[string]failedOperations
type fromRootFailedOperations map[string]failedOperations
func (f fromRootFailedOperations) String() string {
userFriendlyFailures := make(map[string]string)
for rootFailName, fOperations := range f {
for _, fOperation := range fOperations {
fromRootName := fmt.Sprintf("%s/%s", rootFailName, fOperation.Name)
userFriendlyFailures[fromRootName] = fOperation.Reason
}
}
return fmt.Sprintf("%+v", userFriendlyFailures)
}
func (f FailedOperations) GetFailures() fromRootFailedOperations {
failures := make(fromRootFailedOperations)
for rootFailureCategoryName, v := range f {
for subFailureCategoryName, failedOp := range v {
if len(failedOp) > 0 {
fromRootFailureName := fmt.Sprintf("%s/%s", rootFailureCategoryName, subFailureCategoryName)
failures[fromRootFailureName] = append(failures[fromRootFailureName], failedOp...)
}
}
}
return failures
}
type failedOperation struct {
Name string
Reason string
}
type failedOperations []failedOperation
func (f *failedOperations) UnmarshalJSON(b []byte) error {
var rawFailedStr [][]string
if err := json.Unmarshal(b, &rawFailedStr); err != nil {
return err
}
*f = failedOperations{}
for _, failedEntry := range rawFailedStr {
if len(failedEntry) != 2 {
return fmt.Errorf("failed entry %v does not have two elements", failedEntry)
}
*f = append(*f, failedOperation{
Name: failedEntry[0],
Reason: failedEntry[1],
})
}
return nil
}