forked from OpsLevel/opslevel-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets.go
99 lines (91 loc) · 3.11 KB
/
secrets.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
package opslevel
type Secret struct {
Alias string `json:"alias"`
ID ID `json:"id"`
Owner TeamId `json:"team"`
Timestamps Timestamps `json:"timestamps"`
}
type SecretInput struct {
Owner IdentifierInput `json:"owner" yaml:"owner"`
Value string `json:"value" yaml:"value"`
}
type SecretsVaultsSecretConnection struct {
Nodes []Secret
PageInfo PageInfo
TotalCount int `graphql:"-"`
}
func (client *Client) CreateSecret(alias string, input SecretInput) (*Secret, error) {
var m struct {
Payload struct {
Secret Secret
Errors []OpsLevelErrors
} `graphql:"secretsVaultsSecretCreate(alias: $alias, input: $input)"`
}
v := PayloadVariables{
"alias": alias,
"input": input,
}
err := client.Mutate(&m, v, WithName("SecretsVaultsSecretCreate"))
return &m.Payload.Secret, HandleErrors(err, m.Payload.Errors)
}
// List all Secrets for your account.
func (client *Client) ListSecretsVaultsSecret(variables *PayloadVariables) (SecretsVaultsSecretConnection, error) {
var q struct {
Account struct {
SecretsVaultsSecrets SecretsVaultsSecretConnection `graphql:"secretsVaultsSecrets(after: $after, first: $first)"`
}
}
if variables == nil {
variables = client.InitialPageVariablesPointer()
}
if err := client.Query(&q, *variables, WithName("SecretList")); err != nil {
return SecretsVaultsSecretConnection{}, err
}
for q.Account.SecretsVaultsSecrets.PageInfo.HasNextPage {
(*variables)["after"] = q.Account.SecretsVaultsSecrets.PageInfo.End
resp, err := client.ListSecretsVaultsSecret(variables)
if err != nil {
return SecretsVaultsSecretConnection{}, err
}
q.Account.SecretsVaultsSecrets.Nodes = append(q.Account.SecretsVaultsSecrets.Nodes, resp.Nodes...)
q.Account.SecretsVaultsSecrets.PageInfo = resp.PageInfo
}
q.Account.SecretsVaultsSecrets.TotalCount = len(q.Account.SecretsVaultsSecrets.Nodes)
return q.Account.SecretsVaultsSecrets, nil
}
func (client *Client) UpdateSecret(identifier string, secretInput SecretInput) (*Secret, error) {
var m struct {
Payload struct {
Secret Secret
Errors []OpsLevelErrors
} `graphql:"secretsVaultsSecretUpdate(input: $input, secret: $secret)"`
}
v := PayloadVariables{
"input": secretInput,
"secret": *NewIdentifier(identifier),
}
err := client.Mutate(&m, v, WithName("SecretsVaultsSecretUpdate"))
return &m.Payload.Secret, HandleErrors(err, m.Payload.Errors)
}
func (client *Client) DeleteSecret(identifier string) error {
var m struct {
Payload struct {
Errors []OpsLevelErrors `graphql:"errors"`
} `graphql:"secretsVaultsSecretDelete(resource: $input)"`
}
v := PayloadVariables{"input": *NewIdentifier(identifier)}
err := client.Mutate(&m, v, WithName("SecretsVaultsSecretDelete"))
return HandleErrors(err, m.Payload.Errors)
}
func (client *Client) GetSecret(identifier string) (*Secret, error) {
var q struct {
Account struct {
Secret Secret `graphql:"secretsVaultsSecret(input: $input)"`
}
}
v := PayloadVariables{"input": *NewIdentifier(identifier)}
if err := client.Query(&q, v, WithName("SecretsVaultsSecret")); err != nil {
return nil, err
}
return &q.Account.Secret, nil
}