Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing user data on server creation #70 #71

Merged
merged 1 commit into from
Jan 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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