-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecrets.go
More file actions
67 lines (57 loc) · 2.19 KB
/
secrets.go
File metadata and controls
67 lines (57 loc) · 2.19 KB
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
package api
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/google/go-querystring/query"
"gopkg.in/nullstone-io/go-api-client.v0/response"
"gopkg.in/nullstone-io/go-api-client.v0/types"
)
type Secrets struct {
Client *Client
}
func (s Secrets) basePath(stackId, envId int64) string {
return fmt.Sprintf("/orgs/%s/stacks/%d/envs/%d/secrets", s.Client.Config.OrgName, stackId, envId)
}
func (s Secrets) secretPath(stackId, envId int64, secretNameOrId string) string {
return fmt.Sprintf("/orgs/%s/stacks/%d/envs/%d/secrets/%s", s.Client.Config.OrgName, stackId, envId, url.PathEscape(secretNameOrId))
}
func (s Secrets) List(ctx context.Context, stackId, envId int64, location types.SecretLocation) ([]types.Secret, error) {
q, err := query.Values(location)
if err != nil {
return nil, fmt.Errorf("error encoding request query: %w", err)
}
res, err := s.Client.Do(ctx, http.MethodGet, s.basePath(stackId, envId), q, nil, nil)
if err != nil {
return nil, err
}
return response.ReadJsonVal[[]types.Secret](res)
}
type AddSecretInput struct {
Identity types.SecretIdentity `json:"identity"`
Value string `json:"value"`
}
func (s Secrets) Add(ctx context.Context, stackId, envId int64, input AddSecretInput) (*types.Secret, error) {
raw, _ := json.Marshal(input)
res, err := s.Client.Do(ctx, http.MethodPost, s.basePath(stackId, envId), nil, nil, json.RawMessage(raw))
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Secret](res)
}
type UpdateSecretInput struct {
Value string `json:"value"`
}
// Update modifies the secret value in the platform's secret manager
// If secretNameOrId is Name, the id is inferred by using environment's default project/account/region info
// Specify a full Id (see types.SecretIdentity) to specify a different project/account/region
func (s Secrets) Update(ctx context.Context, stackId, envId int64, secretNameOrId string, input UpdateSecretInput) (*types.Secret, error) {
raw, _ := json.Marshal(input)
res, err := s.Client.Do(ctx, http.MethodPut, s.secretPath(stackId, envId, secretNameOrId), nil, nil, json.RawMessage(raw))
if err != nil {
return nil, err
}
return response.ReadJsonPtr[types.Secret](res)
}