diff --git a/builder/hcloud/step_create_server.go b/builder/hcloud/step_create_server.go index fb982c5f..7796d47b 100644 --- a/builder/hcloud/step_create_server.go +++ b/builder/hcloud/step_create_server.go @@ -71,6 +71,9 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu if err != nil { return errorHandler(state, ui, "Could not find image", err) } + if image == nil { + return errorHandler(state, ui, "", fmt.Errorf("Could not find image")) + } } else { image, err = getImageWithSelectors(ctx, client, c, serverType) if err != nil { @@ -78,6 +81,13 @@ func (s *stepCreateServer) Run(ctx context.Context, state multistep.StateBag) mu } } ui.Message(fmt.Sprintf("Using image '%d'", image.ID)) + if image.IsDeprecated() { + ui.Errorf( + "The image '%d' is deprecated since the %s and will soon be unavailable", + image.ID, image.Deprecated.Format("2006-01-02"), + ) + } + state.Put(StateSourceImageID, image.ID) var networks []*hcloud.Network diff --git a/builder/hcloud/step_create_server_test.go b/builder/hcloud/step_create_server_test.go index c0352347..85b273a6 100644 --- a/builder/hcloud/step_create_server_test.go +++ b/builder/hcloud/step_create_server_test.go @@ -597,6 +597,36 @@ func TestStepCreateServer(t *testing.T) { assert.Regexp(t, "Could not fetch primary ip .*", err.Error()) }, }, + { + Name: "fail to get image", + Step: &stepCreateServer{}, + SetupConfigFunc: func(c *Config) {}, + SetupStateFunc: func(state multistep.StateBag) { + state.Put(StateSSHKeyID, int64(1)) + state.Put(StateServerType, &hcloud.ServerType{ID: 9, Name: "cpx11", Architecture: "x86"}) + }, + WantRequests: []mockutil.Request{ + {Method: "GET", Path: "/ssh_keys/1", + Status: 200, + JSONRaw: `{ + "ssh_key": { "id": 1 } + }`, + }, + {Method: "GET", Path: "/images?architecture=x86&include_deprecated=true&name=debian-12", + Status: 200, + JSONRaw: `{ + "images": [] + }`, + }, + }, + WantStepAction: multistep.ActionHalt, + WantStateFunc: func(t *testing.T, state multistep.StateBag) { + err, ok := state.Get(StateError).(error) + assert.True(t, ok) + assert.NotNil(t, err) + assert.EqualError(t, err, "Could not find image") + }, + }, }) }