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 #
126 lines
4.8 KiB
Ruby
126 lines
4.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module ThawedMock
|
|
OBJECTS = {}
|
|
|
|
def self.allow_mocking(obj, *methods)
|
|
mod, mock = OBJECTS[obj]
|
|
unless mod
|
|
mod = Module.new
|
|
mock = Object.new
|
|
|
|
obj.singleton_class.prepend(mod)
|
|
OBJECTS[obj] = [mod, mock].freeze
|
|
end
|
|
|
|
methods.each do |method|
|
|
original_method = obj.method(method)
|
|
|
|
mock.define_singleton_method(method) do |*a, **kw, &b|
|
|
original_method.call(*a, **kw, &b)
|
|
end
|
|
|
|
mod.define_method(method) do |*a, **kw, &b|
|
|
mock.send(method, *a, **kw, &b)
|
|
end
|
|
end
|
|
end
|
|
|
|
module ExpectOverride
|
|
[:allow, :expect].each do |method|
|
|
define_method(method) do |obj = nil, &block|
|
|
return super(&block) if obj.nil? && block
|
|
|
|
_, mock = OBJECTS[obj]
|
|
super(mock || obj, &block)
|
|
end
|
|
end
|
|
end
|
|
RSpec::Core::ExampleGroup.prepend(ExpectOverride)
|
|
|
|
# Ruby Core Classes
|
|
allow_mocking(File, :exist?, :open, :rename, :write)
|
|
allow_mocking(Kernel, :exit, :exit!, :URI)
|
|
allow_mocking(Thread, :new, :list)
|
|
allow_mocking(Time, :now)
|
|
|
|
# Database
|
|
allow_mocking(DB, :[])
|
|
|
|
# Models
|
|
allow_mocking(Account, :[])
|
|
allow_mocking(Address, :where)
|
|
allow_mocking(ArchivedRecord, :create)
|
|
allow_mocking(AssignedVmAddress, :create_with_id)
|
|
allow_mocking(BillingRecord, :create_with_id)
|
|
allow_mocking(BootImage, :where)
|
|
allow_mocking(DnsRecord, :[], :create)
|
|
allow_mocking(DnsZone, :[], :where)
|
|
allow_mocking(Firewall, :generate_uuid)
|
|
allow_mocking(FirewallsPrivateSubnets, :where)
|
|
allow_mocking(FirewallRule, :create_with_id, :generate_uuid)
|
|
allow_mocking(Github, :app_client, :failed_deliveries, :installation_client, :oauth_client, :redeliver_failed_deliveries)
|
|
allow_mocking(GithubRunner, :[], :any?, :first, :join)
|
|
allow_mocking(HostProvider, :create)
|
|
allow_mocking(IpsecTunnel, :[], :create)
|
|
allow_mocking(KubernetesCluster, :[], :kubeconfig)
|
|
allow_mocking(LoadBalancer, :generate_uuid)
|
|
allow_mocking(MinioCluster, :[])
|
|
allow_mocking(Nic, :[], :create, :generate_ubid)
|
|
allow_mocking(Page, :from_tag_parts)
|
|
allow_mocking(PrivateSubnet, :[], :from_ubid, :random_subnet, :generate_ubid)
|
|
allow_mocking(PostgresFirewallRule, :generate_uuid)
|
|
allow_mocking(PostgresLsnMonitor, :new)
|
|
allow_mocking(PostgresMetricDestination, :from_ubid, :generate_uuid)
|
|
allow_mocking(PostgresResource, :[], :generate_uuid)
|
|
allow_mocking(PostgresServer, :create, :run_query)
|
|
allow_mocking(Project, :[], :from_ubid, :order_by)
|
|
allow_mocking(Semaphore, :where, :create)
|
|
allow_mocking(Sshable, :create, :repl?)
|
|
allow_mocking(StorageKeyEncryptionKey, :create)
|
|
allow_mocking(Strand, :create, :create_with_id)
|
|
allow_mocking(UsageAlert, :where)
|
|
allow_mocking(VmHost, :[])
|
|
allow_mocking(Vm, :[], :where, :generate_ubid)
|
|
allow_mocking(VmPool, :[], :where)
|
|
allow_mocking(VmHostCpu, :create)
|
|
allow_mocking(VmHostSlice, :[], :dataset)
|
|
|
|
# Progs
|
|
allow_mocking(Prog::Ai::InferenceEndpointNexus, :assemble, :model_for_id)
|
|
allow_mocking(Prog::Ai::InferenceEndpointReplicaNexus, :assemble)
|
|
allow_mocking(Prog::DnsZone::SetupDnsServerVm, :vms_in_sync?)
|
|
allow_mocking(Prog::Github::DestroyGithubInstallation, :assemble)
|
|
allow_mocking(Prog::Minio::MinioClusterNexus, :assemble)
|
|
allow_mocking(Prog::PageNexus, :assemble)
|
|
allow_mocking(Prog::Postgres::PostgresResourceNexus, :assemble, :dns_zone)
|
|
allow_mocking(Prog::Postgres::PostgresServerNexus, :assemble)
|
|
allow_mocking(Prog::Postgres::PostgresTimelineNexus, :assemble)
|
|
allow_mocking(Prog::Vm::GithubRunner, :assemble)
|
|
allow_mocking(Prog::Vm::HostNexus, :assemble)
|
|
allow_mocking(Prog::Vm::Nexus, :assemble, :assemble_with_sshable)
|
|
allow_mocking(Prog::Vm::VmPool, :assemble)
|
|
allow_mocking(Prog::Vnet::NicNexus, :assemble, :gen_mac, :rand)
|
|
allow_mocking(Prog::Vnet::SubnetNexus, :assemble, :random_private_ipv4, :random_private_ipv6)
|
|
|
|
# Other Classes
|
|
allow_mocking(BillingRate, :from_resource_properties)
|
|
allow_mocking(Clog, :emit)
|
|
allow_mocking(CloudflareClient, :new)
|
|
allow_mocking(Clover, :call)
|
|
allow_mocking(Hosting::Apis, :pull_data_center, :pull_ips, :reimage_server, :hardware_reset_server, :set_server_name)
|
|
allow_mocking(InvoiceGenerator, :new)
|
|
allow_mocking(Minio::Client, :new)
|
|
allow_mocking(Minio::Crypto, :new)
|
|
allow_mocking(Scheduling::Allocator, :allocate)
|
|
allow_mocking(Scheduling::Allocator::Allocation, :best_allocation, :candidate_hosts, :new, :random_score, :update_vm)
|
|
allow_mocking(Scheduling::Allocator::StorageAllocation, :new)
|
|
allow_mocking(Scheduling::Allocator::VmHostCpuAllocation, :new)
|
|
allow_mocking(Scheduling::Allocator::VmHostAllocation, :new)
|
|
allow_mocking(Serializers::Vm, :serialize_internal)
|
|
allow_mocking(SshKey, :generate)
|
|
allow_mocking(ThreadPrinter, :puts, :run)
|
|
allow_mocking(Util, :create_certificate, :create_root_certificate, :rootish_ssh, :send_email)
|
|
allow_mocking(UBID, :class_match?)
|
|
end
|