-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathimage.rs
213 lines (194 loc) · 6.35 KB
/
image.rs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Image, ProjectImage, and SiloImage are almost identical. Image is the model
//! for the underlying table and ProjectImage and SiloImage are backed by views
//! of that table. They have all the same fields except ProjectImage has both a
//! silo_id and a project_id, while SiloImage only has a silo_id. Image has a
//! silo_id and an optional project_id to cover both possibilities.
use super::{BlockSize, ByteCount, Digest};
use crate::schema::{image, project_image, silo_image};
use db_macros::Resource;
use nexus_types::external_api::views;
use nexus_types::identity::Resource;
use omicron_common::api::external::Error;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
// Shared image definition
#[derive(
Queryable,
Insertable,
Selectable,
Clone,
Debug,
Resource,
Serialize,
Deserialize,
)]
#[diesel(table_name = image)]
pub struct Image {
#[diesel(embed)]
pub identity: ImageIdentity,
pub silo_id: Uuid,
pub project_id: Option<Uuid>,
pub volume_id: Uuid,
pub url: Option<String>,
pub os: String,
pub version: String,
pub digest: Option<Digest>,
pub block_size: BlockSize,
#[diesel(column_name = size_bytes)]
pub size: ByteCount,
}
#[derive(
Queryable, Selectable, Clone, Debug, Resource, Serialize, Deserialize,
)]
#[diesel(table_name = project_image)]
pub struct ProjectImage {
#[diesel(embed)]
pub identity: ProjectImageIdentity,
pub silo_id: Uuid,
pub project_id: Uuid,
pub volume_id: Uuid,
pub url: Option<String>,
pub os: String,
pub version: String,
pub digest: Option<Digest>,
pub block_size: BlockSize,
#[diesel(column_name = size_bytes)]
pub size: ByteCount,
}
#[derive(
Queryable, Selectable, Clone, Debug, Resource, Serialize, Deserialize,
)]
#[diesel(table_name = silo_image)]
pub struct SiloImage {
#[diesel(embed)]
pub identity: SiloImageIdentity,
pub silo_id: Uuid,
pub volume_id: Uuid,
pub url: Option<String>,
pub os: String,
pub version: String,
pub digest: Option<Digest>,
pub block_size: BlockSize,
#[diesel(column_name = size_bytes)]
pub size: ByteCount,
}
impl TryFrom<Image> for ProjectImage {
type Error = Error;
fn try_from(image: Image) -> Result<Self, Self::Error> {
match image.project_id {
Some(project_id) => Ok(Self {
identity: ProjectImageIdentity {
id: image.id(),
name: image.name().clone().into(),
description: image.description().to_string(),
time_created: image.time_created(),
time_modified: image.time_modified(),
time_deleted: image.time_deleted(),
},
silo_id: image.silo_id,
project_id,
volume_id: image.volume_id,
url: image.url,
os: image.os,
version: image.version,
digest: image.digest,
block_size: image.block_size,
size: image.size,
}),
None => Err(Error::internal_error(
"tried to convert non-project image to project image",
)),
}
}
}
impl TryFrom<Image> for SiloImage {
type Error = Error;
fn try_from(image: Image) -> Result<Self, Self::Error> {
match image.project_id {
Some(_) => Err(Error::internal_error(
"tried to convert non-silo image to silo image",
)),
None => Ok(Self {
identity: SiloImageIdentity {
id: image.id(),
name: image.name().clone().into(),
description: image.description().to_string(),
time_created: image.time_created(),
time_modified: image.time_modified(),
time_deleted: image.time_deleted(),
},
silo_id: image.silo_id,
volume_id: image.volume_id,
url: image.url,
os: image.os,
version: image.version,
digest: image.digest,
block_size: image.block_size,
size: image.size,
}),
}
}
}
impl From<ProjectImage> for Image {
fn from(image: ProjectImage) -> Self {
Self {
identity: ImageIdentity {
id: image.id(),
name: image.name().clone().into(),
description: image.description().to_string(),
time_created: image.time_created(),
time_modified: image.time_modified(),
time_deleted: image.time_deleted(),
},
silo_id: image.silo_id,
project_id: Some(image.project_id),
volume_id: image.volume_id,
url: image.url,
os: image.os,
version: image.version,
digest: image.digest,
block_size: image.block_size,
size: image.size,
}
}
}
impl From<SiloImage> for Image {
fn from(image: SiloImage) -> Self {
Self {
identity: ImageIdentity {
id: image.id(),
name: image.name().clone().into(),
description: image.description().to_string(),
time_created: image.time_created(),
time_modified: image.time_modified(),
time_deleted: image.time_deleted(),
},
silo_id: image.silo_id,
project_id: None,
volume_id: image.volume_id,
url: image.url,
os: image.os,
version: image.version,
digest: image.digest,
block_size: image.block_size,
size: image.size,
}
}
}
impl From<Image> for views::Image {
fn from(image: Image) -> Self {
Self {
identity: image.identity(),
project_id: image.project_id,
url: image.url,
os: image.os,
version: image.version,
digest: image.digest.map(|x| x.into()),
block_size: image.block_size.into(),
size: image.size.into(),
}
}
}