Class: LMC::Cloud

Inherits:
Object
  • Object
show all
Defined in:
lib/lmc/Cloud.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cloud_host, user, pass, auth = true) ⇒ Cloud

Returns a new instance of Cloud.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/lmc/Cloud.rb', line 21

def initialize(cloud_host, user, pass, auth = true)
  @auth_ok = false
  @cloud_host = cloud_host
  @user = user
  @password = pass
  @verify_tls = Cloud.verify_tls
  @last_authorized_account_ids = nil
  @logger ||= ::LMC::Logger.new(STDOUT) if Cloud.debug
  @logger.cloud = self if Cloud.debug
  RestClient.log = @logger if Cloud.debug
  authorize if auth
end

Class Attribute Details

.cloud_hostObject

Returns the value of attribute cloud_host.



10
11
12
# File 'lib/lmc/Cloud.rb', line 10

def cloud_host
  @cloud_host
end

.debugObject

Returns the value of attribute debug.



10
11
12
# File 'lib/lmc/Cloud.rb', line 10

def debug
  @debug
end

.passwordObject

Returns the value of attribute password.



10
11
12
# File 'lib/lmc/Cloud.rb', line 10

def password
  @password
end

.use_tlsObject

Returns the value of attribute use_tls.



10
11
12
# File 'lib/lmc/Cloud.rb', line 10

def use_tls
  @use_tls
end

.userObject

Returns the value of attribute user.



10
11
12
# File 'lib/lmc/Cloud.rb', line 10

def user
  @user
end

.verboseObject

Returns the value of attribute verbose.



10
11
12
# File 'lib/lmc/Cloud.rb', line 10

def verbose
  @verbose
end

.verify_tlsObject

Returns the value of attribute verify_tls.



10
11
12
# File 'lib/lmc/Cloud.rb', line 10

def verify_tls
  @verify_tls
end

Instance Attribute Details

#auth_okObject (readonly)

Returns the value of attribute auth_ok.



19
20
21
# File 'lib/lmc/Cloud.rb', line 19

def auth_ok
  @auth_ok
end

#cloud_hostObject (readonly)

Returns the value of attribute cloud_host.



19
20
21
# File 'lib/lmc/Cloud.rb', line 19

def cloud_host
  @cloud_host
end

#passwordObject (readonly)

Returns the value of attribute password.



19
20
21
# File 'lib/lmc/Cloud.rb', line 19

def password
  @password
end

#userObject (readonly)

Returns the value of attribute user.



19
20
21
# File 'lib/lmc/Cloud.rb', line 19

def user
  @user
end

Class Method Details

.instance(opts = { authorize: true }) ⇒ Object



15
16
17
# File 'lib/lmc/Cloud.rb', line 15

def self.instance(opts = { authorize: true })
  @@inst ||= new(@cloud_host, @user, @password, opts[:authorize])
end

Instance Method Details

#accept_tos(tos) ⇒ Object



140
141
142
# File 'lib/lmc/Cloud.rb', line 140

def accept_tos(tos)
  authorize([], tos)
end

#auth_for_account(account) ⇒ Object



136
137
138
# File 'lib/lmc/Cloud.rb', line 136

def ()
  authorize([])
end

#auth_for_accounts(accounts) ⇒ Object



132
133
134
# File 'lib/lmc/Cloud.rb', line 132

def auth_for_accounts(accounts)
  authorize(accounts)
end

#build_url(*path_components) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/lmc/Cloud.rb', line 124

def build_url(*path_components)
  protocol = 'https'
  if !Cloud.use_tls
    protocol = 'http'
  end
  ["#{protocol}://#{@cloud_host}", path_components].flatten.compact.join('/')
end

#delete(path, params = nil) ⇒ Object



106
107
108
109
110
111
112
113
114
115
# File 'lib/lmc/Cloud.rb', line 106

def delete(path, params = nil)
  prepared_headers = headers
  prepared_headers[:params] = params
  args = {
      :method => :delete,
      :url => build_url(path),
      :headers => prepared_headers
  }
  execute_request args
end

#get(path, params = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/lmc/Cloud.rb', line 71

def get(path, params = nil)
  prepared_headers = headers
  prepared_headers[:params] = params
  args = {
      :method => :get,
      :url => build_url(path),
      :headers => prepared_headers
  }
  execute_request args
end

#get_accountsObject



43
44
45
# File 'lib/lmc/Cloud.rb', line 43

def get_accounts
  get 'cloud-service-auth/accounts'
end

#get_accounts_objectsObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lmc/Cloud.rb', line 47

def get_accounts_objects
  result = get ['cloud-service-auth', 'accounts']
  if result.code == 200
    accounts = result.map do |aj|
      Account.new(self, aj)
    end
  else
    raise "Unable to fetch accounts: #{result.body.message}"
  end

  accounts
end

#get_backstage_serviceinfosObject



39
40
41
# File 'lib/lmc/Cloud.rb', line 39

def get_backstage_serviceinfos
  get 'cloud-service-backstage/serviceinfos'
end

#inspectObject

hide password from dumps



35
36
37
# File 'lib/lmc/Cloud.rb', line 35

def inspect
  "#<Cloud:#{object_id}, #{build_url}>"
end

#invite_user_to_account(email, account_id, type, authorities = []) ⇒ Object



60
61
62
63
64
# File 'lib/lmc/Cloud.rb', line 60

def (email, , type, authorities = [])
  body = { name: email, state: 'ACTIVE', type: type }
  body['authorities'] = authorities
  post ['cloud-service-auth', 'accounts', , 'members'], body
end

#post(path, body_object, params = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lmc/Cloud.rb', line 94

def post(path, body_object, params=nil )
  prepared_headers = headers
  prepared_headers[:params] = params
  args = {
      :method => :post,
      :url => build_url(path),
      :payload => body_object.to_json,
      :headers => prepared_headers
  }
  execute_request args
end

#preferences(section) ⇒ Object

Parameters:

  • section

    Array of String to indicate section to access. Example: [‘principal’, ‘self’, ‘ui’]



67
68
69
# File 'lib/lmc/Cloud.rb', line 67

def preferences(section)
  LMC::Preferences.new cloud: self, section: section
end

#put(path, body_object, params = nil) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/lmc/Cloud.rb', line 82

def put(path, body_object, params = nil)
  prepared_headers = headers
  prepared_headers[:params] = params
  args = {
      :method => :put,
      :url => build_url(path),
      :payload => body_object.to_json,
      :headers => prepared_headers
  }
  execute_request args
end

#session_tokenObject

public accessors



120
121
122
# File 'lib/lmc/Cloud.rb', line 120

def session_token
  @auth_token['value']
end