cloudpolicy.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 cloudid
  15. import (
  16. "io/ioutil"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. type CloudpolicyListOptions struct {
  22. options.BaseListOptions
  23. ManagerId string `json:"manager_id"`
  24. ClouduserId string `json:"clouduser_id"`
  25. CloudgroupId string `json:"cloudgroup_id"`
  26. PolicyType string `help:"Filter cloudpolicy by policy type" choices:"system|custom"`
  27. }
  28. func (opts *CloudpolicyListOptions) Params() (jsonutils.JSONObject, error) {
  29. return options.ListStructToParams(opts)
  30. }
  31. type CloudpolicyIdOptions struct {
  32. ID string `help:"Cloudpolicy Id"`
  33. }
  34. func (opts *CloudpolicyIdOptions) GetId() string {
  35. return opts.ID
  36. }
  37. func (opts *CloudpolicyIdOptions) Params() (jsonutils.JSONObject, error) {
  38. return nil, nil
  39. }
  40. type CloudpolicyGroupOptions struct {
  41. CloudpolicyIdOptions
  42. CLOUDGROUP_ID string `help:"Cloudgroup Id" json:"cloudgroup_id"`
  43. }
  44. func (opts *CloudpolicyGroupOptions) Params() (jsonutils.JSONObject, error) {
  45. return jsonutils.Marshal(map[string]string{"cloudgroup_id": opts.CLOUDGROUP_ID}), nil
  46. }
  47. type CloudpolicyUpdateOption struct {
  48. CloudpolicyIdOptions
  49. Name string
  50. Description string
  51. PolicyDocument string
  52. }
  53. func (opts *CloudpolicyUpdateOption) Params() (jsonutils.JSONObject, error) {
  54. params := jsonutils.Marshal(opts).(*jsonutils.JSONDict)
  55. params.Remove("policy_document")
  56. if len(opts.PolicyDocument) > 0 {
  57. document, err := jsonutils.Parse([]byte(opts.PolicyDocument))
  58. if err != nil {
  59. return nil, errors.Wrapf(err, "invalid policy document")
  60. }
  61. params.Add(document, "document")
  62. }
  63. return params, nil
  64. }
  65. type CloudpolicyCreateOption struct {
  66. NAME string
  67. PROVIDER string `choices:"Aliyun|Google|Aws|Azure|Huawei"`
  68. Descritpion string
  69. CloudEnv string
  70. POLICY_FILE string
  71. }
  72. func (opts *CloudpolicyCreateOption) Params() (jsonutils.JSONObject, error) {
  73. params := jsonutils.Marshal(opts).(*jsonutils.JSONDict)
  74. data, err := ioutil.ReadFile(opts.POLICY_FILE)
  75. if err != nil {
  76. return nil, errors.Wrapf(err, "ReadFile")
  77. }
  78. params.Remove("policy_file")
  79. document, err := jsonutils.Parse(data)
  80. if err != nil {
  81. return nil, errors.Wrapf(err, "invalid policy document")
  82. }
  83. params.Add(document, "document")
  84. return params, nil
  85. }
  86. type CloudpolicyCacheOption struct {
  87. CloudpolicyIdOptions
  88. ManagerId string `positional:"true"`
  89. }
  90. func (opts *CloudpolicyCacheOption) Params() (jsonutils.JSONObject, error) {
  91. return jsonutils.Marshal(map[string]string{"manager_id": opts.ManagerId}), nil
  92. }