-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloudsync.go
More file actions
174 lines (156 loc) · 6.13 KB
/
cloudsync.go
File metadata and controls
174 lines (156 loc) · 6.13 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package truenas
import (
"encoding/json"
"fmt"
)
// CloudSyncCredentialResponse represents a cloud sync credential from the API.
type CloudSyncCredentialResponse struct {
ID int64 `json:"id"`
Name string `json:"name"`
Provider CloudSyncCredentialProvider `json:"provider"`
}
// CloudSyncCredentialProvider contains the provider type and attributes.
type CloudSyncCredentialProvider struct {
Type string `json:"type"`
// S3 attributes
AccessKeyID string `json:"access_key_id,omitempty"`
SecretAccessKey string `json:"secret_access_key,omitempty"`
Endpoint string `json:"endpoint,omitempty"`
Region string `json:"region,omitempty"`
// B2 attributes
Account string `json:"account,omitempty"`
Key string `json:"key,omitempty"`
// GCS attributes
ServiceAccountCredentials string `json:"service_account_credentials,omitempty"`
}
// CloudSyncTaskCredentialRef is a minimal struct for embedded credential references in tasks.
// The full credential parsing is handled by ParseCredentials when needed.
// This avoids version-specific parsing complexity since task responses embed credentials
// with provider in different formats (string in 24.x, object in 25.x).
type CloudSyncTaskCredentialRef struct {
ID int64 `json:"id"`
Name string `json:"name"`
}
// CloudSyncTaskResponse represents a cloud sync task from the API.
type CloudSyncTaskResponse struct {
ID int64 `json:"id"`
Description string `json:"description"`
Path string `json:"path"`
Credentials CloudSyncTaskCredentialRef `json:"credentials"`
Attributes json.RawMessage `json:"attributes"` // Can be object or false
Schedule ScheduleResponse `json:"schedule"`
Direction string `json:"direction"`
TransferMode string `json:"transfer_mode"`
Encryption bool `json:"encryption"`
EncryptionPassword string `json:"encryption_password,omitempty"`
EncryptionSalt string `json:"encryption_salt,omitempty"`
Snapshot bool `json:"snapshot"`
Transfers int64 `json:"transfers"`
BWLimit []BwLimit `json:"bwlimit"`
Exclude []string `json:"exclude"`
Include []string `json:"include"`
FollowSymlinks bool `json:"follow_symlinks"`
CreateEmptySrcDirs bool `json:"create_empty_src_dirs"`
Enabled bool `json:"enabled"`
Job *JobStatus `json:"job,omitempty"`
}
// BwLimit represents a bandwidth limit entry.
type BwLimit struct {
Time string `json:"time"`
Bandwidth *int64 `json:"bandwidth"` // null when unlimited
}
// ScheduleResponse represents a cron schedule from the API.
type ScheduleResponse struct {
Minute string `json:"minute"`
Hour string `json:"hour"`
Dom string `json:"dom"`
Month string `json:"month"`
Dow string `json:"dow"`
}
// JobStatus represents the last job status for a task.
type JobStatus struct {
ID int64 `json:"id"`
State string `json:"state"`
}
// credentialRaw is the intermediate struct for parsing API responses.
type credentialRaw struct {
ID int64 `json:"id"`
Name string `json:"name"`
Provider json.RawMessage `json:"provider"`
Attributes json.RawMessage `json:"attributes"` // 24.x only
}
// parseCredentialV25 parses a 25.x format credential response.
// Format: { "provider": { "type": "S3", "access_key_id": "...", ... } }
func parseCredentialV25(raw credentialRaw) (CloudSyncCredentialResponse, error) {
var c CloudSyncCredentialResponse
c.ID = raw.ID
c.Name = raw.Name
if err := json.Unmarshal(raw.Provider, &c.Provider); err != nil {
return c, fmt.Errorf("parse provider object: %w", err)
}
return c, nil
}
// parseCredentialV24 parses a 24.x format credential response.
// Format: { "provider": "S3", "attributes": { ... } }
func parseCredentialV24(raw credentialRaw) (CloudSyncCredentialResponse, error) {
var c CloudSyncCredentialResponse
c.ID = raw.ID
c.Name = raw.Name
var providerType string
if err := json.Unmarshal(raw.Provider, &providerType); err != nil {
return c, fmt.Errorf("parse provider string: %w", err)
}
c.Provider.Type = providerType
if len(raw.Attributes) > 0 {
if err := json.Unmarshal(raw.Attributes, &c.Provider); err != nil {
return c, fmt.Errorf("parse attributes: %w", err)
}
}
return c, nil
}
// BuildCredentialsParams builds cloudsync credentials params for the appropriate API version.
// Pre-25.x uses separate "provider" (string) and "attributes" (object).
// 25.x+ uses "provider" as object with "type" merged with attributes.
func BuildCredentialsParams(v Version, name, providerType string, attributes map[string]any) map[string]any {
if v.AtLeast(25, 0) {
// 25.x format: provider is object with type + attributes
providerObj := map[string]any{"type": providerType}
for k, val := range attributes {
providerObj[k] = val
}
return map[string]any{
"name": name,
"provider": providerObj,
}
}
// 24.x format: provider is string, attributes is separate object
attrsCopy := make(map[string]any)
for k, val := range attributes {
attrsCopy[k] = val
}
return map[string]any{
"name": name,
"provider": providerType,
"attributes": attrsCopy,
}
}
// ParseCredentials parses credentials from API response using version-specific logic.
func ParseCredentials(data []byte, v Version) ([]CloudSyncCredentialResponse, error) {
var raws []credentialRaw
if err := json.Unmarshal(data, &raws); err != nil {
return nil, fmt.Errorf("parse response: %w", err)
}
parse := parseCredentialV25
if !v.AtLeast(25, 0) {
parse = parseCredentialV24
}
results := make([]CloudSyncCredentialResponse, 0, len(raws))
for _, raw := range raws {
cred, err := parse(raw)
if err != nil {
return nil, err
}
results = append(results, cred)
}
return results, nil
}