github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/fgs/v2/function/requests.go (about)

     1  package function
     2  
     3  import (
     4  	"io/ioutil"
     5  	"net/http"
     6  
     7  	"github.com/chnsz/golangsdk"
     8  	"github.com/chnsz/golangsdk/openstack/common/tags"
     9  	"github.com/chnsz/golangsdk/pagination"
    10  )
    11  
    12  // Create function
    13  type CreateOptsBuilder interface {
    14  	ToCreateFunctionMap() (map[string]interface{}, error)
    15  }
    16  
    17  // funcCode struct
    18  type FunctionCodeOpts struct {
    19  	File string `json:"file" required:"true"`
    20  	Link string `json:"-"`
    21  }
    22  
    23  // function struct
    24  type CreateOpts struct {
    25  	FuncName            string            `json:"func_name" required:"true"`
    26  	MemorySize          int               `json:"memory_size" required:"true"`
    27  	Package             string            `json:"package" required:"true"`
    28  	Runtime             string            `json:"runtime" required:"true"`
    29  	Timeout             int               `json:"timeout" required:"true"`
    30  	AppXrole            string            `json:"app_xrole,omitempty"`
    31  	CodeFilename        string            `json:"code_filename,omitempty"`
    32  	CodeType            string            `json:"code_type,omitempty"`
    33  	CodeUrl             string            `json:"code_url,omitempty"`
    34  	CustomImage         *CustomImage      `json:"custom_image,omitempty"`
    35  	Description         string            `json:"description,omitempty"`
    36  	EncryptedUserData   string            `json:"encrypted_user_data,omitempty"`
    37  	EnterpriseProjectID string            `json:"enterprise_project_id,omitempty"`
    38  	FuncCode            *FunctionCodeOpts `json:"func_code,omitempty"`
    39  	Handler             string            `json:"handler,omitempty"`
    40  	Type                string            `json:"type,omitempty"`
    41  	UserData            string            `json:"user_data,omitempty"`
    42  	Xrole               string            `json:"xrole,omitempty"`
    43  	LogConfig           *FuncLogConfig    `json:"log_config,omitempty"`
    44  	GPUMemory           int               `json:"gpu_memory,omitempty"`
    45  	GPUType             string            `json:"gpu_type,omitempty"`
    46  	// The pre-stop handler of the function.
    47  	// The value must contain a period (.) in the format of "xx.xx".
    48  	PreStopHandler string `json:"pre_stop_handler,omitempty"`
    49  	// Maximum duration the function can be initialized.
    50  	// Value range: 1s–90s.
    51  	PreStopTimeout int `json:"pre_stop_timeout,omitempty"`
    52  }
    53  
    54  type CustomImage struct {
    55  	Enabled bool   `json:"enabled" required:"true"`
    56  	Image   string `json:"image" required:"true"`
    57  	// The startup commands of the SWR image.
    58  	// Multiple commands are separated by commas (,). For example, "/bin/sh".
    59  	Command string `json:"command,omitempty"`
    60  	// The command line arguments used to start the SWR image.
    61  	// Multiple parameters are separated by commas (,).
    62  	Args string `json:"args,omitempty"`
    63  	// The working directory of the SWR image.
    64  	WorkingDir string `json:"working_dir,omitempty"`
    65  	// The user ID of the SWR image.
    66  	UserId string `json:"uid,omitempty"`
    67  	// The user group ID of the SWR image.
    68  	UserGroupId string `json:"gid,omitempty"`
    69  }
    70  
    71  func (opts CreateOpts) ToCreateFunctionMap() (map[string]interface{}, error) {
    72  	return golangsdk.BuildRequestBody(opts, "")
    73  }
    74  
    75  // create funtion
    76  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    77  	f, err := opts.ToCreateFunctionMap()
    78  	if err != nil {
    79  		r.Err = err
    80  		return
    81  	}
    82  	_, r.Err = c.Post(createURL(c), f, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
    83  	return
    84  }
    85  
    86  // functions list struct
    87  type ListOpts struct {
    88  	Marker      string `q:"marker"`
    89  	MaxItems    string `q:"maxitems"`
    90  	PackageName string `q:"package_name"`
    91  }
    92  
    93  func (opts ListOpts) ToMetricsListQuery() (string, error) {
    94  	q, err := golangsdk.BuildQueryString(opts)
    95  	return q.String(), err
    96  }
    97  
    98  type ListOptsBuilder interface {
    99  	ToMetricsListQuery() (string, error)
   100  }
   101  
   102  // functions list
   103  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   104  	url := listURL(client)
   105  	if opts != nil {
   106  		query, err := opts.ToMetricsListQuery()
   107  		if err != nil {
   108  			return pagination.Pager{Err: err}
   109  		}
   110  		url += query
   111  	}
   112  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   113  		return FunctionPage{pagination.SinglePageBase(r)}
   114  	})
   115  }
   116  
   117  // Querying the Metadata Information of a Function
   118  func GetMetadata(c *golangsdk.ServiceClient, functionUrn string) (r GetResult) {
   119  	_, r.Err = c.Get(getMetadataURL(c, functionUrn), &r.Body, nil)
   120  	return
   121  }
   122  
   123  // Querying the Code of a Function
   124  func GetCode(c *golangsdk.ServiceClient, functionUrn string) (r GetResult) {
   125  	_, r.Err = c.Get(getCodeURL(c, functionUrn), &r.Body, nil)
   126  	return
   127  }
   128  
   129  // Deleting a Function or Function Version
   130  func Delete(c *golangsdk.ServiceClient, functionUrn string) (r DeleteResult) {
   131  	_, r.Err = c.Delete(deleteURL(c, functionUrn), nil)
   132  	return
   133  }
   134  
   135  type UpdateOptsBuilder interface {
   136  	ToUpdateMap() (map[string]interface{}, error)
   137  }
   138  
   139  // Function struct for update
   140  type UpdateCodeOpts struct {
   141  	CodeType     string           `json:"code_type" required:"true"`
   142  	CodeUrl      string           `json:"code_url,omitempty"`
   143  	DependList   []string         `json:"depend_list,omitempty"`
   144  	CodeFileName string           `json:"code_filename,omitempty"`
   145  	FuncCode     FunctionCodeOpts `json:"func_code,omitempty"`
   146  }
   147  
   148  func (opts UpdateCodeOpts) ToUpdateMap() (map[string]interface{}, error) {
   149  	return golangsdk.BuildRequestBody(opts, "")
   150  }
   151  
   152  // Modifying the Code of a Function
   153  func UpdateCode(c *golangsdk.ServiceClient, functionUrn string, opts UpdateOptsBuilder) (r UpdateResult) {
   154  	b, err := opts.ToUpdateMap()
   155  	if err != nil {
   156  		r.Err = err
   157  		return
   158  	}
   159  	_, r.Err = c.Put(updateCodeURL(c, functionUrn), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   160  	return
   161  }
   162  
   163  // Metadata struct for update
   164  type UpdateMetadataOpts struct {
   165  	Handler            string       `json:"handler" required:"true"`
   166  	MemorySize         int          `json:"memory_size" required:"true"`
   167  	Timeout            int          `json:"timeout" required:"true"`
   168  	Runtime            string       `json:"runtime" required:"true"`
   169  	Package            string       `json:"package,omitempty"`
   170  	FuncVpc            *FuncVpc     `json:"func_vpc,omitempty"`
   171  	MountConfig        *MountConfig `json:"mount_config,omitempty"`
   172  	CodeUrl            string       `json:"code_url,omitempty"`
   173  	Description        string       `json:"description,omitempty"`
   174  	UserData           string       `json:"user_data,omitempty"`
   175  	EncryptedUserData  string       `json:"encrypted_user_data,omitempty"`
   176  	Xrole              string       `json:"xrole,omitempty"`
   177  	AppXrole           string       `json:"app_xrole,omitempty"`
   178  	InitializerHandler string       `json:"initializer_handler,omitempty"`
   179  	InitializerTimeout int          `json:"initializer_timeout,omitempty"`
   180  	CustomImage        *CustomImage `json:"custom_image,omitempty"`
   181  	// GPU memory.
   182  	// Range: 1024 to 16,384, and the value is a multiple of 1024.
   183  	GPUMemory int `json:"gpu_memory,omitempty"`
   184  	// GPU type.
   185  	// Currently, only nvidia-t4 is supported.
   186  	GPUType string `json:"gpu_type,omitempty"`
   187  	// Function policy configuration.
   188  	StrategyConfig *StrategyConfig `json:"strategy_config,omitempty"`
   189  	// Extended configuration.
   190  	ExtendConfig string `json:"extend_config,omitempty"`
   191  	// Ephemeral storage size, the maximum value is 10 GB. Defaults to 512 MB.
   192  	EphemeralStorage int `json:"ephemeral_storage,omitempty"`
   193  	// Enterprise project ID.
   194  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
   195  	// Function log configuration.
   196  	LogConfig *FuncLogConfig `json:"log_config,omitempty"`
   197  	// Network configuration.
   198  	NetworkController NetworkControlConfig `json:"network_controller,omitempty"`
   199  	// Whether stateful functions are supported.
   200  	IsStatefulFunction bool `json:"is_stateful_function,omitempty"`
   201  	// Whether to enable dynamic memory allocation.
   202  	EnableDynamicMemory bool `json:"enable_dynamic_memory,omitempty"`
   203  	// Whether to allow authentication information in the request header.
   204  	EnableAuthInHeader bool `json:"enable_auth_in_header,omitempty"`
   205  	// Private domain name.
   206  	DomainNames string `json:"domain_names,omitempty"`
   207  	// Restore Hook entry point for snapshot-based cold start in the format "xx.xx".
   208  	// The period (.) must be included.
   209  	RestoreHookHandler string `json:"restore_hook_handler,omitempty"`
   210  	// Restore Hook timeout of snapshot-based cold start.
   211  	// Range: 1s to 300s.
   212  	RestoreHookTimeout int `json:"restore_hook_timeout,omitempty"`
   213  	// The pre-stop handler of the function.\
   214  	// The value must contain a period (.) in the format of "xx.xx".
   215  	PreStopHandler string `json:"pre_stop_handler,omitempty"`
   216  	// Maximum duration the function can be initialized.
   217  	// Value range: 1s–90s.
   218  	PreStopTimeout int `json:"pre_stop_timeout,omitempty"`
   219  }
   220  
   221  type StrategyConfig struct {
   222  	Concurrency *int `json:"concurrency,omitempty"`
   223  	// The number of concurrent requests supported by single instance. The valid value range is 1 to 1000.
   224  	//  This parameter is only supported by the `v2` version of the function.
   225  	ConcurrencyNum *int `json:"concurrent_num,omitempty"`
   226  }
   227  
   228  type FuncLogConfig struct {
   229  	// Name of the log group bound to the function.
   230  	GroupName string `json:"group_name,omitempty"`
   231  	// ID of the log group bound to the function.
   232  	GroupId string `json:"group_id,omitempty"`
   233  	// Name of the log stream bound to the function.
   234  	StreamName string `json:"stream_name,omitempty"`
   235  	// ID of the log stream bound to the function.
   236  	StreamId string `json:"stream_id,omitempty"`
   237  }
   238  
   239  type NetworkControlConfig struct {
   240  	// Disable public access.
   241  	DisablePublicNetwork bool `json:"disable_public_network,omitempty"`
   242  	// VPC access restriction.
   243  	TriggerAccessVpcs []VpcConfig `json:"trigger_access_vpcs,omitempty"`
   244  }
   245  
   246  type VpcConfig struct {
   247  	// VPC name.
   248  	VpcName string `json:"vpc_name,omitempty"`
   249  	// VPC ID.
   250  	VpcId string `json:"vpc_id,omitempty"`
   251  }
   252  
   253  func (opts UpdateMetadataOpts) ToUpdateMap() (map[string]interface{}, error) {
   254  	return golangsdk.BuildRequestBody(opts, "")
   255  }
   256  
   257  // Modifying the Metadata Information of a Function
   258  func UpdateMetadata(c *golangsdk.ServiceClient, functionUrn string, opts UpdateOptsBuilder) (r UpdateResult) {
   259  	b, err := opts.ToUpdateMap()
   260  	if err != nil {
   261  		r.Err = err
   262  		return
   263  	}
   264  	_, r.Err = c.Put(updateMetadataURL(c, functionUrn), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   265  	return
   266  }
   267  
   268  // verstion struct
   269  type CreateVersionOpts struct {
   270  	Digest      string `json:"digest,omitempty"`
   271  	Description string `json:"description,omitempty"`
   272  	Version     string `json:"version,omitempty"`
   273  }
   274  
   275  func (opts CreateVersionOpts) ToCreateFunctionMap() (map[string]interface{}, error) {
   276  	return golangsdk.BuildRequestBody(opts, "")
   277  }
   278  
   279  // Publishing a Function Version
   280  func CreateVersion(c *golangsdk.ServiceClient, opts CreateOptsBuilder, functionUrn string) (r CreateResult) {
   281  	b, err := opts.ToCreateFunctionMap()
   282  	if err != nil {
   283  		r.Err = err
   284  		return
   285  	}
   286  	_, r.Err = c.Post(createVersionURL(c, functionUrn), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200, 201}})
   287  	return
   288  }
   289  
   290  // Querying the Alias Information of a Function Version
   291  func ListVersions(c *golangsdk.ServiceClient, opts ListOptsBuilder, functionUrn string) pagination.Pager {
   292  	url := listVersionURL(c, functionUrn)
   293  	if opts != nil {
   294  		query, err := opts.ToMetricsListQuery()
   295  		if err != nil {
   296  			return pagination.Pager{Err: err}
   297  		}
   298  		url += query
   299  	}
   300  	return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   301  		return FunctionPage{pagination.SinglePageBase(r)}
   302  	})
   303  }
   304  
   305  // Alias struct
   306  type CreateAliasOpts struct {
   307  	Name    string `json:"name" required:"true"`
   308  	Version string `json:"version" required:"true"`
   309  }
   310  
   311  func (opts CreateAliasOpts) ToCreateFunctionMap() (map[string]interface{}, error) {
   312  	return golangsdk.BuildRequestBody(opts, "")
   313  }
   314  
   315  // Creating an Alias for a Function Version
   316  func CreateAlias(c *golangsdk.ServiceClient, opts CreateOptsBuilder, functionUrn string) (r CreateResult) {
   317  	b, err := opts.ToCreateFunctionMap()
   318  	if err != nil {
   319  		r.Err = err
   320  		return
   321  	}
   322  	_, r.Err = c.Post(createAliasURL(c, functionUrn), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   323  	return
   324  }
   325  
   326  // Alias struct for update
   327  type UpdateAliasOpts struct {
   328  	Version     string `json:"version" required:"true"`
   329  	Description string `json:"description,omitempty"`
   330  }
   331  
   332  func (opts UpdateAliasOpts) ToUpdateMap() (map[string]interface{}, error) {
   333  	return golangsdk.BuildRequestBody(opts, "")
   334  }
   335  
   336  // Modifying the Alias Information of a Function Version
   337  func UpdateAlias(c *golangsdk.ServiceClient, functionUrn, aliasName string, opts UpdateOptsBuilder) (r UpdateResult) {
   338  	b, err := opts.ToUpdateMap()
   339  	if err != nil {
   340  		r.Err = err
   341  		return
   342  	}
   343  	_, r.Err = c.Put(updateAliasURL(c, functionUrn, aliasName), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   344  	return
   345  }
   346  
   347  // Deleting an Alias of a Function Version
   348  func DeleteAlias(c *golangsdk.ServiceClient, functionUrn, aliasName string) (r DeleteResult) {
   349  	_, r.Err = c.Delete(deleteAliasURL(c, functionUrn, aliasName), &golangsdk.RequestOpts{OkCodes: []int{204}})
   350  	return
   351  }
   352  
   353  // Querying the Alias Information of a Function Version
   354  func GetAlias(c *golangsdk.ServiceClient, functionUrn, aliasName string) (r GetResult) {
   355  	_, r.Err = c.Get(getAliasURL(c, functionUrn, aliasName), &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}})
   356  	return
   357  }
   358  
   359  // Querying the Aliases of a Function's All Versions
   360  func ListAlias(c *golangsdk.ServiceClient, functionUrn string) pagination.Pager {
   361  	return pagination.NewPager(c, listAliasURL(c, functionUrn), func(r pagination.PageResult) pagination.Page {
   362  		return FunctionPage{pagination.SinglePageBase(r)}
   363  	})
   364  }
   365  
   366  // Executing a Function Synchronously
   367  func Invoke(c *golangsdk.ServiceClient, m map[string]interface{}, functionUrn string) (r CreateResult) {
   368  	var resp *http.Response
   369  	resp, r.Err = c.Post(invokeURL(c, functionUrn), m, nil, &golangsdk.RequestOpts{
   370  		OkCodes:      []int{200},
   371  		JSONResponse: nil,
   372  	})
   373  	if resp != nil {
   374  		defer resp.Body.Close()
   375  		body, _ := ioutil.ReadAll(resp.Body)
   376  		r.Body = string(body)
   377  	}
   378  	return
   379  }
   380  
   381  // Executing a Function Asynchronously
   382  func AsyncInvoke(c *golangsdk.ServiceClient, m map[string]interface{}, functionUrn string) (r CreateResult) {
   383  	_, r.Err = c.Post(asyncInvokeURL(c, functionUrn), m, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{202}})
   384  	return
   385  }
   386  
   387  // AsyncInvokeConfigOpts is the structure that used to modify the asynchronous invocation configuration.
   388  type AsyncInvokeConfigOpts struct {
   389  	// The maximum validity period of a message.
   390  	MaxAsyncEventAgeInSeconds int `json:"max_async_event_age_in_seconds,omitempty"`
   391  	// The maximum number of retry attempts to be made if asynchronous invocation fails.
   392  	MaxAsyncRetryAttempts int `json:"max_async_retry_attempts,omitempty"`
   393  	// Asynchronous invocation target.
   394  	DestinationConfig DestinationConfig `json:"destination_config,omitempty"`
   395  	// Whether to enable asynchronous invocation status persistence.
   396  	EnableAsyncStatusLog *bool `json:"enable_async_status_log,omitempty"`
   397  }
   398  
   399  // DestinationConfig is the structure that represents the asynchronous invocation target.
   400  type DestinationConfig struct {
   401  	// The target to be invoked when a function is successfully executed.
   402  	OnSuccess DestinationConfigDetails `json:"on_success,omitempty"`
   403  	// The target to be invoked when a function fails to be executed due to a  system error or an internal error.
   404  	OnFailure DestinationConfigDetails `json:"on_failure,omitempty"`
   405  }
   406  
   407  // DestinationConfigDetails is the structure that represents the configuration details of the asynchronous invocation.
   408  type DestinationConfigDetails struct {
   409  	// The object type.
   410  	Destination string `json:"destination,omitempty"`
   411  	// The parameters (in JSON format) corresponding to the target service.
   412  	Param string `json:"param,omitempty"`
   413  }
   414  
   415  var requestOpts = golangsdk.RequestOpts{
   416  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   417  }
   418  
   419  // UpdateAsyncInvokeConfig is the method that used to enable or modify the asynchronous invocation.
   420  func UpdateAsyncInvokeConfig(c *golangsdk.ServiceClient, functionUrn string,
   421  	opts AsyncInvokeConfigOpts) (*AsyncInvokeConfig, error) {
   422  	b, err := golangsdk.BuildRequestBody(opts, "")
   423  	if err != nil {
   424  		return nil, err
   425  	}
   426  
   427  	var r AsyncInvokeConfig
   428  	_, err = c.Put(asyncInvokeConfigURL(c, functionUrn), b, &r, &golangsdk.RequestOpts{
   429  		MoreHeaders: requestOpts.MoreHeaders,
   430  	})
   431  	return &r, err
   432  }
   433  
   434  // GetAsyncInvokeConfig is the method that used to query the configuration details of the asynchronous invocation.
   435  func GetAsyncInvokeConfig(c *golangsdk.ServiceClient, functionUrn string) (*AsyncInvokeConfig, error) {
   436  	var r AsyncInvokeConfig
   437  	_, err := c.Get(asyncInvokeConfigURL(c, functionUrn), &r, &golangsdk.RequestOpts{
   438  		MoreHeaders: requestOpts.MoreHeaders,
   439  	})
   440  	return &r, err
   441  }
   442  
   443  // DeleteAsyncInvokeConfig is the method that used to delete the asynchronous invocation.
   444  func DeleteAsyncInvokeConfig(c *golangsdk.ServiceClient, functionUrn string) error {
   445  	_, err := c.Delete(asyncInvokeConfigURL(c, functionUrn), &golangsdk.RequestOpts{
   446  		MoreHeaders: requestOpts.MoreHeaders,
   447  	})
   448  	return err
   449  }
   450  
   451  // MaxInstanceConfig is the structure used to update the max instance configuration for function.
   452  type MaxInstanceConfig struct {
   453  	// The maximum number of instances of the function.
   454  	Number *int `json:"max_instance_num,omitempty"`
   455  }
   456  
   457  func UpdateMaxInstanceNumber(c *golangsdk.ServiceClient, functionUrn string, num int) (*Function, error) {
   458  	config := MaxInstanceConfig{
   459  		Number: &num,
   460  	}
   461  	var r Function
   462  	_, err := c.Put(maxInstanceNumberURL(c, functionUrn), config, &r, &golangsdk.RequestOpts{
   463  		MoreHeaders: requestOpts.MoreHeaders,
   464  	})
   465  	return &r, err
   466  }
   467  
   468  // TagsActionOpts is an structure that used to manage function tags.
   469  type TagsActionOpts struct {
   470  	// The action name.
   471  	Action string `json:"action,omitempty"`
   472  	// Tag list.
   473  	Tags []tags.ResourceTag `json:"tags,omitempty"`
   474  	// System tag list.
   475  	SysTags []tags.ResourceTag `json:"sys_tags,omitempty"`
   476  }
   477  
   478  // CreateResourceTags is the method that used to add tags to function using given parameters.
   479  func CreateResourceTags(c *golangsdk.ServiceClient, functionUrn string, opts TagsActionOpts) error {
   480  	_, err := c.Post(tagsActionURL(c, functionUrn, "create"), opts, nil, &golangsdk.RequestOpts{
   481  		MoreHeaders: requestOpts.MoreHeaders,
   482  		OkCodes:     []int{204},
   483  	})
   484  	return err
   485  }
   486  
   487  // DeleteResourceTags is the method that used to delete tags from function using given parameters.
   488  func DeleteResourceTags(c *golangsdk.ServiceClient, functionUrn string, opts TagsActionOpts) error {
   489  	_, err := c.DeleteWithBody(tagsActionURL(c, functionUrn, "delete"), opts, &golangsdk.RequestOpts{
   490  		MoreHeaders: requestOpts.MoreHeaders,
   491  	})
   492  	return err
   493  }
   494  
   495  // UpdateReservedInstanceObj is the structure that used to modify information of reserved instance.
   496  type UpdateReservedInstanceObj struct {
   497  	// Function URN.
   498  	FunctionUrn string `json:"-" required:"true"`
   499  	// The number of reserved instance.
   500  	Count *int `json:"count" required:"true"`
   501  	// Whether to enable the idle mode configuration.
   502  	IdleMode *bool `json:"idle_mode,omitempty"`
   503  	// The auto scaling policy configuration.
   504  	TacticsConfig *TacticsConfigObj `json:"tactics_config,omitempty"`
   505  }
   506  
   507  // TacticsConfigObj is the structure that represents the configuration details of the reserved instance policy.
   508  type TacticsConfigObj struct {
   509  	// The list of scheduled configurations.
   510  	CronConfigs []CronConfigObj `json:"cron_configs,omitempty"`
   511  	// The list of traffic configurations.
   512  	MetricConfigs []MetricConfigObj `json:"metric_configs,omitempty"`
   513  }
   514  
   515  // CronConfigsObj is the structure that represents the list of scheduled configurations.
   516  type CronConfigObj struct {
   517  	// The policy name of scheduled configuration.
   518  	Name string `json:"name,omitempty"`
   519  	// The function cron expression.
   520  	Cron string `json:"cron,omitempty"`
   521  	// The number of reserved instance to which the policy belongs.
   522  	Count int `json:"count,omitempty"`
   523  	// The start timestamp of policy.
   524  	StartTime int `json:"start_time,omitempty"`
   525  	// The expiration timestamp of policy.
   526  	ExpiredTime int `json:"expired_time,omitempty"`
   527  }
   528  
   529  // CronConfigsObj is the structure that represents the list of traffic configurations.
   530  type MetricConfigObj struct {
   531  	// The policy name of traffic configuration.
   532  	Name string `json:"name,omitempty"`
   533  	// The type of traffic configuration.
   534  	// + Concurrency: Reserved instance usage.
   535  	Type string `json:"type,omitempty"`
   536  	// The traffic threshold.
   537  	Threshold int `json:"threshold,omitempty"`
   538  	// The minimun of traffic.
   539  	Min int `json:"min,omitempty"`
   540  }
   541  
   542  // UpdateReservedInstanceConfig is the method that used to config reserved instance information.
   543  func UpdateReservedInstanceConfig(c *golangsdk.ServiceClient, opts UpdateReservedInstanceObj) (*UpdateReservedInstanceObj, error) {
   544  	b, err := golangsdk.BuildRequestBody(opts, "")
   545  	if err != nil {
   546  		return nil, err
   547  	}
   548  
   549  	var r UpdateReservedInstanceObj
   550  	_, err = c.Put(reservedInstanceConfigUrl(c, opts.FunctionUrn), b, &r, nil)
   551  	return &r, err
   552  }
   553  
   554  // ListReservedInstanceConfigOpts is the structure that used to query of list reserved instance configurations.
   555  type ListReservedInstanceConfigOpts struct {
   556  	// Function URN.
   557  	FunctionUrn string `q:"function_urn,omitempty"`
   558  	// The current query index. Default 0.
   559  	Marker int `q:"marker,omitempty"`
   560  	// Maximum number of templates to obtain in a request. Default 100, maximum 500.
   561  	Limit int `q:"limit,omitempty"`
   562  }
   563  
   564  // ListReservedInstanceConfigs is the method that used to get of list reserved instance configurations.
   565  func ListReservedInstanceConfigs(c *golangsdk.ServiceClient, opts ListReservedInstanceConfigOpts) ([]ReservedInstancePolicy, error) {
   566  	query, err := golangsdk.BuildQueryString(opts)
   567  	if err != nil {
   568  		return nil, err
   569  	}
   570  
   571  	url := getReservedInstanceConfigUrl(c) + query.String()
   572  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   573  		p := ReservedInstanceConfigPage{pagination.MarkerPageBase{PageResult: r}}
   574  		p.MarkerPageBase.Owner = p
   575  		return p
   576  	}).AllPages()
   577  
   578  	if err != nil {
   579  		return nil, err
   580  	}
   581  	return extractReservedInstanceConfigs(pages)
   582  }