server_forward.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. "yunion.io/x/pkg/errors"
  18. "yunion.io/x/onecloud/cmd/climc/shell"
  19. "yunion.io/x/onecloud/pkg/mcclient"
  20. modules "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  21. options "yunion.io/x/onecloud/pkg/mcclient/options/compute"
  22. )
  23. func init() {
  24. cmd := shell.NewResourceCmd(&modules.Servers)
  25. cmd.Perform("open-forward", &options.ServerOpenForwardOptions{})
  26. cmd.Perform("close-forward", &options.ServerCloseForwardOptions{})
  27. cmd.Perform("list-forward", &options.ServerListForwardOptions{})
  28. }
  29. type forwardInfo struct {
  30. ProxyAddr string `json:"proxy_addr"`
  31. ProxyPort int `json:"proxy_port"`
  32. }
  33. func dump(input jsonutils.JSONObject) (*forwardInfo, error) {
  34. ret := new(forwardInfo)
  35. if err := input.Unmarshal(ret); err != nil {
  36. return nil, errors.Wrap(err, "JSONObject unmarshal failed")
  37. }
  38. if ret.ProxyAddr == "" {
  39. return nil, errors.Errorf("proxy_addr is empty")
  40. }
  41. if ret.ProxyPort <= 0 {
  42. return nil, errors.Errorf("invalid proxy_port %d", ret.ProxyPort)
  43. }
  44. return ret, nil
  45. }
  46. func openForward(session *mcclient.ClientSession, srvid string) (*forwardInfo, error) {
  47. opt := &options.ServerOpenForwardOptions{
  48. ServerIdOptions: options.ServerIdOptions{
  49. ID: srvid,
  50. },
  51. Proto: "tcp",
  52. Port: 22,
  53. }
  54. params, err := opt.Params()
  55. if err != nil {
  56. return nil, errors.Wrap(err, "get open forward params")
  57. }
  58. jsonItem, err := modules.Servers.PerformAction(session, opt.ID, "open-forward", params)
  59. if err != nil {
  60. return nil, err
  61. }
  62. return dump(jsonItem)
  63. }
  64. func closeForward(session *mcclient.ClientSession, srvid string, fItem *forwardInfo) (jsonutils.JSONObject, error) {
  65. opt := &options.ServerCloseForwardOptions{
  66. ServerIdOptions: options.ServerIdOptions{
  67. ID: srvid,
  68. },
  69. Proto: "tcp",
  70. ProxyAddr: fItem.ProxyAddr,
  71. ProxyPort: fItem.ProxyPort,
  72. }
  73. params, err := opt.Params()
  74. if err != nil {
  75. return nil, errors.Wrap(err, "close forward params")
  76. }
  77. return modules.Servers.PerformAction(session, opt.ID, "close-forward", params)
  78. }