CertificateService.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // Copyright 2016, Sander van Harmelen
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. package cloudstack
  17. import (
  18. "encoding/json"
  19. "net/url"
  20. "strconv"
  21. )
  22. type UploadCustomCertificateParams struct {
  23. p map[string]interface{}
  24. }
  25. func (p *UploadCustomCertificateParams) toURLValues() url.Values {
  26. u := url.Values{}
  27. if p.p == nil {
  28. return u
  29. }
  30. if v, found := p.p["certificate"]; found {
  31. u.Set("certificate", v.(string))
  32. }
  33. if v, found := p.p["domainsuffix"]; found {
  34. u.Set("domainsuffix", v.(string))
  35. }
  36. if v, found := p.p["id"]; found {
  37. vv := strconv.Itoa(v.(int))
  38. u.Set("id", vv)
  39. }
  40. if v, found := p.p["name"]; found {
  41. u.Set("name", v.(string))
  42. }
  43. if v, found := p.p["privatekey"]; found {
  44. u.Set("privatekey", v.(string))
  45. }
  46. return u
  47. }
  48. func (p *UploadCustomCertificateParams) SetCertificate(v string) {
  49. if p.p == nil {
  50. p.p = make(map[string]interface{})
  51. }
  52. p.p["certificate"] = v
  53. return
  54. }
  55. func (p *UploadCustomCertificateParams) SetDomainsuffix(v string) {
  56. if p.p == nil {
  57. p.p = make(map[string]interface{})
  58. }
  59. p.p["domainsuffix"] = v
  60. return
  61. }
  62. func (p *UploadCustomCertificateParams) SetId(v int) {
  63. if p.p == nil {
  64. p.p = make(map[string]interface{})
  65. }
  66. p.p["id"] = v
  67. return
  68. }
  69. func (p *UploadCustomCertificateParams) SetName(v string) {
  70. if p.p == nil {
  71. p.p = make(map[string]interface{})
  72. }
  73. p.p["name"] = v
  74. return
  75. }
  76. func (p *UploadCustomCertificateParams) SetPrivatekey(v string) {
  77. if p.p == nil {
  78. p.p = make(map[string]interface{})
  79. }
  80. p.p["privatekey"] = v
  81. return
  82. }
  83. // You should always use this function to get a new UploadCustomCertificateParams instance,
  84. // as then you are sure you have configured all required params
  85. func (s *CertificateService) NewUploadCustomCertificateParams(certificate string, domainsuffix string) *UploadCustomCertificateParams {
  86. p := &UploadCustomCertificateParams{}
  87. p.p = make(map[string]interface{})
  88. p.p["certificate"] = certificate
  89. p.p["domainsuffix"] = domainsuffix
  90. return p
  91. }
  92. // Uploads a custom certificate for the console proxy VMs to use for SSL. Can be used to upload a single certificate signed by a known CA. Can also be used, through multiple calls, to upload a chain of certificates from CA to the custom certificate itself.
  93. func (s *CertificateService) UploadCustomCertificate(p *UploadCustomCertificateParams) (*UploadCustomCertificateResponse, error) {
  94. resp, err := s.cs.newRequest("uploadCustomCertificate", p.toURLValues())
  95. if err != nil {
  96. return nil, err
  97. }
  98. var r UploadCustomCertificateResponse
  99. if err := json.Unmarshal(resp, &r); err != nil {
  100. return nil, err
  101. }
  102. // If we have a async client, we need to wait for the async result
  103. if s.cs.async {
  104. b, err := s.cs.GetAsyncJobResult(r.JobID, s.cs.timeout)
  105. if err != nil {
  106. if err == AsyncTimeoutErr {
  107. return &r, err
  108. }
  109. return nil, err
  110. }
  111. b, err = getRawValue(b)
  112. if err != nil {
  113. return nil, err
  114. }
  115. if err := json.Unmarshal(b, &r); err != nil {
  116. return nil, err
  117. }
  118. }
  119. return &r, nil
  120. }
  121. type UploadCustomCertificateResponse struct {
  122. JobID string `json:"jobid,omitempty"`
  123. Message string `json:"message,omitempty"`
  124. }