routetables.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. "yunion.io/x/jsonutils"
  18. baseoptions "yunion.io/x/onecloud/pkg/mcclient/options"
  19. )
  20. type Route struct {
  21. Type string
  22. Cidr string
  23. NextHopType string
  24. NextHopId string
  25. }
  26. type Routes []*Route
  27. type RoutesOptions struct {
  28. Type []string
  29. Cidr []string
  30. NextHopType []string
  31. NextHopId []string
  32. }
  33. func (opts *RoutesOptions) Params() (jsonutils.JSONObject, error) {
  34. len0 := len(opts.Type)
  35. len1 := len(opts.Cidr)
  36. if len0 != len1 || len0 != len(opts.NextHopType) || len1 != len(opts.NextHopId) {
  37. return nil, fmt.Errorf("there must be equal number of options for each route")
  38. }
  39. routes := []*Route{}
  40. for i := 0; i < len0; i++ {
  41. routes = append(routes, &Route{
  42. Type: opts.Type[i],
  43. Cidr: opts.Cidr[i],
  44. NextHopType: opts.NextHopType[i],
  45. NextHopId: opts.NextHopId[i],
  46. })
  47. }
  48. routesJson := jsonutils.Marshal(routes)
  49. return routesJson, nil
  50. }
  51. type RouteTableCreateOptions struct {
  52. NAME string
  53. Vpc string `required:"true"`
  54. RoutesOptions
  55. }
  56. func (opts *RouteTableCreateOptions) Params() (jsonutils.JSONObject, error) {
  57. params, err := baseoptions.StructToParams(opts)
  58. if err != nil {
  59. return nil, err
  60. }
  61. routesJson, err := opts.RoutesOptions.Params()
  62. if err != nil {
  63. return nil, err
  64. }
  65. params.Set("routes", routesJson)
  66. return params, nil
  67. }
  68. type RouteTableGetOptions struct {
  69. ID string
  70. }
  71. type RouteTableIdOptions struct {
  72. ID string
  73. }
  74. func (opts *RouteTableIdOptions) GetId() string {
  75. return opts.ID
  76. }
  77. func (opts *RouteTableIdOptions) Params() (jsonutils.JSONObject, error) {
  78. return nil, nil
  79. }
  80. type RouteTableUpdateOptions struct {
  81. ID string `json:"-"`
  82. Name string
  83. RoutesOptions
  84. }
  85. func (opts *RouteTableUpdateOptions) GetId() string {
  86. return opts.ID
  87. }
  88. func (opts *RouteTableUpdateOptions) Params() (jsonutils.JSONObject, error) {
  89. params, err := baseoptions.StructToParams(opts)
  90. if err != nil {
  91. return nil, err
  92. }
  93. if len(opts.Cidr) != 0 {
  94. routesJson, err := opts.RoutesOptions.Params()
  95. if err != nil {
  96. return nil, err
  97. }
  98. params.Set("routes", routesJson)
  99. }
  100. return params, nil
  101. }
  102. type RouteTableAddRoutesOptions struct {
  103. ID string `json:"-"`
  104. RoutesOptions
  105. }
  106. func (opts *RouteTableAddRoutesOptions) GetId() string {
  107. return opts.ID
  108. }
  109. func (opts *RouteTableAddRoutesOptions) Params() (jsonutils.JSONObject, error) {
  110. if len(opts.Cidr) == 0 {
  111. return nil, fmt.Errorf("nothing to add")
  112. }
  113. routesJson, err := opts.RoutesOptions.Params()
  114. if err != nil {
  115. return nil, err
  116. }
  117. params := jsonutils.NewDict()
  118. params.Set("routes", routesJson)
  119. return params, nil
  120. }
  121. type RouteTableDelRoutesOptions struct {
  122. ID string `json:"-"`
  123. Cidr []string
  124. }
  125. func (opts *RouteTableDelRoutesOptions) GetId() string {
  126. return opts.ID
  127. }
  128. func (opts *RouteTableDelRoutesOptions) Params() (jsonutils.JSONObject, error) {
  129. if len(opts.Cidr) == 0 {
  130. return nil, fmt.Errorf("nothing to del")
  131. }
  132. params := jsonutils.NewDict()
  133. params.Set("cidrs", jsonutils.Marshal(opts.Cidr))
  134. return params, nil
  135. }
  136. type RouteTableDeleteOptions struct {
  137. ID string
  138. }
  139. type RouteTablePurgeOptions struct {
  140. ID string
  141. }
  142. type RouteTableListOptions struct {
  143. Vpc string
  144. Cloudregion string
  145. baseoptions.BaseListOptions
  146. }
  147. func (opts *RouteTableListOptions) Params() (jsonutils.JSONObject, error) {
  148. return baseoptions.ListStructToParams(opts)
  149. }
  150. type RouteTableSyncstatusOptions struct {
  151. ID string
  152. }