forked from dilutedev/doppler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice_tokens.go
More file actions
130 lines (110 loc) · 3.16 KB
/
Copy pathservice_tokens.go
File metadata and controls
130 lines (110 loc) · 3.16 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
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
126
127
128
129
130
package doppler
import (
"bytes"
"encoding/json"
"net/http"
)
type ServiceToken struct {
Name string `json:"name,omitempty"`
Slug string `json:"slug,omitempty"`
Access string `json:"access,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Key string `json:"key,omitempty"`
Project string `json:"project,omitempty"`
Environment string `json:"environment,omitempty"`
Config string `json:"config,omitempty"`
ExpiresAt string `json:"expires_at,omitempty"`
LastSeenAt string `json:"last_seen_at,omitempty"`
TokenPreview string `json:"token_preview,omitempty"`
}
type ServiceTokens struct {
Tokens []ServiceToken `json:"tokens,omitempty"`
Success bool `json:"success,omitempty"`
}
type ServiceTokenModel struct {
Token ServiceToken `json:"token,omitempty"`
Success bool `json:"success,omitempty"`
}
type CreateTokenParams struct {
Project string `json:"project,omitempty"` // Unique identifier for the project object.
Config string `json:"config,omitempty"` // Name of the config object.
Name string `json:"name,omitempty"` // Name of the service token.
ExpireAt string `json:"expire_at,omitempty"` // Unix timestamp of when token should expire.
Access string `json:"access,omitempty"` // Token's capabilities. "read/write" or "read". Default: read
}
type DeleteTokenParams struct {
Project string `json:"project,omitempty"` // Unique identifier for the project object.
Config string `json:"config,omitempty"` // Name of the config object.
Slug string `json:"slug,omitempty"` // The slug of the service token.
}
func (dp *doppler) ListServiceTokens(project, config string) (*ServiceTokens, error) {
request, err := http.NewRequest(
http.MethodGet,
"/v3/configs/config/tokens?project="+project+"&config="+config,
nil,
)
if err != nil {
return nil, err
}
body, err := dp.makeApiRequest(request)
if err != nil {
return nil, err
}
data := &ServiceTokens{}
err = json.Unmarshal(body, data)
if err != nil {
return nil, err
}
return data, nil
}
func (dp *doppler) CreateServiceToken(params CreateTokenParams) (*ServiceTokenModel, error) {
if params.Access != "read" && params.Access != "read/write" {
params.Access = "read"
}
payload, err := json.Marshal(params)
if err != nil {
return nil, err
}
request, err := http.NewRequest(
http.MethodPost,
"/v3/configs/config/tokens",
bytes.NewReader(payload),
)
if err != nil {
return nil, err
}
body, err := dp.makeApiRequest(request)
if err != nil {
return nil, err
}
data := &ServiceTokenModel{}
err = json.Unmarshal(body, data)
if err != nil {
return nil, err
}
return data, nil
}
func (dp *doppler) DeleteServiceToken(params DeleteTokenParams) (*Success, error) {
payload, err := json.Marshal(params)
if err != nil {
return nil, err
}
request, err := http.NewRequest(
http.MethodDelete,
"/v3/configs/config/tokens/token",
bytes.NewReader(payload),
)
if err != nil {
return nil, err
}
body, err := dp.makeApiRequest(request)
if err != nil {
return nil, err
}
data := &Success{}
err = json.Unmarshal(body, data)
if err != nil {
return nil, err
}
return data, nil
}