Skip to content

Commit

Permalink
Support passing user data on server creation (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
thetechnick authored and thcyron committed Jan 25, 2018
1 parent 0accb9f commit 50e14bd
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## master

* Support passing user data on server creation ([issue #70](https://github.com/hetznercloud/hcloud-go/issues/70))
* Fix leaking response body by not closing it ([issue #68](https://github.com/hetznercloud/hcloud-go/issues/68))

## v1.2.0
Expand Down
1 change: 1 addition & 0 deletions hcloud/schema/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type ServerCreateRequest struct {
SSHKeys []int `json:"ssh_keys,omitempty"`
Location string `json:"location,omitempty"`
Datacenter string `json:"datacenter,omitempty"`
UserData string `json:"user_data,omitempty"`
}

// ServerCreateResponse defines the schema of the response when
Expand Down
2 changes: 2 additions & 0 deletions hcloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ type ServerCreateOpts struct {
SSHKeys []*SSHKey
Location *Location
Datacenter *Datacenter
UserData string
}

// Validate checks if options are valid.
Expand Down Expand Up @@ -224,6 +225,7 @@ func (c *ServerClient) Create(ctx context.Context, opts ServerCreateOpts) (Serve
}

var reqBody schema.ServerCreateRequest
reqBody.UserData = opts.UserData
reqBody.Name = opts.Name
if opts.ServerType.ID != 0 {
reqBody.ServerType = opts.ServerType.ID
Expand Down
34 changes: 34 additions & 0 deletions hcloud/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,40 @@ func TestServersCreateWithLocationName(t *testing.T) {
}
}

func TestServersCreateWithUserData(t *testing.T) {
env := newTestEnv()
defer env.Teardown()

env.Mux.HandleFunc("/servers", func(w http.ResponseWriter, r *http.Request) {
var reqBody schema.ServerCreateRequest
if err := json.NewDecoder(r.Body).Decode(&reqBody); err != nil {
t.Fatal(err)
}
if reqBody.UserData != "---user data---" {
t.Errorf("unexpected userdata: %v", reqBody.UserData)
}
json.NewEncoder(w).Encode(schema.ServerCreateResponse{
Server: schema.Server{
ID: 1,
},
})
})

ctx := context.Background()
result, _, err := env.Client.Server.Create(ctx, ServerCreateOpts{
Name: "test",
ServerType: &ServerType{ID: 1},
Image: &Image{ID: 2},
UserData: "---user data---",
})
if err != nil {
t.Fatal(err)
}
if result.Server == nil {
t.Fatal("no server")
}
}

func TestServersDelete(t *testing.T) {
env := newTestEnv()
defer env.Teardown()
Expand Down

0 comments on commit 50e14bd

Please sign in to comment.