From f365b914662491ab33e6af18e1b02046f6b99538 Mon Sep 17 00:00:00 2001
From: elijah <elijah@riseup.net>
Date: Wed, 28 Jun 2017 13:24:39 -0700
Subject: [PATCH] leap_cli - make fog gem optional

---
 lib/leap_cli/cloud.rb              |  3 +-
 lib/leap_cli/cloud/dependencies.rb | 47 ++++++++++++++----------------
 lib/leap_cli/commands/vm.rb        |  5 +++-
 tests/platform-ci/Gemfile          |  1 +
 4 files changed, 28 insertions(+), 28 deletions(-)

diff --git a/lib/leap_cli/cloud.rb b/lib/leap_cli/cloud.rb
index 268cea38..b8e45b3b 100644
--- a/lib/leap_cli/cloud.rb
+++ b/lib/leap_cli/cloud.rb
@@ -1,4 +1,3 @@
-
-require 'fog/aws'
+require_relative 'cloud/dependencies.rb'
 require_relative 'cloud/cloud.rb'
 require_relative 'cloud/image.rb'
diff --git a/lib/leap_cli/cloud/dependencies.rb b/lib/leap_cli/cloud/dependencies.rb
index fd690e59..670d6134 100644
--- a/lib/leap_cli/cloud/dependencies.rb
+++ b/lib/leap_cli/cloud/dependencies.rb
@@ -1,40 +1,37 @@
 #
-# I am not sure this is a good idea, but it might be. Tricky, so disabled for now
+# Ensure that the needed fog gems are installed
 #
-
-=begin
 module LeapCli
   class Cloud
 
-    def self.check_required_gems
-      begin
-        require "fog"
-      rescue LoadError
-        bail! do
-          log :error, "The 'vm' command requires the gem 'fog-core'. Please run `gem install fog-core` and try again."
-        end
-      end
+    SUPPORTED = {
+      'aws' => {require: 'fog/aws', gem: 'fog-aws'}
+    }.freeze
 
-      fog_gems = @cloud.required_gems
-      if !options[:mock] && fog_gems.empty?
-        bail! do
-          log :warning, "no vm providers are configured in cloud.json."
-          log "You must have credentials for one of: #{@cloud.possible_apis.join(', ')}."
+    def self.check_dependencies!(config)
+      required_gem = map_api_to_gem(config['api'])
+      if required_gem.nil?
+        Util.bail! do
+          Util.log :error, "The API '#{config['api']}' specified in cloud.json is not one that I know how to speak. Try one of #{supported_list}."
         end
       end
 
-      fog_gems.each do |name, gem_name|
-        begin
-          require gem_name.sub('-','/')
-        rescue LoadError
-          bail! do
-            log :error, "The 'vm' command requires the gem '#{gem_name}' (because of what is configured in cloud.json)."
-            log "Please run `sudo gem install #{gem_name}` and try again."
-          end
+      begin
+        require required_gem[:require]
+      rescue LoadError
+        Util.bail! do
+          Util.log :error, "The 'vm' command requires the gem '#{required_gem[:gem]}'. Please run `gem install #{required_gem[:gem]}` and try again."
+          Util.log "(make sure you install the gem in the ruby #{RUBY_VERSION} environment)"
         end
       end
     end
 
+    def self.supported_list
+      SUPPORTED.keys.join(', ')
+    end
+
+    def self.map_api_to_gem(api)
+      SUPPORTED[api]
+    end
   end
 end
-=end
\ No newline at end of file
diff --git a/lib/leap_cli/commands/vm.rb b/lib/leap_cli/commands/vm.rb
index 790774f1..6f97dbce 100644
--- a/lib/leap_cli/commands/vm.rb
+++ b/lib/leap_cli/commands/vm.rb
@@ -415,7 +415,6 @@ module LeapCli; module Commands
     config = manager.env.cloud
     name = nil
     if options[:mock]
-      Fog.mock!
       name = 'mock_aws'
       config['mock_aws'] = {
         "api" => "aws",
@@ -451,6 +450,10 @@ module LeapCli; module Commands
     assert! entry['api'] == 'aws', "cloud.json: currently, only 'aws' is supported for `api`."
     assert! entry['vendor'] == 'aws', "cloud.json: currently, only 'aws' is supported for `vendor`."
 
+    LeapCli::Cloud::check_dependencies!(entry)
+    if options[:mock]
+      Fog.mock!
+    end
     return LeapCli::Cloud.new(name, entry, node)
   end
 
diff --git a/tests/platform-ci/Gemfile b/tests/platform-ci/Gemfile
index 44a8637d..4cf14e43 100644
--- a/tests/platform-ci/Gemfile
+++ b/tests/platform-ci/Gemfile
@@ -16,4 +16,5 @@ group :test do
   # Install fog-aws because the gem dependency of leap_cli is now optional
   gem "fog-aws"
   gem "leap_cli", :git => 'https://leap.se/git/leap_cli.git'
+  gem "fog-aws"
 end
-- 
GitLab