github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/internal/acceptance/openstack/baremetal/v1/baremetal.go (about)

     1  package v1
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/vnpaycloud-console/gophercloud/v2"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/internal/acceptance/tools"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/baremetal/v1/allocations"
    11  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/baremetal/v1/nodes"
    12  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/baremetal/v1/ports"
    13  )
    14  
    15  // CreateNode creates a basic node with a randomly generated name.
    16  func CreateNode(t *testing.T, client *gophercloud.ServiceClient) (*nodes.Node, error) {
    17  	name := tools.RandomString("ACPTTEST", 16)
    18  	t.Logf("Attempting to create bare metal node: %s", name)
    19  
    20  	node, err := nodes.Create(context.TODO(), client, nodes.CreateOpts{
    21  		Name:          name,
    22  		Driver:        "ipmi",
    23  		BootInterface: "ipxe",
    24  		RAIDInterface: "agent",
    25  		DriverInfo: map[string]any{
    26  			"ipmi_port":      "6230",
    27  			"ipmi_username":  "admin",
    28  			"deploy_kernel":  "http://172.22.0.1/images/tinyipa-stable-rocky.vmlinuz",
    29  			"ipmi_address":   "192.168.122.1",
    30  			"deploy_ramdisk": "http://172.22.0.1/images/tinyipa-stable-rocky.gz",
    31  			"ipmi_password":  "admin",
    32  		},
    33  	}).Extract()
    34  
    35  	return node, err
    36  }
    37  
    38  // DeleteNode deletes a bare metal node via its UUID.
    39  func DeleteNode(t *testing.T, client *gophercloud.ServiceClient, node *nodes.Node) {
    40  	// Force deletion of provisioned nodes requires maintenance mode.
    41  	err := nodes.SetMaintenance(context.TODO(), client, node.UUID, nodes.MaintenanceOpts{
    42  		Reason: "forced deletion",
    43  	}).ExtractErr()
    44  	if err != nil {
    45  		t.Fatalf("Unable to move node %s into maintenance mode: %s", node.UUID, err)
    46  	}
    47  
    48  	err = nodes.Delete(context.TODO(), client, node.UUID).ExtractErr()
    49  	if err != nil {
    50  		t.Fatalf("Unable to delete node %s: %s", node.UUID, err)
    51  	}
    52  
    53  	t.Logf("Deleted server: %s", node.UUID)
    54  }
    55  
    56  // CreateAllocation creates an allocation
    57  func CreateAllocation(t *testing.T, client *gophercloud.ServiceClient) (*allocations.Allocation, error) {
    58  	name := tools.RandomString("ACPTTEST", 16)
    59  	t.Logf("Attempting to create bare metal allocation: %s", name)
    60  
    61  	allocation, err := allocations.Create(context.TODO(), client, allocations.CreateOpts{
    62  		Name:          name,
    63  		ResourceClass: "baremetal",
    64  	}).Extract()
    65  
    66  	return allocation, err
    67  }
    68  
    69  // DeleteAllocation deletes a bare metal allocation via its UUID.
    70  func DeleteAllocation(t *testing.T, client *gophercloud.ServiceClient, allocation *allocations.Allocation) {
    71  	err := allocations.Delete(context.TODO(), client, allocation.UUID).ExtractErr()
    72  	if err != nil {
    73  		t.Fatalf("Unable to delete allocation %s: %s", allocation.UUID, err)
    74  	}
    75  
    76  	t.Logf("Deleted allocation: %s", allocation.UUID)
    77  }
    78  
    79  // CreateFakeNode creates a node with fake-hardware.
    80  func CreateFakeNode(t *testing.T, client *gophercloud.ServiceClient) (*nodes.Node, error) {
    81  	name := tools.RandomString("ACPTTEST", 16)
    82  	t.Logf("Attempting to create bare metal node: %s", name)
    83  
    84  	node, err := nodes.Create(context.TODO(), client, nodes.CreateOpts{
    85  		Name:            name,
    86  		Driver:          "fake-hardware",
    87  		BootInterface:   "fake",
    88  		DeployInterface: "fake",
    89  		DriverInfo: map[string]any{
    90  			"ipmi_port":      "6230",
    91  			"ipmi_username":  "admin",
    92  			"deploy_kernel":  "http://172.22.0.1/images/tinyipa-stable-rocky.vmlinuz",
    93  			"ipmi_address":   "192.168.122.1",
    94  			"deploy_ramdisk": "http://172.22.0.1/images/tinyipa-stable-rocky.gz",
    95  			"ipmi_password":  "admin",
    96  		},
    97  	}).Extract()
    98  
    99  	return node, err
   100  }
   101  
   102  func ChangeProvisionStateAndWait(ctx context.Context, client *gophercloud.ServiceClient, node *nodes.Node,
   103  	change nodes.ProvisionStateOpts, expectedState nodes.ProvisionState) (*nodes.Node, error) {
   104  	err := nodes.ChangeProvisionState(ctx, client, node.UUID, change).ExtractErr()
   105  	if err != nil {
   106  		return node, err
   107  	}
   108  
   109  	err = nodes.WaitForProvisionState(ctx, client, node.UUID, expectedState)
   110  	if err != nil {
   111  		return node, err
   112  	}
   113  
   114  	return nodes.Get(ctx, client, node.UUID).Extract()
   115  }
   116  
   117  // DeployFakeNode deploys a node that uses fake-hardware.
   118  func DeployFakeNode(t *testing.T, client *gophercloud.ServiceClient, node *nodes.Node) (*nodes.Node, error) {
   119  	ctx, cancel := context.WithTimeout(context.TODO(), 30*time.Second)
   120  	defer cancel()
   121  
   122  	currentState := node.ProvisionState
   123  
   124  	if currentState == string(nodes.Enroll) {
   125  		t.Logf("moving fake node %s to manageable", node.UUID)
   126  		err := nodes.ChangeProvisionState(ctx, client, node.UUID, nodes.ProvisionStateOpts{
   127  			Target: nodes.TargetManage,
   128  		}).ExtractErr()
   129  		if err != nil {
   130  			return node, err
   131  		}
   132  
   133  		err = nodes.WaitForProvisionState(ctx, client, node.UUID, nodes.Manageable)
   134  		if err != nil {
   135  			return node, err
   136  		}
   137  
   138  		currentState = string(nodes.Manageable)
   139  	}
   140  
   141  	if currentState == string(nodes.Manageable) {
   142  		t.Logf("moving fake node %s to available", node.UUID)
   143  		err := nodes.ChangeProvisionState(ctx, client, node.UUID, nodes.ProvisionStateOpts{
   144  			Target: nodes.TargetProvide,
   145  		}).ExtractErr()
   146  		if err != nil {
   147  			return node, err
   148  		}
   149  
   150  		err = nodes.WaitForProvisionState(ctx, client, node.UUID, nodes.Available)
   151  		if err != nil {
   152  			return node, err
   153  		}
   154  
   155  		currentState = string(nodes.Available)
   156  	}
   157  
   158  	t.Logf("deploying fake node %s", node.UUID)
   159  	return ChangeProvisionStateAndWait(ctx, client, node, nodes.ProvisionStateOpts{
   160  		Target: nodes.TargetActive,
   161  	}, nodes.Active)
   162  }
   163  
   164  // CreatePort - creates a port for a node with a fixed Address
   165  func CreatePort(t *testing.T, client *gophercloud.ServiceClient, node *nodes.Node) (*ports.Port, error) {
   166  	mac := "e6:72:1f:52:00:f4"
   167  	t.Logf("Attempting to create Port for Node: %s with Address: %s", node.UUID, mac)
   168  
   169  	iTrue := true
   170  	port, err := ports.Create(context.TODO(), client, ports.CreateOpts{
   171  		NodeUUID:   node.UUID,
   172  		Address:    mac,
   173  		PXEEnabled: &iTrue,
   174  	}).Extract()
   175  
   176  	return port, err
   177  }
   178  
   179  // DeletePort - deletes a port via its UUID
   180  func DeletePort(t *testing.T, client *gophercloud.ServiceClient, port *ports.Port) {
   181  	err := ports.Delete(context.TODO(), client, port.UUID).ExtractErr()
   182  	if err != nil {
   183  		t.Fatalf("Unable to delete port %s: %s", port.UUID, err)
   184  	}
   185  
   186  	t.Logf("Deleted port: %s", port.UUID)
   187  
   188  }