Class: Fog::Identity::Proxmox::Domains

Inherits:
Proxmox::Collection show all
Defined in:
lib/fog/identity/proxmox/models/domains.rb

Overview

class Domains collection authentication

Instance Attribute Summary

Attributes inherited from Proxmox::Collection

#response

Instance Method Summary collapse

Methods inherited from Proxmox::Collection

#clear_ignored_attributes, #get, #load_response, #not_implemented, #read, #summary

Instance Method Details

#all(_options = {}) ⇒ Object


41
42
43
# File 'lib/fog/identity/proxmox/models/domains.rb', line 41

def all(_options = {})
  load_response(service.list_domains, 'domains')
end

#create(attributes = {}) ⇒ Object


59
60
61
62
63
64
65
66
67
68
# File 'lib/fog/identity/proxmox/models/domains.rb', line 59

def create(attributes = {})
  domain = new(realm: attributes[:realm])
  type_s = attributes[:type]
  tfa_s = attributes[:tfa]
  attr = attributes.reject { |k, _v| %i[realm type tfa].include? k }
  domain.type = to_type(type_s, attr)
  tfa = to_tfa(tfa_s)
  domain.type.tfa = tfa if tfa
  domain.create
end

#destroy(id) ⇒ Object


54
55
56
57
# File 'lib/fog/identity/proxmox/models/domains.rb', line 54

def destroy(id)
  domain = find_by_id(id)
  domain.destroy
end

#find_by_id(id) ⇒ Object


45
46
47
48
49
50
51
52
# File 'lib/fog/identity/proxmox/models/domains.rb', line 45

def find_by_id(id)
  response = service.get_domain(id)
  body = JSON.decode(response.body)
  data = body['data']
  data.store('realm', id)
  data.delete_if { |k, _v| k == 'digest' }
  to_domain(data)
end

#tfa_class(tfa) ⇒ Object


102
103
104
105
106
107
108
109
110
111
# File 'lib/fog/identity/proxmox/models/domains.rb', line 102

def tfa_class(tfa)
  if tfa == 'oath'
    tfa_class = Fog::Identity::Proxmox::Oath
  elsif tfa == 'yubico'
    tfa_class = Fog::Identity::Proxmox::Yubico
  else
    raise Fog::Proxmox::Errors::NotFound, 'domain tfa unknown'
  end
  tfa_class
end

#to_domain(hash) ⇒ Object


30
31
32
33
34
35
36
37
38
39
# File 'lib/fog/identity/proxmox/models/domains.rb', line 30

def to_domain(hash)
  realm = hash['realm']
  type_value = hash['type']
  tfa_value = hash['tfa']
  type_hash = hash.reject { |k, _v| %w[realm type tfa].include? k }
  type = to_type(type_value, type_hash)
  tfa = to_tfa(tfa_value)
  type.tfa = tfa if tfa
  new(realm: realm, type: type)
end

#to_tfa(tfa_s) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fog/identity/proxmox/models/domains.rb', line 74

def to_tfa(tfa_s)
  oath_rxp = /type=oath,step=(?<step>\d+),digits=(?<digits>\d+)/
  yubico_rxp = /type=yubico,id=(?<id>\w+),key=(?<key>\w+),url=(?<url>.+)/
  if oath_rxp.match(tfa_s)
    attributes = oath_rxp.named_captures
    type = 'oath'
  elsif yubico_rxp.match(tfa_s)
    attributes = yubico_rxp.named_captures
    type = 'yubico'
  end
  tfa_class(type).new(attributes) if type && attributes
end

#to_type(type, attributes) ⇒ Object


70
71
72
# File 'lib/fog/identity/proxmox/models/domains.rb', line 70

def to_type(type, attributes)
  type_class(type).new(attributes)
end

#type_class(type) ⇒ Object


87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/fog/identity/proxmox/models/domains.rb', line 87

def type_class(type)
  if type == 'pam'
    type_class = Fog::Identity::Proxmox::Pam
  elsif type == 'pve'
    type_class = Fog::Identity::Proxmox::Pve
  elsif type == 'ldap'
    type_class = Fog::Identity::Proxmox::Ldap
  elsif type == 'ad'
    type_class = Fog::Identity::Proxmox::Activedirectory
  else
    raise Fog::Proxmox::Errors::NotFound, 'domain type unknown'
  end
  type_class
end