github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/waf/v1/certificates/requests.go (about) 1 package certificates 2 3 import ( 4 "github.com/chnsz/golangsdk" 5 "github.com/chnsz/golangsdk/openstack/utils" 6 "github.com/chnsz/golangsdk/pagination" 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 // CreateOptsBuilder allows extensions to add additional parameters to the 14 // Create request. 15 type CreateOptsBuilder interface { 16 ToCertCreateMap() (map[string]interface{}, error) 17 } 18 19 // CreateOpts contains all the values needed to create a new certificate. 20 type CreateOpts struct { 21 // Certificate name 22 Name string `json:"name" required:"true"` 23 // Certificate content 24 Content string `json:"content" required:"true"` 25 // Private Key 26 Key string `json:"key" required:"true"` 27 // The ID of the enterprise project 28 EnterpriseProjectId string `q:"enterprise_project_id" json:"-"` 29 } 30 31 // ToCertCreateMap builds a create request body from CreateOpts. 32 func (opts CreateOpts) ToCertCreateMap() (map[string]interface{}, error) { 33 return golangsdk.BuildRequestBody(opts, "") 34 } 35 36 // Create will create a new certificate based on the values in CreateOpts. 37 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 38 b, err := opts.ToCertCreateMap() 39 if err != nil { 40 r.Err = err 41 return 42 } 43 query, err := golangsdk.BuildQueryString(opts) 44 if err != nil { 45 r.Err = err 46 return 47 } 48 49 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 50 _, r.Err = c.Post(rootURL(c)+query.String(), b, &r.Body, reqOpt) 51 return 52 } 53 54 // UpdateOptsBuilder allows extensions to add additional parameters to the 55 // Update request. 56 type UpdateOptsBuilder interface { 57 ToCertUpdateMap() (map[string]interface{}, error) 58 } 59 60 // UpdateOpts contains all the values needed to update a certificate. 61 type UpdateOpts struct { 62 // Certificate name 63 Name string `json:"name,omitempty"` 64 // Certificate content 65 Content string `json:"content,omitempty"` 66 // Private Key 67 Key string `json:"key,omitempty"` 68 // The ID of the enterprise project 69 EnterpriseProjectId string `q:"enterprise_project_id" json:"-"` 70 } 71 72 // ToCertUpdateMap builds a update request body from UpdateOpts. 73 func (opts UpdateOpts) ToCertUpdateMap() (map[string]interface{}, error) { 74 return golangsdk.BuildRequestBody(opts, "") 75 } 76 77 // Update accepts a UpdateOpts struct and uses the values to update a certificate.The response code from api is 200 78 func Update(c *golangsdk.ServiceClient, certID string, opts UpdateOptsBuilder) (r UpdateResult) { 79 b, err := opts.ToCertUpdateMap() 80 if err != nil { 81 r.Err = err 82 return 83 } 84 query, err := golangsdk.BuildQueryString(opts) 85 if err != nil { 86 r.Err = err 87 return 88 } 89 90 reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}} 91 _, r.Err = c.Put(resourceURL(c, certID)+query.String(), b, nil, reqOpt) 92 return 93 } 94 95 type ListOptsBuilder interface { 96 ToCertificateListQuery() (string, error) 97 } 98 99 // ListOpts the struct is used to query certificate list 100 type ListOpts struct { 101 Page int `q:"page"` 102 Pagesize int `q:"pagesize"` 103 Name string `q:"name"` 104 Host *bool `q:"host"` 105 ExpStatus *int `q:"exp_status"` 106 EnterpriseProjectID string `q:"enterprise_project_id"` 107 } 108 109 // ToCertificateListQuery formats a ListOpts into a query string. 110 func (opts ListOpts) ToCertificateListQuery() (string, error) { 111 q, err := golangsdk.BuildQueryString(opts) 112 return q.String(), err 113 } 114 115 // List sends a request to obtain a certificate list 116 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 117 url := rootURL(c) 118 if opts != nil { 119 query, err := opts.ToCertificateListQuery() 120 if err != nil { 121 return pagination.Pager{Err: err} 122 } 123 url += query 124 } 125 126 pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 127 return CertificatePage{pagination.SinglePageBase(r)} 128 }) 129 pager.Headers = RequestOpts.MoreHeaders 130 return pager 131 } 132 133 // Get retrieves a particular certificate based on its unique ID. 134 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 135 return GetWithEpsID(c, id, "") 136 } 137 138 func GetWithEpsID(c *golangsdk.ServiceClient, id, epsID string) (r GetResult) { 139 reqOpt := &golangsdk.RequestOpts{ 140 OkCodes: []int{200}, 141 MoreHeaders: RequestOpts.MoreHeaders, 142 } 143 _, r.Err = c.Get(resourceURL(c, id)+utils.GenerateEpsIDQuery(epsID), &r.Body, reqOpt) 144 return 145 } 146 147 // Delete will permanently delete a particular certificate based on its unique ID. 148 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 149 return DeleteWithEpsID(c, id, "") 150 } 151 152 func DeleteWithEpsID(c *golangsdk.ServiceClient, id, epsID string) (r DeleteResult) { 153 reqOpt := &golangsdk.RequestOpts{ 154 OkCodes: []int{200, 204}, 155 MoreHeaders: RequestOpts.MoreHeaders, 156 } 157 _, r.Err = c.Delete(resourceURL(c, id)+utils.GenerateEpsIDQuery(epsID), reqOpt) 158 return 159 }