File size: 4,979 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
190
191
192
193
194
195
196
197
198
199
/**
 * Rewind state schemas in this file are wrapped in an odd way with the
 * `stateSchema()` helper so that the validation errors are more useful
 *
 * For example, the expected way we might write this is to use `oneOf`
 * and list the various available state schemas. However, a failure
 * deep down in the list of schemas is cascading up through is-my-json-valid
 * and showing as "no (or more than one) schemas match" and there's
 * no further information about the failure.
 *
 * By providing the `allOf: [ { not, schema }, … ]` pattern instead
 * we can get validation failure message that show which state it was
 * trying to validate when it failed.
 */

export const credential = {
	type: 'object',
	properties: {
		still_valid: { type: 'boolean' },
		type: { type: 'string', enum: [ 'auto', 'ftp', 'managed', 'ssh' ] },
		host: { type: 'string' },
		path: { type: 'string' },
		port: { type: 'integer' },
		role: { type: 'string' },
	},
	required: [ 'role', 'still_valid', 'type' ],
};

export const download = {
	type: 'object',
	properties: {
		downloadId: { type: 'integer' },
		rewindId: { type: 'string' },
		backupPoint: { type: 'integer' },
		startedAt: { type: 'integer' },
		downloadCount: { type: 'integer' },
		validUntil: { type: 'integer' },
		bytesFormatted: { type: 'string' },
	},
	required: [ 'downloadId', 'rewindId', 'backupPoint', 'startedAt' ],
};

export const rewind = {
	type: 'object',
	properties: {
		links: { type: 'object' },
		restore_id: { type: 'integer' },
		rewind_id: { type: 'string' },
		status: { type: 'string', enum: [ 'failed', 'finished', 'queued', 'running' ] },
		started_at: { type: 'string' },
		progress: { type: 'integer' },
		reason: { type: 'string' },
		bytesFormatted: { type: 'string' },
		/**
		 * Commenting these out temporarily because API is returning a null value for current_entry,
		 * triggering a schema validation error. Once this is corrected on the backend (soon), we
		 * will activate these properties again.
		 */
		// message: { type: 'string' },
		// current_entry: { type: 'string' },
	},
	required: [ 'restore_id', 'rewind_id', 'status' ],
};

export const threat = {
	type: 'object',
	properties: {
		id: { type: 'integer' },
		signature: { type: 'string' },
		description: { type: 'string' },
		first_detected: { type: 'string' },
		fixable: { oneOf: [ { type: 'boolean' }, { type: 'object' } ] },
		status: { type: 'string', enum: [ 'current', 'fixed', 'in_progress' ] },
		filename: { type: 'string' },
		context: { type: 'object' },
		extension: { type: 'object' },
	},
};

export const unavailable = stateSchema( 'unavailable', {
	type: 'object',
	properties: {
		state: {
			type: 'string',
			pattern: '^unavailable$',
		},
		reason: {
			type: 'string',
		},
		last_updated: { oneOf: [ { type: 'integer' }, { type: 'string', format: 'date-time' } ] },
		has_cloud: {
			type: 'boolean',
		},
	},
	required: [ 'state', 'last_updated' ],
} );

export const inactive = stateSchema( 'inactive', {
	type: 'object',
	properties: {
		state: {
			type: 'string',
			pattern: '^inactive$',
		},
		credentials: {
			type: 'array',
			items: credential,
		},
		last_updated: { oneOf: [ { type: 'integer' }, { type: 'string', format: 'date-time' } ] },
		has_cloud: {
			type: 'boolean',
		},
	},
	required: [ 'state', 'last_updated' ],
} );

export const awaitingCredentials = stateSchema( 'awaiting_credentials', {
	type: 'object',
	properties: {
		state: {
			type: 'string',
			pattern: '^awaiting_credentials$',
		},
		last_updated: { oneOf: [ { type: 'integer' }, { type: 'string', format: 'date-time' } ] },
		has_cloud: {
			type: 'boolean',
		},
	},
	required: [ 'state', 'last_updated' ],
} );

export const provisioning = stateSchema( 'provisioning', {
	type: 'object',
	properties: {
		state: {
			type: 'string',
			pattern: '^provisioning$',
		},
		credentials: {
			type: 'array',
			items: credential,
		},
		last_updated: { oneOf: [ { type: 'integer' }, { type: 'string', format: 'date-time' } ] },
		has_cloud: {
			type: 'boolean',
		},
	},
	required: [ 'state', 'last_updated' ],
} );

export const active = stateSchema( 'active', {
	type: 'object',
	properties: {
		state: {
			type: 'string',
			pattern: '^active$',
		},
		credentials: {
			type: 'array',
			items: credential,
		},
		downloads: {
			type: 'array',
			items: download,
		},
		rewind,
		alerts: {
			type: 'object',
			items: {
				threats: { type: threat },
			},
		},
		last_updated: { oneOf: [ { type: 'integer' }, { type: 'string', format: 'date-time' } ] },
		has_cloud: {
			type: 'boolean',
		},
	},
	required: [ 'state', 'last_updated' ],
} );

export const rewindStatus = {
	allOf: [ unavailable, inactive, awaitingCredentials, provisioning, active ],
};

function stateSchema( stateName, schema ) {
	return {
		oneOf: [
			{
				not: {
					type: 'object',
					properties: { state: { type: 'string', pattern: `^${ stateName }$` } },
					required: [ 'state' ],
				},
			},
			schema,
		],
	};
}