github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/evs/extensions/volumeactions/upload_image.go (about) 1 package volumeactions 2 3 import ( 4 "encoding/json" 5 "time" 6 7 "github.com/opentelekomcloud/gophertelekomcloud" 8 "github.com/opentelekomcloud/gophertelekomcloud/internal/extract" 9 ) 10 11 type UploadImageOpts struct { 12 // Container format, may be bare, ofv, ova, etc. 13 ContainerFormat string `json:"container_format,omitempty"` 14 // Disk format, may be raw, qcow2, vhd, vdi, vmdk, etc. 15 DiskFormat string `json:"disk_format,omitempty"` 16 // The name of image that will be stored in glance. 17 ImageName string `json:"image_name,omitempty"` 18 // Force image creation, usable if volume attached to instance. 19 Force bool `json:"force,omitempty"` 20 } 21 22 func UploadImage(client *golangsdk.ServiceClient, id string, opts UploadImageOpts) (*VolumeImage, error) { 23 b, err := golangsdk.BuildRequestBody(opts, "os-volume_upload_image") 24 if err != nil { 25 return nil, err 26 } 27 28 raw, err := client.Post(client.ServiceURL("volumes", id, "action"), b, nil, &golangsdk.RequestOpts{ 29 OkCodes: []int{202}, 30 }) 31 if err != nil { 32 return nil, err 33 } 34 35 var res struct { 36 VolumeImage VolumeImage `json:"os-volume_upload_image"` 37 } 38 err = extract.Into(raw.Body, &res) 39 return &res.VolumeImage, err 40 } 41 42 type VolumeImage struct { 43 // The ID of a volume an image is created from. 44 VolumeID string `json:"id"` 45 // Container format, may be bare, ofv, ova, etc. 46 ContainerFormat string `json:"container_format"` 47 // Disk format, may be raw, qcow2, vhd, vdi, vmdk, etc. 48 DiskFormat string `json:"disk_format"` 49 // Human-readable description for the volume. 50 Description string `json:"display_description"` 51 // The ID of the created image. 52 ImageID string `json:"image_id"` 53 // Human-readable display name for the image. 54 ImageName string `json:"image_name"` 55 // Size of the volume in GB. 56 Size int `json:"size"` 57 // Current status of the volume. 58 Status string `json:"status"` 59 // The date when this volume was last updated. 60 UpdatedAt time.Time `json:"-"` 61 // Volume type object of used volume. 62 VolumeType ImageVolumeType `json:"volume_type"` 63 } 64 65 func (r *VolumeImage) UnmarshalJSON(b []byte) error { 66 type tmp VolumeImage 67 var s struct { 68 tmp 69 UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"` 70 } 71 err := json.Unmarshal(b, &s) 72 if err != nil { 73 return err 74 } 75 *r = VolumeImage(s.tmp) 76 77 r.UpdatedAt = time.Time(s.UpdatedAt) 78 return err 79 } 80 81 type ImageVolumeType struct { 82 // The ID of a volume type. 83 ID string `json:"id"` 84 // Human-readable display name for the volume type. 85 Name string `json:"name"` 86 // Human-readable description for the volume type. 87 Description string `json:"display_description"` 88 // Flag for public access. 89 IsPublic bool `json:"is_public"` 90 // Extra specifications for volume type. 91 ExtraSpecs map[string]interface{} `json:"extra_specs"` 92 // ID of quality of service specs. 93 QosSpecsID string `json:"qos_specs_id"` 94 // Flag for deletion status of volume type. 95 Deleted bool `json:"deleted"` 96 // The date when volume type was deleted. 97 DeletedAt time.Time `json:"-"` 98 // The date when volume type was created. 99 CreatedAt time.Time `json:"-"` 100 // The date when this volume was last updated. 101 UpdatedAt time.Time `json:"-"` 102 } 103 104 func (r *ImageVolumeType) UnmarshalJSON(b []byte) error { 105 type tmp ImageVolumeType 106 var s struct { 107 tmp 108 CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"` 109 UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"` 110 DeletedAt golangsdk.JSONRFC3339MilliNoZ `json:"deleted_at"` 111 } 112 err := json.Unmarshal(b, &s) 113 if err != nil { 114 return err 115 } 116 *r = ImageVolumeType(s.tmp) 117 118 r.CreatedAt = time.Time(s.CreatedAt) 119 r.UpdatedAt = time.Time(s.UpdatedAt) 120 r.DeletedAt = time.Time(s.DeletedAt) 121 122 return err 123 }