github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/cbr/v3/policies/requests.go (about)

     1  package policies
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  type CreateOpts struct {
     9  	Name                string          `json:"name" required:"true"`
    10  	OperationDefinition *PolicyODCreate `json:"operation_definition" required:"true"`
    11  	OperationType       string          `json:"operation_type" required:"true"`
    12  	Trigger             *Trigger        `json:"trigger" required:"true"`
    13  	Enabled             *bool           `json:"enabled,omitempty"`
    14  }
    15  
    16  // PolicyODCreate is the operation definition used to create the policy.
    17  type PolicyODCreate struct {
    18  	DailyBackups          int    `json:"day_backups,omitempty"`
    19  	WeekBackups           int    `json:"week_backups,omitempty"`
    20  	YearBackups           int    `json:"year_backups,omitempty"`
    21  	MonthBackups          int    `json:"month_backups,omitempty"`
    22  	MaxBackups            int    `json:"max_backups,omitempty"`
    23  	RetentionDurationDays int    `json:"retention_duration_days,omitempty"`
    24  	Timezone              string `json:"timezone,omitempty"`
    25  	EnableAcceleration    bool   `json:"enable_acceleration,omitempty"`
    26  	DestinationProjectID  string `json:"destination_project_id,omitempty"`
    27  	DestinationRegion     string `json:"destination_region,omitempty"`
    28  	FullBackupInterval    *int   `json:"full_backup_interval,omitempty"`
    29  }
    30  
    31  type TriggerProperties struct {
    32  	Pattern []string `json:"pattern"  required:"true"`
    33  }
    34  
    35  type Trigger struct {
    36  	Properties TriggerProperties `json:"properties"  required:"true"`
    37  }
    38  
    39  type CreateOptsBuilder interface {
    40  	ToPolicyCreateMap() (map[string]interface{}, error)
    41  }
    42  
    43  func (opts CreateOpts) ToPolicyCreateMap() (map[string]interface{}, error) {
    44  	return golangsdk.BuildRequestBody(opts, "policy")
    45  }
    46  
    47  //Create is a method by which to create function that create a CBR policy
    48  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    49  	reqBody, err := opts.ToPolicyCreateMap()
    50  	if err != nil {
    51  		r.Err = err
    52  		return
    53  	}
    54  	_, err = client.Post(rootURL(client), reqBody, &r.Body, &golangsdk.RequestOpts{
    55  		OkCodes: []int{200},
    56  	})
    57  	r.Err = err
    58  	return
    59  }
    60  
    61  type ListOpts struct {
    62  	OperationType string `q:"operation_type"`
    63  	VaultID       string `q:"vault_id"`
    64  }
    65  
    66  type ListOptsBuilder interface {
    67  	ToPolicyListQuery() (string, error)
    68  }
    69  
    70  func (opts ListOpts) ToPolicyListQuery() (string, error) {
    71  	q, err := golangsdk.BuildQueryString(opts)
    72  	if err != nil {
    73  		return "", err
    74  	}
    75  	return q.String(), err
    76  }
    77  
    78  //List is a method to obtain the specified CBR policy according to the vault ID or operation type.
    79  //This method can also obtain all the CBR policies through the default parameter settings.
    80  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    81  	url := rootURL(client)
    82  	if opts != nil {
    83  		query, err := opts.ToPolicyListQuery()
    84  		if err != nil {
    85  			return pagination.Pager{Err: err}
    86  		}
    87  		url += query
    88  	}
    89  
    90  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    91  		return PolicyPage{pagination.SinglePageBase(r)}
    92  	})
    93  }
    94  
    95  //Get is a method to obtain the specified CBR policy according to the policy ID.
    96  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    97  	_, r.Err = client.Get(resourceURL(client, id), &r.Body, nil)
    98  	return
    99  }
   100  
   101  type UpdateOpts struct {
   102  	Enabled             *bool           `json:"enabled,omitempty"`
   103  	Name                string          `json:"name,omitempty"`
   104  	OperationDefinition *PolicyODCreate `json:"operation_definition,omitempty"`
   105  	Trigger             *Trigger        `json:"trigger,omitempty"`
   106  }
   107  
   108  type UpdateOptsBuilder interface {
   109  	ToPolicyUpdateMap() (map[string]interface{}, error)
   110  }
   111  
   112  func (opts UpdateOpts) ToPolicyUpdateMap() (map[string]interface{}, error) {
   113  	return golangsdk.BuildRequestBody(opts, "policy")
   114  }
   115  
   116  //Delete is a method to update an existing CBR policy
   117  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   118  	reqBody, err := opts.ToPolicyUpdateMap()
   119  	if err != nil {
   120  		r.Err = err
   121  		return
   122  	}
   123  	_, err = client.Put(resourceURL(client, id), reqBody, &r.Body, &golangsdk.RequestOpts{
   124  		OkCodes: []int{200},
   125  	})
   126  	r.Err = err
   127  	return
   128  }
   129  
   130  //Delete is a method to delete an existing CBR policy
   131  func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) {
   132  	_, r.Err = client.Delete(resourceURL(client, id), nil)
   133  	return
   134  }