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

Accept no primary IPs with server create with StartAfterCreate = false #205

Merged
merged 6 commits into from
Jul 4, 2022
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
5 changes: 3 additions & 2 deletions hcloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,9 @@ func (o ServerCreateOpts) Validate() error {
return errors.New("location and datacenter are mutually exclusive")
}
if o.PublicNet != nil {
if !o.PublicNet.EnableIPv4 && !o.PublicNet.EnableIPv6 && len(o.Networks) == 0 {
return errors.New("missing networks when EnableIPv4 and EnableIPv6 is false")
if !o.PublicNet.EnableIPv4 && !o.PublicNet.EnableIPv6 &&
len(o.Networks) == 0 && (o.StartAfterCreate == nil || *o.StartAfterCreate) {
return errors.New("missing networks or StartAfterCreate == false when EnableIPv4 and EnableIPv6 is false")
}
}
return nil
Expand Down
67 changes: 67 additions & 0 deletions hcloud/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,24 @@ func TestServersCreateWithoutStarting(t *testing.T) {
}
}

func TestServersCreateFailIfNoIPsAndNetworksAssigned(t *testing.T) {
env := newTestEnv()
defer env.Teardown()
ctx := context.Background()
_, _, err := env.Client.Server.Create(ctx, ServerCreateOpts{
Name: "test",
ServerType: &ServerType{ID: 1},
Image: &Image{ID: 2},
PublicNet: &ServerCreatePublicNet{
EnableIPv4: false,
EnableIPv6: false,
},
})
if err == nil {
t.Fatal(err)
}
}

func TestServerCreateWithPlacementGroup(t *testing.T) {
env := newTestEnv()
defer env.Teardown()
Expand Down Expand Up @@ -951,6 +969,55 @@ func TestServerCreateWithPlacementGroup(t *testing.T) {
}
}

func TestServerCreateWithoutPrimaryIPsButNetwork(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.PublicNet.EnableIPv4 != false && reqBody.PublicNet.EnableIPv6 != false {
t.Errorf("unexpected public net %v", reqBody.PublicNet)
}
json.NewEncoder(w).Encode(schema.ServerCreateResponse{
Server: schema.Server{
ID: 1,
},
NextActions: []schema.Action{
{ID: 2},
},
})
})

ctx := context.Background()
result, _, err := env.Client.Server.Create(ctx, ServerCreateOpts{
Name: "test",
ServerType: &ServerType{ID: 1},
Image: &Image{ID: 2},
Networks: []*Network{
{ID: 1},
},
PublicNet: &ServerCreatePublicNet{
EnableIPv4: false,
EnableIPv6: false,
},
})
if err != nil {
t.Fatal(err)
}
if result.Server == nil {
t.Fatal("no server")
}
if result.Server.ID != 1 {
t.Errorf("unexpected server ID: %d", result.Server.ID)
}
if len(result.NextActions) != 1 || result.NextActions[0].ID != 2 {
t.Errorf("unexpected next actions: %v", result.NextActions)
}
}

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