Class: AnalyticsOps::ServiceAccount::Store

Inherits:
Object
  • Object
show all
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 =

Returns:

  • (Integer)
2
LEGACY_VERSION =

Returns:

  • (Integer)
1
MAX_BYTES =

Returns:

  • (Integer)
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: nil) ⇒ Store

Returns a new instance of Store.

Parameters:

  • path: (String, nil) (defaults to: nil)


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.expand_path(candidate).freeze
end

Instance Attribute Details

#pathString (readonly)

Returns the value of attribute path.

Returns:

  • (String)


136
137
138
# File 'lib/analytics_ops/service_account.rb', line 136

def path
  @path
end

Class Method Details

.default_pathString

Returns:

  • (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(, preferred:, config:, profile:)
  normalized = ServiceAccount.new().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?

Parameters:

  • config: (String)
  • profile: (String)

Returns:

  • (String, nil)


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

Parameters:

  • name: (String, nil) (defaults to: nil)
  • config: (String, nil) (defaults to: nil)
  • profile: (String, nil) (defaults to: nil)

Returns:

  • (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

Parameters:

  • name: (String, nil) (defaults to: nil)
  • config: (String, nil) (defaults to: nil)
  • profile: (String, nil) (defaults to: nil)

Returns:

  • (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

Parameters:

  • config: (String)
  • profile: (String)
  • connection: (String, nil) (defaults to: nil)

Returns:

  • (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?

Parameters:

  • config: (String)

Returns:

  • (String, nil)


216
217
218
# File 'lib/analytics_ops/service_account.rb', line 216

def selected_profile(config:)
  selection(config:)&.fetch("profile")
end

#selection(config:) ⇒ record?

Parameters:

  • config: (String)

Returns:

  • (record, nil)


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

#summariesArray[record]

Returns:

  • (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, |
    {
      "name" => name,
      "available" => File.file?(),
      "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(, name: "default", config: nil, profile: nil, select: true)
  normalized = ServiceAccount.new().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