github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/autoscaling/v1/scheduledtasks/requests.go (about)

     1  package scheduledtasks
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the structure that used to create planned task.
     9  type CreateOpts struct {
    10  	// The name of planned task to be created.
    11  	Name string `json:"name" required:"true"`
    12  	// The policy of planned task to be created.
    13  	ScheduledPolicy ScheduledPolicy `json:"scheduled_policy" required:"true"`
    14  	// The numbers of scaling group instance for planned task to be created.
    15  	InstanceNumber InstanceNumber `json:"instance_number" required:"true"`
    16  }
    17  
    18  // ScheduledPolicy is the structure that used to create the policy of planned task.
    19  type ScheduledPolicy struct {
    20  	// The execution time of the planned task.
    21  	LaunchTime string `json:"launch_time" required:"true"`
    22  	// The type of the planned task.
    23  	RecurrenceType string `json:"recurrence_type,omitempty"`
    24  	// The specific date when the planned task is executed according to the cycle.
    25  	RecurrenceValue string `json:"recurrence_value,omitempty"`
    26  	// The effective start time of the planned task.
    27  	StartTime string `json:"start_time,omitempty"`
    28  	// The effective end time of the planned task.
    29  	EndTime string `json:"end_time,omitempty"`
    30  }
    31  
    32  // InstanceNumber is the structure that used to create the numbers of scaling group instance for planned task.
    33  type InstanceNumber struct {
    34  	// The max number of instances of the scaling group to which the planned task belongs.
    35  	Max *int `json:"max,omitempty"`
    36  	// The min number of instances of the scaling group to which the planned task belongs.
    37  	Min *int `json:"min,omitempty"`
    38  	// The desire number of instances of the scaling group to which the planned task belongs.
    39  	Desire *int `json:"desire,omitempty"`
    40  }
    41  
    42  var requestOpts = golangsdk.RequestOpts{
    43  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    44  }
    45  
    46  // Create is a method used to create planned task using given parameters.
    47  func Create(c *golangsdk.ServiceClient, groupID string, opts CreateOpts) (string, error) {
    48  	b, err := golangsdk.BuildRequestBody(opts, "")
    49  	if err != nil {
    50  		return "", err
    51  	}
    52  
    53  	var r createResp
    54  	_, err = c.Post(rootURL(c, groupID), b, &r, &golangsdk.RequestOpts{
    55  		MoreHeaders: requestOpts.MoreHeaders,
    56  	})
    57  	return r.TaskID, err
    58  }
    59  
    60  // ListOpts is the structure that used to query the planned tasks.
    61  type ListOpts struct {
    62  	// The ID of scaling group.
    63  	GroupID string `q:"scaling_group_id"`
    64  	// Number of records displayed per page.
    65  	// The value must be a positive integer.
    66  	Limit int `q:"limit"`
    67  	// The ID of the last record displayed on the previous page.
    68  	Marker string `q:"marker"`
    69  }
    70  
    71  // List is a method used to query the planned tasks with given parameters.
    72  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]ScheduledTask, error) {
    73  	url := rootURL(c, opts.GroupID)
    74  	query, err := golangsdk.BuildQueryString(opts)
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	url += query.String()
    79  
    80  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    81  		p := ScheduledTaskPage{pagination.MarkerPageBase{PageResult: r}}
    82  		p.MarkerPageBase.Owner = p
    83  		return p
    84  	}).AllPages()
    85  
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	return ExtractScheduledTasks(pages)
    90  }
    91  
    92  // UpdateOpts is the structure that used to update the planned task.
    93  type UpdateOpts struct {
    94  	// The name of planned task to be updated.
    95  	Name string `json:"name,omitempty"`
    96  	// The policy of planned task to be updated.
    97  	ScheduledPolicy *ScheduledPolicy `json:"scheduled_policy,omitempty"`
    98  	// The numbers of scaling group instance for planned task to be updated.
    99  	InstanceNumber *InstanceNumber `json:"instance_number,omitempty"`
   100  }
   101  
   102  // Update is a method used to update planned task using given parameters.
   103  func Update(c *golangsdk.ServiceClient, groupID, taskID string, opts UpdateOpts) error {
   104  	b, err := golangsdk.BuildRequestBody(opts, "")
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	var r UpdateOpts
   110  	_, err = c.Put(resourceURL(c, groupID, taskID), b, &r, &golangsdk.RequestOpts{
   111  		OkCodes: []int{204},
   112  	})
   113  	return err
   114  }
   115  
   116  // Delete is a method used to delete planned task using given parameters.
   117  func Delete(client *golangsdk.ServiceClient, groupID, taskID string) error {
   118  	_, err := client.Delete(resourceURL(client, groupID, taskID), &golangsdk.RequestOpts{
   119  		MoreHeaders: requestOpts.MoreHeaders,
   120  	})
   121  	return err
   122  }