github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/images/requests.go (about) 1 package images 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/pagination" 6 ) 7 8 // ListOptsBuilder allows extensions to add additional parameters to the 9 // ListDetail request. 10 type ListOptsBuilder interface { 11 ToImageListQuery() (string, error) 12 } 13 14 // ListOpts contain options filtering Images returned from a call to ListDetail. 15 type ListOpts struct { 16 // ChangesSince filters Images based on the last changed status (in date-time 17 // format). 18 ChangesSince string `q:"changes-since"` 19 20 // Limit limits the number of Images to return. 21 Limit int `q:"limit"` 22 23 // Mark is an Image UUID at which to set a marker. 24 Marker string `q:"marker"` 25 26 // Name is the name of the Image. 27 Name string `q:"name"` 28 29 // Server is the name of the Server (in URL format). 30 Server string `q:"server"` 31 32 // Status is the current status of the Image. 33 Status string `q:"status"` 34 35 // Type is the type of image (e.g. BASE, SERVER, ALL). 36 Type string `q:"type"` 37 } 38 39 // ToImageListQuery formats a ListOpts into a query string. 40 func (opts ListOpts) ToImageListQuery() (string, error) { 41 q, err := golangsdk.BuildQueryString(opts) 42 return q.String(), err 43 } 44 45 // ListDetail enumerates the available images. 46 func ListDetail(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 47 url := listDetailURL(client) 48 if opts != nil { 49 query, err := opts.ToImageListQuery() 50 if err != nil { 51 return pagination.Pager{Err: err} 52 } 53 url += query 54 } 55 return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page { 56 return ImagePage{pagination.LinkedPageBase{PageResult: r}} 57 }) 58 } 59 60 // Get returns data about a specific image by its ID. 61 func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { 62 _, r.Err = client.Get(getURL(client, id), &r.Body, nil) 63 return 64 } 65 66 // Delete deletes the specified image ID. 67 func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { 68 _, r.Err = client.Delete(deleteURL(client, id), nil) 69 return 70 } 71 72 // IDFromName is a convienience function that returns an image's ID given its 73 // name. 74 func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) { 75 count := 0 76 id := "" 77 allPages, err := ListDetail(client, nil).AllPages() 78 if err != nil { 79 return "", err 80 } 81 82 all, err := ExtractImages(allPages) 83 if err != nil { 84 return "", err 85 } 86 87 for _, f := range all { 88 if f.Name == name { 89 count++ 90 id = f.ID 91 } 92 } 93 94 switch count { 95 case 0: 96 err := &golangsdk.ErrResourceNotFound{} 97 err.ResourceType = "image" 98 err.Name = name 99 return "", err 100 case 1: 101 return id, nil 102 default: 103 err := &golangsdk.ErrMultipleResourcesFound{} 104 err.ResourceType = "image" 105 err.Name = name 106 err.Count = count 107 return "", err 108 } 109 }