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).

Discussion:
@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 44e8cccae..34e4e00d4 100644
--- a/openstack/compute/v2/servers/requests.go
+++ b/openstack/compute/v2/servers/requests.go
@@ -651,6 +651,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 0c5d03449..2e5a21d49 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 a9c3980d8..9d517f46b 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) {
 		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) {
+	th.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 66a33dd68..fd15e20a0 100644
--- a/openstack/compute/v2/servers/testing/requests_test.go
+++ b/openstack/compute/v2/servers/testing/requests_test.go
@@ -1182,3 +1182,18 @@ func TestCreateServerWithHypervisorHostname(t *testing.T) {
 	th.AssertNoErr(t, err)
 	th.CheckJSONEquals(t, expected, actual)
 }
+
+func TestUpdateServerHostname(t *testing.T) {
+	th.SetupHTTP()
+	defer th.TeardownHTTP()
+	HandleServerHostnameUpdateSuccessfully(t)
+
+	client := client.ServiceClient()
+	newHostname := "new-hostname"
+	actual, err := servers.Update(context.TODO(), client, "1234asdf", servers.UpdateOpts{Hostname: &newHostname}).Extract()
+	if err != nil {
+		t.Fatalf("Unexpected Update error: %v", err)
+	}
+
+	th.CheckDeepEquals(t, ServerDerp, *actual)
+}