forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaliases.go
54 lines (49 loc) · 1.25 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
package opslevel
import (
"github.com/shurcooL/graphql"
)
type AliasCreateInput struct {
Alias string `json:"alias"`
OwnerId graphql.ID `json:"ownerId"`
}
//#region Create
// TODO: make sure duplicate aliases throw an error that we can catch
func (client *Client) CreateAliases(ownerId graphql.ID, aliases []string) []string {
var output []string
for _, alias := range aliases {
input := AliasCreateInput{
Alias: alias,
OwnerId: ownerId,
}
result, err := client.CreateAlias(input)
if err != nil {
// TODO: log warning about failed create?
}
for _, resultAlias := range result {
output = append(output, string(resultAlias))
}
}
// TODO: need to treat this like a HashSet to deduplicate
return output
}
func (client *Client) CreateAlias(input AliasCreateInput) ([]string, error) {
var m struct {
Payload struct {
Aliases []graphql.String
OwnerId graphql.String
Errors []OpsLevelErrors
} `graphql:"aliasCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
if err := client.Mutate(&m, v); err != nil {
return nil, err
}
output := make([]string, len(m.Payload.Aliases))
for i, item := range m.Payload.Aliases {
output[i] = string(item)
}
return output, FormatErrors(m.Payload.Errors)
}
//#endregion