Class: CpOraclecloud::SshKey

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Naming
Includes:
ActiveModel::Conversion, ActiveModel::Validations
Defined in:
app/models/cp_oraclecloud/ssh_key.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ SshKey

Returns a new instance of SshKey.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 13

def initialize(attributes = {})
  if !attributes[:name].blank?
    regex = attributes[:name].match(/\/.*\/(.*)/)
    if regex && regex.length > 1
      clean_name = regex[1]
      attributes[:id] = clean_name
      attributes[:name] = clean_name
    end
  end
  attributes.each do |field, value|
    send("#{field}=", value)
  end
end

Instance Attribute Details

#enabledObject

Returns the value of attribute enabled.



7
8
9
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 7

def enabled
  @enabled
end

#idObject

Returns the value of attribute id.



7
8
9
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 7

def id
  @id
end

#keyObject

Returns the value of attribute key.



7
8
9
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 7

def key
  @key
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 7

def name
  @name
end

#uriObject

Returns the value of attribute uri.



7
8
9
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 7

def uri
  @uri
end

Class Method Details

.allObject



39
40
41
42
43
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 39

def self.all
  result = []
  connection.ssh_keys.each { |k| result.push(CpOraclecloud::SshKey.new(k.attributes))}
  result
end

.connectionObject



69
70
71
72
73
74
75
76
77
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 69

def self.connection
  @connection ||= Fog::Compute.new(
    :provider => 'OracleCloud',
    :oracle_username => CpOraclecloud.username,
    :oracle_password => CpOraclecloud.password,
    :oracle_domain => CpOraclecloud.domain,
    :oracle_compute_api => CpOraclecloud.compute_api
    )
end

.create(params) ⇒ Object



50
51
52
53
54
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 50

def self.create(params)
  connection.ssh_keys.create(:name => params[:name],
                             :enabled => params[:enabled],
                             :key => params[:key])
end

.delete(id) ⇒ Object



65
66
67
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 65

def self.delete(id)
  connection.ssh_keys.get(id).destroy
end

.find_by_id(id) ⇒ Object



45
46
47
48
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 45

def self.find_by_id(id)
  data = connection.ssh_keys.get(id)
  CpOraclecloud::SshKey.new(data.attributes)
end

Instance Method Details

#persisted?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 35

def persisted?
  !id.blank?
end

#to_modelObject



31
32
33
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 31

def to_model
  self
end

#to_sObject



27
28
29
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 27

def to_s
  name
end

#update(params) ⇒ Object



56
57
58
59
60
61
62
63
# File 'app/models/cp_oraclecloud/ssh_key.rb', line 56

def update(params)
  fog_key = self.class.connection.ssh_keys.get(id)

  [:name, :enabled, :key].each do |field|
    fog_key.attributes[field.to_sym] = params[field.to_sym]
  end
  fog_key.update
end