disks.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. "yunion.io/x/jsonutils"
  17. api "yunion.io/x/onecloud/pkg/apis/compute"
  18. "yunion.io/x/onecloud/pkg/cloudcommon/cmdline"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. type DiskCreateOptions struct {
  22. Manager string `help:"Preferred manager where virtual server should be created" json:"prefer_manager_id"`
  23. Region string `help:"Preferred region where virtual server should be created" json:"prefer_region_id"`
  24. Zone string `help:"Preferred zone where virtual server should be created" json:"prefer_zone_id"`
  25. Wire string `help:"Preferred wire where virtual server should be created" json:"prefer_wire_id"`
  26. Host string `help:"Preferred host where virtual server should be created" json:"prefer_host_id"`
  27. Count int `help:"Count to create" json:"count"`
  28. NAME string `help:"Name of the disk"`
  29. DISKDESC string `help:"Image size or size of virtual disk"`
  30. Desc string `help:"Description" metavar:"Description"`
  31. Storage string `help:"ID or name of storage where the disk is created"`
  32. Hypervisor string `help:"Hypervisor of this disk, used by schedule"`
  33. Backend string `help:"Backend of this disk"`
  34. Schedtag []string `help:"Schedule policy, key = aggregate name, value = require|exclude|prefer|avoid" metavar:"<KEY:VALUE>"`
  35. TaskNotify bool `help:"Setup task notify"`
  36. SnapshotId string `help:"snapshot id"`
  37. BackupId string `help:"Backup id"`
  38. Project string `help:"Owner project"`
  39. }
  40. func (o DiskCreateOptions) Params() (*api.DiskCreateInput, error) {
  41. config, err := cmdline.ParseDiskConfig(o.DISKDESC, 0)
  42. if err != nil {
  43. return nil, err
  44. }
  45. if len(o.Backend) > 0 {
  46. config.Backend = o.Backend
  47. }
  48. for _, desc := range o.Schedtag {
  49. tag, err := cmdline.ParseSchedtagConfig(desc)
  50. if err != nil {
  51. return nil, err
  52. }
  53. config.Schedtags = append(config.Schedtags, tag)
  54. }
  55. params := &api.DiskCreateInput{
  56. PreferManager: o.Manager,
  57. PreferRegion: o.Region,
  58. PreferZone: o.Zone,
  59. PreferWire: o.Wire,
  60. PreferHost: o.Host,
  61. DiskConfig: config,
  62. Hypervisor: o.Hypervisor,
  63. }
  64. params.Description = o.Desc
  65. params.Name = o.NAME
  66. params.ProjectId = o.Project
  67. if o.Storage != "" {
  68. params.Storage = o.Storage
  69. }
  70. params.BackupId = o.BackupId
  71. return params, nil
  72. }
  73. type DiskIdOptions struct {
  74. ID string `help:"ID or Name of disk"`
  75. }
  76. func (o *DiskIdOptions) GetId() string {
  77. return o.ID
  78. }
  79. func (o *DiskIdOptions) Params() (jsonutils.JSONObject, error) {
  80. return nil, nil
  81. }
  82. type DiskMigrateOptions struct {
  83. DiskIdOptions
  84. TargetStorageId string `help:"Disk migrate target storage id or name" json:"target_storage_id"`
  85. }
  86. func (o *DiskMigrateOptions) Params() (jsonutils.JSONObject, error) {
  87. return options.StructToParams(o)
  88. }
  89. type DiskChangeStorageTypeOptions struct {
  90. DiskIdOptions
  91. StorageType string `help:"Disk migrate target storage type" json:"storage_type"`
  92. }
  93. func (o *DiskChangeStorageTypeOptions) Params() (jsonutils.JSONObject, error) {
  94. return options.StructToParams(o)
  95. }
  96. type DiskResetTemplateOptions struct {
  97. DiskIdOptions
  98. TemplateId string `help:"reset disk tempalte id" json:"template_id"`
  99. }
  100. func (o *DiskResetTemplateOptions) Params() (jsonutils.JSONObject, error) {
  101. return options.StructToParams(o)
  102. }
  103. type DiskRebuildOptions struct {
  104. options.ResourceIdOptions
  105. BackupId string `help:"disk backup id" json:"backup_id"`
  106. TemplateId string `help:"disk template id" json:"template_id"`
  107. Size string `help:"disk size in MB" json:"size"`
  108. Fs string `help:"disk fs type"`
  109. FsFeatureF2fsCaseInsensitive *bool `help:"f2fs enable CaseInsensitive" json:"-"`
  110. FsFeatureF2fsOverprovisionRatioPercentage *int `help:"f2fs OverprovisionRatioPercentage" json:"-"`
  111. FsFeatureExt4CaseInsensitive *bool `help:"ext4 enable CaseInsensitive" json:"-"`
  112. FsFeatureExt4ReservedBlocksPercentage *int `help:"ext4 ReservedBlocksPercentage" json:"-"`
  113. }
  114. func (o *DiskRebuildOptions) Params() (jsonutils.JSONObject, error) {
  115. res := api.DiskRebuildInput{}
  116. if o.BackupId != "" {
  117. res.BackupId = &o.BackupId
  118. }
  119. if o.TemplateId != "" {
  120. res.TemplateId = &o.TemplateId
  121. }
  122. if o.Size != "" {
  123. res.TemplateId = &o.Size
  124. }
  125. if o.Fs != "" {
  126. res.Fs = &o.Fs
  127. }
  128. if o.FsFeatureExt4CaseInsensitive != nil || o.FsFeatureExt4ReservedBlocksPercentage != nil {
  129. if res.FsFeatures == nil {
  130. res.FsFeatures = &api.DiskFsFeatures{}
  131. }
  132. res.FsFeatures.Ext4 = &api.DiskFsExt4Features{}
  133. if o.FsFeatureExt4CaseInsensitive != nil {
  134. res.FsFeatures.Ext4.CaseInsensitive = *o.FsFeatureExt4CaseInsensitive
  135. }
  136. if o.FsFeatureExt4ReservedBlocksPercentage != nil {
  137. res.FsFeatures.Ext4.ReservedBlocksPercentage = *o.FsFeatureExt4ReservedBlocksPercentage
  138. }
  139. }
  140. if o.FsFeatureF2fsCaseInsensitive != nil || o.FsFeatureF2fsOverprovisionRatioPercentage != nil {
  141. if res.FsFeatures == nil {
  142. res.FsFeatures = &api.DiskFsFeatures{}
  143. }
  144. res.FsFeatures.F2fs = &api.DiskFsF2fsFeatures{}
  145. if o.FsFeatureF2fsCaseInsensitive != nil {
  146. res.FsFeatures.F2fs.CaseInsensitive = *o.FsFeatureF2fsCaseInsensitive
  147. }
  148. if o.FsFeatureF2fsOverprovisionRatioPercentage != nil {
  149. res.FsFeatures.F2fs.OverprovisionRatioPercentage = *o.FsFeatureF2fsOverprovisionRatioPercentage
  150. }
  151. }
  152. return jsonutils.Marshal(res), nil
  153. }
  154. type DiskListOptions struct {
  155. options.BaseListOptions
  156. Unused *bool `help:"Show unused disks"`
  157. Share *bool `help:"Show Share storage disks"`
  158. Local *bool `help:"Show Local storage disks"`
  159. ServerId []string `help:"Guest ID or name"`
  160. GuestStatus string `help:"Guest Status"`
  161. OrderByServer string `help:"Order By Server"`
  162. Storage string `help:"Storage ID or name"`
  163. Type string `help:"Disk type" choices:"sys|data|swap|volume"`
  164. CloudType string `help:"Public cloud or private cloud" choices:"Public|Private"`
  165. OrderByGuestCount string `help:"Order By Guest Count"`
  166. BillingType string `help:"billing type" choices:"postpaid|prepaid"`
  167. SnapshotpolicyId string `help:"snapshotpolicy id"`
  168. StorageHostId string `help:"filter disk by host"`
  169. BindingServerSnapshotpolicy *bool `help:"filter disk by binding server snapshotpolicy" negative:"no-binding-server-snapshotpolicy"`
  170. BindingSnapshotpolicy *bool `help:"filter disk by binding snapshotpolicy" negative:"no-binding-snapshotpolicy"`
  171. }
  172. func (opts *DiskListOptions) Params() (jsonutils.JSONObject, error) {
  173. params, err := options.ListStructToParams(opts)
  174. if err != nil {
  175. return nil, err
  176. }
  177. if len(opts.CloudType) > 0 {
  178. if opts.CloudType == "Public" {
  179. params.Add(jsonutils.JSONTrue, "public_cloud")
  180. } else if opts.CloudType == "Private" {
  181. params.Add(jsonutils.JSONTrue, "private_cloud")
  182. }
  183. }
  184. return params, nil
  185. }
  186. type DiskChangeBillingTypeOptions struct {
  187. DiskIdOptions
  188. BillingType string `choices:"prepaid|postpaid"`
  189. }
  190. func (o *DiskChangeBillingTypeOptions) Params() (jsonutils.JSONObject, error) {
  191. return jsonutils.Marshal(map[string]string{"billing_type": o.BillingType}), nil
  192. }