github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v3/volumes/results.go (about)

     1  package volumes
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
     9  )
    10  
    11  // Attachment represents a Volume Attachment record
    12  type Attachment struct {
    13  	AttachedAt   time.Time `json:"-"`
    14  	AttachmentID string    `json:"attachment_id"`
    15  	Device       string    `json:"device"`
    16  	HostName     string    `json:"host_name"`
    17  	ID           string    `json:"id"`
    18  	ServerID     string    `json:"server_id"`
    19  	VolumeID     string    `json:"volume_id"`
    20  }
    21  
    22  // UnmarshalJSON is our unmarshalling helper
    23  func (r *Attachment) UnmarshalJSON(b []byte) error {
    24  	type tmp Attachment
    25  	var s struct {
    26  		tmp
    27  		AttachedAt gophercloud.JSONRFC3339MilliNoZ `json:"attached_at"`
    28  	}
    29  	err := json.Unmarshal(b, &s)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	*r = Attachment(s.tmp)
    34  
    35  	r.AttachedAt = time.Time(s.AttachedAt)
    36  
    37  	return err
    38  }
    39  
    40  // Volume contains all the information associated with an OpenStack Volume.
    41  type Volume struct {
    42  	// Unique identifier for the volume.
    43  	ID string `json:"id"`
    44  	// Current status of the volume.
    45  	Status string `json:"status"`
    46  	// Size of the volume in GB.
    47  	Size int `json:"size"`
    48  	// AvailabilityZone is which availability zone the volume is in.
    49  	AvailabilityZone string `json:"availability_zone"`
    50  	// The date when this volume was created.
    51  	CreatedAt time.Time `json:"-"`
    52  	// The date when this volume was last updated
    53  	UpdatedAt time.Time `json:"-"`
    54  	// Instances onto which the volume is attached.
    55  	Attachments []Attachment `json:"attachments"`
    56  	// Human-readable display name for the volume.
    57  	Name string `json:"name"`
    58  	// Human-readable description for the volume.
    59  	Description string `json:"description"`
    60  	// The type of volume to create, either SATA or SSD.
    61  	VolumeType string `json:"volume_type"`
    62  	// The ID of the snapshot from which the volume was created
    63  	SnapshotID string `json:"snapshot_id"`
    64  	// The ID of another block storage volume from which the current volume was created
    65  	SourceVolID string `json:"source_volid"`
    66  	// The backup ID, from which the volume was restored
    67  	// This field is supported since 3.47 microversion
    68  	BackupID *string `json:"backup_id"`
    69  	// Arbitrary key-value pairs defined by the user.
    70  	Metadata map[string]string `json:"metadata"`
    71  	// UserID is the id of the user who created the volume.
    72  	UserID string `json:"user_id"`
    73  	// Indicates whether this is a bootable volume.
    74  	Bootable string `json:"bootable"`
    75  	// Encrypted denotes if the volume is encrypted.
    76  	Encrypted bool `json:"encrypted"`
    77  	// ReplicationStatus is the status of replication.
    78  	ReplicationStatus string `json:"replication_status"`
    79  	// ConsistencyGroupID is the consistency group ID.
    80  	ConsistencyGroupID string `json:"consistencygroup_id"`
    81  	// Multiattach denotes if the volume is multi-attach capable.
    82  	Multiattach bool `json:"multiattach"`
    83  	// Image metadata entries, only included for volumes that were created from an image, or from a snapshot of a volume originally created from an image.
    84  	VolumeImageMetadata map[string]string `json:"volume_image_metadata"`
    85  	// Host is the identifier of the host holding the volume.
    86  	Host string `json:"os-vol-host-attr:host"`
    87  	// TenantID is the id of the project that owns the volume.
    88  	TenantID string `json:"os-vol-tenant-attr:tenant_id"`
    89  }
    90  
    91  // UnmarshalJSON another unmarshalling function
    92  func (r *Volume) UnmarshalJSON(b []byte) error {
    93  	type tmp Volume
    94  	var s struct {
    95  		tmp
    96  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
    97  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
    98  	}
    99  	err := json.Unmarshal(b, &s)
   100  	if err != nil {
   101  		return err
   102  	}
   103  	*r = Volume(s.tmp)
   104  
   105  	r.CreatedAt = time.Time(s.CreatedAt)
   106  	r.UpdatedAt = time.Time(s.UpdatedAt)
   107  
   108  	return err
   109  }
   110  
   111  // VolumePage is a pagination.pager that is returned from a call to the List function.
   112  type VolumePage struct {
   113  	pagination.LinkedPageBase
   114  }
   115  
   116  // IsEmpty returns true if a ListResult contains no Volumes.
   117  func (r VolumePage) IsEmpty() (bool, error) {
   118  	if r.StatusCode == 204 {
   119  		return true, nil
   120  	}
   121  
   122  	volumes, err := ExtractVolumes(r)
   123  	return len(volumes) == 0, err
   124  }
   125  
   126  func (page VolumePage) NextPageURL() (string, error) {
   127  	var s struct {
   128  		Links []gophercloud.Link `json:"volumes_links"`
   129  	}
   130  	err := page.ExtractInto(&s)
   131  	if err != nil {
   132  		return "", err
   133  	}
   134  	return gophercloud.ExtractNextURL(s.Links)
   135  }
   136  
   137  // ExtractVolumes extracts and returns Volumes. It is used while iterating over a volumes.List call.
   138  func ExtractVolumes(r pagination.Page) ([]Volume, error) {
   139  	var s []Volume
   140  	err := ExtractVolumesInto(r, &s)
   141  	return s, err
   142  }
   143  
   144  type commonResult struct {
   145  	gophercloud.Result
   146  }
   147  
   148  // Extract will get the Volume object out of the commonResult object.
   149  func (r commonResult) Extract() (*Volume, error) {
   150  	var s Volume
   151  	err := r.ExtractInto(&s)
   152  	return &s, err
   153  }
   154  
   155  // ExtractInto converts our response data into a volume struct
   156  func (r commonResult) ExtractInto(v any) error {
   157  	return r.Result.ExtractIntoStructPtr(v, "volume")
   158  }
   159  
   160  // ExtractVolumesInto similar to ExtractInto but operates on a `list` of volumes
   161  func ExtractVolumesInto(r pagination.Page, v any) error {
   162  	return r.(VolumePage).Result.ExtractIntoSlicePtr(v, "volumes")
   163  }
   164  
   165  // CreateResult contains the response body and error from a Create request.
   166  type CreateResult struct {
   167  	commonResult
   168  }
   169  
   170  // GetResult contains the response body and error from a Get request.
   171  type GetResult struct {
   172  	commonResult
   173  }
   174  
   175  // UpdateResult contains the response body and error from an Update request.
   176  type UpdateResult struct {
   177  	commonResult
   178  }
   179  
   180  // DeleteResult contains the response body and error from a Delete request.
   181  type DeleteResult struct {
   182  	gophercloud.ErrResult
   183  }
   184  
   185  // AttachResult contains the response body and error from an Attach request.
   186  type AttachResult struct {
   187  	gophercloud.ErrResult
   188  }
   189  
   190  // BeginDetachingResult contains the response body and error from a BeginDetach
   191  // request.
   192  type BeginDetachingResult struct {
   193  	gophercloud.ErrResult
   194  }
   195  
   196  // DetachResult contains the response body and error from a Detach request.
   197  type DetachResult struct {
   198  	gophercloud.ErrResult
   199  }
   200  
   201  // UploadImageResult contains the response body and error from an UploadImage
   202  // request.
   203  type UploadImageResult struct {
   204  	gophercloud.Result
   205  }
   206  
   207  // SetImageMetadataResult contains the response body and error from an SetImageMetadata
   208  // request.
   209  type SetImageMetadataResult struct {
   210  	gophercloud.ErrResult
   211  }
   212  
   213  // SetBootableResult contains the response body and error from a SetBootable
   214  // request.
   215  type SetBootableResult struct {
   216  	gophercloud.ErrResult
   217  }
   218  
   219  // ReserveResult contains the response body and error from a Reserve request.
   220  type ReserveResult struct {
   221  	gophercloud.ErrResult
   222  }
   223  
   224  // UnreserveResult contains the response body and error from an Unreserve
   225  // request.
   226  type UnreserveResult struct {
   227  	gophercloud.ErrResult
   228  }
   229  
   230  // TerminateConnectionResult contains the response body and error from a
   231  // TerminateConnection request.
   232  type TerminateConnectionResult struct {
   233  	gophercloud.ErrResult
   234  }
   235  
   236  // InitializeConnectionResult contains the response body and error from an
   237  // InitializeConnection request.
   238  type InitializeConnectionResult struct {
   239  	gophercloud.Result
   240  }
   241  
   242  // ExtendSizeResult contains the response body and error from an ExtendSize request.
   243  type ExtendSizeResult struct {
   244  	gophercloud.ErrResult
   245  }
   246  
   247  // Extract will get the connection information out of the
   248  // InitializeConnectionResult object.
   249  //
   250  // This will be a generic map[string]any and the results will be
   251  // dependent on the type of connection made.
   252  func (r InitializeConnectionResult) Extract() (map[string]any, error) {
   253  	var s struct {
   254  		ConnectionInfo map[string]any `json:"connection_info"`
   255  	}
   256  	err := r.ExtractInto(&s)
   257  	return s.ConnectionInfo, err
   258  }
   259  
   260  // ImageVolumeType contains volume type information obtained from UploadImage
   261  // action.
   262  type ImageVolumeType struct {
   263  	// The ID of a volume type.
   264  	ID string `json:"id"`
   265  
   266  	// Human-readable display name for the volume type.
   267  	Name string `json:"name"`
   268  
   269  	// Human-readable description for the volume type.
   270  	Description string `json:"display_description"`
   271  
   272  	// Flag for public access.
   273  	IsPublic bool `json:"is_public"`
   274  
   275  	// Extra specifications for volume type.
   276  	ExtraSpecs map[string]any `json:"extra_specs"`
   277  
   278  	// ID of quality of service specs.
   279  	QosSpecsID string `json:"qos_specs_id"`
   280  
   281  	// Flag for deletion status of volume type.
   282  	Deleted bool `json:"deleted"`
   283  
   284  	// The date when volume type was deleted.
   285  	DeletedAt time.Time `json:"-"`
   286  
   287  	// The date when volume type was created.
   288  	CreatedAt time.Time `json:"-"`
   289  
   290  	// The date when this volume was last updated.
   291  	UpdatedAt time.Time `json:"-"`
   292  }
   293  
   294  func (r *ImageVolumeType) UnmarshalJSON(b []byte) error {
   295  	type tmp ImageVolumeType
   296  	var s struct {
   297  		tmp
   298  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
   299  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
   300  		DeletedAt gophercloud.JSONRFC3339MilliNoZ `json:"deleted_at"`
   301  	}
   302  	err := json.Unmarshal(b, &s)
   303  	if err != nil {
   304  		return err
   305  	}
   306  	*r = ImageVolumeType(s.tmp)
   307  
   308  	r.CreatedAt = time.Time(s.CreatedAt)
   309  	r.UpdatedAt = time.Time(s.UpdatedAt)
   310  	r.DeletedAt = time.Time(s.DeletedAt)
   311  
   312  	return err
   313  }
   314  
   315  // VolumeImage contains information about volume uploaded to an image service.
   316  type VolumeImage struct {
   317  	// The ID of a volume an image is created from.
   318  	VolumeID string `json:"id"`
   319  
   320  	// Container format, may be bare, ofv, ova, etc.
   321  	ContainerFormat string `json:"container_format"`
   322  
   323  	// Disk format, may be raw, qcow2, vhd, vdi, vmdk, etc.
   324  	DiskFormat string `json:"disk_format"`
   325  
   326  	// Human-readable description for the volume.
   327  	Description string `json:"display_description"`
   328  
   329  	// The ID of the created image.
   330  	ImageID string `json:"image_id"`
   331  
   332  	// Human-readable display name for the image.
   333  	ImageName string `json:"image_name"`
   334  
   335  	// Size of the volume in GB.
   336  	Size int `json:"size"`
   337  
   338  	// Current status of the volume.
   339  	Status string `json:"status"`
   340  
   341  	// Visibility defines who can see/use the image.
   342  	// supported since 3.1 microversion
   343  	Visibility string `json:"visibility"`
   344  
   345  	// whether the image is not deletable.
   346  	// supported since 3.1 microversion
   347  	Protected bool `json:"protected"`
   348  
   349  	// The date when this volume was last updated.
   350  	UpdatedAt time.Time `json:"-"`
   351  
   352  	// Volume type object of used volume.
   353  	VolumeType ImageVolumeType `json:"volume_type"`
   354  }
   355  
   356  func (r *VolumeImage) UnmarshalJSON(b []byte) error {
   357  	type tmp VolumeImage
   358  	var s struct {
   359  		tmp
   360  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
   361  	}
   362  	err := json.Unmarshal(b, &s)
   363  	if err != nil {
   364  		return err
   365  	}
   366  	*r = VolumeImage(s.tmp)
   367  
   368  	r.UpdatedAt = time.Time(s.UpdatedAt)
   369  
   370  	return err
   371  }
   372  
   373  // Extract will get an object with info about the uploaded image out of the
   374  // UploadImageResult object.
   375  func (r UploadImageResult) Extract() (VolumeImage, error) {
   376  	var s struct {
   377  		VolumeImage VolumeImage `json:"os-volume_upload_image"`
   378  	}
   379  	err := r.ExtractInto(&s)
   380  	return s.VolumeImage, err
   381  }
   382  
   383  // ForceDeleteResult contains the response body and error from a ForceDelete request.
   384  type ForceDeleteResult struct {
   385  	gophercloud.ErrResult
   386  }
   387  
   388  // ChangeTypeResult contains the response body and error from an ChangeType request.
   389  type ChangeTypeResult struct {
   390  	gophercloud.ErrResult
   391  }
   392  
   393  // ReImageResult contains the response body and error from a ReImage request.
   394  type ReImageResult struct {
   395  	gophercloud.ErrResult
   396  }
   397  
   398  // ResetStatusResult contains the response error from a ResetStatus request.
   399  type ResetStatusResult struct {
   400  	gophercloud.ErrResult
   401  }