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