diff --git a/lib/vagrant/box_metadata.rb b/lib/vagrant/box_metadata.rb index 28388c7b6..7fa4a6a29 100644 --- a/lib/vagrant/box_metadata.rb +++ b/lib/vagrant/box_metadata.rb @@ -5,6 +5,9 @@ module Vagrant # it should have, a description of it, the versions it has, and # more. class BoxMetadata + + autoload :Remote, "vagrant/box_metadata/remote" + # The name that the box should be if it is added. # # @return [String] diff --git a/lib/vagrant/box_metadata/remote.rb b/lib/vagrant/box_metadata/remote.rb new file mode 100644 index 000000000..1cfd792c3 --- /dev/null +++ b/lib/vagrant/box_metadata/remote.rb @@ -0,0 +1,83 @@ +module Vagrant + class BoxMetadata + # This module enables the BoxMetadata for server mode + module Remote + + # Add an attribute reader for the client + # when applied to the BoxMetadata class + def self.prepended(klass) + klass.class_eval do + attr_reader :client + end + end + + attr_accessor :name + attr_accessor :description + + # @param [IO] io An IO object to read the metadata from. + def initialize(io, client: nil) + @logger = Log4r::Logger.new("vagrant::box") + + if client.nil? + raise ArgumentError, + "Remote client is required for `#{self.class.name}'" + end + @client = client + end + + def version(version, **opts) + v = client.version(version, opts[:provider]) + Version.new(v, ver: v["version"], client: @client) + end + + def versions(**opts) + provider = nil + provider = opts[:provider].to_sym if opts[:provider] + client.versions(provider) + end + + class Version + attr_accessor :version + + def initialize(raw=nil, ver: nil, client: nil) + return if raw.nil? + + @version = ver + if client.nil? + raise ArgumentError, + "Remote client is required for `#{self.class.name}'" + end + @client = client + end + + def provider(name) + p = client.provider(@version, name) + Provider.new(p, @client) + end + + def providers + client.providers(@version) + end + + class Provider + attr_accessor :name + attr_accessor :url + attr_accessor :checksum + attr_accessor :checksum_type + + def initialize(raw, client: nil) + @name = raw["name"] + @url = raw["url"] + @checksum = raw["checksum"] + @checksum_type = raw["checksum_type"] + if client.nil? + raise ArgumentError, + "Remote client is required for `#{self.class.name}'" + end + @client = client + end + end + end + end + end +end diff --git a/lib/vagrant/shared_helpers.rb b/lib/vagrant/shared_helpers.rb index 6c70489c0..8466dc21a 100644 --- a/lib/vagrant/shared_helpers.rb +++ b/lib/vagrant/shared_helpers.rb @@ -260,6 +260,7 @@ module Vagrant SERVER_MODE_CALLBACKS = [ ->{ Vagrant::Box.prepend(Vagrant::Box::Remote) }, ->{ Vagrant::BoxCollection.prepend(Vagrant::BoxCollection::Remote) }, + ->{ Vagrant::BoxMetadata.prepend(Vagrant::BoxMetadata::Remote) }, ->{ Vagrant::Guest.prepend(Vagrant::Guest::Remote) }, ->{ Vagrant::Host.prepend(Vagrant::Host::Remote) }, ->{ Vagrant::Machine.prepend(Vagrant::Machine::Remote) }, diff --git a/plugins/commands/serve/client.rb b/plugins/commands/serve/client.rb index 5338b8ec1..5f44040e0 100644 --- a/plugins/commands/serve/client.rb +++ b/plugins/commands/serve/client.rb @@ -4,6 +4,7 @@ module VagrantPlugins autoload :Basis, Vagrant.source_root.join("plugins/commands/serve/client/basis").to_s autoload :Box, Vagrant.source_root.join("plugins/commands/serve/client/box").to_s autoload :BoxCollection, Vagrant.source_root.join("plugins/commands/serve/client/box_collection").to_s + autoload :BoxMetadata, Vagrant.source_root.join("plugins/commands/serve/client/box_metadata").to_s autoload :CapabilityPlatform, Vagrant.source_root.join("plugins/commands/serve/client/capability_platform").to_s autoload :Communicator, Vagrant.source_root.join("plugins/commands/serve/client/communicator").to_s autoload :Command, Vagrant.source_root.join("plugins/commands/serve/client/command").to_s diff --git a/plugins/commands/serve/client/box_metadata.rb b/plugins/commands/serve/client/box_metadata.rb new file mode 100644 index 000000000..c5e32daf4 --- /dev/null +++ b/plugins/commands/serve/client/box_metadata.rb @@ -0,0 +1,44 @@ +module VagrantPlugins + module CommandServe + class Client + class BoxMetadata < Client + def name + client.name(Empty.new) + end + + # @param [String] version The version to return, this can also + # be a constraint. + # @param [String] (optional) adds a provider constraint to the version + def version(version, provider) + v = client.version(SDK::BoxMetadata::VersionRequest.new( + version: version, + opts: SDK::BoxMetadata::BoxMetadataOpts.new(name: provider) + )) + v.to_h + end + + # @param [String] (optional) adds a provider constraint to the version list + def list_versions(provider) + v = client.list_version(SDK::BoxMetadata::BoxMetadataOpts.new( + name: provider + )) + v.versions + end + + def provider(version, name) + p = client.provider(SDK::BoxMetadata::ProviderRequest.new( + version: version, name: name + )) + p.to_h + end + + def list_providers(version) + p = client.list_providers(SDK::BoxMetadata::ListProvidersRequest.new( + version: version + )) + p.providers + end + end + end + end +end