github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/css/v1/snapshots/requests.go (about)

     1  package snapshots
     2  
     3  import (
     4  	"github.com/huaweicloud/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  // PolicyCreate will create a new snapshot policy based on the values in PolicyCreateOpts.
    30  func PolicyCreate(client *golangsdk.ServiceClient, opts CreateOptsBuilder, clusterId string) (r ErrorResult) {
    31  	b, err := opts.ToSnapshotCreateMap()
    32  	if err != nil {
    33  		r.Err = err
    34  		return
    35  	}
    36  	_, r.Err = client.Post(policyURL(client, clusterId), b, nil, &golangsdk.RequestOpts{
    37  		OkCodes: []int{200},
    38  	})
    39  	return
    40  }
    41  
    42  // PolicyGet retrieves the snapshot policy with the provided cluster ID.
    43  // To extract the snapshot policy object from the response, call the Extract method on the GetResult.
    44  func PolicyGet(client *golangsdk.ServiceClient, clusterId string) (r PolicyResult) {
    45  	_, r.Err = client.Get(policyURL(client, clusterId), &r.Body, nil)
    46  	return
    47  }
    48  
    49  // Enable will enable the Snapshot function with the provided ID.
    50  func Enable(client *golangsdk.ServiceClient, clusterId string) (r ErrorResult) {
    51  	body := make(map[string]interface{})
    52  	_, r.Err = client.Post(enableURL(client, clusterId), body, nil, &golangsdk.RequestOpts{
    53  		OkCodes:     []int{200},
    54  		MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    55  	})
    56  	return
    57  }
    58  
    59  // Disable will disable the Snapshot function with the provided ID.
    60  func Disable(client *golangsdk.ServiceClient, clusterId string) (r ErrorResult) {
    61  	_, r.Err = client.Delete(disableURL(client, clusterId), nil)
    62  	return
    63  }
    64  
    65  // CreateOpts contains options for creating a snapshot.
    66  // This object is passed to the snapshots.Create function.
    67  type CreateOpts struct {
    68  	Name        string `json:"name" required:"true"`
    69  	Description string `json:"description,omitempty"`
    70  	Indices     string `json:"indices,omitempty"`
    71  }
    72  
    73  // ToSnapshotCreateMap assembles a request body based on the contents of a
    74  // CreateOpts.
    75  func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
    76  	return golangsdk.BuildRequestBody(opts, "")
    77  }
    78  
    79  // Create will create a new snapshot based on the values in CreateOpts.
    80  // To extract the result from the response, call the Extract method on the CreateResult.
    81  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder, clusterId string) (r CreateResult) {
    82  	b, err := opts.ToSnapshotCreateMap()
    83  	if err != nil {
    84  		r.Err = err
    85  		return
    86  	}
    87  	_, r.Err = client.Post(createURL(client, clusterId), b, &r.Body, &golangsdk.RequestOpts{
    88  		OkCodes: []int{201},
    89  	})
    90  	return
    91  }
    92  
    93  // List retrieves the Snapshots with the provided ID. To extract the Snapshot
    94  // objects from the response, call the Extract method on the GetResult.
    95  func List(client *golangsdk.ServiceClient, clusterId string) (r ListResult) {
    96  	_, r.Err = client.Get(listURL(client, clusterId), &r.Body, nil)
    97  	return
    98  }
    99  
   100  // Delete will delete the existing Snapshot ID with the provided ID.
   101  func Delete(client *golangsdk.ServiceClient, clusterId, id string) (r ErrorResult) {
   102  	_, r.Err = client.Delete(deleteURL(client, clusterId, id), &golangsdk.RequestOpts{
   103  		OkCodes:     []int{200},
   104  		MoreHeaders: map[string]string{"Content-Type": "application/json"},
   105  	})
   106  	return
   107  }