From 5f862c4db6459cc342b3cab92dc94184a66987b3 Mon Sep 17 00:00:00 2001 From: lpick Date: Mon, 4 Jul 2022 11:44:43 +0200 Subject: [PATCH 1/6] Adjust server create call by accepting no primary IPs with StartAfterCreate=false --- hcloud/server.go | 4 ++-- hcloud/server_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/hcloud/server.go b/hcloud/server.go index 142e8429..5fcecceb 100644 --- a/hcloud/server.go +++ b/hcloud/server.go @@ -348,8 +348,8 @@ 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 == Bool(false) { + return errors.New("missing networks or StartAfterCreate == false when EnableIPv4 and EnableIPv6 is false") } } return nil diff --git a/hcloud/server_test.go b/hcloud/server_test.go index 4add328b..9bf7f130 100644 --- a/hcloud/server_test.go +++ b/hcloud/server_test.go @@ -893,6 +893,10 @@ func TestServersCreateWithoutStarting(t *testing.T) { ServerType: &ServerType{ID: 1}, Image: &Image{ID: 2}, StartAfterCreate: Bool(false), + PublicNet: &ServerCreatePublicNet{ + EnableIPv4: false, + EnableIPv6: false, + }, }) if err != nil { t.Fatal(err) @@ -951,6 +955,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() From 2877cf97bf16e9aa26e1b3ab5c58c8ebc6aabc4f Mon Sep 17 00:00:00 2001 From: lpick Date: Mon, 4 Jul 2022 11:55:30 +0200 Subject: [PATCH 2/6] fix condition --- hcloud/server.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/hcloud/server.go b/hcloud/server.go index 5fcecceb..01fb9ba0 100644 --- a/hcloud/server.go +++ b/hcloud/server.go @@ -348,7 +348,8 @@ 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 && o.StartAfterCreate == Bool(false) { + if !o.PublicNet.EnableIPv4 && !o.PublicNet.EnableIPv6 && + len(o.Networks) == 0 && o.StartAfterCreate == Bool(true) { return errors.New("missing networks or StartAfterCreate == false when EnableIPv4 and EnableIPv6 is false") } } From 0a939af1c88366da4ea5d96aa513193a2a3c43b2 Mon Sep 17 00:00:00 2001 From: lpick Date: Mon, 4 Jul 2022 13:55:17 +0200 Subject: [PATCH 3/6] fix condition --- hcloud/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hcloud/server.go b/hcloud/server.go index 01fb9ba0..c5e54bd6 100644 --- a/hcloud/server.go +++ b/hcloud/server.go @@ -349,7 +349,7 @@ func (o ServerCreateOpts) Validate() error { } if o.PublicNet != nil { if !o.PublicNet.EnableIPv4 && !o.PublicNet.EnableIPv6 && - len(o.Networks) == 0 && o.StartAfterCreate == Bool(true) { + len(o.Networks) == 0 && (o.StartAfterCreate == nil || *o.StartAfterCreate == true) { return errors.New("missing networks or StartAfterCreate == false when EnableIPv4 and EnableIPv6 is false") } } From 52bff413ba04f27f758a8a30c264b6238ca3705b Mon Sep 17 00:00:00 2001 From: lpick Date: Mon, 4 Jul 2022 13:57:16 +0200 Subject: [PATCH 4/6] make it simple --- hcloud/server.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hcloud/server.go b/hcloud/server.go index c5e54bd6..00bcb903 100644 --- a/hcloud/server.go +++ b/hcloud/server.go @@ -349,7 +349,7 @@ func (o ServerCreateOpts) Validate() error { } if o.PublicNet != nil { if !o.PublicNet.EnableIPv4 && !o.PublicNet.EnableIPv6 && - len(o.Networks) == 0 && (o.StartAfterCreate == nil || *o.StartAfterCreate == true) { + len(o.Networks) == 0 && (o.StartAfterCreate == nil || *o.StartAfterCreate) { return errors.New("missing networks or StartAfterCreate == false when EnableIPv4 and EnableIPv6 is false") } } From a77fef9995942edebb28a8d68c414bf00d18fa3f Mon Sep 17 00:00:00 2001 From: lpick Date: Mon, 4 Jul 2022 14:07:08 +0200 Subject: [PATCH 5/6] Add unit test for edge case: no primary ips, networks + no start_after_create --- hcloud/server_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/hcloud/server_test.go b/hcloud/server_test.go index 9bf7f130..4c97c43e 100644 --- a/hcloud/server_test.go +++ b/hcloud/server_test.go @@ -906,6 +906,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() From aa1acbb5ee84b660ffb173a40239add69cc8f8e2 Mon Sep 17 00:00:00 2001 From: lpick Date: Mon, 4 Jul 2022 14:24:01 +0200 Subject: [PATCH 6/6] remove test adjustment --- hcloud/server_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/hcloud/server_test.go b/hcloud/server_test.go index 4c97c43e..81681cdc 100644 --- a/hcloud/server_test.go +++ b/hcloud/server_test.go @@ -893,10 +893,6 @@ func TestServersCreateWithoutStarting(t *testing.T) { ServerType: &ServerType{ID: 1}, Image: &Image{ID: 2}, StartAfterCreate: Bool(false), - PublicNet: &ServerCreatePublicNet{ - EnableIPv4: false, - EnableIPv6: false, - }, }) if err != nil { t.Fatal(err)