module Qtc::Cli::Common

Attributes

datacenter_id[RW]

Public Instance Methods

client() click to toggle source

@return [Qtc::Client]

# File lib/qtc/cli/common.rb, line 131
def client
  if @client.nil?
    @client = Qtc::Client.new(base_url)
  end

  @client
end
current_cloud_dc() click to toggle source

@return [String]

# File lib/qtc/cli/common.rb, line 34
def current_cloud_dc
  unless @current_cloud_dc
    unless inifile['platform']['current_cloud']
      raise ArgumentError.new("Please specify used cloud first: qtc-cli clouds:use <id>")
    end
    @current_cloud_dc = inifile['platform']['current_dc']
  end
  @current_cloud_dc
end
current_cloud_id() click to toggle source

@return [String]

# File lib/qtc/cli/common.rb, line 21
def current_cloud_id
  unless @current_cloud_id
    unless inifile['platform']['current_cloud']
      raise ArgumentError.new("Please specify used cloud first: qtc-cli clouds:use <id>")
    end
    @current_cloud_id = inifile['platform']['current_cloud']
    self.datacenter_id = inifile['platform']['current_dc']
  end
  @current_cloud_id
end
current_cloud_token() click to toggle source

@return [String]

# File lib/qtc/cli/common.rb, line 46
def current_cloud_token
  token = nil
  begin
    authorizations = platform_client.get("/accounts/#{current_cloud_id}/authorizations")
    unless authorizations['results'][0]
      platform_client.post("/accounts/#{current_cloud_id}/authorizations", {})
      raise StandardError.new "retry"
    end
    token = authorizations['results'][0]['access_token']
  rescue ArgumentError => e
    raise e
  rescue Qtc::Errors::StandardError => e
    if e.status == 404
      raise ArgumentError.new("Cloud not found. Please specify used cloud first: qtc-cli clouds:use <id>")
    end
  rescue => e
    retry
  end

  token
end
extract_app_in_dir(remote = nil) click to toggle source

@param [String,NilClass] @return [String]

# File lib/qtc/cli/common.rb, line 111
def extract_app_in_dir(remote = nil)
  if File.exists?(File.expand_path('./.git/config'))
    remotes = []
    git_remotes = `git remote -v`
    git_remotes.lines.each do |line|
      if match = line.match(/#{remote}\s+git@git-mar-(.*)\:(.*) \(push\)/)
        remotes << match[2]
      end
    end
    apps = remotes.uniq
    if apps.size == 1
      return apps[0]
    elsif apps.size > 1
      raise ArgumentError.new("Multiple app git remotes\nSpecify app with --remote REMOTE or --app APP")
    end
  end
end
ini_filename() click to toggle source

@return [String]

# File lib/qtc/cli/common.rb, line 86
def ini_filename
  File.join(Dir.home, '/.qtc_client')
end
inifile() click to toggle source

@return [Hash]

# File lib/qtc/cli/common.rb, line 92
def inifile
  if @inifile.nil?
    if File.exists?(ini_filename)
      @inifile = IniFile.load(ini_filename)
    else
      @inifile = IniFile.new
    end
  end

  unless @inifile['platform']
    @inifile['platform'] = {}
  end

  @inifile
end
instance_info(instance_id) click to toggle source

@param [String] instance_id

# File lib/qtc/cli/common.rb, line 10
def instance_info(instance_id)
  instance_data = platform_client.get("/instances/#{instance_id}")
  unless instance_data
    abort("Error: instance not found")
  end

  instance_data
end
platform_base_url() click to toggle source

@return [String]

# File lib/qtc/cli/common.rb, line 141
def platform_base_url
  ENV['QTC_PLATFORM_URL'] || 'https://api.qtc.io/v1'
end
platform_client(token = nil) click to toggle source

@param [String] token @return [Qtc::Client]

# File lib/qtc/cli/common.rb, line 71
def platform_client(token = nil)
  inifile['platform']['token'] = token unless token.nil?
  unless inifile['platform']['token']
    raise ArgumentError.new("Please login first using: qtc-cli login")
  end

  if @platform_client.nil?
    @platform_client = Qtc::Client.new(platform_base_url, {'Authorization' => "Bearer #{inifile['platform']['token']}"})
  end

  @platform_client
end