github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/ims/v2/cloudimages/requests.go (about) 1 package cloudimages 2 3 import ( 4 "fmt" 5 "net/url" 6 "time" 7 8 "github.com/huaweicloud/golangsdk" 9 "github.com/huaweicloud/golangsdk/pagination" 10 ) 11 12 type ListOptsBuilder interface { 13 ToImageListQuery() (string, error) 14 } 15 16 type ListOpts struct { 17 Isregistered string `q:"__isregistered"` 18 Imagetype string `q:"__imagetype"` 19 Protected bool `q:"protected"` 20 Visibility string `q:"visibility"` 21 Owner string `q:"owner"` 22 ID string `q:"id"` 23 Status string `q:"status"` 24 Name string `q:"name"` 25 ContainerFormat string `q:"container_format"` 26 DiskFormat string `q:"disk_format"` 27 MinRam int `q:"min_ram"` 28 MinDisk int `q:"min_disk"` 29 OsBit string `q:"__os_bit"` 30 Platform string `q:"__platform"` 31 Marker string `q:"marker"` 32 Limit int `q:"limit"` 33 SortKey string `q:"sort_key"` 34 SortDir string `q:"sort_dir"` 35 OsType string `q:"__os_type"` 36 Tag string `q:"tag"` 37 MemberStatus string `q:"member_status"` 38 SupportKvm string `q:"__support_kvm"` 39 SupportXen string `q:"__support_xen"` 40 SupportLargeMemory string `q:"__support_largememory"` 41 SupportDiskintensive string `q:"__support_diskintensive"` 42 SupportHighperformance string `q:"__support_highperformance"` 43 SupportXenGpuType string `q:"__support_xen_gpu_type"` 44 SupportKvmGpuType string `q:"__support_kvm_gpu_type"` 45 SupportXenHana string `q:"__support_xen_hana"` 46 SupportKvmInfiniband string `q:"__support_kvm_infiniband"` 47 VirtualEnvType string `q:"virtual_env_type"` 48 // CreatedAtQuery filters images based on their creation date. 49 CreatedAtQuery *ImageDateQuery 50 // UpdatedAtQuery filters images based on their updated date. 51 UpdatedAtQuery *ImageDateQuery 52 } 53 54 // ImageDateFilter represents a valid filter to use for filtering 55 // images by their date during a List. 56 type ImageDateFilter string 57 58 const ( 59 FilterGT ImageDateFilter = "gt" 60 FilterGTE ImageDateFilter = "gte" 61 FilterLT ImageDateFilter = "lt" 62 FilterLTE ImageDateFilter = "lte" 63 FilterNEQ ImageDateFilter = "neq" 64 FilterEQ ImageDateFilter = "eq" 65 ) 66 67 // ImageDateQuery represents a date field to be used for listing images. 68 // If no filter is specified, the query will act as though FilterEQ was 69 // set. 70 type ImageDateQuery struct { 71 Date time.Time 72 Filter ImageDateFilter 73 } 74 75 // ToImageListQuery formats a ListOpts into a query string. 76 func (opts ListOpts) ToImageListQuery() (string, error) { 77 q, err := golangsdk.BuildQueryString(opts) 78 params := q.Query() 79 80 if opts.CreatedAtQuery != nil { 81 createdAt := opts.CreatedAtQuery.Date.Format(time.RFC3339) 82 if v := opts.CreatedAtQuery.Filter; v != "" { 83 createdAt = fmt.Sprintf("%s:%s", v, createdAt) 84 } 85 params.Add("created_at", createdAt) 86 } 87 88 if opts.UpdatedAtQuery != nil { 89 updatedAt := opts.UpdatedAtQuery.Date.Format(time.RFC3339) 90 if v := opts.UpdatedAtQuery.Filter; v != "" { 91 updatedAt = fmt.Sprintf("%s:%s", v, updatedAt) 92 } 93 params.Add("updated_at", updatedAt) 94 } 95 96 q = &url.URL{RawQuery: params.Encode()} 97 return q.String(), err 98 } 99 100 // List implements images list request 101 func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 102 url := listURL(client) 103 if opts != nil { 104 query, err := opts.ToImageListQuery() 105 if err != nil { 106 return pagination.Pager{Err: err} 107 } 108 url += query 109 } 110 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 111 imagePage := ImagePage{ 112 serviceURL: client.ServiceURL(), 113 LinkedPageBase: pagination.LinkedPageBase{PageResult: r}, 114 } 115 116 return imagePage 117 }) 118 } 119 120 // CreateOptsBuilder allows extensions to add parameters to the Create request. 121 type CreateOptsBuilder interface { 122 // Returns value that can be passed to json.Marshal 123 ToImageCreateMap() (map[string]interface{}, error) 124 } 125 126 // CreateOpts represents options used to create an image. 127 type CreateByServerOpts struct { 128 // the name of the system disk image 129 Name string `json:"name" required:"true"` 130 // Description of the image 131 Description string `json:"description,omitempty"` 132 // server id to be converted 133 InstanceId string `json:"instance_id" required:"true"` 134 // the data disks to be converted 135 DataImages []DataImage `json:"data_images,omitempty"` 136 // image label "key.value" 137 Tags []string `json:"tags,omitempty"` 138 // One or more tag key and value pairs to associate with the image 139 ImageTags []ImageTag `json:"image_tags,omitempty"` 140 // the maximum memory of the image in the unit of MB 141 MaxRam int `json:"max_ram,omitempty"` 142 // the minimum memory of the image in the unit of MB 143 MinRam int `json:"min_ram,omitempty"` 144 // Enterprise project ID 145 EnterpriseProjectID string `json:"enterprise_project_id,omitempty"` 146 } 147 148 // CreateOpts represents options used to create an image. 149 type CreateByOBSOpts struct { 150 // the name of the system disk image 151 Name string `json:"name" required:"true"` 152 // Description of image 153 Description string `json:"description,omitempty"` 154 // the OS version 155 OsVersion string `json:"os_version,omitempty"` 156 // the URL of the external image file in the OBS bucket 157 ImageUrl string `json:"image_url" required:"true"` 158 // the minimum size of the system disk in the unit of GB 159 MinDisk int `json:"min_disk" required:"true"` 160 //whether automatic configuration is enabled,the value can be true or false 161 IsConfig bool `json:"is_config,omitempty"` 162 // the master key used for encrypting an image 163 CmkId string `json:"cmk_id,omitempty"` 164 // image label "key.value" 165 Tags []string `json:"tags,omitempty"` 166 // One or more tag key and value pairs to associate with the image 167 ImageTags []ImageTag `json:"image_tags,omitempty"` 168 // the image type, the value can be ECS,BMS,FusionCompute, or Ironic 169 Type string `json:"type,omitempty"` 170 // the maximum memory of the image in the unit of MB 171 MaxRam int `json:"max_ram,omitempty"` 172 // the minimum memory of the image in the unit of MB 173 MinRam int `json:"min_ram,omitempty"` 174 // Enterprise project ID 175 EnterpriseProjectID string `json:"enterprise_project_id,omitempty"` 176 } 177 178 // CreateOpts represents options used to create an image. 179 type CreateDataImageByServerOpts struct { 180 // the data disks to be converted 181 DataImages []DataImage `json:"data_images" required:"true"` 182 } 183 184 // CreateOpts represents options used to create an image. 185 type CreateDataImageByOBSOpts struct { 186 // the name of the data disk image 187 Name string `json:"name" required:"true"` 188 // Description of image 189 Description string `json:"description,omitempty"` 190 // the OS type 191 OsType string `json:"os_type" required:"true"` 192 // the URL of the external image file in the OBS bucket 193 ImageUrl string `json:"image_url" required:"true"` 194 // the minimum size of the system disk in the unit of GB 195 MinDisk int `json:"min_disk" required:"true"` 196 // the master key used for encrypting an image 197 CmkId string `json:"cmk_id,omitempty"` 198 // Enterprise project ID 199 EnterpriseProjectID string `json:"enterprise_project_id,omitempty"` 200 } 201 202 type DataImage struct { 203 // the data disk image name 204 Name string `json:"name" required:"true"` 205 // the data disk ID 206 VolumeId string `json:"volume_id" required:"true"` 207 // information about the data disk 208 Description string `json:"description,omitempty"` 209 // the data disk image tags 210 Tags []string `json:"tags,omitempty"` 211 } 212 213 type ImageTag struct { 214 Key string `json:"key" required:"true"` 215 Value string `json:"value,omitempty"` 216 } 217 218 // ToImageCreateMap assembles a request body based on the contents of 219 // a CreateByServerOpts. 220 func (opts CreateByServerOpts) ToImageCreateMap() (map[string]interface{}, error) { 221 return golangsdk.BuildRequestBody(opts, "") 222 } 223 224 func (opts CreateByOBSOpts) ToImageCreateMap() (map[string]interface{}, error) { 225 return golangsdk.BuildRequestBody(opts, "") 226 } 227 228 func (opts CreateDataImageByServerOpts) ToImageCreateMap() (map[string]interface{}, error) { 229 return golangsdk.BuildRequestBody(opts, "") 230 } 231 232 func (opts CreateDataImageByOBSOpts) ToImageCreateMap() (map[string]interface{}, error) { 233 return golangsdk.BuildRequestBody(opts, "") 234 } 235 236 // Create implements create image request. 237 func CreateImageByServer(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { 238 b, err := opts.ToImageCreateMap() 239 if err != nil { 240 r.Err = err 241 return 242 } 243 244 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 245 return 246 } 247 248 // Create implements create image request. 249 func CreateImageByOBS(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { 250 b, err := opts.ToImageCreateMap() 251 if err != nil { 252 r.Err = err 253 return 254 } 255 256 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 257 return 258 } 259 260 // Create implements create image request. 261 func CreateDataImageByServer(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { 262 b, err := opts.ToImageCreateMap() 263 if err != nil { 264 r.Err = err 265 return 266 } 267 268 _, r.Err = client.Post(createURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 269 return 270 } 271 272 // Create implements create image request. 273 func CreateDataImageByOBS(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r JobResult) { 274 b, err := opts.ToImageCreateMap() 275 if err != nil { 276 r.Err = err 277 return 278 } 279 280 _, r.Err = client.Post(createDataImageURL(client), b, &r.Body, &golangsdk.RequestOpts{OkCodes: []int{200}}) 281 return 282 }