Class: AnalyticsOps::ServiceAccount::Store
- Inherits:
-
Object
- Object
- AnalyticsOps::ServiceAccount::Store
- Defined in:
- lib/analytics_ops/service_account.rb,
sig/analytics_ops.rbs
Overview
User-level pointer to a validated key. The key itself is never copied.
Constant Summary collapse
- VERSION =
2- LEGACY_VERSION =
1- MAX_BYTES =
64 * 1024
- FIELDS =
%w[configs connections version].freeze
- LEGACY_FIELDS =
%w[service_account_path version].freeze
- CONFIG_FIELDS =
%w[profile_connections selected_profile].freeze
- CONNECTION_NAME =
/\A[A-Za-z][A-Za-z0-9_-]{0,63}\z/- PROFILE_NAME =
/\A[A-Za-z][A-Za-z0-9_]{0,63}\z/
Instance Attribute Summary collapse
-
#path ⇒ String
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #connection_name_for(service_account_path, preferred:, config:, profile:) ⇒ Object
-
#initialize(path: nil) ⇒ Store
constructor
A new instance of Store.
- #profile_connection(config:, profile:) ⇒ String?
- #read(name: nil, config: nil, profile: nil) ⇒ String
- #resolve_connection_name(name: nil, config: nil, profile: nil) ⇒ String
- #select(config:, profile:, connection: nil) ⇒ record
- #selected_profile(config:) ⇒ String?
- #selection(config:) ⇒ record?
- #summaries ⇒ Array[record]
- #write(service_account_path, name: "default", config: nil, profile: nil, select: true) ⇒ Object
Constructor Details
#initialize(path: nil) ⇒ Store
Returns a new instance of Store.
144 145 146 147 148 149 150 151 |
# File 'lib/analytics_ops/service_account.rb', line 144 def initialize(path: nil) candidate = path || self.class.default_path unless candidate.is_a?(String) && !candidate.empty? && !candidate.match?(CONTROL_CHARACTERS) raise AuthenticationError, "The Analytics Ops connection path is invalid" end @path = File.(candidate).freeze end |
Instance Attribute Details
#path ⇒ String (readonly)
Returns the value of attribute path.
136 137 138 |
# File 'lib/analytics_ops/service_account.rb', line 136 def path @path end |
Class Method Details
.default_path ⇒ String
138 139 140 141 142 |
# File 'lib/analytics_ops/service_account.rb', line 138 def self.default_path File.join(Dir.home, ".config", "analytics_ops", "connection.json") rescue ArgumentError raise AuthenticationError, "Cannot determine the user configuration directory" end |
Instance Method Details
#connection_name_for(service_account_path, preferred:, config:, profile:) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/analytics_ops/service_account.rb', line 163 def connection_name_for(service_account_path, preferred:, config:, profile:) normalized = ServiceAccount.new(service_account_path).path validate_association!(config, profile) preferred_name = validate_connection_name!(preferred) saved = read_document(required: false) associated = associated_connection(saved, config:, profile:) return associated if associated connections = saved.fetch("connections") return preferred_name unless connections.key?(preferred_name) return preferred_name if connections.fetch(preferred_name) == normalized unique_connection_name(connections, preferred_name, normalized) end |
#profile_connection(config:, profile:) ⇒ String?
220 221 222 223 224 225 226 227 228 |
# File 'lib/analytics_ops/service_account.rb', line 220 def profile_connection(config:, profile:) saved = read_document(required: false) associated = associated_connection(saved, config:, profile:) return associated if associated resolve_name(saved, name: nil, config:, profile:) rescue AuthenticationError nil end |
#read(name: nil, config: nil, profile: nil) ⇒ String
153 154 155 156 157 |
# File 'lib/analytics_ops/service_account.rb', line 153 def read(name: nil, config: nil, profile: nil) saved = read_document(required: true) selected = resolve_name(saved, name:, config:, profile:) saved.fetch("connections").fetch(selected) end |
#resolve_connection_name(name: nil, config: nil, profile: nil) ⇒ String
159 160 161 |
# File 'lib/analytics_ops/service_account.rb', line 159 def resolve_connection_name(name: nil, config: nil, profile: nil) resolve_name(read_document(required: true), name:, config:, profile:) end |
#select(config:, profile:, connection: nil) ⇒ record
195 196 197 198 199 200 201 202 |
# File 'lib/analytics_ops/service_account.rb', line 195 def select(config:, profile:, connection: nil) validate_association!(config, profile) saved = read_document(required: true) selected = resolve_name(saved, name: connection, config:, profile:) associate!(saved, config:, profile:, connection: selected, select: true) persist(saved) selection(config:) end |
#selected_profile(config:) ⇒ String?
216 217 218 |
# File 'lib/analytics_ops/service_account.rb', line 216 def selected_profile(config:) selection(config:)&.fetch("profile") end |
#selection(config:) ⇒ record?
204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/analytics_ops/service_account.rb', line 204 def selection(config:) saved = read_document(required: false) entry = saved.fetch("configs")[config_key(config)] return nil unless entry profile = entry.fetch("selected_profile") { "profile" => profile, "connection" => entry.fetch("profile_connections").fetch(profile) }.freeze end |
#summaries ⇒ Array[record]
230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/analytics_ops/service_account.rb', line 230 def summaries saved = read_document(required: false) selected_names = saved.fetch("configs").values.flat_map do |entry| entry.fetch("profile_connections").values end.uniq saved.fetch("connections").sort.map do |name, service_account_path| { "name" => name, "available" => File.file?(service_account_path), "in_use" => selected_names.include?(name) }.freeze end.freeze end |
#write(service_account_path, name: "default", config: nil, profile: nil, select: true) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/analytics_ops/service_account.rb', line 178 def write(service_account_path, name: "default", config: nil, profile: nil, select: true) normalized = ServiceAccount.new(service_account_path).path connection_name = validate_connection_name!(name) validate_association!(config, profile) raise AuthenticationError, "select must be true or false" unless [true, false].include?(select) saved = read_document(required: false) if connection_name != "default" && saved.fetch("configs").empty? && saved.fetch("connections") == { "default" => normalized } saved.fetch("connections").delete("default") end saved.fetch("connections")[connection_name] = normalized associate!(saved, config:, profile:, connection: connection_name, select:) if config persist(saved) end |