Module: Brainiac::Plugins::Fizzy::EnvUrl

Defined in:
lib/brainiac/plugins/fizzy/env_url.rb

Overview

Derives the public URL of an ephemeral Belt environment from its terraform.tfvars. Kept separate from Helpers so the pure URL/HCL logic stays small and independently testable.

Class Method Summary collapse

Class Method Details

.for_card(card_number) ⇒ Object

URL of the ephemeral Belt environment for a card, if one is active. The env name is fizzy-<card_number>; the public URL is derived from the env's terraform.tfvars, matching the frontend_urls convention in the Belt infra module:

parent set -> https://<env>.<parent>.<domain>
prod       -> https://<domain>
otherwise  -> https://<env>.<domain>


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/brainiac/plugins/fizzy/env_url.rb', line 19

def for_card(card_number)
  return nil unless defined?(BeltConfig)

  env_name = BeltConfig.ephemeral_env_for_card(card_number)
  return nil unless BeltConfig.ephemeral_env?(env_name)

  info = BeltConfig.respond_to?(:ephemeral_env_info) ? BeltConfig.ephemeral_env_info(env_name) : nil
  worktree = info && info["worktree"]
  return nil unless worktree

  tfvars = File.join(worktree, "infrastructure", env_name.to_s, "terraform.tfvars")
  return nil unless File.exist?(tfvars)

  from_tfvars(tfvars)
rescue StandardError => e
  LOG.warn "[Fizzy] Could not detect env URL for card ##{card_number}: #{e.message}" if defined?(LOG)
  nil
end

.from_tfvars(tfvars_path) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/brainiac/plugins/fizzy/env_url.rb', line 38

def from_tfvars(tfvars_path)
  vars = parse(File.read(tfvars_path))
  domain = vars["domain"].to_s
  return nil if domain.empty?

  environment = vars["environment"].to_s
  parent = vars["parent_environment"].to_s

  host =
    if !parent.empty?
      "#{environment}.#{parent}.#{domain}"
    elsif environment == "prod"
      domain
    else
      "#{environment}.#{domain}"
    end

  "https://#{host}"
end

.parse(contents) ⇒ Object

Minimal HCL parser for key = "value" lines in terraform.tfvars.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/brainiac/plugins/fizzy/env_url.rb', line 59

def parse(contents)
  vars = {}
  contents.each_line do |line|
    stripped = line.strip
    next if stripped.empty? || stripped.start_with?("#")

    match = stripped.match(/\A(\w+)\s*=\s*"([^"]*)"/)
    vars[match[1]] = match[2] if match
  end
  vars
end