Skip to content

Commit

Permalink
lib: implement actions for images
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan Koch committed Jun 22, 2023
1 parent 407dde2 commit f03402c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 15 deletions.
2 changes: 2 additions & 0 deletions lib/hcloud/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class Image
updatable :description, :type
destructible

has_actions

def to_snapshot
update(type: 'snapshot')
end
Expand Down
19 changes: 4 additions & 15 deletions spec/hcloud/image_actions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
require 'spec_helper'

describe Hcloud::Image, doubles: :image do
include_context 'test doubles'
include_context 'action tests'

let :images do
Array.new(Faker::Number.within(range: 20..150)).map { new_image }
end
Expand All @@ -21,21 +24,7 @@

context '#change_protection' do
it 'works' do
expectation = stub_action(:images, image[:id], :change_protection) do |req, _info|
expect(req).to have_body_params(a_hash_including({ 'delete' => true }))

{
action: build_action_resp(
:change_protection, :success,
resources: [{ id: 42, type: 'image' }]
)
}
end

action = image_obj.change_protection(delete: true)
expect(expectation.times_called).to eq(1)
expect(action).to be_a(Hcloud::Action)
expect(action.resources[0]['id']).to eq(42)
test_action(:change_protection, params: { delete: true })
end
end
end
2 changes: 2 additions & 0 deletions spec/hcloud/image_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require 'support/it_supports_update'
require 'support/it_supports_destroy'
require 'support/it_supports_labels_on_update'
require 'support/it_supports_action_fetch'

describe Hcloud::Image, doubles: :image do
let :images do
Expand All @@ -26,6 +27,7 @@
include_examples 'it_supports_update', described_class, { description: 'new description' }
include_examples 'it_supports_destroy', described_class
include_examples 'it_supports_labels_on_update', described_class
include_examples 'it_supports_action_fetch', described_class

it '#to_snapshot' do
expectation = stub_update(:image, image, { type: 'snapshot' })
Expand Down

0 comments on commit f03402c

Please sign in to comment.