Skip to content

Commit

Permalink
Add full server endpoints to CwServer, specs for GET & POST methods
Browse files Browse the repository at this point in the history
  • Loading branch information
bradpotts committed Oct 30, 2024
1 parent b570629 commit 80a625f
Show file tree
Hide file tree
Showing 5 changed files with 285 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ GEM
minitest (5.25.1)
multi_xml (0.7.1)
bigdecimal (~> 3.1)
nio4r (2.7.3)
nio4r (2.7.4)
nokogiri (1.16.7)
mini_portile2 (~> 2.8.2)
racc (~> 1.4)
Expand Down Expand Up @@ -109,7 +109,7 @@ GEM
rubocop-ast (>= 1.32.2, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.32.3)
rubocop-ast (1.33.0)
parser (>= 3.3.1.0)
rubocop-performance (1.22.1)
rubocop (>= 1.48.1, < 2.0)
Expand Down
107 changes: 105 additions & 2 deletions lib/ruby_api_pack_cloudways/api/cw_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,116 @@
module RubyApiPackCloudways
module Api
class CwServer
# Server endpoints
ENDPOINTS = {
list: '/server',
details: '/server/details',
attach_block_storage: '/server/attach_block_storage',
clone: '/server/clone',
create: '/server/create',
delete: '/server/delete',
disk_usage: '/server/disk_usage',
restart: '/server/restart',
scale_block_storage: '/server/scale_block_storage',
scale_volume_size: '/server/scale_volume_size',
start: '/server/start',
stop: '/server/stop',
update_label: '/server/update_label',
upgrade: '/server/upgrade'
}.freeze

# List all servers
def self.server_list
fetch_list('/server')['servers']
fetch_list(ENDPOINTS[:list])['servers']
end

# Get server details
def self.server_details(server_id)
fetch_with_id(ENDPOINTS[:details], server_id)
end

# Attach block storage to a server
def self.attach_block_storage(server_id, params)
post_with_id(ENDPOINTS[:attach_block_storage], server_id, params)
end

# Clone a server
def self.clone_server(server_id, params)
post_with_id(ENDPOINTS[:clone], server_id, params)
end

# Create a new server
def self.create_server(params)
post_without_id(ENDPOINTS[:create], params)
end

# Delete a server
def self.delete_server(server_id)
fetch_with_id(ENDPOINTS[:delete], server_id)
end

# Get disk usage information
def self.disk_usage(server_id)
fetch_with_id(ENDPOINTS[:disk_usage], server_id)
end

# Restart a server
def self.restart_server(server_id)
fetch_with_id(ENDPOINTS[:restart], server_id)
end

# Scale block storage of a server
def self.scale_block_storage(server_id, params)
post_with_id(ENDPOINTS[:scale_block_storage], server_id, params)
end

def self.fetch_list(endpoint)
# Scale volume size of a server
def self.scale_volume_size(server_id, params)
post_with_id(ENDPOINTS[:scale_volume_size], server_id, params)
end

# Start a server
def self.start_server(server_id)
fetch_with_id(ENDPOINTS[:start], server_id)
end

# Stop a server
def self.stop_server(server_id)
fetch_with_id(ENDPOINTS[:stop], server_id)
end

# Update server label
def self.update_server_label(server_id, params)
post_with_id(ENDPOINTS[:update_label], server_id, params)
end

# Upgrade a server
def self.upgrade_server(server_id, params)
post_with_id(ENDPOINTS[:upgrade], server_id, params)
end

# Private methods to fetch data
private_class_method def self.fetch_list(endpoint)
Connection::CwConnect.new(RubyApiPackCloudways.configuration.api_url, endpoint).cloudways_api_connection
end

# Private methods to fetch data with server id
private_class_method def self.fetch_with_id(endpoint, server_id)
Connection::CwConnect.new(RubyApiPackCloudways.configuration.api_url, "#{endpoint}/#{server_id}")
.cloudways_api_connection
end

