Module: GClouder::Project::ID

Includes:
Config::CLIArgs, Config::Project, Shell
Defined in:
lib/gclouder/resources/project_id.rb

Class Method Summary collapse

Methods included from Shell

included, #shell

Methods included from Logging

#add, #bad, #change, #debug, #error, #fatal, #good, included, #info, log, loggers, #remove, report, #resource_state, setup, #warn, #warning

Methods included from Config::CLIArgs

check, #cli_args, cli_args, included, valid_resources

Methods included from Config::Project

project, #project

Methods included from Helpers

#hash_to_args, included, #module_exists?, #to_arg, #to_deep_merge_hash, #valid_json?

Class Method Details

.bailObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gclouder/resources/project_id.rb', line 27

def self.bail
  puts "not authenticated against API and --activate-service-accounts option not passed"
  puts ""
  puts "please either:"
  puts ""
  puts "  run: gcloud auth login && gcloud auth application-default login"
  puts ""
  puts "  or: specify --activate-service-accounts flag and make sure the relevant keys exist in the keys dir"
  puts ""
  exit 1
end

.currentObject



19
20
21
22
23
24
25
# File 'lib/gclouder/resources/project_id.rb', line 19

def self.current
  return if cli_args[:ignore_default_service_account]
  id = shell("gcloud auth list --format json | jq -r '.[] | select(.status == \"ACTIVE\") | .account'")
  return id if !id.empty?
  return if cli_args[:activate_service_accounts]
  bail
end

.defaultObject



59
60
61
# File 'lib/gclouder/resources/project_id.rb', line 59

def self.default
  shell("gcloud config set account #{project['project_id']}")
end

.dirObject



63
64
65
# File 'lib/gclouder/resources/project_id.rb', line 63

def self.dir
  cli_args[:keys_dir] || File.join(ENV["HOME"], "keys")
end

.idObject



10
11
12
# File 'lib/gclouder/resources/project_id.rb', line 10

def self.id
  @id
end

.key_file(project_id) ⇒ Object



67
68
69
# File 'lib/gclouder/resources/project_id.rb', line 67

def self.key_file(project_id)
  File.join(dir, "gcloud-service-key-#{project_id}.json")
end

.loadObject



14
15
16
17
# File 'lib/gclouder/resources/project_id.rb', line 14

def self.load
  @id ||= current
  switch(project["project_id"]) if @id.nil?
end

.rescueObject



46
47
48
49
50
51
52
53
# File 'lib/gclouder/resources/project_id.rb', line 46

def self.rescue
  if @id.nil?
    shell("gcloud config unset account")
    return
  end

  switch(@id)
end

.resetObject



55
56
57
# File 'lib/gclouder/resources/project_id.rb', line 55

def self.reset
  switch(project["project_id"])
end

.switch(project_id) ⇒ Object



39
40
41
42
43
44
# File 'lib/gclouder/resources/project_id.rb', line 39

def self.switch(project_id)
  return unless project_id
  if cli_args[:activate_service_accounts]
    shell("gcloud --quiet auth activate-service-account --key-file #{key_file(project_id)}")
  end
end