github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/sharedfilesystems/v2/snapshots/requests.go (about)

     1  package snapshots
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/vnpaycloud-console/gophercloud/v2"
     7  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     8  )
     9  
    10  // CreateOptsBuilder allows extensions to add additional parameters to the
    11  // Create request.
    12  type CreateOptsBuilder interface {
    13  	ToSnapshotCreateMap() (map[string]any, error)
    14  }
    15  
    16  // CreateOpts contains the options for create a Snapshot. This object is
    17  // passed to snapshots.Create(). For more information about these parameters,
    18  // please refer to the Snapshot object, or the shared file systems API v2
    19  // documentation
    20  type CreateOpts struct {
    21  	// The UUID of the share from which to create a snapshot
    22  	ShareID string `json:"share_id" required:"true"`
    23  	// Defines the snapshot name
    24  	Name string `json:"name,omitempty"`
    25  	// Defines the snapshot description
    26  	Description string `json:"description,omitempty"`
    27  	// DisplayName is equivalent to Name. The API supports using both
    28  	// This is an inherited attribute from the block storage API
    29  	DisplayName string `json:"display_name,omitempty"`
    30  	// DisplayDescription is equivalent to Description. The API supports using both
    31  	// This is an inherited attribute from the block storage API
    32  	DisplayDescription string `json:"display_description,omitempty"`
    33  }
    34  
    35  // ToSnapshotCreateMap assembles a request body based on the contents of a
    36  // CreateOpts.
    37  func (opts CreateOpts) ToSnapshotCreateMap() (map[string]any, error) {
    38  	return gophercloud.BuildRequestBody(opts, "snapshot")
    39  }
    40  
    41  // Create will create a new Snapshot based on the values in CreateOpts. To extract
    42  // the Snapshot object from the response, call the Extract method on the
    43  // CreateResult.
    44  func Create(ctx context.Context, client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    45  	b, err := opts.ToSnapshotCreateMap()
    46  	if err != nil {
    47  		r.Err = err
    48  		return
    49  	}
    50  	resp, err := client.Post(ctx, createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    51  		OkCodes: []int{200, 201, 202},
    52  	})
    53  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    54  	return
    55  }
    56  
    57  // ListOpts holds options for listing Snapshots. It is passed to the
    58  // snapshots.List function.
    59  type ListOpts struct {
    60  	// (Admin only). Defines whether to list the requested resources for all projects.
    61  	AllTenants bool `q:"all_tenants"`
    62  	// The snapshot name.
    63  	Name string `q:"name"`
    64  	// Filter  by a snapshot description.
    65  	Description string `q:"description"`
    66  	// Filters by a share from which the snapshot was created.
    67  	ShareID string `q:"share_id"`
    68  	// Filters by a snapshot size in GB.
    69  	Size int `q:"size"`
    70  	// Filters by a snapshot status.
    71  	Status string `q:"status"`
    72  	// The maximum number of snapshots to return.
    73  	Limit int `q:"limit"`
    74  	// The offset to define start point of snapshot or snapshot group listing.
    75  	Offset int `q:"offset"`
    76  	// The key to sort a list of snapshots.
    77  	SortKey string `q:"sort_key"`
    78  	// The direction to sort a list of snapshots.
    79  	SortDir string `q:"sort_dir"`
    80  	// The UUID of the project in which the snapshot was created. Useful with all_tenants parameter.
    81  	ProjectID string `q:"project_id"`
    82  	// The name pattern that can be used to filter snapshots, snapshot snapshots, snapshot networks or snapshot groups.
    83  	NamePattern string `q:"name~"`
    84  	// The description pattern that can be used to filter snapshots, snapshot snapshots, snapshot networks or snapshot groups.
    85  	DescriptionPattern string `q:"description~"`
    86  }
    87  
    88  // ListOptsBuilder allows extensions to add additional parameters to the List
    89  // request.
    90  type ListOptsBuilder interface {
    91  	ToSnapshotListQuery() (string, error)
    92  }
    93  
    94  // ToSnapshotListQuery formats a ListOpts into a query string.
    95  func (opts ListOpts) ToSnapshotListQuery() (string, error) {
    96  	q, err := gophercloud.BuildQueryString(opts)
    97  	return q.String(), err
    98  }
    99  
   100  // ListDetail returns []Snapshot optionally limited by the conditions provided in ListOpts.
   101  func ListDetail(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   102  	url := listDetailURL(client)
   103  	if opts != nil {
   104  		query, err := opts.ToSnapshotListQuery()
   105  		if err != nil {
   106  			return pagination.Pager{Err: err}
   107  		}
   108  		url += query
   109  	}
   110  
   111  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   112  		p := SnapshotPage{pagination.MarkerPageBase{PageResult: r}}
   113  		p.MarkerPageBase.Owner = p
   114  		return p
   115  	})
   116  }
   117  
   118  // Delete will delete an existing Snapshot with the given UUID.
   119  func Delete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r DeleteResult) {
   120  	resp, err := client.Delete(ctx, deleteURL(client, id), nil)
   121  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   122  	return
   123  }
   124  
   125  // Get will get a single snapshot with given UUID
   126  func Get(ctx context.Context, client *gophercloud.ServiceClient, id string) (r GetResult) {
   127  	resp, err := client.Get(ctx, getURL(client, id), &r.Body, nil)
   128  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   129  	return
   130  }
   131  
   132  // UpdateOptsBuilder allows extensions to add additional parameters to the
   133  // Update request.
   134  type UpdateOptsBuilder interface {
   135  	ToSnapshotUpdateMap() (map[string]any, error)
   136  }
   137  
   138  // UpdateOpts contain options for updating an existing Snapshot. This object is passed
   139  // to the snapshot.Update function. For more information about the parameters, see
   140  // the Snapshot object.
   141  type UpdateOpts struct {
   142  	// Snapshot name. Manila snapshot update logic doesn't have a "name" alias.
   143  	DisplayName *string `json:"display_name,omitempty"`
   144  	// Snapshot description. Manila snapshot update logic doesn't have a "description" alias.
   145  	DisplayDescription *string `json:"display_description,omitempty"`
   146  }
   147  
   148  // ToSnapshotUpdateMap assembles a request body based on the contents of an
   149  // UpdateOpts.
   150  func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]any, error) {
   151  	return gophercloud.BuildRequestBody(opts, "snapshot")
   152  }
   153  
   154  // Update will update the Snapshot with provided information. To extract the updated
   155  // Snapshot from the response, call the Extract method on the UpdateResult.
   156  func Update(ctx context.Context, client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   157  	b, err := opts.ToSnapshotUpdateMap()
   158  	if err != nil {
   159  		r.Err = err
   160  		return
   161  	}
   162  	resp, err := client.Put(ctx, updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   163  		OkCodes: []int{200},
   164  	})
   165  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   166  	return
   167  }
   168  
   169  // ResetStatusOptsBuilder allows extensions to add additional parameters to the
   170  // ResetStatus request.
   171  type ResetStatusOptsBuilder interface {
   172  	ToSnapshotResetStatusMap() (map[string]any, error)
   173  }
   174  
   175  // ResetStatusOpts contains options for resetting a Snapshot status.
   176  // For more information about these parameters, please, refer to the shared file systems API v2,
   177  // Snapshot Actions, ResetStatus share documentation.
   178  type ResetStatusOpts struct {
   179  	// Status is a snapshot status to reset to. Can be "available", "error",
   180  	// "creating", "deleting", "manage_starting", "manage_error",
   181  	// "unmanage_starting", "unmanage_error" or "error_deleting".
   182  	Status string `json:"status"`
   183  }
   184  
   185  // ToSnapshotResetStatusMap assembles a request body based on the contents of a
   186  // ResetStatusOpts.
   187  func (opts ResetStatusOpts) ToSnapshotResetStatusMap() (map[string]any, error) {
   188  	return gophercloud.BuildRequestBody(opts, "reset_status")
   189  }
   190  
   191  // ResetStatus will reset the existing snapshot status. ResetStatusResult contains only the error.
   192  // To extract it, call the ExtractErr method on the ResetStatusResult.
   193  func ResetStatus(ctx context.Context, client *gophercloud.ServiceClient, id string, opts ResetStatusOptsBuilder) (r ResetStatusResult) {
   194  	b, err := opts.ToSnapshotResetStatusMap()
   195  	if err != nil {
   196  		r.Err = err
   197  		return
   198  	}
   199  
   200  	resp, err := client.Post(ctx, resetStatusURL(client, id), b, nil, &gophercloud.RequestOpts{
   201  		OkCodes: []int{202},
   202  	})
   203  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   204  	return
   205  }
   206  
   207  // ForceDelete will delete the existing snapshot in any state. ForceDeleteResult contains only the error.
   208  // To extract it, call the ExtractErr method on the ForceDeleteResult.
   209  func ForceDelete(ctx context.Context, client *gophercloud.ServiceClient, id string) (r ForceDeleteResult) {
   210  	b := map[string]any{
   211  		"force_delete": nil,
   212  	}
   213  	resp, err := client.Post(ctx, forceDeleteURL(client, id), b, nil, &gophercloud.RequestOpts{
   214  		OkCodes: []int{202},
   215  	})
   216  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   217  	return
   218  }