Skip to content

Commit d4f6ef7

Browse files
committed
baremetal: introduce Node Inventory API
The plugin-specific part of the API gets a more sophisticated handling than it was the case for Inspector: a new opaque type PluginData is used for handling different formats. The Ironic-native plugin data format will be introduced later after some more preparation work is done. Part of #2612
1 parent 117d6fc commit d4f6ef7

5 files changed

Lines changed: 104 additions & 0 deletions

File tree

openstack/baremetal/v1/nodes/requests.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,3 +880,12 @@ func UnsetMaintenance(client *gophercloud.ServiceClient, id string) (r SetMainte
880880
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
881881
return
882882
}
883+
884+
// GetInventory return stored data from successful inspection.
885+
func GetInventory(client *gophercloud.ServiceClient, id string) (r InventoryResult) {
886+
resp, err := client.Get(inventoryURL(client, id), &r.Body, &gophercloud.RequestOpts{
887+
OkCodes: []int{200},
888+
})
889+
_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
890+
return
891+
}

openstack/baremetal/v1/nodes/results.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package nodes
22

33
import (
4+
"encoding/json"
45
"time"
56

67
"github.com/gophercloud/gophercloud"
8+
"github.com/gophercloud/gophercloud/openstack/baremetal/inventory"
9+
"github.com/gophercloud/gophercloud/openstack/baremetalintrospection/v1/introspection"
710
"github.com/gophercloud/gophercloud/pagination"
811
)
912

@@ -528,3 +531,43 @@ type SubscriptionVendorPassthru struct {
528531
type SetMaintenanceResult struct {
529532
gophercloud.ErrResult
530533
}
534+
535+
// PluginData is an abstraction around plugin-specific data from inspection.
536+
// The format of PluginData is different between ironic-inspector and the native in-band inspection in Ironic.
537+
// We may need an opaque structure that can be extracted in two (or more) ways.
538+
type PluginData struct {
539+
// Raw JSON data.
540+
json.RawMessage
541+
}
542+
543+
// Interpret plugin data as a free-form mapping.
544+
func (pd PluginData) AsMap() (result map[string]interface{}, err error) {
545+
err = json.Unmarshal(pd.RawMessage, &result)
546+
return
547+
}
548+
549+
// Interpret plugin data as coming from ironic-inspector.
550+
func (pd PluginData) AsInspectorData() (result introspection.Data, err error) {
551+
err = json.Unmarshal(pd.RawMessage, &result)
552+
return
553+
}
554+
555+
// InventoryData is the full node inventory.
556+
type InventoryData struct {
557+
// Formally specified bare metal node inventory.
558+
Inventory inventory.InventoryType `json:"inventory"`
559+
// Data from inspection plugins.
560+
PluginData PluginData `json:"plugin_data"`
561+
}
562+
563+
// InventoryResult is the response from a GetInventory operation.
564+
type InventoryResult struct {
565+
gophercloud.Result
566+
}
567+
568+
// Extract interprets a InventoryResult as a InventoryData struct, if possible.
569+
func (r InventoryResult) Extract() (*InventoryData, error) {
570+
var data InventoryData
571+
err := r.ExtractInto(&data)
572+
return &data, err
573+
}

openstack/baremetal/v1/nodes/testing/fixtures.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"testing"
77
"time"
88

9+
inventorytest "github.com/gophercloud/gophercloud/openstack/baremetal/inventory/testing"
910
"github.com/gophercloud/gophercloud/openstack/baremetal/v1/nodes"
1011
th "github.com/gophercloud/gophercloud/testhelper"
1112
"github.com/gophercloud/gophercloud/testhelper/client"
@@ -813,6 +814,20 @@ const NodeSetMaintenanceBody = `
813814
}
814815
`
815816

817+
var NodeInventoryBody = fmt.Sprintf(`
818+
{
819+
"inventory": %s,
820+
"plugin_data":{
821+
"macs":[
822+
"52:54:00:90:35:d6"
823+
],
824+
"local_gb":10,
825+
"cpu_arch":"x86_64",
826+
"memory_mb":2048
827+
}
828+
}
829+
`, inventorytest.InventorySample)
830+
816831
var (
817832
createdAtFoo, _ = time.Parse(time.RFC3339, "2019-01-31T19:59:28+00:00")
818833
createdAtBar, _ = time.Parse(time.RFC3339, "2019-01-31T19:59:29+00:00")
@@ -1173,6 +1188,10 @@ var (
11731188
EventTypes: []string{"Alert"},
11741189
Protocol: "Redfish",
11751190
}
1191+
1192+
NodeInventoryData = nodes.InventoryData{
1193+
Inventory: inventorytest.Inventory,
1194+
}
11761195
)
11771196

11781197
// HandleNodeListSuccessfully sets up the test server to respond to a server List request.
@@ -1568,3 +1587,13 @@ func HandleUnsetNodeMaintenanceSuccessfully(t *testing.T) {
15681587
w.WriteHeader(http.StatusAccepted)
15691588
})
15701589
}
1590+
1591+
// HandleGetInventorySuccessfully sets up the test server to respond to a get inventory request for a node
1592+
func HandleGetInventorySuccessfully(t *testing.T) {
1593+
th.Mux.HandleFunc("/nodes/1234asdf/inventory", func(w http.ResponseWriter, r *http.Request) {
1594+
th.TestMethod(t, r, "GET")
1595+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
1596+
w.WriteHeader(http.StatusOK)
1597+
fmt.Fprintf(w, NodeInventoryBody)
1598+
})
1599+
}

openstack/baremetal/v1/nodes/testing/requests_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,3 +711,22 @@ func TestUnsetMaintenance(t *testing.T) {
711711
err := nodes.UnsetMaintenance(c, "1234asdf").ExtractErr()
712712
th.AssertNoErr(t, err)
713713
}
714+
715+
func TestGetInventory(t *testing.T) {
716+
th.SetupHTTP()
717+
defer th.TeardownHTTP()
718+
HandleGetInventorySuccessfully(t)
719+
720+
c := client.ServiceClient()
721+
actual, err := nodes.GetInventory(c, "1234asdf").Extract()
722+
th.AssertNoErr(t, err)
723+
th.CheckDeepEquals(t, NodeInventoryData.Inventory, actual.Inventory)
724+
725+
pluginData, err := actual.PluginData.AsMap()
726+
th.AssertNoErr(t, err)
727+
th.AssertEquals(t, "x86_64", pluginData["cpu_arch"].(string))
728+
729+
compatData, err := actual.PluginData.AsInspectorData()
730+
th.AssertNoErr(t, err)
731+
th.AssertEquals(t, "x86_64", compatData.CPUArch)
732+
}

openstack/baremetal/v1/nodes/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,7 @@ func vendorPassthruCallURL(client *gophercloud.ServiceClient, id string) string
7777
func maintenanceURL(client *gophercloud.ServiceClient, id string) string {
7878
return client.ServiceURL("nodes", id, "maintenance")
7979
}
80+
81+
func inventoryURL(client *gophercloud.ServiceClient, id string) string {
82+
return client.ServiceURL("nodes", id, "inventory")
83+
}

0 commit comments

Comments
 (0)