disk.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package compute
  15. import (
  16. "reflect"
  17. "time"
  18. "yunion.io/x/cloudmux/pkg/multicloud/esxi/vcenter"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/gotypes"
  21. "yunion.io/x/pkg/util/fileutils"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. "yunion.io/x/onecloud/pkg/apis/billing"
  24. billing_api "yunion.io/x/onecloud/pkg/apis/billing"
  25. "yunion.io/x/onecloud/pkg/httperrors"
  26. )
  27. func init() {
  28. gotypes.RegisterSerializable(reflect.TypeOf(new(DiskFsFeatures)), func() gotypes.ISerializable {
  29. return new(DiskFsFeatures)
  30. })
  31. }
  32. type DiskCreateInput struct {
  33. apis.VirtualResourceCreateInput
  34. apis.EncryptedResourceCreateInput
  35. *DiskConfig
  36. // 调度使用指定的云账号
  37. PreferManager string `json:"prefer_manager_id"`
  38. // 此参数仅适用于未指定storage时进行调度到指定区域创建磁盘
  39. // required: false
  40. PreferRegion string `json:"prefer_region_id"`
  41. // 此参数仅适用于未指定storage时进行调度到指定可用区区创建磁盘
  42. // required: false
  43. PreferZone string `json:"prefer_zone_id"`
  44. // swagger:ignore
  45. PreferWire string `json:"prefer_wire_id"`
  46. // 此参数仅适用于未指定storage时进行调度到指定可用区区创建磁盘
  47. // required: false
  48. PreferHost string `json:"prefer_host_id"`
  49. // 此参数仅适用于未指定storage时进行调度到指定平台创建磁盘
  50. // default: kvm
  51. // enum: ["kvm", "openstack", "esxi", "aliyun", "aws", "qcloud", "azure", "huawei", "ucloud", "zstack", "google", "ctyun"]
  52. Hypervisor string `json:"hypervisor"`
  53. }
  54. // ToServerCreateInput used by disk schedule
  55. func (req *DiskCreateInput) ToServerCreateInput() *ServerCreateInput {
  56. input := ServerCreateInput{
  57. ServerConfigs: &ServerConfigs{
  58. PreferManager: req.PreferManager,
  59. PreferRegion: req.PreferRegion,
  60. PreferZone: req.PreferZone,
  61. PreferWire: req.PreferWire,
  62. PreferHost: req.PreferHost,
  63. Hypervisor: req.Hypervisor,
  64. Disks: []*DiskConfig{req.DiskConfig},
  65. // Project: req.Project,
  66. // Domain: req.Domain,
  67. },
  68. }
  69. input.Name = req.Name
  70. input.ProjectId = req.ProjectId
  71. input.ProjectDomainId = req.ProjectDomainId
  72. return &input
  73. }
  74. func (req *ServerCreateInput) ToDiskCreateInput() *DiskCreateInput {
  75. input := DiskCreateInput{
  76. DiskConfig: req.Disks[0],
  77. PreferRegion: req.PreferRegion,
  78. PreferHost: req.PreferHost,
  79. PreferZone: req.PreferZone,
  80. PreferWire: req.PreferWire,
  81. Hypervisor: req.Hypervisor,
  82. }
  83. input.Name = req.Name
  84. input.ProjectId = req.ProjectId
  85. input.ProjectDomainId = req.ProjectDomainId
  86. return &input
  87. }
  88. type SnapshotPolicyResourceInput struct {
  89. // filter disk by snapshotpolicy
  90. SnapshotpolicyId string `json:"snapshotpolicy_id"`
  91. // swagger:ignore
  92. // Deprecated
  93. // filter disk by snapshotpolicy_id
  94. Snapshotpolicy string `json:"snapshotpolicy" yunion-deprecated-by:"snapshotpolicy_id"`
  95. }
  96. type SnapshotPolicyFilterListInput struct {
  97. SnapshotPolicyResourceInput
  98. // 以快照策略名称排序
  99. OrderBySnapshotpolicy string `json:"order_by_snapshotpolicy"`
  100. }
  101. type DiskListInput struct {
  102. apis.VirtualResourceListInput
  103. apis.ExternalizedResourceBaseListInput
  104. apis.MultiArchResourceBaseListInput
  105. apis.AutoDeleteResourceBaseListInput
  106. billing.BillingResourceListInput
  107. StorageFilterListInput
  108. SnapshotPolicyFilterListInput
  109. // 虚拟机(ID或Name)列表
  110. ServerId []string `json:"server_id"`
  111. // swagger:ignore
  112. Server []string `json:"server" yunion-deprecated-by:"server_id"`
  113. // filter disk by whether it is being used
  114. Unused *bool `json:"unused"`
  115. // swagger:ignore
  116. // Deprecated
  117. // filter by disk type
  118. Type string `json:"type" yunion-deprecated-by:"disk_type"`
  119. // 过滤指定disk_type的磁盘列表,可能的值为:sys, data, swap. volume
  120. //
  121. // | disk_type值 | 说明 |
  122. // |-------------|----------|
  123. // | sys | 虚拟机系统盘 |
  124. // | data | 虚拟机数据盘 |
  125. // | swap | 虚拟机内存交换盘 |
  126. // | volume | 容器volumn盘 |
  127. //
  128. DiskType string `json:"disk_type"`
  129. DiskFormat string `json:"disk_format"`
  130. DiskSize int `json:"disk_size"`
  131. FsFormat string `json:"fs_format"`
  132. OrderByServer string `json:"order_by_server" choices:"asc|desc"`
  133. OrderByGuestCount string `json:"order_by_guest_count" choices:"asc|desc"`
  134. // 镜像
  135. ImageId string `json:"image_id"`
  136. // swagger:ignore
  137. // Deprecated
  138. Template string `json:"template" yunion-deprecated-by:"image_id"`
  139. // swagger:ignore
  140. // Deprecated
  141. TemplateId string `json:"template_id" yunion-deprecated-by:"image_id"`
  142. // 快照
  143. SnapshotId string `json:"snapshot_id"`
  144. // swagger:ignore
  145. // Deprecated
  146. Snapshot string `json:"snapshot" yunion-deprecated-by:"snapshot_id"`
  147. // 根据虚拟机状态过滤
  148. GuestStatus string `json:"guest_status"`
  149. // 根据是否绑定快照策略过滤
  150. BindingSnapshotpolicy *bool `json:"binding_snapshotpolicy"`
  151. // 根据是否磁盘所在虚拟机是否绑定主机快照策略
  152. BindingServerSnapshotpolicy *bool `json:"binding_server_snapshotpolicy"`
  153. }
  154. type DiskResourceInput struct {
  155. // 虚拟磁盘(ID或Name)
  156. DiskId string `json:"disk_id"`
  157. // swagger:ignore
  158. // Deprecated
  159. // filter by disk_id
  160. Disk string `json:"disk" yunion-deprecated-by:"disk_id"`
  161. }
  162. type DiskFilterListInputBase struct {
  163. DiskResourceInput
  164. // 以磁盘名称排序
  165. // pattern:asc|desc
  166. OrderByDisk string `json:"order_by_disk"`
  167. }
  168. type DiskFilterListInput struct {
  169. StorageFilterListInput
  170. DiskFilterListInputBase
  171. }
  172. type SimpleGuest struct {
  173. // 主机名称
  174. Name string `json:"name"`
  175. // 主机ID
  176. Id string `json:"id"`
  177. // 主机状态
  178. Status string `json:"status"`
  179. // 磁盘序号
  180. Index int `json:"index"`
  181. // 磁盘驱动
  182. Driver string `json:"driver"`
  183. // 缓存模式
  184. CacheMode string `json:"cache_mode"`
  185. // 磁盘并发数
  186. Iops int `json:"iops"`
  187. // 磁盘吞吐
  188. Bps int `json:"bps"`
  189. // 计费类型
  190. BillingType string `json:"billing_type"`
  191. // 磁盘绑定的快照策略列表
  192. Snapshotpolicy []SimpleSnapshotPolicy `json:"snapshotpolicy"`
  193. }
  194. type SimpleSnapshotPolicy struct {
  195. // 快照策略ID
  196. Id string `json:"id"`
  197. // 快照策略名称
  198. Name string `json:"name"`
  199. // 快照策略类型
  200. ResourceType string `json:"resource_type"`
  201. // 快照策略重复周期
  202. RepeatWeekdays []int `json:"repeat_weekdays"`
  203. // 快照策略时间点
  204. TimePoints []int `json:"time_points"`
  205. }
  206. type DiskDetails struct {
  207. apis.VirtualResourceDetails
  208. StorageResourceInfo
  209. apis.EncryptedResourceDetails
  210. SDisk
  211. // 所挂载的虚拟机
  212. Guests []SimpleGuest `json:"guests"`
  213. // 所挂载的虚拟机
  214. Guest string `json:"guest"`
  215. // 所挂载虚拟机的数量
  216. GuestCount int `json:"guest_count"`
  217. // 所挂载虚拟机状态
  218. GuestStatus string `json:"guest_status"`
  219. // 所挂载虚拟机计费类型
  220. GuestBillingType string `json:"guest_billing_type"`
  221. // 磁盘所在虚拟机绑定的主机快照策略数量
  222. GuestSnapshotpolicyCount int `json:"guest_snapshotpolicy_count"`
  223. // 自动清理时间
  224. AutoDeleteAt time.Time `json:"auto_delete_at"`
  225. // 自动快照策略
  226. Snapshotpolicies []SimpleSnapshotPolicy `json:"snapshotpolicies"`
  227. }
  228. type DiskResourceInfoBase struct {
  229. // 磁盘名称
  230. Disk string `json:"disk"`
  231. }
  232. type DiskResourceInfo struct {
  233. DiskResourceInfoBase
  234. // 存储ID
  235. StorageId string `json:"storage_id"`
  236. StorageResourceInfo
  237. }
  238. type DiskSyncstatusInput struct {
  239. }
  240. type DiskUpdateInput struct {
  241. apis.VirtualResourceBaseUpdateInput
  242. // 磁盘类型
  243. DiskType string `json:"disk_type"`
  244. // 关机自动重置
  245. AutoReset *bool `json:"auto_reset"`
  246. // 是否跟随主机删除而自动删除
  247. // 默认跟随主机创建的磁盘为 true
  248. // required: false
  249. AutoDelete *bool `json:"auto_delete,omitempty"`
  250. }
  251. type DiskSaveInput struct {
  252. Name string `json:"name"`
  253. Format string `json:"format"`
  254. // swagger:ignore
  255. ImageId string `json:"image_id"`
  256. }
  257. type DiskResizeInput struct {
  258. // default unit: Mb
  259. // example: 1024; 40G; 1024M
  260. Size string `json:"size"`
  261. }
  262. func (self DiskResizeInput) SizeMb() (int, error) {
  263. if len(self.Size) == 0 {
  264. return 0, httperrors.NewMissingParameterError("size")
  265. }
  266. return fileutils.GetSizeMb(self.Size, 'M', 1024)
  267. }
  268. type DiskAllocateInput struct {
  269. Format string `json:"format"`
  270. DiskSizeMb int `json:"disk_size_mb"`
  271. ImageId string `json:"image_id"`
  272. ImageFormat string `json:"image_format"`
  273. FsFormat string `json:"fs_format"`
  274. FsFeatures *DiskFsFeatures `json:"fs_features"`
  275. Rebuild bool `json:"rebuild"`
  276. BackingDiskId string `json:"backing_disk_id"`
  277. SnapshotId string `json:"snapshot_id"`
  278. BackupId string `json:"backup_id"`
  279. Backup *DiskAllocateFromBackupInput `json:"backup"`
  280. SnapshotUrl string `json:"snapshot_url"`
  281. SnapshotOutOfChain bool `json:"snapshot_out_of_chain"`
  282. Protocol string `json:"protocol"`
  283. SrcDiskId string `json:"src_disk_id"`
  284. SrcPool string `json:"src_pool"`
  285. ExistingPath string `json:"existing_path"`
  286. // vmware
  287. HostIp string `json:"host_ip"`
  288. Datastore vcenter.SVCenterAccessInfo `json:"datastore"`
  289. // encryption
  290. Encryption bool `json:"encryption"`
  291. EncryptInfo apis.SEncryptInfo `json:"encrypt_info"`
  292. }
  293. type DiskAllocateFromBackupInput struct {
  294. BackupId string `json:"backup_id"`
  295. BackupStorageId string `json:"backup_storage_id"`
  296. BackupStorageAccessInfo *jsonutils.JSONDict `json:"backup_storage_access_info"`
  297. DiskConfig *DiskConfig `json:"disk_config"`
  298. BackupAsTar *DiskBackupAsTarInput `json:"backup_as_tar"`
  299. }
  300. type DiskDeleteInput struct {
  301. SkipRecycle *bool `json:"skip_recycle"`
  302. EsxiFlatFilePath string `json:"esxi_flat_file_path"`
  303. CleanSnapshots bool `json:"clean_snapshots"`
  304. }
  305. type DiskResetInput struct {
  306. SnapshotId string `json:"snapshot_id"`
  307. AutoStart bool `json:"auto_start"`
  308. }
  309. type DiskMigrateInput struct {
  310. TargetStorageId string `json:"target_storage_id"`
  311. }
  312. type DiskChagneStorageTypeInput struct {
  313. // 目标存储类型
  314. StorageType string `json:"storage_type"`
  315. }
  316. type DiskSnapshotpolicyInput struct {
  317. // 快照策略ID
  318. SnapshotpolicyId string `json:"snapshotpolicy_id"`
  319. }
  320. type DiskRebuildInput struct {
  321. BackupId *string `json:"backup_id,allowempty"`
  322. TemplateId *string `json:"template_id,allowempty"`
  323. Size *string `json:"size,allowempty"`
  324. Fs *string `json:"fs,allowempty"`
  325. FsFeatures *DiskFsFeatures `json:"fs_features,allowempty"`
  326. }
  327. type DiskFsExt4Features struct {
  328. CaseInsensitive bool `json:"case_insensitive"`
  329. ReservedBlocksPercentage int `json:"reserved_blocks_percentage"`
  330. }
  331. type DiskFsF2fsFeatures struct {
  332. CaseInsensitive bool `json:"case_insensitive"`
  333. OverprovisionRatioPercentage int `json:"overprovision_ratio_percentage"`
  334. }
  335. type DiskFsFeatures struct {
  336. Ext4 *DiskFsExt4Features `json:"ext4"`
  337. F2fs *DiskFsF2fsFeatures `json:"f2fs"`
  338. }
  339. func (d *DiskFsFeatures) String() string {
  340. return jsonutils.Marshal(d).String()
  341. }
  342. func (d *DiskFsFeatures) IsZero() bool {
  343. if reflect.DeepEqual(*d, DiskFsFeatures{}) {
  344. return true
  345. }
  346. return false
  347. }
  348. type DiskChangeBillingTypeInput struct {
  349. // 仅在磁盘挂载在虚拟机上时调用
  350. // 目前支持阿里云
  351. // enmu: [postpaid, prepaid]
  352. // required: true
  353. BillingType billing_api.TBillingType `json:"billing_type"`
  354. }