class OcpCommander

Public Instance Methods

run() click to toggle source

include whatever modules you need

# File lib/commander/OcpCommander.rb, line 24
def run

  program :name, 'ocp'
  program :version, '0.0.1'
  program :description, 'A Ruby command line tool to interact with OpenShift Container Platform'

  global_option('-t','--token [TOKEN]','Provide a token.  This option will over-ride what is in a config file')
  global_option('--clientcertfile [CLIENTCERT]','Provide a path to the client certificate file.  This option will over-ride what is in a config file')
  global_option('--clientkeyfile [CLIENTKEY]','Provide a path to the client key file.  This option will over-ride what is in a config file')
  global_option('--clientcafile [CLIENTCA]','Provide a path to the client CA certificate file.  This option will over-ride what is in a config file')
  global_option('-c','--config [CONFIG]','Provide a config file.  See README.md for an example')
  global_option('-m','--master [MASTER]','OpenShift Cluster to which wish to connect.  This option will over-ride what is in a config file')
  global_option('-n','--noverifyssl','Do not verify SSL certificate of master.  This option will over-ride what is in a config file')
  global_option('-o','--output [OUTPUT]',[:yaml, :json], "Select the output format")
  global_option('-d','--debug', "Dump debug information to console")
  global_option('-p','--pretty', "Generate pretty output where possible")

  command :getocpapi do |c|
    c.syntax = 'ocp getocpapi [options]'
    c.description = 'Retrieve the OpenShift Container Platform API'
    c.action do |args, options|

      ocpapi = OcpApi.new
      ocpapi.setup_by_config_file(options.config)
      ocpapi.list
      puts "#{ocpapi.response}"
    end
  end

  command :getapi do |c|
    c.syntax = 'ocp getapi [options]'
    c.summary = 'Retrieve the Kubernetes API'
    c.description = ''
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      api = Api.new
      api.setup_by_config_file(options.config)
      puts "#{api.showapi}"
    end
  end

  command :projectexists do |c|
    c.syntax = 'ocp projectexists [options]'
    c.summary = 'Check if a project exists'
    c.description = 'Check if a project exists'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      proj = Project.new
      proj.setup_by_config_file(options.config, options.pretty, options.debug)

      if proj.exists?(options.name)
        puts "Project #{options.name} exists"
      else
        puts "Project #{options.name} doesn't exist"
      end

    end
  end

  command :listprojects do |c|
    c.syntax = 'ocp projectexists [options]'
    c.summary = 'Check if a project exists'
    c.description = 'Check if a project exists'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      proj = Project.new
      proj.setup_by_config_file(options.config, options.pretty, options.debug)

      puts "#{proj.listprojects}"

    end
  end

  command :list_projects_by_user do |c|
    c.syntax = 'ocp projectexists [options]'
    c.summary = 'List projects by user'
    c.description = 'List projects by user'
    c.option '--username USERNAME', String, 'The user to search by'
    c.option '--role ROLE', String, 'The optional role to filter by'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      valid_project_list = Array.new
      proj = Project.new
      proj.setup_by_config_file(options.config, options.pretty, options.debug)

      proj.list_projects_array.each do |projname|
        role_bindings = RoleBinding.new(projname)

        bindings_list = JSON.parse role_bindings.to_s
        list = bindings_list['items']
        list.each do |key|

          unless options.role.nil?
            if key['metadata']['name'].eql? options.role and !key['userNames'].nil? and key['userNames'].include? options.username
              valid_project_list.push projname
            end
          else
            if !key['userNames'].nil? and key['userNames'].include? options.username
              valid_project_list.push projname
            end
          end
        end

      end

      puts "#{valid_project_list}"

    end
  end

  command :createproject do |c|
    c.syntax = 'ocp createproject [options]'
    c.summary = 'Create a project request'
    c.description = 'Create a project request'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.option '--description [DESCRIPTION]', String, 'The description for the project'
    c.option '--displayname [DISPLAYNAME]', String, 'The displayname for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      projreq = ProjectRequest.new
      projreq.setup_by_config_file(options.config)

      data = projreq.createprojectrequest(options.name, options.description, options.displayname)

      puts "#{data}"
    end
  end

  command :deleteproject do |c|
    c.syntax = 'ocp deleteproject [options]'
    c.summary = 'Delete a project'
    c.description = 'Delete a project'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getapi
      project = Project.new
      project.setup_by_config_file(options.config)

      data = project.deleteproject(options.name)

      puts "#{data}"
    end
  end

  command :listpolicies do |c|
    c.syntax = 'ocp listpolicies [options]'
    c.description = 'Retrieve the Policies for a NameSpace'
    c.option '--name PROJECTNAME', String, 'The name for the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      policy = Policy.new
      policy.setup_by_config_file(options.config)
      puts "#{policy}"
    end
  end

  command :listoauthclientauthorizations do |c|
    c.syntax = 'ocp oauthclientauthorizations [options]'
    c.description = 'Retrieve the OAuthClientAuthorizations'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      oauthpolicy = OAuthClientAuthorization.new
      oauthpolicy.setup_by_config_file(options.config)
      puts "#{oauthpolicy}"
    end
  end

  command :listroles do |c|
    c.syntax = 'ocp listroles [options]'
    c.description = 'Retrieve the Roles'
    c.option '--name [PROJECTNAME]', String, 'Optionally the name of the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      roles = nil
      if options.name.nil?
        roles = Role.new
      else
        roles = Role.new(options.name)
      end
      roles.setup_by_config_file(options.config)

      puts "#{roles}"
    end
  end

  command :listrolebindings do |c|
    c.syntax = 'ocp listrolebindings [options]'
    c.description = 'Retrieve the Roles Bindings'
    c.option '--name [PROJECTNAME]', String, 'Optionally the name of the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      roles = nil
      if options.name.nil?
        roles = RoleBinding.new
      else
        roles = RoleBinding.new(options.name)
      end
      roles.setup_by_config_file(options.config, options.pretty, options.debug)


      puts "#{roles}"
    end
  end

  command :create_role_binding do |c|
    c.syntax = 'ocp createrolebinding [options]'
    c.description = 'Create a Project Roles Binding'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.option '--user USERNAME', String, 'The name of the user'
    c.option '--role ROLE', String, 'The role to add to the user'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      roles = RoleBinding.new(options.name)

      roles.setup_by_config_file(options.config, options.pretty, options.debug)
      resp = roles.create_role_binding(options.user, options.role)
      puts "#{resp}"

    end
  end

  command :update_role_binding do |c|
    c.syntax = 'ocp update_role_binding [options]'
    c.description = 'Update a Project Roles Binding'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.option '--user USERNAME', String, 'The name of the user'
    c.option '--role ROLE', String, 'The role to add to the user'
    c.action do |args, options|

      roles = RoleBinding.new(options.name)

      roles.setup_by_config_file(options.config, options.pretty, options.debug)
      resp = roles.update_role_binding(options.user, options.role)
      puts "#{resp}"

    end
  end

  command :list_resource_quotas do |c|
    c.syntax = 'ocp list_resource_quotas [options]'
    c.description = 'List a Project\'s Resource Quotas'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.action do |args, options|
      # Do something or c.when_called Ocp::Commands::Getoapi,
      rq = ResourceQuota.new(options.name)

      rq.setup_by_config_file(options.config, options.pretty, options.debug)
      resp = rq.list_resource_quotas
      puts "#{resp}"

    end
  end

  command :create_resource_quota do |c|
    c.syntax = 'ocp create_resource_quota [options]'
    c.description = 'Create a Project\'s Resource Quota'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.option '--pods PODS', Integer, 'The number of pods'
    c.option '--replicationcontrollers RCS', Integer, 'The number of replication controllers'
    c.option '--services SERVICES', Integer, 'The number of services'
    c.option '--configmaps CONFIGMAPS', Integer, 'The number of configmaps'
    c.option '--persistentvolumeclaims PVCS', Integer, 'The number of persistent volume claims'
    c.option '--resourcequotas RQS', Integer, 'The number of resource quotas'
    c.action do |args, options|
      rq = ResourceQuota.new(options.name)

      rq.setup_by_config_file(options.config, options.pretty, options.debug)
      resp = rq.create_resource_quota(options.name, options.pods,
                  options.replicationcontrollers, options.services,
                  options.configmaps, options.persistentvolumeclaims, options.resourcequotas)
      puts "#{resp}"
    end
  end
  command :delete_resource_quota do |c|
    c.syntax = 'ocp delete_resource_quota [options]'
    c.description = 'Delete a Project\'s Resource Quota'
    c.option '--name PROJECTNAME', String, 'The name of the project'
    c.option '--quotaname QUOTANAME', String, 'The name of the quota object'
    c.action do |args, options|
      rq = ResourceQuota.new(options.name)

      rq.setup_by_config_file(options.config)
      resp = rq.delete_resource_quota(options.quotaname)
      puts "#{resp}"

    end
  end



  run!
end