github.com/gophercloud/gophercloud@v1.11.0/openstack/blockstorage/v3/snapshots/requests.go (about)

     1  package snapshots
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // CreateOptsBuilder allows extensions to add additional parameters to the
     9  // Create request.
    10  type CreateOptsBuilder interface {
    11  	ToSnapshotCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  // CreateOpts contains options for creating a Snapshot. This object is passed to
    15  // the snapshots.Create function. For more information about these parameters,
    16  // see the Snapshot object.
    17  type CreateOpts struct {
    18  	VolumeID    string            `json:"volume_id" required:"true"`
    19  	Force       bool              `json:"force,omitempty"`
    20  	Name        string            `json:"name,omitempty"`
    21  	Description string            `json:"description,omitempty"`
    22  	Metadata    map[string]string `json:"metadata,omitempty"`
    23  }
    24  
    25  // ToSnapshotCreateMap assembles a request body based on the contents of a
    26  // CreateOpts.
    27  func (opts CreateOpts) ToSnapshotCreateMap() (map[string]interface{}, error) {
    28  	return gophercloud.BuildRequestBody(opts, "snapshot")
    29  }
    30  
    31  // Create will create a new Snapshot based on the values in CreateOpts. To
    32  // extract the Snapshot object from the response, call the Extract method on the
    33  // CreateResult.
    34  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    35  	b, err := opts.ToSnapshotCreateMap()
    36  	if err != nil {
    37  		r.Err = err
    38  		return
    39  	}
    40  	resp, err := client.Post(createURL(client), b, &r.Body, &gophercloud.RequestOpts{
    41  		OkCodes: []int{202},
    42  	})
    43  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    44  	return
    45  }
    46  
    47  // Delete will delete the existing Snapshot with the provided ID.
    48  func Delete(client *gophercloud.ServiceClient, id string) (r DeleteResult) {
    49  	resp, err := client.Delete(deleteURL(client, id), nil)
    50  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    51  	return
    52  }
    53  
    54  // Get retrieves the Snapshot with the provided ID. To extract the Snapshot
    55  // object from the response, call the Extract method on the GetResult.
    56  func Get(client *gophercloud.ServiceClient, id string) (r GetResult) {
    57  	resp, err := client.Get(getURL(client, id), &r.Body, nil)
    58  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    59  	return
    60  }
    61  
    62  // ListOptsBuilder allows extensions to add additional parameters to the List
    63  // request.
    64  type ListOptsBuilder interface {
    65  	ToSnapshotListQuery() (string, error)
    66  }
    67  
    68  // ListOpts holds options for listing Snapshots. It is passed to the snapshots.List
    69  // function.
    70  type ListOpts struct {
    71  	// AllTenants will retrieve snapshots of all tenants/projects.
    72  	AllTenants bool `q:"all_tenants"`
    73  
    74  	// Name will filter by the specified snapshot name.
    75  	Name string `q:"name"`
    76  
    77  	// Status will filter by the specified status.
    78  	Status string `q:"status"`
    79  
    80  	// TenantID will filter by a specific tenant/project ID.
    81  	// Setting AllTenants is required to use this.
    82  	TenantID string `q:"project_id"`
    83  
    84  	// VolumeID will filter by a specified volume ID.
    85  	VolumeID string `q:"volume_id"`
    86  
    87  	// Comma-separated list of sort keys and optional sort directions in the
    88  	// form of <key>[:<direction>].
    89  	Sort string `q:"sort"`
    90  
    91  	// Requests a page size of items.
    92  	Limit int `q:"limit"`
    93  
    94  	// Used in conjunction with limit to return a slice of items.
    95  	Offset int `q:"offset"`
    96  
    97  	// The ID of the last-seen item.
    98  	Marker string `q:"marker"`
    99  }
   100  
   101  // ToSnapshotListQuery formats a ListOpts into a query string.
   102  func (opts ListOpts) ToSnapshotListQuery() (string, error) {
   103  	q, err := gophercloud.BuildQueryString(opts)
   104  	return q.String(), err
   105  }
   106  
   107  // List returns Snapshots optionally limited by the conditions provided in
   108  // ListOpts.
   109  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   110  	url := listURL(client)
   111  	if opts != nil {
   112  		query, err := opts.ToSnapshotListQuery()
   113  		if err != nil {
   114  			return pagination.Pager{Err: err}
   115  		}
   116  		url += query
   117  	}
   118  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   119  		return SnapshotPage{pagination.LinkedPageBase{PageResult: r}}
   120  	})
   121  }
   122  
   123  // UpdateOptsBuilder allows extensions to add additional parameters to the
   124  // Update request.
   125  type UpdateOptsBuilder interface {
   126  	ToSnapshotUpdateMap() (map[string]interface{}, error)
   127  }
   128  
   129  // UpdateOpts contain options for updating an existing Snapshot. This object is passed
   130  // to the snapshots.Update function. For more information about the parameters, see
   131  // the Snapshot object.
   132  type UpdateOpts struct {
   133  	Name        *string `json:"name,omitempty"`
   134  	Description *string `json:"description,omitempty"`
   135  }
   136  
   137  // ToSnapshotUpdateMap assembles a request body based on the contents of an
   138  // UpdateOpts.
   139  func (opts UpdateOpts) ToSnapshotUpdateMap() (map[string]interface{}, error) {
   140  	return gophercloud.BuildRequestBody(opts, "snapshot")
   141  }
   142  
   143  // Update will update the Snapshot with provided information. To extract the updated
   144  // Snapshot from the response, call the Extract method on the UpdateResult.
   145  func Update(client *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   146  	b, err := opts.ToSnapshotUpdateMap()
   147  	if err != nil {
   148  		r.Err = err
   149  		return
   150  	}
   151  	resp, err := client.Put(updateURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   152  		OkCodes: []int{200},
   153  	})
   154  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   155  	return
   156  }
   157  
   158  // UpdateMetadataOptsBuilder allows extensions to add additional parameters to
   159  // the Update request.
   160  type UpdateMetadataOptsBuilder interface {
   161  	ToSnapshotUpdateMetadataMap() (map[string]interface{}, error)
   162  }
   163  
   164  // UpdateMetadataOpts contain options for updating an existing Snapshot. This
   165  // object is passed to the snapshots.Update function. For more information
   166  // about the parameters, see the Snapshot object.
   167  type UpdateMetadataOpts struct {
   168  	Metadata map[string]interface{} `json:"metadata,omitempty"`
   169  }
   170  
   171  // ToSnapshotUpdateMetadataMap assembles a request body based on the contents of
   172  // an UpdateMetadataOpts.
   173  func (opts UpdateMetadataOpts) ToSnapshotUpdateMetadataMap() (map[string]interface{}, error) {
   174  	return gophercloud.BuildRequestBody(opts, "")
   175  }
   176  
   177  // UpdateMetadata will update the Snapshot with provided information. To
   178  // extract the updated Snapshot from the response, call the ExtractMetadata
   179  // method on the UpdateMetadataResult.
   180  func UpdateMetadata(client *gophercloud.ServiceClient, id string, opts UpdateMetadataOptsBuilder) (r UpdateMetadataResult) {
   181  	b, err := opts.ToSnapshotUpdateMetadataMap()
   182  	if err != nil {
   183  		r.Err = err
   184  		return
   185  	}
   186  	resp, err := client.Put(updateMetadataURL(client, id), b, &r.Body, &gophercloud.RequestOpts{
   187  		OkCodes: []int{200},
   188  	})
   189  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   190  	return
   191  }
   192  
   193  // ResetStatusOptsBuilder allows extensions to add additional parameters to the
   194  // ResetStatus request.
   195  type ResetStatusOptsBuilder interface {
   196  	ToSnapshotResetStatusMap() (map[string]interface{}, error)
   197  }
   198  
   199  // ResetStatusOpts contains options for resetting a Snapshot status.
   200  // For more information about these parameters, please, refer to the Block Storage API V3,
   201  // Snapshot Actions, ResetStatus snapshot documentation.
   202  type ResetStatusOpts struct {
   203  	// Status is a snapshot status to reset to.
   204  	Status string `json:"status"`
   205  }
   206  
   207  // ToSnapshotResetStatusMap assembles a request body based on the contents of a
   208  // ResetStatusOpts.
   209  func (opts ResetStatusOpts) ToSnapshotResetStatusMap() (map[string]interface{}, error) {
   210  	return gophercloud.BuildRequestBody(opts, "os-reset_status")
   211  }
   212  
   213  // ResetStatus will reset the existing snapshot status. ResetStatusResult contains only the error.
   214  // To extract it, call the ExtractErr method on the ResetStatusResult.
   215  func ResetStatus(client *gophercloud.ServiceClient, id string, opts ResetStatusOptsBuilder) (r ResetStatusResult) {
   216  	b, err := opts.ToSnapshotResetStatusMap()
   217  	if err != nil {
   218  		r.Err = err
   219  		return
   220  	}
   221  
   222  	resp, err := client.Post(resetStatusURL(client, id), b, nil, &gophercloud.RequestOpts{
   223  		OkCodes: []int{202},
   224  	})
   225  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   226  	return
   227  }
   228  
   229  // UpdateStatusOptsBuilder allows extensions to add additional parameters to the
   230  // UpdateStatus request.
   231  type UpdateStatusOptsBuilder interface {
   232  	ToSnapshotUpdateStatusMap() (map[string]interface{}, error)
   233  }
   234  
   235  // UpdateStatusOpts contains options for resetting a Snapshot status.
   236  // For more information about these parameters, please, refer to the Block Storage API V3,
   237  // Snapshot Actions, UpdateStatus snapshot documentation.
   238  type UpdateStatusOpts struct {
   239  	// Status is a snapshot status to update to.
   240  	Status string `json:"status"`
   241  	// A progress percentage value for snapshot build progress.
   242  	Progress string `json:"progress,omitempty"`
   243  }
   244  
   245  // ToSnapshotUpdateStatusMap assembles a request body based on the contents of a
   246  // UpdateStatusOpts.
   247  func (opts UpdateStatusOpts) ToSnapshotUpdateStatusMap() (map[string]interface{}, error) {
   248  	return gophercloud.BuildRequestBody(opts, "os-update_snapshot_status")
   249  }
   250  
   251  // UpdateStatus will reset the existing snapshot status. UpdateStatusResult contains only the error.
   252  // To extract it, call the ExtractErr method on the UpdateStatusResult.
   253  func UpdateStatus(client *gophercloud.ServiceClient, id string, opts UpdateStatusOptsBuilder) (r UpdateStatusResult) {
   254  	b, err := opts.ToSnapshotUpdateStatusMap()
   255  	if err != nil {
   256  		r.Err = err
   257  		return
   258  	}
   259  
   260  	resp, err := client.Post(resetStatusURL(client, id), b, nil, &gophercloud.RequestOpts{
   261  		OkCodes: []int{202},
   262  	})
   263  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   264  	return
   265  }
   266  
   267  // ForceDelete will delete the existing snapshot in any state. ForceDeleteResult contains only the error.
   268  // To extract it, call the ExtractErr method on the ForceDeleteResult.
   269  func ForceDelete(client *gophercloud.ServiceClient, id string) (r ForceDeleteResult) {
   270  	b := map[string]interface{}{
   271  		"os-force_delete": struct{}{},
   272  	}
   273  	resp, err := client.Post(forceDeleteURL(client, id), b, nil, &gophercloud.RequestOpts{
   274  		OkCodes: []int{202},
   275  	})
   276  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   277  	return
   278  }