From c0d33b4f690cc8d25914545738de0b60fbc33855 Mon Sep 17 00:00:00 2001 From: Himanshu Roy Date: Mon, 3 Feb 2025 19:04:34 +0530 Subject: [PATCH 1/3] add support to get virtual media for a node --- openstack/baremetal/v1/nodes/requests.go | 12 ++++++++++++ openstack/baremetal/v1/nodes/results.go | 5 +++++ 2 files changed, 17 insertions(+) diff --git a/openstack/baremetal/v1/nodes/requests.go b/openstack/baremetal/v1/nodes/requests.go index cddd269a3d..6268f5037e 100644 --- a/openstack/baremetal/v1/nodes/requests.go +++ b/openstack/baremetal/v1/nodes/requests.go @@ -1003,6 +1003,18 @@ func DetachVirtualMedia(ctx context.Context, client *gophercloud.ServiceClient, return } +// Request the list of virtual media devices attached to the Node. +// Requires microversion 1.93 or later. +func GetVirtualMedia(ctx context.Context, client *gophercloud.ServiceClient, id string) (r VirtualMediaGetResult) { + + resp, err := client.Get(ctx, virtualMediaURL(client, id), &r.Body, &gophercloud.RequestOpts{ + OkCodes: []int{200}, + }) + + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) + return +} + // VirtualInterfaceOpts defines options for attaching a VIF to a node type VirtualInterfaceOpts struct { // The UUID or name of the VIF diff --git a/openstack/baremetal/v1/nodes/results.go b/openstack/baremetal/v1/nodes/results.go index 3e1976d3ef..b33517a388 100644 --- a/openstack/baremetal/v1/nodes/results.go +++ b/openstack/baremetal/v1/nodes/results.go @@ -711,6 +711,11 @@ type VirtualMediaDetachResult struct { gophercloud.ErrResult } +// Requires microversion 1.93 or later. +type VirtualMediaGetResult struct { + gophercloud.Result +} + // VirtualInterfaceAttachResult is the response from an AttachVirtualInterface operation. type VirtualInterfaceAttachResult struct { gophercloud.ErrResult From 78032669bb51c54165e6f7e56f4d833e460e1f9a Mon Sep 17 00:00:00 2001 From: Himanshu Roy Date: Fri, 14 Feb 2025 17:19:45 +0530 Subject: [PATCH 2/3] virtual media get api tests Signed-off-by: Himanshu Roy --- .../openstack/baremetal/v1/nodes_test.go | 5 ++- .../v1/nodes/testing/fixtures_test.go | 35 +++++++++++++++++++ .../v1/nodes/testing/requests_test.go | 20 +++++++++++ 3 files changed, 59 insertions(+), 1 deletion(-) diff --git a/internal/acceptance/openstack/baremetal/v1/nodes_test.go b/internal/acceptance/openstack/baremetal/v1/nodes_test.go index 1151135e62..a4865783a1 100644 --- a/internal/acceptance/openstack/baremetal/v1/nodes_test.go +++ b/internal/acceptance/openstack/baremetal/v1/nodes_test.go @@ -219,7 +219,7 @@ func TestNodesVirtualMedia(t *testing.T) { client, err := clients.NewBareMetalV1Client() th.AssertNoErr(t, err) - client.Microversion = "1.89" + client.Microversion = "1.93" node, err := CreateNode(t, client) th.AssertNoErr(t, err) @@ -241,6 +241,9 @@ func TestNodesVirtualMedia(t *testing.T) { err = nodes.DetachVirtualMedia(context.TODO(), client, node.UUID, nodes.DetachVirtualMediaOpts{}).ExtractErr() th.AssertNoErr(t, err) + + err = nodes.GetVirtualMedia(context.TODO(), client, node.UUID).Err + th.AssertNoErr(t, err) } func TestNodesServicingHold(t *testing.T) { diff --git a/openstack/baremetal/v1/nodes/testing/fixtures_test.go b/openstack/baremetal/v1/nodes/testing/fixtures_test.go index 282d5dbc7f..371a4f00a9 100644 --- a/openstack/baremetal/v1/nodes/testing/fixtures_test.go +++ b/openstack/baremetal/v1/nodes/testing/fixtures_test.go @@ -900,6 +900,28 @@ const NodeVirtualMediaAttachBodyWithSource = ` } ` +const NodeVirtualMediaGetBodyAttached = ` +{ + "image": "https://example.com/image", + "inserted": true, + "media_types": [ + "CD", + "DVD" + ] +} +` + +const NodeVirtualMediaGetBodyNotAttached = ` +{ + "image": "", + "inserted": false, + "media_types": [ + "CD", + "DVD" + ] +} +` + var ( createdAtFoo, _ = time.Parse(time.RFC3339, "2019-01-31T19:59:28+00:00") createdAtBar, _ = time.Parse(time.RFC3339, "2019-01-31T19:59:29+00:00") @@ -1850,6 +1872,19 @@ func HandleDetachVirtualMediaSuccessfully(t *testing.T, withType bool) { }) } +func HandleGetVirtualMediaSuccessfully(t *testing.T, attached bool) { + th.Mux.HandleFunc("/nodes/1234asdf/vmedia", func(w http.ResponseWriter, r *http.Request) { + th.TestMethod(t, r, "GET") + th.TestHeader(t, r, "X-Auth-Token", client.TokenID) + w.WriteHeader(http.StatusOK) + if attached { + fmt.Fprint(w, NodeVirtualMediaGetBodyAttached) + } else { + fmt.Fprint(w, NodeVirtualMediaGetBodyNotAttached) + } + }) +} + // HandleListVirtualInterfacesSuccessfully sets up the test server to respond to a ListVirtualInterfaces request func HandleListVirtualInterfacesSuccessfully(t *testing.T) { th.Mux.HandleFunc("/nodes/1234asdf/vifs", diff --git a/openstack/baremetal/v1/nodes/testing/requests_test.go b/openstack/baremetal/v1/nodes/testing/requests_test.go index 487c350175..24452be0c2 100644 --- a/openstack/baremetal/v1/nodes/testing/requests_test.go +++ b/openstack/baremetal/v1/nodes/testing/requests_test.go @@ -830,6 +830,26 @@ func TestVirtualMediaDetachWithTypes(t *testing.T) { th.AssertNoErr(t, err) } +func TestVirtualMediaGetAttached(t *testing.T) { + th.SetupHTTP() + defer th.TeardownHTTP() + HandleGetVirtualMediaSuccessfully(t, true) + + c := client.ServiceClient() + err := nodes.GetVirtualMedia(context.TODO(), c, "1234asdf").Err + th.AssertNoErr(t, err) +} + +func TestVirtualMediaGetNotAttached(t *testing.T) { + th.SetupHTTP() + defer th.TeardownHTTP() + HandleGetVirtualMediaSuccessfully(t, false) + + c := client.ServiceClient() + err := nodes.GetVirtualMedia(context.TODO(), c, "1234asdf").Err + th.AssertNoErr(t, err) +} + func TestListVirtualInterfaces(t *testing.T) { th.SetupHTTP() defer th.TeardownHTTP() From 6751a7a3b16050b5cbf82cce8e8cfe9ce633cd4a Mon Sep 17 00:00:00 2001 From: Himanshu Roy Date: Fri, 7 Mar 2025 10:44:15 +0530 Subject: [PATCH 3/3] ignore error for vmedia get api for ipmi driver Signed-off-by: Himanshu Roy --- internal/acceptance/openstack/baremetal/v1/nodes_test.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/internal/acceptance/openstack/baremetal/v1/nodes_test.go b/internal/acceptance/openstack/baremetal/v1/nodes_test.go index a4865783a1..be48ee9197 100644 --- a/internal/acceptance/openstack/baremetal/v1/nodes_test.go +++ b/internal/acceptance/openstack/baremetal/v1/nodes_test.go @@ -243,7 +243,13 @@ func TestNodesVirtualMedia(t *testing.T) { th.AssertNoErr(t, err) err = nodes.GetVirtualMedia(context.TODO(), client, node.UUID).Err - th.AssertNoErr(t, err) + // Since Virtual Media GET api call is synchronous, we get a HTTP 400 + // response as CreateNode has ipmi driver hardcoded, but the api is + // only supported by the redfish driver + // (TODO: hroyrh) fix this once redfish driver is used in the tests + if node.Driver == "redfish" { + th.AssertNoErr(t, err) + } } func TestNodesServicingHold(t *testing.T) {