github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/scm/v3/certificates/requests.go (about) 1 package certificates 2 3 import ( 4 "strings" 5 6 "github.com/huaweicloud/golangsdk" 7 ) 8 9 var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{ 10 MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"}, 11 } 12 13 // ImportOptsBuilder is the interface options structs have to satisfy in order 14 // to be used in the Import operation in this package. 15 type ImportOptsBuilder interface { 16 ToCertificateImportMap() (map[string]interface{}, error) 17 } 18 19 // ImportOpts is the struct be used in the Import operation 20 type ImportOpts struct { 21 Name string `json:"name,omitempty" required:"true"` 22 Certificate string `json:"certificate" required:"true"` 23 CertificateChain string `json:"certificate_chain" required:"true"` 24 PrivateKey string `json:"private_key" required:"true"` 25 } 26 27 // ToCertificateImportMap casts a CreateOpts struct to a map. 28 func (opts ImportOpts) ToCertificateImportMap() (map[string]interface{}, error) { 29 // Remove the blank content of both ends of the authentication value. 30 // Otherwise, the service will go wrong. 31 opts.PrivateKey = strings.Trim(opts.PrivateKey, "\r\n") 32 opts.Certificate = strings.Trim(opts.Certificate, "\r\n") 33 opts.CertificateChain = strings.Trim(opts.CertificateChain, "\r\n") 34 35 return golangsdk.BuildRequestBody(opts, "") 36 } 37 38 // Import the certification into Huawei Cloud 39 func Import(c *golangsdk.ServiceClient, opts ImportOptsBuilder) (r ImportResult) { 40 b, err := opts.ToCertificateImportMap() 41 if err != nil { 42 r.Err = err 43 return 44 } 45 _, r.Err = c.Post(importURL(c), b, &r.Body, &golangsdk.RequestOpts{ 46 OkCodes: []int{200, 201}, 47 MoreHeaders: RequestOpts.MoreHeaders, 48 }) 49 return 50 } 51 52 // PushOptsBuilder is the interface options structs have to satisfy in order 53 // to be used in the Push operation in this package. 54 type PushOptsBuilder interface { 55 ToCertificatePushMap() (map[string]interface{}, error) 56 } 57 58 // PushOpts is the struct be used in the Import operation 59 type PushOpts struct { 60 TargetProject string `json:"target_project"` 61 TargetService string `json:"target_service" required:"true"` 62 } 63 64 // ToCertificatePushMap casts a PushOpts struct to a map. 65 func (opts PushOpts) ToCertificatePushMap() (map[string]interface{}, error) { 66 return golangsdk.BuildRequestBody(opts, "") 67 } 68 69 // Push the certification of imported to services 70 func Push(c *golangsdk.ServiceClient, id string, opts PushOptsBuilder) (r PushResult) { 71 b, err := opts.ToCertificatePushMap() 72 if err != nil { 73 r.Err = err 74 return 75 } 76 77 _, r.Err = c.Post(pushURL(c, id), b, nil, &golangsdk.RequestOpts{ 78 OkCodes: []int{204}, 79 MoreHeaders: RequestOpts.MoreHeaders, 80 }) 81 return 82 } 83 84 // Obtain information about the imported certificate by ID. 85 // Contain no certificate key or private key. 86 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 87 _, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{ 88 OkCodes: []int{200}, 89 MoreHeaders: RequestOpts.MoreHeaders, 90 }) 91 return 92 } 93 94 // Get the certification keyćprivate key and certification chain by id. 95 func Export(c *golangsdk.ServiceClient, id string) (r ExportResult) { 96 body := map[string]interface{}{} 97 _, r.Err = c.Post(exportURL(c, id), body, &r.Body, &golangsdk.RequestOpts{ 98 OkCodes: []int{200, 201}, 99 MoreHeaders: RequestOpts.MoreHeaders, 100 }) 101 return 102 } 103 104 // Delete the imported certificate based on its unique ID. 105 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 106 _, r.Err = c.Delete(resourceURL(c, id), nil) 107 return 108 }