github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/fgs/v2/function/requests.go (about) 1 package function 2 3 import ( 4 "io/ioutil" 5 "net/http" 6 7 "github.com/huaweicloud/golangsdk" 8 "github.com/huaweicloud/golangsdk/pagination" 9 ) 10 11 //Create function 12 type CreateOptsBuilder interface { 13 ToCreateFunctionMap() (map[string]interface{}, error) 14 } 15 16 //funcCode struct 17 type FunctionCodeOpts struct { 18 File string `json:"file" required:"true"` 19 Link string `json:"-"` 20 } 21 22 //function struct 23 type CreateOpts struct { 24 FuncName string `json:"func_name" required:"true"` 25 Package string `json:"package" required:"true"` 26 CodeType string `json:"code_type" required:"true"` 27 CodeUrl string `json:"code_url,omitempty"` 28 Description string `json:"description,omitempty"` 29 CodeFilename string `json:"code_filename,omitempty"` 30 Handler string `json:"handler" required:"true"` 31 MemorySize int `json:"memory_size" required:"true"` 32 Runtime string `json:"runtime" required:"true"` 33 Timeout int `json:"timeout" required:"true"` 34 UserData string `json:"user_data,omitempty"` 35 Xrole string `json:"xrole,omitempty"` 36 AppXrole string `json:"app_xrole,omitempty"` 37 FuncCode FunctionCodeOpts `json:"func_code,omitempty"` 38 EnterpriseProjectID string `json:"enterprise_project_id,omitempty"` 39 } 40 41 func (opts CreateOpts) ToCreateFunctionMap() (map[string]interface{}, error) { 42 return golangsdk.BuildRequestBody(opts, "") 43 } 44 45 //create funtion 46 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 47 f, err := opts.ToCreateFunctionMap() 48 if err != nil { 49 r.Err = err 50 return 51 } 52 _, r.Err = c.Post(createURL(c), f, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 53 return 54 } 55 56 //functions list struct 57 type ListOpts struct { 58 Marker string `q:"marker"` 59 MaxItems string `q:"maxitems"` 60 } 61 62 func (opts ListOpts) ToMetricsListQuery() (string, error) { 63 q, err := golangsdk.BuildQueryString(opts) 64 return q.String(), err 65 } 66 67 type ListOptsBuilder interface { 68 ToMetricsListQuery() (string, error) 69 } 70 71 //functions list 72 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 73 url := listURL(client) 74 if opts != nil { 75 query, err := opts.ToMetricsListQuery() 76 if err != nil { 77 return pagination.Pager{Err: err} 78 } 79 url += query 80 } 81 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 82 return FunctionPage{pagination.SinglePageBase(r)} 83 }) 84 } 85 86 //Querying the Metadata Information of a Function 87 func GetMetadata(c *golangsdk.ServiceClient, functionUrn string) (r GetResult) { 88 _, r.Err = c.Get(getMetadataURL(c, functionUrn), &r.Body, nil) 89 return 90 } 91 92 //Querying the Code of a Function 93 func GetCode(c *golangsdk.ServiceClient, functionUrn string) (r GetResult) { 94 _, r.Err = c.Get(getCodeURL(c, functionUrn), &r.Body, nil) 95 return 96 } 97 98 //Deleting a Function or Function Version 99 func Delete(c *golangsdk.ServiceClient, functionUrn string) (r DeleteResult) { 100 _, r.Err = c.Delete(deleteURL(c, functionUrn), nil) 101 return 102 } 103 104 type UpdateOptsBuilder interface { 105 ToUpdateMap() (map[string]interface{}, error) 106 } 107 108 //Function struct for update 109 type UpdateCodeOpts struct { 110 CodeType string `json:"code_type" required:"true"` 111 CodeUrl string `json:"code_url,omitempty"` 112 DependList []string `json:"depend_list,omitempty"` 113 CodeFileName string `json:"code_filename,omitempty"` 114 FuncCode FunctionCodeOpts `json:"func_code,omitempty"` 115 } 116 117 func (opts UpdateCodeOpts) ToUpdateMap() (map[string]interface{}, error) { 118 return golangsdk.BuildRequestBody(opts, "") 119 } 120 121 //Modifying the Code of a Function 122 func UpdateCode(c *golangsdk.ServiceClient, functionUrn string, opts UpdateOptsBuilder) (r UpdateResult) { 123 b, err := opts.ToUpdateMap() 124 if err != nil { 125 r.Err = err 126 return 127 } 128 _, r.Err = c.Put(updateCodeURL(c, functionUrn), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 129 return 130 } 131 132 //Metadata struct for update 133 type UpdateMetadataOpts struct { 134 Handler string `json:"handler" required:"true"` 135 MemorySize int `json:"memory_size" required:"true"` 136 Timeout int `json:"timeout" required:"true"` 137 Runtime string `json:"runtime" required:"true"` 138 Package string `json:"package,omitempty"` 139 FuncVpc *FuncVpc `json:"func_vpc,omitempty"` 140 MountConfig *MountConfig `json:"mount_config,omitempty"` 141 CodeUrl string `json:"code_url,omitempty"` 142 Description string `json:"description,omitempty"` 143 UserData string `json:"user_data,omitempty"` 144 Xrole string `json:"xrole,omitempty"` 145 AppXrole string `json:"app_xrole,omitempty"` 146 InitializerHandler string `json:"initializer_handler,omitempty"` 147 InitializerTimeout int `json:"initializer_timeout,omitempty"` 148 } 149 150 func (opts UpdateMetadataOpts) ToUpdateMap() (map[string]interface{}, error) { 151 return golangsdk.BuildRequestBody(opts, "") 152 } 153 154 //Modifying the Metadata Information of a Function 155 func UpdateMetadata(c *golangsdk.ServiceClient, functionUrn string, opts UpdateOptsBuilder) (r UpdateResult) { 156 b, err := opts.ToUpdateMap() 157 if err != nil { 158 r.Err = err 159 return 160 } 161 _, r.Err = c.Put(updateMetadataURL(c, functionUrn), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 162 return 163 } 164 165 //verstion struct 166 type CreateVersionOpts struct { 167 Digest string `json:"digest,omitempty"` 168 Description string `json:"description,omitempty"` 169 Version string `json:"version,omitempty"` 170 } 171 172 func (opts CreateVersionOpts) ToCreateFunctionMap() (map[string]interface{}, error) { 173 return golangsdk.BuildRequestBody(opts, "") 174 } 175 176 //Publishing a Function Version 177 func CreateVersion(c *golangsdk.ServiceClient, opts CreateOptsBuilder, functionUrn string) (r CreateResult) { 178 b, err := opts.ToCreateFunctionMap() 179 if err != nil { 180 r.Err = err 181 return 182 } 183 _, r.Err = c.Post(createVersionURL(c, functionUrn), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200, 201}}) 184 return 185 } 186 187 //Querying the Alias Information of a Function Version 188 func ListVersions(c *golangsdk.ServiceClient, opts ListOptsBuilder, functionUrn string) pagination.Pager { 189 url := listVersionURL(c, functionUrn) 190 if opts != nil { 191 query, err := opts.ToMetricsListQuery() 192 if err != nil { 193 return pagination.Pager{Err: err} 194 } 195 url += query 196 } 197 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 198 return FunctionPage{pagination.SinglePageBase(r)} 199 }) 200 } 201 202 //Alias struct 203 type CreateAliasOpts struct { 204 Name string `json:"name" required:"true"` 205 Version string `json:"version" required:"true"` 206 } 207 208 func (opts CreateAliasOpts) ToCreateFunctionMap() (map[string]interface{}, error) { 209 return golangsdk.BuildRequestBody(opts, "") 210 } 211 212 //Creating an Alias for a Function Version 213 func CreateAlias(c *golangsdk.ServiceClient, opts CreateOptsBuilder, functionUrn string) (r CreateResult) { 214 b, err := opts.ToCreateFunctionMap() 215 if err != nil { 216 r.Err = err 217 return 218 } 219 _, r.Err = c.Post(createAliasURL(c, functionUrn), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 220 return 221 } 222 223 //Alias struct for update 224 type UpdateAliasOpts struct { 225 Version string `json:"version" required:"true"` 226 Description string `json:"description,omitempty"` 227 } 228 229 func (opts UpdateAliasOpts) ToUpdateMap() (map[string]interface{}, error) { 230 return golangsdk.BuildRequestBody(opts, "") 231 } 232 233 //Modifying the Alias Information of a Function Version 234 func UpdateAlias(c *golangsdk.ServiceClient, functionUrn, aliasName string, opts UpdateOptsBuilder) (r UpdateResult) { 235 b, err := opts.ToUpdateMap() 236 if err != nil { 237 r.Err = err 238 return 239 } 240 _, r.Err = c.Put(updateAliasURL(c, functionUrn, aliasName), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 241 return 242 } 243 244 //Deleting an Alias of a Function Version 245 func DeleteAlias(c *golangsdk.ServiceClient, functionUrn, aliasName string) (r DeleteResult) { 246 _, r.Err = c.Delete(deleteAliasURL(c, functionUrn, aliasName), &golangsdk.RequestOpts{OkCodes: []int{204}}) 247 return 248 } 249 250 //Querying the Alias Information of a Function Version 251 func GetAlias(c *golangsdk.ServiceClient, functionUrn, aliasName string) (r GetResult) { 252 _, r.Err = c.Get(getAliasURL(c, functionUrn, aliasName), &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 253 return 254 } 255 256 //Querying the Aliases of a Function's All Versions 257 func ListAlias(c *golangsdk.ServiceClient, functionUrn string) pagination.Pager { 258 return pagination.NewPager(c, listAliasURL(c, functionUrn), func(r pagination.PageResult) pagination.Page { 259 return FunctionPage{pagination.SinglePageBase(r)} 260 }) 261 } 262 263 //Executing a Function Synchronously 264 func Invoke(c *golangsdk.ServiceClient, m map[string]interface{}, functionUrn string) (r CreateResult) { 265 var resp *http.Response 266 resp, r.Err = c.Post(invokeURL(c, functionUrn), m, nil, &golangsdk.RequestOpts{ 267 OkCodes: []int{200}, 268 JSONResponse: nil, 269 }) 270 if resp != nil { 271 defer resp.Body.Close() 272 body, _ := ioutil.ReadAll(resp.Body) 273 r.Body = string(body) 274 } 275 return 276 } 277 278 //Executing a Function Asynchronously 279 func AsyncInvoke(c *golangsdk.ServiceClient, m map[string]interface{}, functionUrn string) (r CreateResult) { 280 _, r.Err = c.Post(asyncInvokeURL(c, functionUrn), m, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{202}}) 281 return 282 }