github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/image/v2/imageimport/requests.go (about)

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