Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[v1] Add conductor API to Baremetal V1 #2723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions acceptance/openstack/baremetal/v1/conductors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//go:build acceptance || baremetal || conductors
// +build acceptance baremetal conductors

package v1

import (
"testing"

"github.com/gophercloud/gophercloud/acceptance/clients"
"github.com/gophercloud/gophercloud/acceptance/tools"
"github.com/gophercloud/gophercloud/openstack/baremetal/v1/conductors"
"github.com/gophercloud/gophercloud/pagination"

th "github.com/gophercloud/gophercloud/testhelper"
)

func TestConductorsListAndGet(t *testing.T) {
clients.RequireLong(t)

client, err := clients.NewBareMetalV1Client()
th.AssertNoErr(t, err)
client.Microversion = "1.49"

err = conductors.List(client, conductors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
conductorList, err := conductors.ExtractConductors(page)
if err != nil {
return false, err
}

tools.PrintResource(t, conductorList)

if len(conductorList) > 0 {
conductor, err := conductors.Get(client, conductorList[0].Hostname).Extract()
th.AssertNoErr(t, err)

tools.PrintResource(t, conductor)
}

return true, nil
})
th.AssertNoErr(t, err)
}
46 changes: 46 additions & 0 deletions openstack/baremetal/v1/conductors/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Package conductors provides information and interaction with the conductors API
resource in the OpenStack Bare Metal service.

Example to List Conductors with Detail

conductors.List(client, conductors.ListOpts{Detail: true}).EachPage(func(page pagination.Page) (bool, error) {
conductorList, err := conductors.ExtractConductors(page)
if err != nil {
return false, err
}

for _, n := range conductorList {
// Do something
}

return true, nil
})

Example to List Conductors

listOpts := conductors.ListOpts{
Fields: []string{"hostname"},
}

conductors.List(client, listOpts).EachPage(func(page pagination.Page) (bool, error) {
conductorList, err := conductors.ExtractConductors(page)
if err != nil {
return false, err
}

for _, n := range conductorList {
// Do something
}

return true, nil
})

Example to Get Conductor

showConductor, err := conductors.Get(client, "compute2.localdomain").Extract()
if err != nil {
panic(err)
}
*/
package conductors
72 changes: 72 additions & 0 deletions openstack/baremetal/v1/conductors/requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package conductors

import (
"fmt"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)

// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToConductorListQuery() (string, error)
}

// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the conductor attributes you want to see returned. Marker and Limit are used
// for pagination.
type ListOpts struct {
// One or more fields to be returned in the response.
Fields []string `q:"fields"`

// Requests a page size of items.
Limit int `q:"limit"`

// The ID of the last-seen item.
Marker string `q:"marker"`

// Sorts the response by the requested sort direction.
SortDir string `q:"sort_dir"`

// Sorts the response by the this attribute value.
SortKey string `q:"sort_key"`

// Provide additional information for the BIOS Settings
Detail bool `q:"detail"`
}

// ToConductorListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToConductorListQuery() (string, error) {
if opts.Detail == true && len(opts.Fields) > 0 {
return "", fmt.Errorf("cannot have both fields and detail options for conductors")
}

q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}

// List makes a request against the API to list conductors accessible to you.
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
url := listURL(client)
if opts != nil {
query, err := opts.ToConductorListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return ConductorPage{pagination.LinkedPageBase{PageResult: r}}
})
}

// Get requests details on a single conductor by hostname
func Get(client *gophercloud.ServiceClient, name string) (r GetResult) {
resp, err := client.Get(getURL(client, name), &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200},
})
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
return
}
93 changes: 93 additions & 0 deletions openstack/baremetal/v1/conductors/results.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package conductors

import (
"time"

"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)

type conductorResult struct {
gophercloud.Result
}

// Extract interprets any conductorResult as a Conductor, if possible.
func (r conductorResult) Extract() (*Conductor, error) {
var s Conductor
err := r.ExtractInto(&s)
return &s, err
}

func (r conductorResult) ExtractInto(v interface{}) error {
return r.Result.ExtractIntoStructPtr(v, "")
}

func ExtractConductorInto(r pagination.Page, v interface{}) error {
return r.(ConductorPage).Result.ExtractIntoSlicePtr(v, "conductors")
}

// Conductor represents a conductor in the OpenStack Bare Metal API.
type Conductor struct {
// Whether or not this Conductor is alive or not
Alive bool `json:"alive"`

// Hostname of this conductor
Hostname string `json:"hostname"`

// Array of drivers for this conductor.
Drivers []string `json:"drivers"`

// Conductor group for a conductor. Case-insensitive string up to 255 characters, containing a-z, 0-9, _, -, and ..
ConductorGroup string `json:"conductor_group"`

// The UTC date and time when the resource was created, ISO 8601 format.
CreatedAt time.Time `json:"created_at"`

// The UTC date and time when the resource was updated, ISO 8601 format. May be “null”.
UpdatedAt time.Time `json:"updated_at"`
}

// ConductorPage abstracts the raw results of making a List() request against
// the API. As OpenStack extensions may freely alter the response bodies of
// structures returned to the client, you may only safely access the data
// provided through the ExtractConductor call.
type ConductorPage struct {
pagination.LinkedPageBase
}

// IsEmpty returns true if a page contains no conductor results.
func (r ConductorPage) IsEmpty() (bool, error) {
if r.StatusCode == 204 {
return true, nil
}

s, err := ExtractConductors(r)
return len(s) == 0, err
}

// NextPageURL uses the response's embedded link reference to navigate to the
// next page of results.
func (r ConductorPage) NextPageURL() (string, error) {
var s struct {
Links []gophercloud.Link `json:"conductor_links"`
}
err := r.ExtractInto(&s)
if err != nil {
return "", err
}
return gophercloud.ExtractNextURL(s.Links)
}

// ExtractConductors interprets the results of a single page from a List() call,
// producing a slice of Conductor entities.
func ExtractConductors(r pagination.Page) ([]Conductor, error) {
var s []Conductor
err := ExtractConductorInto(r, &s)
return s, err
}

// GetResult is the response from a Get operation. Call its Extract
// method to interpret it as a Conductor.
type GetResult struct {
conductorResult
}
2 changes: 2 additions & 0 deletions openstack/baremetal/v1/conductors/testing/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// conductors unit tests
package testing
Loading
Loading