github.com/gophercloud/gophercloud@v1.11.0/openstack/imageservice/v2/imageimport/requests.go (about) 1 package imageimport 2 3 import "github.com/gophercloud/gophercloud" 4 5 // ImportMethod represents valid Import API method. 6 type ImportMethod string 7 8 const ( 9 // GlanceDirectMethod represents glance-direct Import API method. 10 GlanceDirectMethod ImportMethod = "glance-direct" 11 12 // WebDownloadMethod represents web-download Import API method. 13 WebDownloadMethod ImportMethod = "web-download" 14 ) 15 16 // Get retrieves Import API information data. 17 func Get(c *gophercloud.ServiceClient) (r GetResult) { 18 resp, err := c.Get(infoURL(c), &r.Body, nil) 19 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 20 return 21 } 22 23 // CreateOptsBuilder allows to add additional parameters to the Create request. 24 type CreateOptsBuilder interface { 25 ToImportCreateMap() (map[string]interface{}, error) 26 } 27 28 // CreateOpts specifies parameters of a new image import. 29 type CreateOpts struct { 30 Name ImportMethod `json:"name"` 31 URI string `json:"uri"` 32 } 33 34 // ToImportCreateMap constructs a request body from CreateOpts. 35 func (opts CreateOpts) ToImportCreateMap() (map[string]interface{}, error) { 36 b, err := gophercloud.BuildRequestBody(opts, "") 37 if err != nil { 38 return nil, err 39 } 40 return map[string]interface{}{"method": b}, nil 41 } 42 43 // Create requests the creation of a new image import on the server. 44 func Create(client *gophercloud.ServiceClient, imageID string, opts CreateOptsBuilder) (r CreateResult) { 45 b, err := opts.ToImportCreateMap() 46 if err != nil { 47 r.Err = err 48 return 49 } 50 resp, err := client.Post(importURL(client, imageID), b, nil, &gophercloud.RequestOpts{ 51 OkCodes: []int{202}, 52 }) 53 _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) 54 return 55 }