# Private methods to post data with server id
private_class_method def self.post_with_id(endpoint, server_id, params)
connection = Connection::CwConnect.new(RubyApiPackCloudways.configuration.api_url, "#{endpoint}/#{server_id}")
connection.cloudways_api_post_connection(params)
end

# Private methods to post data without server id
private_class_method def self.post_without_id(endpoint, params)
connection = Connection::CwConnect.new(RubyApiPackCloudways.configuration.api_url, endpoint)
connection.cloudways_api_post_connection(params)
end
end
end
end
16 changes: 16 additions & 0 deletions lib/ruby_api_pack_cloudways/connection/cw_connect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
module RubyApiPackCloudways
module Connection
class CwConnect
# Initialize Cloudways API URL and path
attr_reader :cw_api_url_base, :cw_api_path

# Initialize Cloudways API URL and path
def initialize(cw_api_url_base, cw_api_path)
@cw_api_url_base = cw_api_url_base
@cw_api_path = cw_api_path
end

# GET request to Cloudways API
def cloudways_api_connection
token = CwToken.new.cw_api_token
response = HTTParty.get(
Expand All @@ -19,8 +22,20 @@ def cloudways_api_connection
handle_response(response)
end

# POST request to Cloudways API
def cloudways_api_post_connection(params)
token = CwToken.new.cw_api_token
response = HTTParty.post(
"#{@cw_api_url_base}#{@cw_api_path}",
headers: { 'Authorization' => "Bearer #{token}", 'Content-Type' => 'application/json' },
body: params.to_json
)
handle_response(response)
end

private

# Handle response from Cloudways API
def handle_response(response)
case response.code
when 200
Expand All @@ -30,6 +45,7 @@ def handle_response(response)
end
end

# Parse response from Cloudways API
def parse_response(response)
Oj.load(response.body)
rescue Oj::ParseError => e
Expand Down
116 changes: 112 additions & 4 deletions spec/ruby_api_pack_cloudways/api/cw_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,120 @@

describe '.server_list' do
it 'returns a list of servers' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return(
'servers' => ['server1']
)

allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return('servers' => ['server1'])
servers = described_class.server_list
expect(servers).to eq(['server1'])
end
end

describe '.server_details' do
it 'returns server details' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return('details' => 'server details')
details = described_class.server_details('123')
expect(details).to eq('details' => 'server details')
end
end

describe '.attach_block_storage' do
it 'attaches block storage to the server' do
params = { storage_size: 100 }
allow(cw_connect_instance).to receive(:cloudways_api_post_connection).with(params).and_return('status' => 'attached')
result = described_class.attach_block_storage('123', params)
expect(result).to eq('status' => 'attached')
end
end

describe '.clone_server' do
it 'clones a server' do
params = { name: 'new_server' }
allow(cw_connect_instance).to receive(:cloudways_api_post_connection).with(params).and_return('status' => 'cloned')
result = described_class.clone_server('123', params)
expect(result).to eq('status' => 'cloned')
end
end

describe '.create_server' do
it 'creates a new server' do
params = { server_name: 'my_server' }
allow(cw_connect_instance).to receive(:cloudways_api_post_connection).with(params).and_return('status' => 'created')
result = described_class.create_server(params)
expect(result).to eq('status' => 'created')
end
end

describe '.delete_server' do
it 'deletes a server' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return('status' => 'deleted')
result = described_class.delete_server('123')
expect(result).to eq('status' => 'deleted')
end
end

describe '.disk_usage' do
it 'returns the disk usage of the server' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return('disk_usage' => '20GB')
result = described_class.disk_usage('123')
expect(result).to eq('disk_usage' => '20GB')
end
end

describe '.restart_server' do
it 'restarts the server' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return('status' => 'restarted')
result = described_class.restart_server('123')
expect(result).to eq('status' => 'restarted')
end
end

describe '.scale_block_storage' do
it 'scales the block storage of the server' do
params = { new_size: 200 }
allow(cw_connect_instance).to receive(:cloudways_api_post_connection).with(params).and_return('status' => 'scaled')
result = described_class.scale_block_storage('123', params)
expect(result).to eq('status' => 'scaled')
end
end

describe '.scale_volume_size' do
it 'scales the volume size of the server' do
params = { new_size: 500 }
allow(cw_connect_instance).to receive(:cloudways_api_post_connection).with(params).and_return('status' => 'volume_scaled')
result = described_class.scale_volume_size('123', params)
expect(result).to eq('status' => 'volume_scaled')
end
end

describe '.start_server' do
it 'starts the server' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return('status' => 'started')
result = described_class.start_server('123')
expect(result).to eq('status' => 'started')
end
end

describe '.stop_server' do
it 'stops the server' do
allow(cw_connect_instance).to receive(:cloudways_api_connection).and_return('status' => 'stopped')
result = described_class.stop_server('123')
expect(result).to eq('status' => 'stopped')
end
end

describe '.update_server_label' do
it 'updates the server label' do
params = { label: 'New Server Label' }
allow(cw_connect_instance).to receive(:cloudways_api_post_connection).with(params).and_return('status' => 'label_updated')
result = described_class.update_server_label('123', params)
expect(result).to eq('status' => 'label_updated')
end
end

describe '.upgrade_server' do
it 'upgrades the server' do
params = { plan: 'premium' }
allow(cw_connect_instance).to receive(:cloudways_api_post_connection).with(params).and_return('status' => 'upgraded')
result = described_class.upgrade_server('123', params)
expect(result).to eq('status' => 'upgraded')
end
end
end
51 changes: 50 additions & 1 deletion spec/ruby_api_pack_cloudways/connection/cw_connect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@
let(:connection) { described_class.new('https://api.cloudways.com/api/v1', '/some_path') }
let(:token_instance) { instance_double(RubyApiPackCloudways::Connection::CwToken, cw_api_token: 'fake_token') }
let(:http_response) { instance_double(HTTParty::Response, code: 200, body: '{"data":"value"}') }
let(:post_params) { { key: 'value' } }

before do
allow(RubyApiPackCloudways::Connection::CwToken).to receive(:new).and_return(token_instance)
allow(HTTParty).to receive(:get).and_return(http_response)
allow(HTTParty).to receive(:post).and_return(http_response)
end

describe '#cloudways_api_connection' do
it 'creates a HTTParty GET connection' do
it 'creates an HTTParty GET connection' do
connection.cloudways_api_connection

expect(HTTParty).to have_received(:get).with(
Expand Down Expand Up @@ -53,4 +55,51 @@
end
end
end

describe '#cloudways_api_post_connection' do
it 'creates an HTTParty POST connection with JSON body' do
connection.cloudways_api_post_connection(post_params)

expect(HTTParty).to have_received(:post).with(
'https://api.cloudways.com/api/v1/some_path',
headers: {
'Authorization' => 'Bearer fake_token',
'Content-Type' => 'application/json'
},
body: post_params.to_json
)
end

it 'returns a successful response for POST request' do
response = connection.cloudways_api_post_connection(post_params)
expect(response['data']).to eq('value')
end

context 'when the POST response code is not 200' do
let(:error_response) { instance_double(HTTParty::Response, code: 500, body: '{"error":"Server error"}') }

before do
allow(HTTParty).to receive(:post).and_return(error_response)
end

it 'raises an error with the response code for POST request' do
expect { connection.cloudways_api_post_connection(post_params) }.to raise_error('Error: Received status 500')
end
end

context 'when parsing the POST response fails' do
let(:faulty_response) { instance_double(HTTParty::Response, code: 200, body: 'invalid_json') }

before do
allow(HTTParty).to receive(:post).and_return(faulty_response)
allow(Oj).to receive(:load).and_raise(Oj::ParseError.new('Unexpected character'))
end

it 'raises a parsing error for POST request' do
expect do
connection.cloudways_api_post_connection(post_params)
end.to raise_error(/Error parsing response: Unexpected character/)
end
end
end
end

0 comments on commit 80a625f

Please sign in to comment.