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

Skip to content

Commit 08f27f3

Browse files
authored
Merge pull request #2722 from dkt26111/ironic_conductors
Add conductor API to Baremetal V1
2 parents 2efc557 + 6ff19a7 commit 08f27f3

File tree

8 files changed

+553
-0
lines changed

8 files changed

+553
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
//go:build acceptance || baremetal || conductors
2+
// +build acceptance baremetal conductors
3+
4+
package v1
5+
6+
import (
7+
"testing"
8+
9+
"github.com/gophercloud/gophercloud/acceptance/clients"
10+
"github.com/gophercloud/gophercloud/acceptance/tools"
11+
"github.com/gophercloud/gophercloud/openstack/baremetal/v1/conductors"
12+
"github.com/gophercloud/gophercloud/pagination"
13+
14+
th "github.com/gophercloud/gophercloud/testhelper"
15+
)
16+
17+
func TestConductorsListAndGet(t *testing.T) {
18+
clients.RequireLong(t)
19+
20+
client, err := clients.NewBareMetalV1Client()
21+
th.AssertNoErr(t, err)
22+
client.Microversion = "1.49"
23+
24+
err = conductors.List(client, conductors.ListOpts{}).EachPage(func(page pagination.Page) (bool, error) {
25+
conductorList, err := conductors.ExtractConductors(page)
26+
if err != nil {
27+
return false, err
28+
}
29+
30+
tools.PrintResource(t, conductorList)
31+
32+
if len(conductorList) > 0 {
33+
conductor, err := conductors.Get(client, conductorList[0].Hostname).Extract()
34+
th.AssertNoErr(t, err)
35+
36+
tools.PrintResource(t, conductor)
37+
}
38+
39+
return true, nil
40+
})
41+
th.AssertNoErr(t, err)
42+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Package conductors provides information and interaction with the conductors API
3+
resource in the OpenStack Bare Metal service.
4+
5+
Example to List Conductors with Detail
6+
7+
conductors.List(client, conductors.ListOpts{Detail: true}).EachPage(func(page pagination.Page) (bool, error) {
8+
conductorList, err := conductors.ExtractConductors(page)
9+
if err != nil {
10+
return false, err
11+
}
12+
13+
for _, n := range conductorList {
14+
// Do something
15+
}
16+
17+
return true, nil
18+
})
19+
20+
Example to List Conductors
21+
22+
listOpts := conductors.ListOpts{
23+
Fields: []string{"hostname"},
24+
}
25+
26+
conductors.List(client, listOpts).EachPage(func(page pagination.Page) (bool, error) {
27+
conductorList, err := conductors.ExtractConductors(page)
28+
if err != nil {
29+
return false, err
30+
}
31+
32+
for _, n := range conductorList {
33+
// Do something
34+
}
35+
36+
return true, nil
37+
})
38+
39+
Example to Get Conductor
40+
41+
showConductor, err := conductors.Get(client, "compute2.localdomain").Extract()
42+
if err != nil {
43+
panic(err)
44+
}
45+
*/
46+
package conductors
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package conductors
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/gophercloud/gophercloud"
7+
"github.com/gophercloud/gophercloud/pagination"
8+
)
9+
10+
// ListOptsBuilder allows extensions to add additional parameters to the
11+
// List request.
12+
type ListOptsBuilder interface {
13+
ToConductorListQuery() (string, error)
14+
}
15+
16+
// ListOpts allows the filtering and sorting of paginated collections through
17+
// the API. Filtering is achieved by passing in struct field values that map to
18+
// the conductor attributes you want to see returned. Marker and Limit are used
19+
// for pagination.
20+
type ListOpts struct {
21+
// One or more fields to be returned in the response.
22+
Fields []string `q:"fields"`
23+
24+
// Requests a page size of items.
25+
Limit int `q:"limit"`
26+
27+
// The ID of the last-seen item.
28+
Marker string `q:"marker"`
29+
30+
// Sorts the response by the requested sort direction.
31+
SortDir string `q:"sort_dir"`
32+
33+
// Sorts the response by the this attribute value.
34+
SortKey string `q:"sort_key"`
35+
36+
// Provide additional information for the BIOS Settings
37+
Detail bool `q:"detail"`
38+
}
39+
40+
// ToConductorListQuery formats a ListOpts into a query string.
41+
func (opts ListOpts) ToConductorListQuery() (string, error) {
42+
if opts.Detail == true && len(opts.Fields) > 0 {
43+
return "", fmt.Errorf("cannot have both fields and detail options for conductors")
44+
}
45+
46+
q, err := gophercloud.BuildQueryString(opts)
47+
return q.String(), err
48+
}
49+
50+
// List makes a request against the API to list conductors accessible to you.
51+
func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
52+
url := listURL(client)
53+
if opts != nil {
54+
query, err := opts.ToConductorListQuery()
55+
if err != nil {
56+
return pagination.Pager{Err: err}
57+
}
58+
url += query
59+
}
60+
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
61+
return ConductorPage{pagination.LinkedPageBase{PageResult: r}}
62+
})
63+
}
64+
65+
// Get requests details on a single conductor by hostname
66+
func Get(client *gophercloud.ServiceClient, name string) (r GetResult) {
67+
resp, err := client.Get(getURL(client, name), &r.Body, &gophercloud.RequestOpts{
68+
OkCodes: []int{200},
69+
})
70+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
71+
return
72+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package conductors
2+
3+
import (
4+
"time"
5+
6+
"github.com/gophercloud/gophercloud"
7+
"github.com/gophercloud/gophercloud/pagination"
8+
)
9+
10+
type conductorResult struct {
11+
gophercloud.Result
12+
}
13+
14+
// Extract interprets any conductorResult as a Conductor, if possible.
15+
func (r conductorResult) Extract() (*Conductor, error) {
16+
var s Conductor
17+
err := r.ExtractInto(&s)
18+
return &s, err
19+
}
20+
21+
func (r conductorResult) ExtractInto(v interface{}) error {
22+
return r.Result.ExtractIntoStructPtr(v, "")
23+
}
24+
25+
func ExtractConductorInto(r pagination.Page, v interface{}) error {
26+
return r.(ConductorPage).Result.ExtractIntoSlicePtr(v, "conductors")
27+
}
28+
29+
// Conductor represents a conductor in the OpenStack Bare Metal API.
30+
type Conductor struct {
31+
// Whether or not this Conductor is alive or not
32+
Alive bool `json:"alive"`
33+
34+
// Hostname of this conductor
35+
Hostname string `json:"hostname"`
36+
37+
// Array of drivers for this conductor.
38+
Drivers []string `json:"drivers"`
39+
40+
// Conductor group for a conductor. Case-insensitive string up to 255 characters, containing a-z, 0-9, _, -, and ..
41+
ConductorGroup string `json:"conductor_group"`
42+
43+
// The UTC date and time when the resource was created, ISO 8601 format.
44+
CreatedAt time.Time `json:"created_at"`
45+
46+
// The UTC date and time when the resource was updated, ISO 8601 format. May be “null”.
47+
UpdatedAt time.Time `json:"updated_at"`
48+
}
49+
50+
// ConductorPage abstracts the raw results of making a List() request against
51+
// the API. As OpenStack extensions may freely alter the response bodies of
52+
// structures returned to the client, you may only safely access the data
53+
// provided through the ExtractConductor call.
54+
type ConductorPage struct {
55+
pagination.LinkedPageBase
56+
}
57+
58+
// IsEmpty returns true if a page contains no conductor results.
59+
func (r ConductorPage) IsEmpty() (bool, error) {
60+
if r.StatusCode == 204 {
61+
return true, nil
62+
}
63+
64+
s, err := ExtractConductors(r)
65+
return len(s) == 0, err
66+
}
67+
68+
// NextPageURL uses the response's embedded link reference to navigate to the
69+
// next page of results.
70+
func (r ConductorPage) NextPageURL() (string, error) {
71+
var s struct {
72+
Links []gophercloud.Link `json:"conductor_links"`
73+
}
74+
err := r.ExtractInto(&s)
75+
if err != nil {
76+
return "", err
77+
}
78+
return gophercloud.ExtractNextURL(s.Links)
79+
}
80+
81+
// ExtractConductors interprets the results of a single page from a List() call,
82+
// producing a slice of Conductor entities.
83+
func ExtractConductors(r pagination.Page) ([]Conductor, error) {
84+
var s []Conductor
85+
err := ExtractConductorInto(r, &s)
86+
return s, err
87+
}
88+
89+
// GetResult is the response from a Get operation. Call its Extract
90+
// method to interpret it as a Conductor.
91+
type GetResult struct {
92+
conductorResult
93+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// conductors unit tests
2+
package testing

0 commit comments

Comments
 (0)