Sha256: 7465a27e666f8e98232c382fad4d38d8337cda737b0444eb396b583fd709f711

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'yaml'
require 'fog'
require 'thor'
require 'hashdiff'
require 'diffy'

module Kaname
  class CLI < Thor
    option :dryrun, type: :boolean
    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

1 entries across 1 versions & 1 rubygems

Version Path
kaname-0.2.0 lib/kaname/cli.rb