github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/ims/v2/cloudimages/results.go (about)

     1  package cloudimages
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/chnsz/golangsdk"
     8  	"github.com/chnsz/golangsdk/pagination"
     9  )
    10  
    11  // Image represents an image found in the IMS.
    12  type Image struct {
    13  	// the URL for uploading and downloading the image file
    14  	File string `json:"file"`
    15  	// the image owner
    16  	Owner string `json:"owner"`
    17  	// the image id
    18  	ID string `json:"id"`
    19  	// the image URL
    20  	Self string `json:"self"`
    21  	// the image schema
    22  	Schema string `json:"schema"`
    23  	// the image status, the value can be [queued, saving, deleted, killed,active]
    24  	Status string `json:"status"`
    25  	// the image tags
    26  	Tags []string `json:"tags"`
    27  	// whether the image can be seen by others
    28  	Visibility string `json:"visibility"`
    29  	// the image name
    30  	Name string `json:"name"`
    31  	// whether the image has been deleted
    32  	Deleted bool `json:"deleted"`
    33  	// whether the image is protected
    34  	Protected bool `json:"protected"`
    35  	// the container type
    36  	ContainerFormat string `json:"container_format"`
    37  	// the minimum memory size (MB) required for running the image
    38  	MinRam int `json:"min_ram"`
    39  	// the maximum memory of the image in the unit of MB, notice: string
    40  	MaxRam string `json:"max_ram"`
    41  	// the disk format, the value can be [vhd, raw, zvhd, qcow2]
    42  	DiskFormat string `json:"disk_format"`
    43  	// the minimum disk space (GB) required for running the image
    44  	MinDisk int `json:"min_disk"`
    45  	// the environment where the image is used
    46  	VirtualEnvType string `json:"virtual_env_type"`
    47  	// Enterprise project ID
    48  	EnterpriseProjectID string `json:"enterprise_project_id"`
    49  	// *size, virtual_size and checksum parameter are unavailable currently*
    50  	Size        int64  `json:"size"`
    51  	VirtualSize int    `json:"virtual_size"`
    52  	Checksum    string `json:"checksum"`
    53  	// created_at and updated_at are in UTC format
    54  	CreatedAt time.Time `json:"-"`
    55  	UpdatedAt time.Time `json:"-"`
    56  	DeletedAt string    `json:"deleted_at"`
    57  	// the OS architecture: 32 or 64
    58  	OsBit                  string `json:"__os_bit"`
    59  	OsVersion              string `json:"__os_version"`
    60  	Description            string `json:"__description"`
    61  	OsType                 string `json:"__os_type"`
    62  	Isregistered           string `json:"__isregistered"`
    63  	Platform               string `json:"__platform"`
    64  	ImageSourceType        string `json:"__image_source_type"`
    65  	Imagetype              string `json:"__imagetype"`
    66  	WholeImage             string `json:"__whole_image"`
    67  	Originalimagename      string `json:"__originalimagename"`
    68  	BackupID               string `json:"__backup_id"`
    69  	Productcode            string `json:"__productcode"`
    70  	ImageSize              string `json:"__image_size"`
    71  	DataOrigin             string `json:"__data_origin"`
    72  	LazyLoading            string `json:"__lazyloading"`
    73  	ActiveAt               string `json:"active_at"`
    74  	OsFeatureList          string `json:"__os_feature_list"`
    75  	SupportKvm             string `json:"__support_kvm"`
    76  	SupportXen             string `json:"__support_xen"`
    77  	SupportLargeMemory     string `json:"__support_largememory"`
    78  	SupportDiskintensive   string `json:"__support_diskintensive"`
    79  	SupportHighperformance string `json:"__support_highperformance"`
    80  	SupportXenGpuType      string `json:"__support_xen_gpu_type"`
    81  	SupportKvmGpuType      string `json:"__support_kvm_gpu_type"`
    82  	SupportXenHana         string `json:"__support_xen_hana"`
    83  	SupportKvmInfiniband   string `json:"__support_kvm_infiniband"`
    84  	SystemSupportMarket    bool   `json:"__system_support_market"`
    85  	IsOffshelved           string `json:"__is_offshelved"`
    86  	RootOrigin             string `json:"__root_origin"`
    87  	SequenceNum            string `json:"__sequence_num"`
    88  	SupportFcInject        string `json:"__support_fc_inject"`
    89  	HwFirmwareType         string `json:"hw_firmware_type"`
    90  	HwVifMultiqueueEnabled string `json:"hw_vif_multiqueue_enabled"`
    91  	SupportArm             string `json:"__support_arm"`
    92  	SupportAgentList       string `json:"__support_agent_list"`
    93  	SystemCmkid            string `json:"__system__cmkid"`
    94  	AccountCode            string `json:"__account_code"`
    95  	SupportAmd             string `json:"__support_amd"`
    96  }
    97  
    98  func (r *Image) UnmarshalJSON(b []byte) error {
    99  	type tmp Image
   100  	var s struct {
   101  		tmp
   102  		CreatedAt golangsdk.JSONRFC3339Milli `json:"created_at"`
   103  		UpdatedAt golangsdk.JSONRFC3339Milli `json:"updated_at"`
   104  	}
   105  	err := json.Unmarshal(b, &s)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	*r = Image(s.tmp)
   110  
   111  	r.CreatedAt = time.Time(s.CreatedAt)
   112  	r.UpdatedAt = time.Time(s.UpdatedAt)
   113  
   114  	return err
   115  }
   116  
   117  type commonResult struct {
   118  	golangsdk.Result
   119  }
   120  
   121  // UpdateResult represents the result of an Update operation. Call its Extract
   122  // method to interpret it as an Image.
   123  type UpdateResult struct {
   124  	commonResult
   125  }
   126  
   127  // Extract will get the Image object out of the commonResult object.
   128  func (r commonResult) Extract() (*Image, error) {
   129  	var s Image
   130  	err := r.ExtractInto(&s)
   131  	return &s, err
   132  }
   133  
   134  // ExtractInto converts our response data into a volume struct
   135  func (r commonResult) ExtractInto(v interface{}) error {
   136  	return r.Result.ExtractIntoStructPtr(v, "images")
   137  }
   138  
   139  // ImagePage represents the results of a List request.
   140  type ImagePage struct {
   141  	serviceURL string
   142  	pagination.LinkedPageBase
   143  }
   144  
   145  // IsEmpty returns true if an ImagePage contains no Images results.
   146  func (r ImagePage) IsEmpty() (bool, error) {
   147  	images, err := ExtractImages(r)
   148  	return len(images) == 0, err
   149  }
   150  
   151  // NextPageURL uses the response's embedded link reference to navigate to
   152  // the next page of results.
   153  func (r ImagePage) NextPageURL() (string, error) {
   154  	var s struct {
   155  		Next string `json:"next"`
   156  	}
   157  	err := r.ExtractInto(&s)
   158  	if err != nil {
   159  		return "", err
   160  	}
   161  
   162  	if s.Next == "" {
   163  		return "", nil
   164  	}
   165  
   166  	return nextPageURL(r.serviceURL, s.Next)
   167  }
   168  
   169  // ExtractImages interprets the results of a single page from a List() call,
   170  // producing a slice of Image entities.
   171  func ExtractImages(r pagination.Page) ([]Image, error) {
   172  	var s struct {
   173  		Images []Image `json:"images"`
   174  	}
   175  
   176  	err := (r.(ImagePage)).ExtractInto(&s)
   177  	return s.Images, err
   178  }