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

     1  package snapshots
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  )
     6  
     7  // CreateOptsBuilder allows extensions to add additional parameters to the
     8  // Create request.
     9  type CreateOptsBuilder interface {
    10  	ToSnapshotCreateMap() (map[string]interface{}, error)
    11  }
    12  
    13  // PolicyCreateOpts contains options for creating a snapshot policy.
    14  // This object is passed to the snapshots.PolicyCreate function.
    15  type PolicyCreateOpts struct {
    16  	Prefix     string `json:"prefix" required:"true"`
    17  	Period     string `json:"period" required:"true"`
    18  	KeepDay    int    `json:"keepday" required:"true"`
    19  	Enable     string `json:"enable" required:"true"`
    20  	DeleteAuto string `json:"deleteAuto,omitempty"`
    21  }
    22  
    23  // ToSnapshotCreateMap assembles a request body based on the contents of a
    24  // PolicyCreateOpts.
    25  func (opts PolicyCreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
    26  	return golangsdk.BuildRequestBody(opts, "")
    27  }
    28  
    29  type UpdateSnapshotSettingReq struct {
    30  	// OBS bucket used for index data backup. If there is snapshot data in an OBS bucket, only the OBS bucket is used
    31  	// and cannot be changed.
    32  	Bucket   string `json:"bucket" required:"true"`
    33  	BasePath string `json:"basePath" required:"true"` // Storage path of the snapshot in the OBS bucket.
    34  	Agency   string `json:"agency" required:"true"`   // IAM agency used to access OBS.
    35  }
    36  
    37  var RequestOpts = golangsdk.RequestOpts{
    38  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    39  }
    40  
    41  // PolicyCreate will create a new snapshot policy based on the values in PolicyCreateOpts.
    42  func PolicyCreate(client *golangsdk.ServiceClient, opts CreateOptsBuilder, clusterId string) (r ErrorResult) {
    43  	b, err := opts.ToSnapshotCreateMap()
    44  	if err != nil {
    45  		r.Err = err
    46  		return
    47  	}
    48  	_, r.Err = client.Post(policyURL(client, clusterId), b, nil, &golangsdk.RequestOpts{
    49  		OkCodes: []int{200},
    50  	})
    51  	return
    52  }
    53  
    54  // PolicyGet retrieves the snapshot policy with the provided cluster ID.
    55  // To extract the snapshot policy object from the response, call the Extract method on the GetResult.
    56  func PolicyGet(client *golangsdk.ServiceClient, clusterId string) (r PolicyResult) {
    57  	_, r.Err = client.Get(policyURL(client, clusterId), &r.Body, nil)
    58  	return
    59  }
    60  
    61  // Enable will enable the Snapshot function with the provided ID.
    62  func Enable(client *golangsdk.ServiceClient, clusterId string) (r ErrorResult) {
    63  	body := make(map[string]interface{})
    64  	_, r.Err = client.Post(enableURL(client, clusterId), body, nil, &golangsdk.RequestOpts{
    65  		OkCodes:     []int{200},
    66  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    67  	})
    68  	return
    69  }
    70  
    71  // Disable will disable the Snapshot function with the provided ID.
    72  func Disable(client *golangsdk.ServiceClient, clusterId string) (r ErrorResult) {
    73  	_, r.Err = client.Delete(disableURL(client, clusterId), nil)
    74  	return
    75  }
    76  
    77  // CreateOpts contains options for creating a snapshot.
    78  // This object is passed to the snapshots.Create function.
    79  type CreateOpts struct {
    80  	Name        string `json:"name" required:"true"`
    81  	Description string `json:"description,omitempty"`
    82  	Indices     string `json:"indices,omitempty"`
    83  }
    84  
    85  // ToSnapshotCreateMap assembles a request body based on the contents of a
    86  // CreateOpts.
    87  func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
    88  	return golangsdk.BuildRequestBody(opts, "")
    89  }
    90  
    91  // Create will create a new snapshot based on the values in CreateOpts.
    92  // To extract the result from the response, call the Extract method on the CreateResult.
    93  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder, clusterId string) (r CreateResult) {
    94  	b, err := opts.ToSnapshotCreateMap()
    95  	if err != nil {
    96  		r.Err = err
    97  		return
    98  	}
    99  	_, r.Err = client.Post(createURL(client, clusterId), b, &r.Body, &golangsdk.RequestOpts{
   100  		OkCodes: []int{201},
   101  	})
   102  	return
   103  }
   104  
   105  // List retrieves the Snapshots with the provided ID. To extract the Snapshot
   106  // objects from the response, call the Extract method on the GetResult.
   107  func List(client *golangsdk.ServiceClient, clusterId string) (r ListResult) {
   108  	_, r.Err = client.Get(listURL(client, clusterId), &r.Body, nil)
   109  	return
   110  }
   111  
   112  // Delete will delete the existing Snapshot ID with the provided ID.
   113  func Delete(client *golangsdk.ServiceClient, clusterId, id string) (r ErrorResult) {
   114  	_, r.Err = client.Delete(deleteURL(client, clusterId, id), &golangsdk.RequestOpts{
   115  		OkCodes:     []int{200},
   116  		MoreHeaders: map[string]string{"Content-Type": "application/json"},
   117  	})
   118  	return
   119  }
   120  
   121  // UpdateSnapshotSetting
   122  func UpdateSnapshotSetting(c *golangsdk.ServiceClient, clusterId string, opts UpdateSnapshotSettingReq) (*golangsdk.Result, error) {
   123  	b, err := golangsdk.BuildRequestBody(opts, "")
   124  	if err != nil {
   125  		return nil, err
   126  	}
   127  
   128  	var r golangsdk.Result
   129  	_, err = c.Post(settingURL(c, clusterId), b, &r.Body, &golangsdk.RequestOpts{
   130  		MoreHeaders: RequestOpts.MoreHeaders,
   131  	})
   132  	return &r, err
   133  }