buckets.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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/cloudmux/pkg/multicloud/objectstore"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/onecloud/pkg/apis/compute"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. type BucketListOptions struct {
  22. options.BaseListOptions
  23. DistinctField string `help:"query specified distinct field"`
  24. }
  25. func (opts *BucketListOptions) Params() (jsonutils.JSONObject, error) {
  26. params, err := options.ListStructToParams(opts)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return params, nil
  31. }
  32. type BucketGetPropertyOptions struct {
  33. DistinctField string `help:"query specified distinct field"`
  34. }
  35. func (opts *BucketGetPropertyOptions) Params() (jsonutils.JSONObject, error) {
  36. params, err := options.ListStructToParams(opts)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return params, nil
  41. }
  42. func (opts *BucketGetPropertyOptions) Property() string {
  43. return "distinct-field"
  44. }
  45. type BucketIdOptions struct {
  46. options.BaseIdOptions
  47. }
  48. type BucketUpdateOptions struct {
  49. BucketIdOptions
  50. Name string `help:"new name of bucket" json:"name"`
  51. Desc string `help:"Description of bucket" json:"description" token:"desc"`
  52. EnablePerfMon bool `help:"enable performance monitor" json:"-"`
  53. }
  54. func (opts *BucketUpdateOptions) Params() (jsonutils.JSONObject, error) {
  55. params := jsonutils.Marshal(opts)
  56. if opts.EnablePerfMon {
  57. params.(*jsonutils.JSONDict).Add(jsonutils.JSONTrue, "enable_perf_mon")
  58. }
  59. return params, nil
  60. }
  61. type BucketCreateOptions struct {
  62. NAME string `help:"name of bucket" json:"name"`
  63. CLOUDREGION string `help:"location of bucket" json:"cloudregion"`
  64. MANAGER string `help:"cloud provider" json:"manager"`
  65. StorageClass string `help:"bucket storage class"`
  66. Acl string `help:"bucket ACL"`
  67. EnablePerfMon bool `help:"enable performance monitor"`
  68. }
  69. func (opts *BucketCreateOptions) Params() (jsonutils.JSONObject, error) {
  70. params, err := options.StructToParams(opts)
  71. if err != nil {
  72. return nil, err
  73. }
  74. return params, nil
  75. }
  76. type BucketListObjectsOptions struct {
  77. BucketIdOptions
  78. Prefix string `help:"List objects with prefix"`
  79. Recursive bool `help:"List objects recursively"`
  80. Limit int `help:"maximal items per request"`
  81. PagingMarker string `help:"paging marker"`
  82. }
  83. func (opts *BucketListObjectsOptions) Params() (jsonutils.JSONObject, error) {
  84. params, err := options.ListStructToParams(opts)
  85. if err != nil {
  86. return nil, err
  87. }
  88. return params, nil
  89. }
  90. type BucketDeleteObjectsOptions struct {
  91. BucketIdOptions
  92. KEYS []string `help:"List of objects to delete"`
  93. }
  94. func (opts *BucketDeleteObjectsOptions) Params() (jsonutils.JSONObject, error) {
  95. params, err := options.StructToParams(opts)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return params, nil
  100. }
  101. type BucketMakeDirOptions struct {
  102. BucketIdOptions
  103. KEY string `help:"DIR key to create"`
  104. }
  105. func (opts *BucketMakeDirOptions) Params() (jsonutils.JSONObject, error) {
  106. params, err := options.StructToParams(opts)
  107. if err != nil {
  108. return nil, err
  109. }
  110. return params, nil
  111. }
  112. type BucketPresignObjectsOptions struct {
  113. BucketIdOptions
  114. KEY string `help:"Key of object to upload"`
  115. Method string `help:"Request method" choices:"GET|PUT|DELETE"`
  116. ExpireSeconds int `help:"expire in seconds" default:"60"`
  117. }
  118. func (opts *BucketPresignObjectsOptions) Params() (jsonutils.JSONObject, error) {
  119. params, err := options.StructToParams(opts)
  120. if err != nil {
  121. return nil, err
  122. }
  123. return params, nil
  124. }
  125. type BucketSetAclOptions struct {
  126. BucketIdOptions
  127. ACL string `help:"ACL to set" choices:"default|private|public-read|public-read-write" json:"acl"`
  128. Key []string `help:"Optional object key" json:"key"`
  129. }
  130. func (opts *BucketSetAclOptions) Params() (jsonutils.JSONObject, error) {
  131. params, err := options.StructToParams(opts)
  132. if err != nil {
  133. return nil, err
  134. }
  135. return params, nil
  136. }
  137. type BucketAclOptions struct {
  138. BucketIdOptions
  139. Key string `help:"Optional object key"`
  140. }
  141. func (opts *BucketAclOptions) Params() (jsonutils.JSONObject, error) {
  142. params, err := options.StructToParams(opts)
  143. if err != nil {
  144. return nil, err
  145. }
  146. return params, nil
  147. }
  148. type BucketSyncOptions struct {
  149. BucketIdOptions
  150. StatsOnly bool `help:"sync statistics only"`
  151. }
  152. func (opts *BucketSyncOptions) Params() (jsonutils.JSONObject, error) {
  153. params, err := options.StructToParams(opts)
  154. if err != nil {
  155. return nil, err
  156. }
  157. return params, nil
  158. }
  159. type BucketLimitOptions struct {
  160. BucketIdOptions
  161. SizeBytes int64 `help:"size limit in bytes"`
  162. ObjectCount int64 `help:"object count limit"`
  163. }
  164. func (opts *BucketLimitOptions) Params() (jsonutils.JSONObject, error) {
  165. limit := jsonutils.Marshal(opts)
  166. params := jsonutils.NewDict()
  167. params.Set("limit", limit)
  168. return params, nil
  169. }
  170. type BucketAccessInfoOptions struct {
  171. BucketIdOptions
  172. }
  173. func (opts *BucketAccessInfoOptions) Params() (jsonutils.JSONObject, error) {
  174. params, err := options.StructToParams(opts)
  175. if err != nil {
  176. return nil, err
  177. }
  178. return params, nil
  179. }
  180. type BucketSetMetadataOptions struct {
  181. BucketIdOptions
  182. Key []string `help:"Optional object key" json:"key"`
  183. objectstore.ObjectHeaderOptions
  184. }
  185. func (opts *BucketSetMetadataOptions) Params() (jsonutils.JSONObject, error) {
  186. input := compute.BucketMetadataInput{}
  187. input.Key = opts.Key
  188. input.Metadata = opts.ObjectHeaderOptions.Options2Header()
  189. err := input.Validate()
  190. if err != nil {
  191. return nil, err
  192. }
  193. return jsonutils.Marshal(input), nil
  194. }
  195. type BucketSetWebsiteOption struct {
  196. BucketIdOptions
  197. // 主页
  198. Index string `help:"main page"`
  199. // 错误时返回的文档
  200. ErrorDocument string `help:"error return"`
  201. // http或https
  202. Protocol string `help:"force https" choices:"http|https"`
  203. }
  204. func (opts *BucketSetWebsiteOption) Params() (jsonutils.JSONObject, error) {
  205. conf := compute.BucketWebsiteConf{
  206. Index: opts.Index,
  207. ErrorDocument: opts.ErrorDocument,
  208. Protocol: opts.Protocol,
  209. }
  210. return jsonutils.Marshal(conf), nil
  211. }
  212. type BucketGetWebsiteConfOption struct {
  213. BucketIdOptions
  214. }
  215. func (opts *BucketGetWebsiteConfOption) Params() (jsonutils.JSONObject, error) {
  216. params, err := options.StructToParams(opts)
  217. if err != nil {
  218. return nil, err
  219. }
  220. return params, nil
  221. }
  222. type BucketDeleteWebsiteConfOption struct {
  223. BucketIdOptions
  224. }
  225. func (opts *BucketDeleteWebsiteConfOption) Params() (jsonutils.JSONObject, error) {
  226. params, err := options.StructToParams(opts)
  227. if err != nil {
  228. return nil, err
  229. }
  230. return params, nil
  231. }
  232. type BucketSetCorsOption struct {
  233. BucketIdOptions
  234. AllowedMethods []string `help:"allowed http method" choices:"PUT|GET|POST|DELETE|HEAD"`
  235. // 允许的源站,可以设为*
  236. AllowedOrigins []string
  237. AllowedHeaders []string
  238. MaxAgeSeconds int
  239. ExposeHeaders []string
  240. RuleId string
  241. }
  242. func (args *BucketSetCorsOption) Params() (jsonutils.JSONObject, error) {
  243. rule := compute.BucketCORSRule{
  244. AllowedOrigins: args.AllowedOrigins,
  245. AllowedMethods: args.AllowedMethods,
  246. AllowedHeaders: args.AllowedHeaders,
  247. MaxAgeSeconds: args.MaxAgeSeconds,
  248. ExposeHeaders: args.ExposeHeaders,
  249. Id: args.RuleId,
  250. }
  251. rules := compute.BucketCORSRules{Data: []compute.BucketCORSRule{rule}}
  252. return jsonutils.Marshal(rules), nil
  253. }
  254. type BucketGetCorsOption struct {
  255. BucketIdOptions
  256. }
  257. func (opts *BucketGetCorsOption) Params() (jsonutils.JSONObject, error) {
  258. params, err := options.StructToParams(opts)
  259. if err != nil {
  260. return nil, err
  261. }
  262. return params, nil
  263. }
  264. type BucketDeleteCorsOption struct {
  265. BucketIdOptions
  266. RuleId []string `help:"Id of rules to delete"`
  267. }
  268. func (opts *BucketDeleteCorsOption) Params() (jsonutils.JSONObject, error) {
  269. input := compute.BucketCORSRuleDeleteInput{}
  270. input.Id = opts.RuleId
  271. return jsonutils.Marshal(input), nil
  272. }
  273. type BucketSetRefererOption struct {
  274. BucketIdOptions
  275. // 域名列表
  276. DomainList []string
  277. // 是否允许空referer 访问
  278. AllowEmptyRefer bool `help:"all empty refer access"`
  279. Enabled bool
  280. RerererType string `help:"Referer type" choices:"Black-List|White-List"`
  281. }
  282. func (args *BucketSetRefererOption) Params() (jsonutils.JSONObject, error) {
  283. conf := compute.BucketRefererConf{
  284. Enabled: args.Enabled,
  285. AllowEmptyRefer: args.AllowEmptyRefer,
  286. RefererType: args.RerererType,
  287. DomainList: args.DomainList,
  288. }
  289. return jsonutils.Marshal(conf), nil
  290. }
  291. type BucketGetRefererOption struct {
  292. BucketIdOptions
  293. }
  294. func (opts *BucketGetRefererOption) Params() (jsonutils.JSONObject, error) {
  295. params, err := options.StructToParams(opts)
  296. if err != nil {
  297. return nil, err
  298. }
  299. return params, nil
  300. }
  301. type BucketGetCdnDomainOption struct {
  302. BucketIdOptions
  303. }
  304. func (opts *BucketGetCdnDomainOption) Params() (jsonutils.JSONObject, error) {
  305. params, err := options.StructToParams(opts)
  306. if err != nil {
  307. return nil, err
  308. }
  309. return params, nil
  310. }
  311. type BucketGetPolicyOption struct {
  312. BucketIdOptions
  313. }
  314. func (opts *BucketGetPolicyOption) Params() (jsonutils.JSONObject, error) {
  315. params, err := options.StructToParams(opts)
  316. if err != nil {
  317. return nil, err
  318. }
  319. return params, nil
  320. }
  321. type BucketSetPolicyOption struct {
  322. BucketIdOptions
  323. // 格式主账号id:子账号id
  324. PrincipalId []string `help:"ext account id, accountId:subaccountId"`
  325. // Read|ReadWrite|FullControl
  326. CannedAction string `help:"authority action" choice:"Read|FullControl"`
  327. // Allow|Deny
  328. Effect string `help:"allow or deny" choice:"Allow|Deny"`
  329. // 被授权的资源地址
  330. ResourcePath []string
  331. // ip 条件
  332. IpEquals []string
  333. IpNotEquals []string
  334. }
  335. func (args *BucketSetPolicyOption) Params() (jsonutils.JSONObject, error) {
  336. opts := compute.BucketPolicyStatementInput{}
  337. opts.CannedAction = args.CannedAction
  338. opts.Effect = args.Effect
  339. opts.IpEquals = args.IpEquals
  340. opts.IpNotEquals = args.IpNotEquals
  341. opts.ResourcePath = args.ResourcePath
  342. opts.PrincipalId = args.PrincipalId
  343. return jsonutils.Marshal(opts), nil
  344. }
  345. type BucketDeletePolicyOption struct {
  346. BucketIdOptions
  347. PolicyId []string `help:"policy id to delete"`
  348. }
  349. func (args *BucketDeletePolicyOption) Params() (jsonutils.JSONObject, error) {
  350. input := compute.BucketPolicyDeleteInput{}
  351. input.Id = args.PolicyId
  352. return jsonutils.Marshal(input), nil
  353. }
  354. type BucketUploadObjectsOptions struct {
  355. BucketIdOptions
  356. KEY string `help:"Key of object to upload"`
  357. Path string `help:"Path to file to upload" required:"true"`
  358. ContentLength int64 `help:"Content lenght (bytes)" default:"-1"`
  359. StorageClass string `help:"storage CLass"`
  360. Acl string `help:"object acl." choices:"private|public-read|public-read-write"`
  361. objectstore.ObjectHeaderOptions
  362. }
  363. type BucketPerfMonOptions struct {
  364. BucketIdOptions
  365. Payload string `help:"test payload in bytes, e.g. 1, 32, 1024M" default:"4M"`
  366. }