Class: CloudControl::API::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudcontrol/api/core.rb

Direct Known Subclasses

Account, Drs, Image, Infrastructure, Network, Report, Server, Tagging, VIP

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Core

Returns a new instance of Core.



4
5
6
# File 'lib/cloudcontrol/api/core.rb', line 4

def initialize(client)
  @client = client
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



3
4
5
# File 'lib/cloudcontrol/api/core.rb', line 3

def client
  @client
end

Instance Method Details

#endpoint(e) ⇒ Object

request options



19
20
21
# File 'lib/cloudcontrol/api/core.rb', line 19

def endpoint(e)
  @endpoint = e
end

#getObject

perform request



41
42
43
# File 'lib/cloudcontrol/api/core.rb', line 41

def get
  perform :get
end

#get_simpleObject



49
50
51
# File 'lib/cloudcontrol/api/core.rb', line 49

def get_simple
    perform :get, true
end

#log(*args) ⇒ Object

client methods



9
10
11
# File 'lib/cloudcontrol/api/core.rb', line 9

def log(*args)
  @client.log(*args)
end

#org_endpoint(e) ⇒ Object



23
24
25
# File 'lib/cloudcontrol/api/core.rb', line 23

def org_endpoint(e)
  endpoint("/#{org_id}" + e)
end

#org_idObject



13
14
15
# File 'lib/cloudcontrol/api/core.rb', line 13

def org_id
  @client.org_id
end

#perform(method, simple = false) ⇒ Object

do not parse response if simple if simple, post body is also simple…



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/cloudcontrol/api/core.rb', line 59

def perform(method, simple=false)
  if simple
    request = @client.build_request(method, @endpoint, request_query_string, request_simple_body, false)
  else
    request = @client.build_request(method, @endpoint, request_query_string, request_xml_body)
  end
  response = @client.perform_request(request)

  @client.log_response(request, response)

  # return parsed object if it's good
  if response.success?
    if simple
      result = response.body
    else
      result = @client.parse_response_xml_body(response.body)
      if result['total_count']
        log "#{result['total_count']} total", :yellow, :bold
        result.delete('page_size')
        result.delete('total_count')
        result.delete('page_count')
        result.delete('page_number')
      end
      # unwind some arrays of elements
      result.values.count == 1 ? result.values.first : result
    end
  else
    {}
  end
end

#postObject



45
46
47
# File 'lib/cloudcontrol/api/core.rb', line 45

def post
  perform :post
end

#post_simpleObject



53
54
55
# File 'lib/cloudcontrol/api/core.rb', line 53

def post_simple
    perform :post, true
end

#query_params(q) ⇒ Object



27
28
29
# File 'lib/cloudcontrol/api/core.rb', line 27

def query_params(q)
  @query_params = q
end

#request_query_stringObject

build request



101
102
103
104
105
106
# File 'lib/cloudcontrol/api/core.rb', line 101

def request_query_string
  fparams = @client.filter_params || {}
  qparams = @query_params || {}
  params = fparams.merge(qparams)
  @client.url_query(params) if params.present?
end

#request_simple_bodyObject



117
118
119
120
121
122
# File 'lib/cloudcontrol/api/core.rb', line 117

def request_simple_body
  return if @simple_params.blank?
  body = @client.build_request_simple_body(@simple_params)
  log(body, :green)
  body
end

#request_xml_bodyObject



108
109
110
111
112
113
114
115
116
# File 'lib/cloudcontrol/api/core.rb', line 108

def request_xml_body
  return if @xml_params.blank?
  schema = @xml_params.delete(:schema)
  tag = @xml_params.delete(:tag)

  body = @client.build_request_xml_body(schema, tag, @xml_params)
  log(body, :green)
  body
end

#simple_params(x) ⇒ Object



35
36
37
# File 'lib/cloudcontrol/api/core.rb', line 35

def simple_params(x)
  @simple_params = x
end

#single(results) ⇒ Object



90
91
92
93
94
95
96
# File 'lib/cloudcontrol/api/core.rb', line 90

def single(results)
  if results.is_a? Array
    results.first
  else
    results
  end
end

#xml_params(x) ⇒ Object



31
32
33
# File 'lib/cloudcontrol/api/core.rb', line 31

def xml_params(x)
  @xml_params = x
end