loadbalancercertificates.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. "io/ioutil"
  18. "yunion.io/x/jsonutils"
  19. "yunion.io/x/onecloud/pkg/mcclient/options"
  20. )
  21. func loadbalancerCertificateLoadFiles(cert, pkey string, allowEmpty bool) (*jsonutils.JSONDict, error) {
  22. params := jsonutils.NewDict()
  23. pathM := map[string]string{
  24. "certificate": cert,
  25. "private_key": pkey,
  26. }
  27. for fieldName, path := range pathM {
  28. if path == "" {
  29. if allowEmpty {
  30. continue
  31. } else {
  32. return nil, fmt.Errorf("%s: empty path", fieldName)
  33. }
  34. }
  35. d, err := ioutil.ReadFile(path)
  36. if err != nil {
  37. return nil, fmt.Errorf("%s: read %s: %s", fieldName, path, err)
  38. }
  39. if len(d) == 0 {
  40. return nil, fmt.Errorf("%s: empty file %s", fieldName, path)
  41. }
  42. params.Set(fieldName, jsonutils.NewString(string(d)))
  43. }
  44. return params, nil
  45. }
  46. type LoadbalancerCertificateCreateOptions struct {
  47. options.SharableProjectizedResourceBaseCreateInput
  48. NAME string
  49. Manager string `json:"manager_id"`
  50. Region string `json:"cloudregion"`
  51. Cert string `required:"true" json:"-" help:"path to certificate file"`
  52. Pkey string `required:"true" json:"-" help:"path to private key file"`
  53. }
  54. func (opts *LoadbalancerCertificateCreateOptions) Params() (jsonutils.JSONObject, error) {
  55. params, err := options.StructToParams(opts)
  56. if err != nil {
  57. return nil, err
  58. }
  59. sp, err := opts.SharableProjectizedResourceBaseCreateInput.Params()
  60. if err != nil {
  61. return nil, err
  62. }
  63. params.Update(sp)
  64. paramsCertKey, err := loadbalancerCertificateLoadFiles(opts.Cert, opts.Pkey, false)
  65. if err != nil {
  66. return nil, err
  67. }
  68. params.Update(paramsCertKey)
  69. return params, nil
  70. }
  71. type LoadbalancerCertificateIdOptions struct {
  72. ID string `json:"-"`
  73. }
  74. func (opts *LoadbalancerCertificateIdOptions) GetId() string {
  75. return opts.ID
  76. }
  77. func (opts *LoadbalancerCertificateIdOptions) Params() (jsonutils.JSONObject, error) {
  78. return nil, nil
  79. }
  80. type LoadbalancerCertificateDeleteOptions struct {
  81. ID string `json:"-"`
  82. }
  83. type LoadbalancerCertificateListOptions struct {
  84. options.BaseListOptions
  85. PublicKeyAlgorithm string
  86. PublicKeyBitLen *int
  87. SignatureAlgorithm string
  88. Cloudregion string
  89. Usable *bool `help:"List certificates are usable"`
  90. }
  91. func (opts *LoadbalancerCertificateListOptions) Params() (jsonutils.JSONObject, error) {
  92. return options.ListStructToParams(opts)
  93. }
  94. type LoadbalancerCertificateUpdateOptions struct {
  95. LoadbalancerCertificateIdOptions
  96. Name string
  97. Cert string `json:"-" help:"path to certificate file"`
  98. Pkey string `json:"-" help:"path to private key file"`
  99. }
  100. func (opts *LoadbalancerCertificateUpdateOptions) Params() (jsonutils.JSONObject, error) {
  101. paramsCertKey, err := loadbalancerCertificateLoadFiles(opts.Cert, opts.Pkey, true)
  102. if err != nil {
  103. return nil, err
  104. }
  105. return paramsCertKey, nil
  106. }
  107. type LoadbalancerCertificatePublicOptions struct {
  108. LoadbalancerCertificateIdOptions
  109. options.SharableResourcePublicBaseOptions
  110. }
  111. func (opts *LoadbalancerCertificatePublicOptions) Params() (jsonutils.JSONObject, error) {
  112. return jsonutils.Marshal(opts.SharableResourcePublicBaseOptions), nil
  113. }
  114. type LoadbalancerCertificatePrivateOptions struct {
  115. ID string `json:"-"`
  116. }
  117. type LoadbalancerCachedCertificateListOptions struct {
  118. LoadbalancerCertificateListOptions
  119. CertificateId string `help:"related certificate id"`
  120. }
  121. func (opts *LoadbalancerCachedCertificateListOptions) Params() (jsonutils.JSONObject, error) {
  122. return options.ListStructToParams(opts)
  123. }
  124. type LoadbalancerCachedCertificateCreateOptions struct {
  125. CLOUDPROVIDER string `help:"cloud provider id"`
  126. CLOUDREGION string `help:"cloud region id"`
  127. CERTIFICATE string `help:"certificate id"`
  128. }
  129. func (opts *LoadbalancerCachedCertificateCreateOptions) Params() (jsonutils.JSONObject, error) {
  130. return options.StructToParams(opts)
  131. }