-
Notifications
You must be signed in to change notification settings - Fork 13
/
common.go
137 lines (114 loc) · 3.06 KB
/
common.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
package opslevel
import (
"errors"
"fmt"
"slices"
"strings"
"time"
"github.com/hasura/go-graphql-client"
"github.com/relvacode/iso8601"
)
type PageInfo struct {
HasNextPage bool `graphql:"hasNextPage"`
HasPreviousPage bool `graphql:"hasPreviousPage"`
Start string `graphql:"startCursor"`
End string `graphql:"endCursor"`
}
type PayloadVariables map[string]interface{}
// WithoutDeactivedUsers filters out deactivated users on ListUsers query
func (pv *PayloadVariables) WithoutDeactivedUsers() *PayloadVariables {
omitDeactivedUsersFilter := UsersFilterInput{
Key: UsersFilterEnumDeactivatedAt,
Type: RefOf(BasicTypeEnumEquals),
}
(*pv)["filter"] = RefOf([]UsersFilterInput{omitDeactivedUsersFilter})
return pv
}
type OpsLevelWarnings struct {
Message string
}
type OpsLevelErrors struct {
Message string
Path []string
}
type Timestamps struct {
CreatedAt iso8601.Time `json:"createdAt"`
UpdatedAt iso8601.Time `json:"updatedAt"`
}
func NullString() *string {
var output *string
return output
}
func RefOf[T any](v T) *T {
return &v
}
func RefTo[T any](v T) *T {
return &v
}
func HandleErrors(err error, errs []OpsLevelErrors) error {
if err != nil {
return err
}
return FormatErrors(errs)
}
func FormatErrors(errs []OpsLevelErrors) error {
if len(errs) == 0 {
return nil
}
allErrors := fmt.Errorf("OpsLevel API Errors:")
for _, err := range errs {
if len(err.Path) == 1 && err.Path[0] == "base" {
err.Path[0] = ""
}
newErr := fmt.Errorf("\t- '%s' %s", strings.Join(err.Path, "."), err.Message)
allErrors = errors.Join(allErrors, newErr)
}
return allErrors
}
// IsOpsLevelApiError checks if the error is returned by OpsLevel's API
func IsOpsLevelApiError(err error) bool {
if _, ok := err.(graphql.Errors); !ok {
return false
}
for _, hasuraErr := range err.(graphql.Errors) {
if len(hasuraErr.Path) > 0 {
return true
}
}
return false
}
func NewISO8601Date(datetime string) iso8601.Time {
date, _ := iso8601.ParseString(datetime)
return iso8601.Time{Time: date}
}
func NewISO8601DateNow() iso8601.Time {
return iso8601.Time{Time: time.Now()}
}
func removeDuplicates(data []string) []string {
keys := make(map[string]bool)
var list []string
for _, entry := range data {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
// Given actual aliases and wanted aliases, returns aliasesToCreate and aliasesToDelete lists
func extractAliases(existingAliases, aliasesWanted []string) ([]string, []string) {
var aliasesToCreate, aliasesToDelete []string
// collect aliasesToDelete - existing aliases that are no longer wanted
for _, alias := range existingAliases {
if !slices.Contains(aliasesWanted, alias) {
aliasesToDelete = append(aliasesToDelete, alias)
}
}
// collect aliasesToCreate - wanted aliases that do not yet exist
for _, aliasWanted := range aliasesWanted {
if !slices.Contains(existingAliases, aliasWanted) {
aliasesToCreate = append(aliasesToCreate, aliasWanted)
}
}
return aliasesToCreate, aliasesToDelete
}