From e9608b0368b0f8199c430e2cd360c49c6ba9d271 Mon Sep 17 00:00:00 2001 From: bumblebeeK <83360112+bumblebeeK@users.noreply.github.com> Date: Fri, 3 Nov 2023 14:34:08 +0800 Subject: [PATCH] Add support for create ports in bulk --- openstack/networking/v2/ports/requests.go | 30 +++++++++++++++++++++++ openstack/networking/v2/ports/results.go | 21 ++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/openstack/networking/v2/ports/requests.go b/openstack/networking/v2/ports/requests.go index 46c09e9fb..87269cda7 100644 --- a/openstack/networking/v2/ports/requests.go +++ b/openstack/networking/v2/ports/requests.go @@ -131,6 +131,36 @@ func (opts CreateOpts) ToPortCreateMap() (map[string]interface{}, error) { return gophercloud.BuildRequestBody(opts, "port") } +// BulkCreateOpts represents the attributes used when creating bulk new ports. +type BulkCreateOpts struct { + Ports []CreateOpts `json:"ports"` +} + +// BulkCreateOptsBuilder allows extensions to add additional parameters to the +// Create request. +type BulkCreateOptsBuilder interface { + ToBulkPortCreateMap() (map[string]interface{}, error) +} + +// ToBulkPortCreateMap builds a request body from BulkCreateOpts. +func (opts BulkCreateOpts) ToBulkPortCreateMap() (map[string]interface{}, error) { + return gophercloud.BuildRequestBody(opts, "") +} + +// BulkCreate accepts a BulkCreateOpts struct and creates a new network using the values +// provided. You must remember to provide a NetworkID value. +func BulkCreate(c *gophercloud.ServiceClient, opts BulkCreateOpts) (r BulkCreateResult) { + b, err := opts.ToBulkPortCreateMap() + if err != nil { + r.Err = err + return + } + + resp, err := c.Post(createURL(c), b, &r.Body, nil) + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) + return +} + // Create accepts a CreateOpts struct and creates a new network using the values // provided. You must remember to provide a NetworkID value. func Create(c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { diff --git a/openstack/networking/v2/ports/results.go b/openstack/networking/v2/ports/results.go index a39133fc0..ba6be7608 100644 --- a/openstack/networking/v2/ports/results.go +++ b/openstack/networking/v2/ports/results.go @@ -12,6 +12,21 @@ type commonResult struct { gophercloud.Result } +type bulkResult struct { + gophercloud.Result +} + +// Extract is a function that accepts a result and extracts []port resource. +func (r bulkResult) Extract() ([]Port, error) { + var s []Port + err := r.ExtractInto(&s) + return s, err +} + +func (r bulkResult) ExtractInto(v interface{}) error { + return r.Result.ExtractIntoSlicePtr(v, "ports") +} + // Extract is a function that accepts a result and extracts a port resource. func (r commonResult) Extract() (*Port, error) { var s Port @@ -29,6 +44,12 @@ type CreateResult struct { commonResult } +// BulkCreateResult represents the result of a create operation. Call its Extract +// method to interpret it as []Port. +type BulkCreateResult struct { + bulkResult +} + // GetResult represents the result of a get operation. Call its Extract // method to interpret it as a Port. type GetResult struct {