-
Notifications
You must be signed in to change notification settings - Fork 0
/
migration.go
189 lines (175 loc) · 6.6 KB
/
migration.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package connect
import "encoding/json"
type MigrationStatusEnum string
const (
migNotStarted MigrationStatusEnum = "migNotStarted" // Migration planed but not started
migCompressionStarted MigrationStatusEnum = "migCompressionStarted" // Migration started - packing of the mailbox and sending through network
migCompressionFinished MigrationStatusEnum = "migCompressionFinished" // Source server actions finished
migTransferStarted MigrationStatusEnum = "migTransferStarted" // Downloading packed mailbox
migTransferFinished MigrationStatusEnum = "migTransferFinished" // Download completed
migDecompressionStarted MigrationStatusEnum = "migDecompressionStarted" // Target server actions - unpacking of the mailbox
migDecompressionFinished MigrationStatusEnum = "migDecompressionFinished" // Mailbox successfully decompressed
migFinished MigrationStatusEnum = "migFinished" // Migration finished
migCanceled MigrationStatusEnum = "migCanceled" // Migration canceled by user
migError MigrationStatusEnum = "migError" // Migration failed - detailed error description can be found in logs
)
// MigrationStatus - Status of the migration task and progress of migration in percents.
type MigrationStatus struct {
MigrationStatus MigrationStatusEnum `json:"migrationStatus"` // current status of the migration
ProgressInPercents int `json:"progressInPercents"` // number from 0 to 100
ErrorMessage string `json:"errorMessage"` // If migrationStatus is migError, errorMessage optionaly contains detailed info about error in english
}
// This structure contains information about:
// - user which will be/is migrated
// - current homeserver (sourceServer)
// In get methods is returned also status of user's migration.
// MigrationTask - Whole structure is READ-ONLY
type MigrationTask struct {
Id KId `json:"id"` // id of migration task
UserId KId `json:"userId"` // id of migrated user
UserName string `json:"userName"` // name of migrated user
SourceServer HomeServer `json:"sourceServer"` // source homeserver
// HomeServer targetServer; // in current implementation it's always current server -
Status MigrationStatus `json:"status"` // status of the migration
}
// MigrationTaskList - List of migration tasks
type MigrationTaskList []MigrationTask
// MigrationCancel - Cancel planned or running migration tasks.
// Parameters
// taskIdList - Identifiers of migration tasks which should be canceled
// Return
// errors - error message list
func (s *ServerConnection) MigrationCancel(taskIdList KIdList) (ErrorList, error) {
params := struct {
TaskIdList KIdList `json:"taskIdList"`
}{taskIdList}
data, err := s.CallRaw("Migration.cancel", params)
if err != nil {
return nil, err
}
errors := struct {
Result struct {
Errors ErrorList `json:"errors"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &errors)
return errors.Result.Errors, err
}
// MigrationGet - Obtain list of migration tasks.
// Parameters
// query - query attributes and limits
// Return
// list - migration tasks
// totalItems - total number of tasks
func (s *ServerConnection) MigrationGet(query SearchQuery) (MigrationTaskList, int, error) {
query = addMissedParametersToSearchQuery(query)
params := struct {
Query SearchQuery `json:"query"`
}{query}
data, err := s.CallRaw("Migration.get", params)
if err != nil {
return nil, 0, err
}
list := struct {
Result struct {
List MigrationTaskList `json:"list"`
TotalItems int `json:"totalItems"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &list)
return list.Result.List, list.Result.TotalItems, err
}
// MigrationGetCurrentHomeServer - Note: This method should be moved to DistributedDomain
// Return
// homeServer - homeserver attributes
func (s *ServerConnection) MigrationGetCurrentHomeServer() (*HomeServer, error) {
data, err := s.CallRaw("Migration.getCurrentHomeServer", nil)
if err != nil {
return nil, err
}
homeServer := struct {
Result struct {
HomeServer HomeServer `json:"homeServer"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &homeServer)
return &homeServer.Result.HomeServer, err
}
// MigrationGetCurrentStatus - Obtain status of currently running migration task.
// Return
// taskId - migration task identifier
// status - migration task status
func (s *ServerConnection) MigrationGetCurrentStatus() (*KId, *MigrationStatus, error) {
data, err := s.CallRaw("Migration.getCurrentStatus", nil)
if err != nil {
return nil, nil, err
}
taskId := struct {
Result struct {
TaskId KId `json:"taskId"`
Status MigrationStatus `json:"status"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &taskId)
return &taskId.Result.TaskId, &taskId.Result.Status, err
}
// MigrationGetStatus - Obtain status of migration task specified by the task ID.
// Parameters
// taskId - migration task identifier
// Return
// status - migration task status
func (s *ServerConnection) MigrationGetStatus(taskId KId) (*MigrationStatus, error) {
params := struct {
TaskId KId `json:"taskId"`
}{taskId}
data, err := s.CallRaw("Migration.getStatus", params)
if err != nil {
return nil, err
}
status := struct {
Result struct {
Status MigrationStatus `json:"status"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &status)
return &status.Result.Status, err
}
// MigrationIsInProgress - Note: This method may fail if caller does not have full admin rights.
// Return
// isInProgress - is there any migration task running?
func (s *ServerConnection) MigrationIsInProgress() (bool, error) {
data, err := s.CallRaw("Migration.isInProgress", nil)
if err != nil {
return false, err
}
isInProgress := struct {
Result struct {
IsInProgress bool `json:"isInProgress"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &isInProgress)
return isInProgress.Result.IsInProgress, err
}
// MigrationStart - Start a new migration task.
// Parameters
// userIds - users to be migrated
// Return
// errors - error message list
// result
func (s *ServerConnection) MigrationStart(userIds KIdList) (ErrorList, CreateResultList, error) {
params := struct {
UserIds KIdList `json:"userIds"`
}{userIds}
data, err := s.CallRaw("Migration.start", params)
if err != nil {
return nil, nil, err
}
errors := struct {
Result struct {
Errors ErrorList `json:"errors"`
Result CreateResultList `json:"result"`
} `json:"result"`
}{}
err = json.Unmarshal(data, &errors)
return errors.Result.Errors, errors.Result.Result, err
}