Skip to content

Commit b74d3b1

Browse files
authored
Merge pull request #2968 from till/create-server-hostname
compute: Allow setting the hostname when creating a server
2 parents 477b8b0 + a9bafc2 commit b74d3b1

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

openstack/compute/v2/servers/requests.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,12 @@ type CreateOpts struct {
241241
// Tags allows a server to be tagged with single-word metadata.
242242
// Requires microversion 2.52 or later.
243243
Tags []string `json:"tags,omitempty"`
244+
245+
// (Available from 2.90) Hostname specifies the hostname to configure for the
246+
// instance in the metadata service. Starting with microversion 2.94, this can
247+
// be a Fully Qualified Domain Name (FQDN) of up to 255 characters in length.
248+
// If not set, OpenStack will derive the server's hostname from the Name field.
249+
Hostname string `json:"hostname,omitempty"`
244250
}
245251

246252
// ToServerCreateMap assembles a request body based on the contents of a

openstack/compute/v2/servers/testing/fixtures_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ const SingleServerBody = `
267267
"OS-EXT-STS:task_state": null,
268268
"OS-EXT-STS:vm_state": "active",
269269
"OS-EXT-SRV-ATTR:instance_name": "instance-0000001d",
270+
"OS-EXT-SRV-ATTR:hostname": "derp.local",
270271
"OS-SRV-USG:launched_at": "2014-09-25T13:04:49.000000",
271272
"OS-EXT-SRV-ATTR:hypervisor_hostname": "devstack",
272273
"flavor": {
@@ -849,6 +850,25 @@ func HandleServerCreationWithCustomFieldSuccessfully(t *testing.T, response stri
849850
})
850851
}
851852

853+
func HandleServerCreationWithHostname(t *testing.T, response string) {
854+
th.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
855+
th.TestMethod(t, r, "POST")
856+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
857+
th.TestJSONRequest(t, r, `{
858+
"server": {
859+
"name": "derp",
860+
"hostname": "derp.local",
861+
"imageRef": "f90f6034-2570-4974-8351-6b49732ef2eb",
862+
"flavorRef": "1"
863+
}
864+
}`)
865+
866+
w.WriteHeader(http.StatusAccepted)
867+
w.Header().Add("Content-Type", "application/json")
868+
fmt.Fprintf(w, response)
869+
})
870+
}
871+
852872
// HandleServerCreationWithUserdata sets up the test server to respond to a server creation request
853873
// with a given response.
854874
func HandleServerCreationWithUserdata(t *testing.T, response string) {

openstack/compute/v2/servers/testing/requests_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ func TestCreateServerWithUserdataEncoded(t *testing.T) {
204204
th.CheckDeepEquals(t, ServerDerp, *actual)
205205
}
206206

207+
func TestCreateServerWithHostname(t *testing.T) {
208+
th.SetupHTTP()
209+
defer th.TeardownHTTP()
210+
HandleServerCreationWithHostname(t, SingleServerBody)
211+
212+
actual, err := servers.Create(context.TODO(), client.ServiceClient(), servers.CreateOpts{
213+
Name: "derp",
214+
ImageRef: "f90f6034-2570-4974-8351-6b49732ef2eb",
215+
FlavorRef: "1",
216+
Hostname: "derp.local",
217+
}).Extract()
218+
th.AssertNoErr(t, err)
219+
220+
th.CheckDeepEquals(t, ServerDerp, *actual)
221+
}
222+
207223
func TestDeleteServer(t *testing.T) {
208224
th.SetupHTTP()
209225
defer th.TeardownHTTP()

0 commit comments

Comments
 (0)