Skip to content

Commit 9e222ec

Browse files
committed
Baremetal Inspector Client Node Introspection
For #1485 This patch adds the Node Introspection API calls according to [1] Start Introspection (POST) Get Introspection status (GET) List all Introspection statuses (GET) Abort Introspection (POST) Get Introspection Data (GET) Reapply Introspection on stored data (POST) Source code reference: https://github.com/openstack/ironic-inspector/tree/master/ironic_inspector [1] https://developer.openstack.org/api-ref/baremetal-introspection/#node-introspection
1 parent fe1ba5c commit 9e222ec

10 files changed

Lines changed: 941 additions & 0 deletions

File tree

acceptance/clients/clients.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,27 @@ func NewBareMetalV1NoAuthClient() (*gophercloud.ServiceClient, error) {
285285
})
286286
}
287287

288+
// NewBareMetalIntrospectionV1Client returns a *ServiceClient for making calls
289+
// to the OpenStack Bare Metal Introspection v1 API. An error will be returned
290+
// if authentication or client creation was not possible.
291+
func NewBareMetalIntrospectionV1Client() (*gophercloud.ServiceClient, error) {
292+
ao, err := openstack.AuthOptionsFromEnv()
293+
if err != nil {
294+
return nil, err
295+
}
296+
297+
client, err := openstack.AuthenticatedClient(ao)
298+
if err != nil {
299+
return nil, err
300+
}
301+
302+
client = configureDebug(client)
303+
304+
return openstack.NewBareMetalIntrospectionV1(client, gophercloud.EndpointOpts{
305+
Region: os.Getenv("OS_REGION_NAME"),
306+
})
307+
}
308+
288309
// NewDBV1Client returns a *ServiceClient for making calls
289310
// to the OpenStack Database v1 API. An error will be returned
290311
// if authentication or client creation was not possible.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package introspection
2+
3+
/*
4+
Package introspection contains the functionality for Starting introspection,
5+
Get introspection status, List all introspection statuses, Abort an
6+
introspection, Get stored introspection data and reapply introspection on
7+
stored data.
8+
9+
API reference https://developer.openstack.org/api-ref/baremetal-introspection/#node-introspection
10+
11+
// Example to Start Introspection
12+
introspection.StartIntrospection(client, NodeUUID, introspection.StartOpts{}).ExtractErr()
13+
14+
// Example to Get an Introspection status
15+
introspection.GetIntrospectionStatus(client, NodeUUID).Extract()
16+
if err != nil {
17+
panic(err)
18+
}
19+
20+
// Example to List all introspection statuses
21+
introspection.ListIntrospections(client.ServiceClient(), introspection.ListIntrospectionsOpts{}).EachPage(func(page pagination.Page) (bool, error) {
22+
introspectionsList, err := introspection.ExtractIntrospections(page)
23+
if err != nil {
24+
return false, err
25+
}
26+
for _, n := range introspectionsList {
27+
// Do something
28+
}
29+
return true, nil
30+
})
31+
32+
// Example to Abort an Introspection
33+
introspection.AbortIntrospection(client, NodeUUID).ExtractErr()
34+
35+
// Example to Get stored Introspection Data
36+
introspection.GetIntrospectionData(c, NodeUUID).Extract()
37+
38+
// Example to apply Introspection Data
39+
introspection.ApplyIntrospectionData(c, NodeUUID).ExtractErr()
40+
*/
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package introspection
2+
3+
import (
4+
"github.com/gophercloud/gophercloud"
5+
"github.com/gophercloud/gophercloud/pagination"
6+
)
7+
8+
// ListIntrospectionsOptsBuilder allows extensions to add additional parameters to the
9+
// ListIntrospections request.
10+
type ListIntrospectionsOptsBuilder interface {
11+
ToIntrospectionsListQuery() (string, error)
12+
}
13+
14+
// ListIntrospectionsOpts allows the filtering and sorting of paginated collections through
15+
// the Introspection API. Filtering is achieved by passing in struct field values that map to
16+
// the node attributes you want to see returned. Marker and Limit are used
17+
// for pagination.
18+
type ListIntrospectionsOpts struct {
19+
// Requests a page size of items.
20+
Limit int `q:"limit"`
21+
22+
// The ID of the last-seen item.
23+
Marker string `q:"marker"`
24+
}
25+
26+
// ToIntrospectionsListQuery formats a ListIntrospectionsOpts into a query string.
27+
func (opts ListIntrospectionsOpts) ToIntrospectionsListQuery() (string, error) {
28+
q, err := gophercloud.BuildQueryString(opts)
29+
return q.String(), err
30+
}
31+
32+
// ListIntrospections makes a request against the Inspector API to list the current introspections.
33+
func ListIntrospections(client *gophercloud.ServiceClient, opts ListIntrospectionsOptsBuilder) pagination.Pager {
34+
url := listIntrospectionsURL(client)
35+
if opts != nil {
36+
query, err := opts.ToIntrospectionsListQuery()
37+
if err != nil {
38+
return pagination.Pager{Err: err}
39+
}
40+
url += query
41+
}
42+
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
43+
var rpage = IntrospectionPage{pagination.LinkedPageBase{PageResult: r}}
44+
return rpage
45+
})
46+
}
47+
48+
// GetIntrospectionStatus makes a request against the Inspector API to get the
49+
// status of a single introspection.
50+
func GetIntrospectionStatus(client *gophercloud.ServiceClient, nodeID string) (r GetIntrospectionStatusResult) {
51+
_, r.Err = client.Get(introspectionURL(client, nodeID), &r.Body, &gophercloud.RequestOpts{
52+
OkCodes: []int{200},
53+
})
54+
return
55+
}
56+
57+
// StartOptsBuilder allows extensions to add additional parameters to the
58+
// Start request.
59+
type StartOptsBuilder interface {
60+
ToStartIntrospectionQuery() (string, error)
61+
}
62+
63+
// StartOpts represents options to start an introspection.
64+
type StartOpts struct {
65+
// Whether the current installation of ironic-inspector can manage PXE booting of nodes.
66+
ManageBoot *bool `q:"manage_boot"`
67+
}
68+
69+
// ToStartIntrospectionQuery converts a StartOpts into a request.
70+
func (opts StartOpts) ToStartIntrospectionQuery() (string, error) {
71+
q, err := gophercloud.BuildQueryString(opts)
72+
return q.String(), err
73+
}
74+
75+
// StartIntrospection initiate hardware introspection for node NodeID .
76+
// All power management configuration for this node needs to be done prior to calling the endpoint.
77+
func StartIntrospection(client *gophercloud.ServiceClient, nodeID string, opts StartOptsBuilder) (r StartResult) {
78+
_, err := opts.ToStartIntrospectionQuery()
79+
if err != nil {
80+
r.Err = err
81+
return
82+
}
83+
84+
_, r.Err = client.Post(introspectionURL(client, nodeID), nil, nil, &gophercloud.RequestOpts{
85+
OkCodes: []int{202},
86+
})
87+
88+
return
89+
}
90+
91+
// AbortIntrospection abort running introspection.
92+
func AbortIntrospection(client *gophercloud.ServiceClient, nodeID string) (r AbortResult) {
93+
_, r.Err = client.Post(abortIntrospectionURL(client, nodeID), nil, nil, &gophercloud.RequestOpts{
94+
OkCodes: []int{202},
95+
})
96+
97+
return
98+
}
99+
100+
// GetIntrospectionData return stored data from successful introspection.
101+
func GetIntrospectionData(client *gophercloud.ServiceClient, nodeID string) (r DataResult) {
102+
_, r.Err = client.Get(introspectionDataURL(client, nodeID), &r.Body, &gophercloud.RequestOpts{
103+
OkCodes: []int{200},
104+
})
105+
return
106+
}
107+
108+
// ReApplyIntrospection triggers introspection on stored unprocessed data.
109+
// No data is allowed to be sent along with the request.
110+
func ReApplyIntrospection(client *gophercloud.ServiceClient, nodeID string) (r ApplyDataResult) {
111+
_, r.Err = client.Post(introspectionUnprocessedDataURL(client, nodeID), nil, nil, &gophercloud.RequestOpts{
112+
OkCodes: []int{202},
113+
})
114+
115+
return
116+
}

0 commit comments

Comments
 (0)