Sha256: ff96f9f273a60143e3545f49b944bc7b83ec3655ad8294783f9585eb4eb82b12

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
kaname-0.1.6 lib/kaname/cli.rb
kaname-0.1.5 lib/kaname/cli.rb
kaname-0.1.4 lib/kaname/cli.rb
kaname-0.1.3 lib/kaname/cli.rb