-
Notifications
You must be signed in to change notification settings - Fork 13
/
aliases.go
95 lines (84 loc) · 2.15 KB
/
aliases.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
package opslevel
import (
"fmt"
"strings"
)
type AliasCreateInput struct {
Alias string `json:"alias"`
OwnerId ID `json:"ownerId"`
}
type AliasDeleteInput struct {
Alias string `json:"alias"`
OwnerType AliasOwnerTypeEnum `json:"ownerType"`
}
// #region Create
// TODO: make sure duplicate aliases throw an error that we can catch
func (client *Client) CreateAliases(ownerId ID, aliases []string) ([]string, error) {
var output []string
var errors []string
for _, alias := range aliases {
input := AliasCreateInput{
Alias: alias,
OwnerId: ownerId,
}
result, err := client.CreateAlias(input)
if err != nil {
errors = append(errors, err.Error())
}
for _, resultAlias := range result {
output = append(output, string(resultAlias))
}
}
output = removeDuplicates(output)
if len(errors) > 0 {
return output, fmt.Errorf(strings.Join(errors, "\n"))
} else {
return output, nil
}
}
func (client *Client) CreateAlias(input AliasCreateInput) ([]string, error) {
var m struct {
Payload struct {
Aliases []string
OwnerId string
Errors []OpsLevelErrors
} `graphql:"aliasCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("AliasCreate"))
output := make([]string, len(m.Payload.Aliases))
for i, item := range m.Payload.Aliases {
output[i] = string(item)
}
return output, HandleErrors(err, m.Payload.Errors)
}
//#endregion
//#region Delete
func (client *Client) DeleteServiceAlias(alias string) error {
return client.DeleteAlias(AliasDeleteInput{
Alias: alias,
OwnerType: AliasOwnerTypeEnumService,
})
}
func (client *Client) DeleteTeamAlias(alias string) error {
return client.DeleteAlias(AliasDeleteInput{
Alias: alias,
OwnerType: AliasOwnerTypeEnumTeam,
})
}
func (client *Client) DeleteAlias(input AliasDeleteInput) error {
var m struct {
Payload struct {
Alias string `graphql:"deletedAlias"`
Errors []OpsLevelErrors
} `graphql:"aliasDelete(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("AliasDelete"))
return HandleErrors(err, m.Payload.Errors)
}
//#endregion