Files
ubicloud/sdk/ruby/lib/ubicloud.rb
Jeremy Evans 7e335516a6 Add Ruby SDK
This adds a custom (i.e. not-autogenerated) Ruby SDK, designed specifically
around Ubicloud.  Examples of use:

```ruby
require "ubicloud"

# Setup SDK context
UBI = Ubicloud.new(:net_http, token: "YOUR_API_TOKEN", project_id: "pj...")

# Get list of VMs
UBI.vm.list

# Create a firewall
UBI.firewall.create(location: "eu-central-h1", name: "my-fw")

# Retrieive a load balancer,
lb = UBI["1b345678901234567890123456"]

# then destroy it
lb.destroy

# Schedule a PostgreSQL database restart
UBI.postgres.new("eu-central-h1/my-fw").restart
```

The SDK comes with two adapters, net_http and rack. net_http uses the
net/http standard library to submit HTTP requests.  rack directly calls
rack applications.  Users are expected to use net_http.  The following
commit will use the rack adapter to implement Ubicloud's CLI using the
Ruby SDK.

This adds a rake task to build the SDK gem.

# Please enter the commit message for your changes. Lines starting
# with '#' will be kept; you may remove them yourself if you want to.
# An empty message aborts the commit.
#
# Date:      Tue Mar 25 17:01:03 2025 -0700
#
# On branch jeremy-ruby-sdk
# Changes to be committed:
#	modified:   .gitignore
#	modified:   Rakefile
#	new file:   sdk/ruby/MIT-LICENSE
#	new file:   sdk/ruby/README.md
#	new file:   sdk/ruby/lib/ubicloud.rb
#	new file:   sdk/ruby/lib/ubicloud/adapter.rb
#	new file:   sdk/ruby/lib/ubicloud/adapter/net_http.rb
#	new file:   sdk/ruby/lib/ubicloud/adapter/rack.rb
#	new file:   sdk/ruby/lib/ubicloud/context.rb
#	new file:   sdk/ruby/lib/ubicloud/model.rb
#	new file:   sdk/ruby/lib/ubicloud/model/firewall.rb
#	new file:   sdk/ruby/lib/ubicloud/model/load_balancer.rb
#	new file:   sdk/ruby/lib/ubicloud/model/postgres.rb
#	new file:   sdk/ruby/lib/ubicloud/model/private_subnet.rb
#	new file:   sdk/ruby/lib/ubicloud/model/vm.rb
#	new file:   sdk/ruby/lib/ubicloud/model_adapter.rb
#	new file:   sdk/ruby/ubicloud.gemspec
#	new file:   spec/ruby_sdk_spec.rb
#	modified:   spec/thawed_mock.rb
#
# Changes not staged for commit:
#	modified:   cli-commands/fw/post/add-rule.rb
#	modified:   cli-commands/fw/post/attach-subnet.rb
#	modified:   cli-commands/fw/post/create.rb
#	modified:   cli-commands/fw/post/delete-rule.rb
#	modified:   cli-commands/fw/post/detach-subnet.rb
#	modified:   cli-commands/fw/post/show.rb
#	modified:   cli-commands/lb/post/attach-vm.rb
#	modified:   cli-commands/lb/post/create.rb
#	modified:   cli-commands/lb/post/detach-vm.rb
#	modified:   cli-commands/lb/post/show.rb
#	modified:   cli-commands/lb/post/update.rb
#	modified:   cli-commands/pg/post/add-firewall-rule.rb
#	modified:   cli-commands/pg/post/add-metric-destination.rb
#	modified:   cli-commands/pg/post/create.rb
#	modified:   cli-commands/pg/post/delete-firewall-rule.rb
#	modified:   cli-commands/pg/post/delete-metric-destination.rb
#	modified:   cli-commands/pg/post/reset-superuser-password.rb
#	modified:   cli-commands/pg/post/restart.rb
#	modified:   cli-commands/pg/post/restore.rb
#	modified:   cli-commands/pg/post/show.rb
#	modified:   cli-commands/ps/post/connect.rb
#	modified:   cli-commands/ps/post/create.rb
#	modified:   cli-commands/ps/post/disconnect.rb
#	modified:   cli-commands/ps/post/show.rb
#	modified:   cli-commands/vm/post/create.rb
#	modified:   cli-commands/vm/post/restart.rb
#	modified:   cli-commands/vm/post/show.rb
#	modified:   lib/ubi_cli.rb
#	modified:   spec/routes/api/cli/golden-files/vm vmdzyppz6j166jh5e9t0dwrfas show.txt
#
2025-03-28 16:25:47 -07:00

49 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require_relative "ubicloud/adapter"
require_relative "ubicloud/model"
require_relative "ubicloud/model_adapter"
require_relative "ubicloud/context"
# The Ubicloud module is the namespace for Ubicloud's Ruby SDK,
# and also the primary entry point. Even though it is a module,
# users are expected to call +Ubicloud.new+ to return an appropriate
# context (Ubicloud::Context) that is used to make requests to
# Ubicloud's API.
module Ubicloud
# Error class used for errors raised by Ubicloud's Ruby SDK.
class Error < StandardError
# The integer HTTP status code related to the error. Can be
# nil if the Error is not related to an HTTP request.
attr_reader :code
# Accept the code and body keyword arguments for metadata
# related to this error.
def initialize(message, code: nil, body: nil)
super(message)
@code = code
@body = body
end
# A hash of parameters. This is the parsed JSON response body
# for the request that resulted in an error. If an invalid
# body is given, or the error is not related to an HTTP request,
# returns an empty hash.
def params
@body ? JSON.parse(@body) : {}
rescue
{}
end
end
# Create a new Ubicloud::Context for the given adapter type
# and parameters. This is the main entry point to the library.
# In general, users of the SDK will want to use the :net_http
# adapter type:
#
# Ubicloud.new(:net_http, token: "YOUR_API_TOKEN", project_id: "pj...")
def self.new(adapter_type, **params)
Context.new(Adapter.adapter_class(adapter_type).new(**params))
end
end