-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.rb
More file actions
135 lines (103 loc) · 3.62 KB
/
config.rb
File metadata and controls
135 lines (103 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# frozen_string_literal: true
require 'erb'
require 'yaml'
require 'json'
require 'logger'
require_relative '../lib/docker'
# NOTE: dotenv/load must come after ../lib/docker
require 'dotenv/load'
# Config class to hold/manage our configuration
class Config
# Define your Structs for Secrets:
Ucpath = Struct.new(:root, :id, :key, keyword_init: true)
Sis = Struct.new(:root, :id, :key, keyword_init: true)
Ldap = Struct.new(:host, :pass, keyword_init: true)
Secrets = Struct.new(:ucpath, :sis, :ldap, keyword_init: true)
class << self
attr_reader :secrets, :settings, :help
def load!
# Load the ENV vars
Docker::Secret.setup_environment!
load_settings!('config/settings.yml')
load_secrets!('config/secrets.yml')
end
# Returns specified field value from settings.yml
def setting(field)
@settings[field.to_sym]
end
def ucpath_employee_fields
@ucpath_fields['employee']['fields']
end
def sis_fields
@sis_fields['SIS']['fields']
end
def ucpath_job_fields
@ucpath_fields['job']['fields']
end
def student_affiated?(affiliation)
@ldap_fields['student_affiliation'].include? affiliation
end
def ldap_attributes
@ldap_fields['attributes']
end
def check_ucpath_code(type, value)
@ucpath_codes[type].include? value
end
# If the Job Code is in either the fte_check_exclusions or emeritus_job_code
# lists, you must skip the FTE check..., otherwise DO NOT skip the check.
def skip_fte_check?(job_code)
return true if check_ucpath_code('fte_check_exclusions', job_code)
return true if check_ucpath_code('emeritus_job_code', job_code)
false
end
private
def load_settings!(path)
raw = yaml_with_erb(path)
# Load those config settings from the yaml hash:
@settings = create_struct_from_hash(
name: 'Settings',
hash: raw.fetch('settings')
)
# keep help separate...it's just a string
@help = raw['help']
end
def load_secrets!(path)
raw = yaml_with_erb(path)
@secrets = Secrets.new(
ucpath: Ucpath.new(**symbolize(raw.fetch('ucpath'))),
sis: Sis.new(**symbolize(raw.fetch('sis'))),
ldap: Ldap.new(**symbolize(raw.fetch('ldap')))
)
# Over-ride the LDAP host if we're in CI land... JUST to make
# sure we don't hit the actual host when we're running rspec!
@secrets.ldap.host = 'ldap.fake.edu' if ENV['CI']
end
def symbolize(hash)
hash.transform_keys(&:to_sym)
end
def yaml_with_erb(path)
YAML.safe_load(ERB.new(File.read(path)).result)
end
# Since settings isn't nested hash of hashes easy enough to create this struct dynamically:
def create_struct_from_hash(name:, hash:)
# Since Structs need symbols....
sym_hash = hash.transform_keys(&:to_sym)
struct_class = if const_defined?(name, false)
const_get(name)
else
const_set(name, Struct.new(*sym_hash.keys, keyword_init: true))
end
struct_class.new(**sym_hash)
end
end
# Let's do this!!!!
load!
ucpath_contents = File.read('config/ucpath_fields.yml')
@ucpath_fields = YAML.safe_load(ERB.new(ucpath_contents).result)
ldap_contents = File.read('config/ldap_fields.yml')
@ldap_fields = YAML.safe_load(ERB.new(ldap_contents).result)
ucpath_codes = File.read('config/ucpath_codes.yml')
@ucpath_codes = YAML.safe_load(ERB.new(ucpath_codes).result)
sis_contents = File.read('config/sis_fields.yml')
@sis_fields = YAML.safe_load(ERB.new(sis_contents).result)
end