Class: RightScaleCLI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/rightscale_cli/client.rb

Overview

Represents a RightScale CLI Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rightscale_cli/client.rb', line 28

def initialize(options)
  config = RightScaleCLI::Config.new.directives
  config[:account_id] = options['account'] if options[:account]
  config[:email] = options['user'] if options[:user]
  config[:api_version] = options['api'] if options[:api]
  config[:timeout] = nil

  if options['password'] || \
     (!config[:password] && \
      !config[:password_base64] &&
      !config[:access_token])
    # fallback to asking for user password
    config[:password] = ask_pass
    # set this to nil so it is not used by precedence
    config[:password_base64] = nil
  end

  @options = options
  @client = RightApi::Client.new(config)
  @logger = RightScaleCLI::Logger.new
end

Instance Attribute Details

#clientObject

Returns the value of attribute client.



25
26
27
# File 'lib/rightscale_cli/client.rb', line 25

def client
  @client
end

#render(data, root_element) ⇒ Object

Returns the value of attribute render.



26
27
28
# File 'lib/rightscale_cli/client.rb', line 26

def render
  @render
end

Instance Method Details

#create(resource, params) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/rightscale_cli/client.rb', line 69

def create(resource, params)
  if params[:cloud]
    resource = @client.clouds(id: params[:cloud]).show.send("#{resource}s")
               .create(params)
  else
    resource = @client.send("#{resource}s").create(params)
  end
  @logger.info("Created #{resource.href}.")
  resource.href
end

#destroy(resource, resource_id) ⇒ Object



80
81
82
83
84
# File 'lib/rightscale_cli/client.rb', line 80

def destroy(resource, resource_id)
  resource = @client.send("#{resource}s").index(id: resource_id)
  resource.destroy
  @logger.info("Deleted #{resource.href}.")
end

#get(resource) ⇒ Object



50
51
52
53
54
# File 'lib/rightscale_cli/client.rb', line 50

def get(resource)
  result = []
  @client.send(resource).index.each { |record| result.push(record.raw) }
  result
end

#show(resource, resource_id, *args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rightscale_cli/client.rb', line 56

def show(resource, resource_id, *args)
  if args.count > 0
    result = []
    records = @client.send(resource).index(id: resource_id)
              .show.send(args[0]).index
    records.each { |record| result.push(record.raw) }
    @logger.info("Resource count: #{result.count}.")
  else
    result = @client.send(resource).index(id: resource_id).show.raw
  end
  result
end