Skip to content

Commit

Permalink
Add support for create ports in bulk
Browse files Browse the repository at this point in the history
  • Loading branch information
bumblebeeK committed Nov 3, 2023
1 parent bf72530 commit e9608b0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
30 changes: 30 additions & 0 deletions openstack/networking/v2/ports/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
21 changes: 21 additions & 0 deletions openstack/networking/v2/ports/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit e9608b0

Please sign in to comment.