require 'yaml' require 'fog' require 'thor' require 'hashdiff' require 'diffy' module Kaname class CLI < Thor option :dryrun, type: :boolean, aliases: "-d", default: true desc 'apply', 'Commands about configuration apply' def apply adapter = if options[:dryrun] Kaname::Adapter::Mock.new else Kaname::Adapter::Real.new end if Kaname::Resource.yaml diffs = HashDiff.diff(Kaname::Resource.users_hash, Kaname::Resource.yaml) diffs.each do |diff| resource = diff[1].split('.') if resource.size == 1 # "user" if diff[0] == "+" user = adapter.create_user(resource[0], diff[2]['email']) diff[2]["tenants"].each do |tenant, role| adapter.create_user_role(tenant, user, role) end else adapter.delete_user(resource[0]) end elsif resource.size == 3 # "user.tenants.foo" user_hash = adapter.find_user(resource[0]) case diff[0] when "+" adapter.create_user_role(resource[2], user_hash, diff[2]) when "-" adapter.delete_user_role(resource[2], user_hash, diff[2]) when "~" adapter.change_user_role(resource[2], user_hash, diff[2], diff[3]) end else # "user.tenants" # XXX end end else puts "Please put you keystone configuration file named keystone.yml to current directory." end end desc 'diff', 'Commands about show diffs from your openstack' def diff puts Diffy::Diff.new(YAML.dump(Kaname::Resource.users_hash), YAML.dump(Kaname::Resource.yaml)) end desc 'dump', 'Commands about dump Keystone configuration.' def dump puts YAML.dump(Kaname::Resource.users_hash) end end end