secgroup.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. "fmt"
  17. "strconv"
  18. "strings"
  19. "yunion.io/x/jsonutils"
  20. "yunion.io/x/pkg/errors"
  21. "yunion.io/x/pkg/util/secrules"
  22. "yunion.io/x/onecloud/pkg/apis"
  23. baseoptions "yunion.io/x/onecloud/pkg/mcclient/options"
  24. )
  25. type SecgroupListOptions struct {
  26. baseoptions.BaseListOptions
  27. Equals string `help:"Secgroup ID or Name, filter secgroups whose rules equals the specified one"`
  28. Server string `help:"Filter secgroups bound to specified server"`
  29. Ip string `help:"Filter secgroup by ip"`
  30. Ports string `help:"Filter secgroup by ports"`
  31. Direction string `help:"Filter secgroup by ports" choices:"all|in|out"`
  32. DBInstance string `help:"Filter secgroups bound to specified rds" json:"dbinstance"`
  33. Cloudregion string `help:"Filter secgroups by region"`
  34. VpcId string
  35. Cloudaccount string `help:"Filter secgroups by account"`
  36. LoadbalancerId string
  37. }
  38. func (opts *SecgroupListOptions) Params() (jsonutils.JSONObject, error) {
  39. return baseoptions.ListStructToParams(opts)
  40. }
  41. type SecgroupCreateOptions struct {
  42. baseoptions.BaseCreateOptions
  43. VpcId string
  44. Tags []string
  45. Rules []string `help:"security rule to create"`
  46. }
  47. func (opts *SecgroupCreateOptions) Params() (jsonutils.JSONObject, error) {
  48. params := jsonutils.Marshal(opts).(*jsonutils.JSONDict)
  49. params.Remove("rules")
  50. rules := []secrules.SecurityRule{}
  51. for i, ruleStr := range opts.Rules {
  52. rule, err := secrules.ParseSecurityRule(ruleStr)
  53. if err != nil {
  54. return nil, errors.Wrapf(err, "ParseSecurityRule(%s)", ruleStr)
  55. }
  56. rule.Priority = i + 1
  57. rules = append(rules, *rule)
  58. }
  59. if len(rules) > 0 {
  60. params.Add(jsonutils.Marshal(rules), "rules")
  61. }
  62. params.Remove("tags")
  63. tags := map[string]string{}
  64. for _, tag := range opts.Tags {
  65. info := strings.Split(tag, "=")
  66. if len(info) != 2 {
  67. return nil, fmt.Errorf("invalid tag %s, tag should like key=value", tag)
  68. }
  69. tags["user:"+info[0]] = info[1]
  70. }
  71. if len(tags) > 0 {
  72. params.Set("__meta__", jsonutils.Marshal(tags))
  73. }
  74. return params, nil
  75. }
  76. type SecgroupIdOptions struct {
  77. ID string `help:"ID or Name of security group destination"`
  78. }
  79. func (opts *SecgroupIdOptions) GetId() string {
  80. return opts.ID
  81. }
  82. func (opts *SecgroupIdOptions) Params() (jsonutils.JSONObject, error) {
  83. return nil, nil
  84. }
  85. type SecgroupMergeOptions struct {
  86. SecgroupIdOptions
  87. SECGROUPS []string `help:"source IDs or Names of secgroup"`
  88. }
  89. func (opts *SecgroupMergeOptions) Params() (jsonutils.JSONObject, error) {
  90. return jsonutils.Marshal(map[string][]string{"secgroup_ids": opts.SECGROUPS}), nil
  91. }
  92. type SecgroupsAddRuleOptions struct {
  93. SecgroupIdOptions
  94. DIRECTION string `help:"Direction of rule" choices:"in|out"`
  95. PROTOCOL string `help:"Protocol of rule" choices:"any|tcp|udp|icmp"`
  96. ACTION string `help:"Actin of rule" choices:"allow|deny"`
  97. PRIORITY int `help:"Priority for rule, range 1 ~ 100"`
  98. Cidr string `help:"IP or CIRD for rule"`
  99. Description string `help:"Desciption for rule"`
  100. Ports string `help:"Port for rule"`
  101. }
  102. func (opts *SecgroupsAddRuleOptions) Params() (jsonutils.JSONObject, error) {
  103. params := jsonutils.Marshal(opts).(*jsonutils.JSONDict)
  104. params.Remove("id")
  105. return params, nil
  106. }
  107. type SecgroupCloneOptions struct {
  108. SecgroupIdOptions
  109. NAME string `help:"Name of new secgroup"`
  110. Desc string `help:"Description of new secgroup"`
  111. }
  112. func (opts *SecgroupCloneOptions) Params() (jsonutils.JSONObject, error) {
  113. return jsonutils.Marshal(map[string]string{"name": opts.NAME, "description": opts.Desc}), nil
  114. }
  115. type SecurityGroupCacheOptions struct {
  116. SecgroupIdOptions
  117. VPC_ID string `help:"ID or Name of vpc"`
  118. }
  119. func (opts *SecurityGroupCacheOptions) Params() (jsonutils.JSONObject, error) {
  120. params := jsonutils.Marshal(opts).(*jsonutils.JSONDict)
  121. params.Remove("id")
  122. return params, nil
  123. }
  124. type SecurityGroupUncacheSecurityGroup struct {
  125. SecgroupIdOptions
  126. CACHE string `help:"ID of secgroup cache"`
  127. }
  128. func (opts *SecurityGroupUncacheSecurityGroup) Params() (jsonutils.JSONObject, error) {
  129. params := jsonutils.Marshal(opts).(*jsonutils.JSONDict)
  130. params.Remove("id")
  131. return params, nil
  132. }
  133. type SecgroupChangeOwnerOptions struct {
  134. SecgroupIdOptions
  135. apis.ProjectizedResourceInput
  136. }
  137. type SecgroupImportRulesOptions struct {
  138. SecgroupIdOptions
  139. RULE []string `help:"rule pattern: rule|priority eg: in:allow any 1"`
  140. }
  141. func (opts *SecgroupImportRulesOptions) Params() (jsonutils.JSONObject, error) {
  142. rules := jsonutils.NewArray()
  143. for _, rule := range opts.RULE {
  144. priority := 1
  145. var r *secrules.SecurityRule = nil
  146. var err error
  147. info := strings.Split(rule, "|")
  148. switch len(info) {
  149. case 1:
  150. case 2:
  151. priority, err = strconv.Atoi(info[1])
  152. if err != nil {
  153. return nil, errors.Wrapf(err, "Parse rule %s priority %s", rule, info[1])
  154. }
  155. default:
  156. return nil, fmt.Errorf("invalid rule %s", rule)
  157. }
  158. r, err = secrules.ParseSecurityRule(info[0])
  159. if err != nil {
  160. return nil, errors.Wrapf(err, "ParseSecurityRule(%s)", rule)
  161. }
  162. r.Priority = priority
  163. rules.Add(jsonutils.Marshal(r))
  164. }
  165. return jsonutils.Marshal(map[string]*jsonutils.JSONArray{"rules": rules}), nil
  166. }
  167. type SecgroupCleanOptions struct {
  168. }
  169. func (opts *SecgroupCleanOptions) Params() (jsonutils.JSONObject, error) {
  170. return nil, nil
  171. }
  172. type ServerNetworkSecgroupListOptions struct {
  173. baseoptions.BaseListOptions
  174. Server string `help:"Server Id or name"`
  175. Secgroup string `help:"Secgroup Id or name"`
  176. NetworkIndex *int `help:"Server network index"`
  177. IsAdmin bool `help:"Is admin secgroup"`
  178. }
  179. func (opts *ServerNetworkSecgroupListOptions) Params() (jsonutils.JSONObject, error) {
  180. return baseoptions.ListStructToParams(opts)
  181. }