-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathaliases.go
125 lines (111 loc) · 3.44 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
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
package opslevel
import (
"errors"
"fmt"
)
type AliasableResourceInterface interface {
GetAliases() []string
ResourceId() ID
AliasableType() AliasOwnerTypeEnum
}
func (client *Client) GetAliasableResource(resourceType AliasOwnerTypeEnum, identifier string) (AliasableResourceInterface, error) {
var err error
var aliasableResource AliasableResourceInterface
switch resourceType {
case AliasOwnerTypeEnumService:
if IsID(identifier) {
aliasableResource, err = client.GetService(identifier)
} else {
aliasableResource, err = client.GetServiceWithAlias(identifier)
}
case AliasOwnerTypeEnumTeam:
if IsID(identifier) {
aliasableResource, err = client.GetTeam(ID(identifier))
} else {
aliasableResource, err = client.GetTeamWithAlias(identifier)
}
case AliasOwnerTypeEnumSystem:
aliasableResource, err = client.GetSystem(identifier)
case AliasOwnerTypeEnumDomain:
aliasableResource, err = client.GetDomain(identifier)
case AliasOwnerTypeEnumInfrastructureResource:
aliasableResource, err = client.GetInfrastructure(identifier)
case AliasOwnerTypeEnumScorecard:
aliasableResource, err = client.GetScorecard(identifier)
default:
err = fmt.Errorf("not an aliasable resource type '%s'", resourceType)
}
return aliasableResource, err
}
func (client *Client) CreateAliases(ownerId ID, aliases []string) ([]string, error) {
var output []string
var allErrors error
for _, alias := range aliases {
input := AliasCreateInput{
Alias: alias,
OwnerId: ID(ownerId),
}
result, err := client.CreateAlias(input)
allErrors = errors.Join(allErrors, err)
output = append(output, result...)
}
output = removeDuplicates(output)
return output, allErrors
}
func (client *Client) CreateAlias(input AliasCreateInput) ([]string, error) {
var m struct {
Payload AliasCreatePayload `graphql:"aliasCreate(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("AliasCreate"))
output := make([]string, len(m.Payload.Aliases))
copy(output, m.Payload.Aliases)
return output, HandleErrors(err, m.Payload.Errors)
}
// Deprecated: use client.DeleteAlias instead
func (client *Client) DeleteInfraAlias(alias string) error {
return client.DeleteAlias(AliasDeleteInput{
Alias: alias,
OwnerType: AliasOwnerTypeEnumInfrastructureResource,
})
}
// Deprecated: use client.DeleteAlias instead
func (client *Client) DeleteServiceAlias(alias string) error {
return client.DeleteAlias(AliasDeleteInput{
Alias: alias,
OwnerType: AliasOwnerTypeEnumService,
})
}
// Deprecated: use client.DeleteAlias instead
func (client *Client) DeleteTeamAlias(alias string) error {
return client.DeleteAlias(AliasDeleteInput{
Alias: alias,
OwnerType: AliasOwnerTypeEnumTeam,
})
}
func (client *Client) DeleteAliases(aliasOwnerType AliasOwnerTypeEnum, aliases []string) error {
var allErrors error
for _, alias := range aliases {
input := AliasDeleteInput{
Alias: alias,
OwnerType: aliasOwnerType,
}
allErrors = errors.Join(allErrors, client.DeleteAlias(input))
}
return allErrors
}
func (client *Client) DeleteAlias(input AliasDeleteInput) error {
var m struct {
Payload struct { // TODO: we don't need this but removing it breaks alot of tests
Alias string `graphql:"deletedAlias"`
BasePayload
} `graphql:"aliasDelete(input: $input)"`
}
v := PayloadVariables{
"input": input,
}
err := client.Mutate(&m, v, WithName("AliasDelete"))
return HandleErrors(err, m.Payload.Errors)
}