forked from dilutedev/doppler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecrets_sync.go
107 lines (89 loc) · 2.47 KB
/
secrets_sync.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
package doppler
import (
"encoding/json"
"net/http"
"strconv"
)
type Sync struct {
Slug string `json:"slug,omitempty"`
Integration string `json:"integration,omitempty"`
Project string `json:"project,omitempty"`
Config string `json:"config,omitempty"`
Enabled bool `json:"enabled,omitempty"`
LastSyncedAt string `json:"last_synced_at,omitempty"`
}
type SyncData struct {
Sync Sync `json:"sync,omitempty"`
Success bool `json:"success,omitempty"`
}
type SyncQueryParams struct {
Project string // The project slug
Config string // The config slug
Sync string // The sync slug: use with RetrieveSync and DeleteSync
DeleteFromTarget bool // use with DeleteSync function only
}
type SyncBodyParams struct {
Integration string // The integration slug which the sync will use
ImportOption string // prefer_doppler or prefer_integration, defaults to none
}
// Create a new secrets sync.
func (dp *doppler) CreateSync(queryParams SyncQueryParams, bodyParams SyncBodyParams) (*SyncData, error) {
if bodyParams.ImportOption == "" {
bodyParams.ImportOption = "none"
}
request, err := http.NewRequest(
http.MethodPost,
"/v3/configs/config/syncs?project="+queryParams.Project+"&config="+queryParams.Config,
nil,
)
if err != nil {
return nil, err
}
body, err := dp.makeApiRequest(request)
if err != nil {
return nil, err
}
data := &SyncData{}
err = json.Unmarshal(body, data)
if err != nil {
return nil, err
}
return data, nil
}
// Retrieve an existing secrets sync.
func (dp *doppler) RetrieveSync(params SyncQueryParams) (*SyncData, error) {
request, err := http.NewRequest(
http.MethodGet,
"/v3/configs/config/syncs/sync?project="+params.Project+"&config="+params.Config+"&sync="+params.Sync,
nil,
)
if err != nil {
return nil, err
}
body, err := dp.makeApiRequest(request)
if err != nil {
return nil, err
}
data := &SyncData{}
err = json.Unmarshal(body, data)
if err != nil {
return nil, err
}
return data, nil
}
// Delete an existing sync.
func (dp *doppler) DeleteSync(params SyncQueryParams) (string, error) {
request, err := http.NewRequest(
http.MethodDelete,
"/v3/configs/config/syncs/sync?project="+params.Project+"&config="+params.Config+"&sync="+params.Sync+"&delete_from_target="+strconv.FormatBool(params.DeleteFromTarget),
nil,
)
if err != nil {
return "", err
}
body, err := dp.makeApiRequest(request)
if err != nil {
return "", err
}
return string(body), nil
}