Repository: gophercloud/gophercloud

Issue:
nova: add support for hostname updates
This will allow users to update the hostname of an instance which is currently missing.

I'd like to make a PR which adds the ability to change the hostname of an instance by adding the following field to `UpdateOpts` struct in `compute/v2/servers/requests.go`.
* [hostname](https://github.com/openstack/nova/blob/0d586ccca88ae90b9634ee00b8f7f86a78b09cd0/nova/objects/instance.py#L131)

This is needed so I can add the update functionality in [terraform-provider-openstack](https://github.com/terraform-provider-openstack/terraform-provider-openstack/pull/1910).

Hints from issue comments:
@github-actions: Thank you for reporting your first issue! Be sure that we will be looking at it, but keep in mind
this sometimes takes a while.
Please let the maintainers know if your issue has not got enough attention after a few days.
If any doubt, please consult our issue [tutorial](https://github.com/gophercloud/gophercloud/blob/main/docs/contributor-tutorial/step-02-issues.md).

Solution patch:
diff --git a/openstack/compute/v2/servers/requests.go b/openstack/compute/v2/servers/requests.go
index b74b98768..6754d0a93 100644
--- a/openstack/compute/v2/servers/requests.go
+++ b/openstack/compute/v2/servers/requests.go
@@ -654,6 +654,12 @@ type UpdateOpts struct {
 
 	// AccessIPv6 provides a new IPv6 address for the instance.
 	AccessIPv6 *string `json:"accessIPv6,omitempty"`
+
+	// Hostname changes the hostname of the server.
+	// Requires microversion 2.90 or later.
+	// Note: This information is published via the metadata service and requires
+	// application such as cloud-init to propagate it through to the instance.
+	Hostname *string `json:"hostname,omitempty"`
 }
 
 // ToServerUpdateMap formats an UpdateOpts structure into a request body.



Test patch:
diff --git a/internal/acceptance/openstack/compute/v2/servers_test.go b/internal/acceptance/openstack/compute/v2/servers_test.go
index 7f769e6e9..cebeb43e5 100644
--- a/internal/acceptance/openstack/compute/v2/servers_test.go
+++ b/internal/acceptance/openstack/compute/v2/servers_test.go
@@ -631,3 +631,40 @@ func TestServerNoNetworkCreateDestroy(t *testing.T) {
 		t.Fatalf("Instance must not be a member of specified network")
 	}
 }
+
+func TestServersUpdateHostname(t *testing.T) {
+	clients.RequireLong(t)
+
+	client, err := clients.NewComputeV2Client()
+	th.AssertNoErr(t, err)
+	client.Microversion = "2.90"
+
+	server, err := CreateMicroversionServer(t, client)
+	th.AssertNoErr(t, err)
+	defer DeleteServer(t, client, server)
+
+	alternateHostname := tools.RandomString("ACPTTEST", 16)
+	for alternateHostname == *server.Hostname {
+		alternateHostname = tools.RandomString("ACPTTEST", 16)
+	}
+
+	t.Logf("Attempting to change the server's hostname to %s.", alternateHostname)
+
+	updateOpts := servers.UpdateOpts{
+		Hostname: &alternateHostname,
+	}
+
+	updated, err := servers.Update(context.TODO(), client, server.ID, updateOpts).Extract()
+	th.AssertNoErr(t, err)
+
+	th.AssertEquals(t, server.ID, updated.ID)
+
+	err = tools.WaitFor(func(ctx context.Context) (bool, error) {
+		latest, err := servers.Get(ctx, client, updated.ID).Extract()
+		if err != nil {
+			return false, err
+		}
+		return *latest.Hostname == alternateHostname, nil
+	})
+	th.AssertNoErr(t, err)
+}
diff --git a/openstack/compute/v2/servers/testing/fixtures_test.go b/openstack/compute/v2/servers/testing/fixtures_test.go
index 7d7239d2f..5c5153887 100644
--- a/openstack/compute/v2/servers/testing/fixtures_test.go
+++ b/openstack/compute/v2/servers/testing/fixtures_test.go
@@ -1338,3 +1338,17 @@ func HandleServerWithTagsCreationSuccessfully(t *testing.T, fakeServer th.FakeSe
 		fmt.Fprint(w, SingleServerWithTagsBody)
 	})
 }
+
+// HandleServerHostnameUpdateSuccessfully sets up the test server to respond to a server update
+// request changing the hostname.
+func HandleServerHostnameUpdateSuccessfully(t *testing.T, fakeServer th.FakeServer) {
+	fakeServer.Mux.HandleFunc("/servers/1234asdf", func(w http.ResponseWriter, r *http.Request) {
+		th.TestMethod(t, r, "PUT")
+		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
+		th.TestHeader(t, r, "Accept", "application/json")
+		th.TestHeader(t, r, "Content-Type", "application/json")
+		th.TestJSONRequest(t, r, `{ "server": { "hostname": "new-hostname" } }`)
+
+		fmt.Fprint(w, SingleServerBody)
+	})
+}
diff --git a/openstack/compute/v2/servers/testing/requests_test.go b/openstack/compute/v2/servers/testing/requests_test.go
index 16a16530f..96cb77f14 100644
--- a/openstack/compute/v2/servers/testing/requests_test.go
+++ b/openstack/compute/v2/servers/testing/requests_test.go
@@ -1183,3 +1183,17 @@ func TestCreateServerWithHypervisorHostname(t *testing.T) {
 	th.AssertNoErr(t, err)
 	th.CheckJSONEquals(t, expected, actual)
 }
+
+func TestUpdateServerHostname(t *testing.T) {
+	fakeServer := th.SetupHTTP()
+	defer fakeServer.Teardown()
+	HandleServerHostnameUpdateSuccessfully(t, fakeServer)
+
+	client := client.ServiceClient(fakeServer)
+	actual, err := servers.Update(context.TODO(), client, "1234asdf", servers.UpdateOpts{Hostname: ptr.To("new-hostname")}).Extract()
+	if err != nil {
+		t.Fatalf("Unexpected Update error: %v", err)
+	}
+
+	th.CheckDeepEquals(t, ServerDerp, *actual)
+}