github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/ims/v2/cloudimages/results.go (about)

     1  package cloudimages
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/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  	Originalimagename      string `json:"__originalimagename"`
    67  	BackupID               string `json:"__backup_id"`
    68  	Productcode            string `json:"__productcode"`
    69  	ImageSize              string `json:"__image_size"`
    70  	DataOrigin             string `json:"__data_origin"`
    71  	SupportKvm             string `json:"__support_kvm"`
    72  	SupportXen             string `json:"__support_xen"`
    73  	SupportLargeMemory     string `json:"__support_largememory"`
    74  	SupportDiskintensive   string `json:"__support_diskintensive"`
    75  	SupportHighperformance string `json:"__support_highperformance"`
    76  	SupportXenGpuType      string `json:"__support_xen_gpu_type"`
    77  	SupportKvmGpuType      string `json:"__support_kvm_gpu_type"`
    78  	SupportXenHana         string `json:"__support_xen_hana"`
    79  	SupportKvmInfiniband   string `json:"__support_kvm_infiniband"`
    80  	SystemSupportMarket    bool   `json:"__system_support_market"`
    81  	RootOrigin             string `json:"__root_origin"`
    82  	SequenceNum            string `json:"__sequence_num"`
    83  }
    84  
    85  func (r *Image) UnmarshalJSON(b []byte) error {
    86  	type tmp Image
    87  	var s struct {
    88  		tmp
    89  		CreatedAt golangsdk.JSONRFC3339Milli `json:"created_at"`
    90  		UpdatedAt golangsdk.JSONRFC3339Milli `json:"updated_at"`
    91  	}
    92  	err := json.Unmarshal(b, &s)
    93  	if err != nil {
    94  		return err
    95  	}
    96  	*r = Image(s.tmp)
    97  
    98  	r.CreatedAt = time.Time(s.CreatedAt)
    99  	r.UpdatedAt = time.Time(s.UpdatedAt)
   100  
   101  	return err
   102  }
   103  
   104  type commonResult struct {
   105  	golangsdk.Result
   106  }
   107  
   108  // Extract will get the Image object out of the commonResult object.
   109  func (r commonResult) Extract() (*Image, error) {
   110  	var s Image
   111  	err := r.ExtractInto(&s)
   112  	return &s, err
   113  }
   114  
   115  // ExtractInto converts our response data into a volume struct
   116  func (r commonResult) ExtractInto(v interface{}) error {
   117  	return r.Result.ExtractIntoStructPtr(v, "images")
   118  }
   119  
   120  // ImagePage represents the results of a List request.
   121  type ImagePage struct {
   122  	serviceURL string
   123  	pagination.LinkedPageBase
   124  }
   125  
   126  // IsEmpty returns true if an ImagePage contains no Images results.
   127  func (r ImagePage) IsEmpty() (bool, error) {
   128  	images, err := ExtractImages(r)
   129  	return len(images) == 0, err
   130  }
   131  
   132  // NextPageURL uses the response's embedded link reference to navigate to
   133  // the next page of results.
   134  func (r ImagePage) NextPageURL() (string, error) {
   135  	var s struct {
   136  		Next string `json:"next"`
   137  	}
   138  	err := r.ExtractInto(&s)
   139  	if err != nil {
   140  		return "", err
   141  	}
   142  
   143  	if s.Next == "" {
   144  		return "", nil
   145  	}
   146  
   147  	return nextPageURL(r.serviceURL, s.Next)
   148  }
   149  
   150  // ExtractImages interprets the results of a single page from a List() call,
   151  // producing a slice of Image entities.
   152  func ExtractImages(r pagination.Page) ([]Image, error) {
   153  	var s struct {
   154  		Images []Image `json:"images"`
   155  	}
   156  
   157  	err := (r.(ImagePage)).ExtractInto(&s)
   158  	return s.Images, err
   159  }