Module: GClouder::Resource

Defined Under Namespace

Modules: Cleaner, Find

Class Method Summary collapse

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, load, valid_resources

Methods included from GCloud

#gcloud, included, #verify

Methods included from Config::Project

load, #project, project

Methods included from Helpers

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

Methods included from Shell

included, #shell

Class Method Details

.describe(resource, name, args = nil, force: true, failure: true, silent: false, project_id: nil) ⇒ Object



32
33
34
35
# File 'lib/gclouder/resource.rb', line 32

def self.describe(resource, name, args = nil, force: true, failure: true, silent: false, project_id: nil)
  return if resource?(resource, name, silent: silent, project_id: project_id)
  gcloud "#{resource} describe #{name} #{args}", force: force, failure: failure, silent: silent, project_id: project_id
end

.ensure(resource, name, args = nil, project_id: nil, extra_info: nil, silent: false, indent: 3, filter_key: "name", filter_value: nil, filter: "#{filter_key} ~ ^#{filter_value || name}$") ⇒ Object



37
38
39
40
41
# File 'lib/gclouder/resource.rb', line 37

def self.ensure(resource, name, args = nil, project_id: nil, extra_info: nil, silent: false, indent: 3, filter_key: "name", filter_value: nil, filter: "#{filter_key} ~ ^#{filter_value || name}$")
  return if resource?(resource, name, project_id: project_id, extra_info: extra_info, silent: silent, indent: indent, filter_key: filter_key, filter_value: filter_value, filter: filter)
  feedback("add", resource, name, extra_info: extra_info, indent: indent, silent: silent)
  gcloud "#{resource} create #{name} #{args}", project_id: project_id, silent: silent
end

.feedback(action, resource, name, extra_info: nil, indent: 3, silent: false) ⇒ Object



9
10
11
12
# File 'lib/gclouder/resource.rb', line 9

def self.feedback(action, resource, name, extra_info: nil, indent: 3, silent: false)
  return if silent
  send action, "#{name} #{extra_info}", indent: indent
end

.list(resource, args = nil, snake_case: false, project_id: nil) ⇒ Object



50
51
52
53
# File 'lib/gclouder/resource.rb', line 50

def self.list(resource, args = nil, snake_case: false, project_id: nil)
  list = gcloud "#{resource} list #{args}", force: true, project_id: project_id
  snake_case ? list.to_snake_keys : list
end

.purge(resource, name, args = nil, project_id: nil, silent: false, indent: 3, filter_key: "name", filter_value: nil, filter: "#{filter_key} ~ ^#{filter_value || name}$") ⇒ Object



43
44
45
46
47
48
# File 'lib/gclouder/resource.rb', line 43

def self.purge(resource, name, args = nil, project_id: nil, silent: false, indent: 3, filter_key: "name", filter_value: nil, filter: "#{filter_key} ~ ^#{filter_value || name}$")
  return unless resource?(resource, name, project_id: project_id, silent: true, indent: indent, filter_key: filter_key, filter_value: filter_value, filter: filter)
  extra_info = "[skipped] (--purge not specified)" unless cli_args[:purge]
  feedback("remove", resource, name, extra_info: extra_info, indent: indent, silent: silent)
  gcloud "#{resource} delete #{name} #{args}", project_id: project_id if cli_args[:purge]
end

.resource?(resource, name, args = nil, filter_key: "name", filter_value: nil, filter: "#{filter_key} ~ ^#{filter_value || name}$", project_id: nil, silent: false, extra_info: nil, indent: 3) ⇒ Boolean

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gclouder/resource.rb', line 14

def self.resource?(resource, name, args = nil, filter_key: "name", filter_value: nil, filter: "#{filter_key} ~ ^#{filter_value || name}$", project_id: nil, silent: false, extra_info: nil, indent: 3)

  exists = \
    gcloud("#{resource} list --filter '#{filter}' #{args} | jq '. | length'", force: true, project_id: project_id) > 0

  # if silent is specified then shell call returns boolean, otherwise integer
  if exists.is_a?(Integer)
    exists = exists > 0 ? true : false
  end

  if exists
    feedback("good", resource, name, extra_info: extra_info, indent: indent, silent: silent) unless silent
    return true
  end

  false
end

.set(resource, name, value, args = nil, project_id: nil, extra_info: nil, silent: false, indent: 3) ⇒ Object



55
56
57
58
# File 'lib/gclouder/resource.rb', line 55

def self.set(resource, name, value, args = nil, project_id: nil, extra_info: nil, silent: false, indent: 3)
  feedback("good", resource, name, extra_info: "[#{value}]", indent: indent, silent: silent)
  gcloud "#{resource} set #{name} #{value} #{args}", project_id: project_id, silent: silent
end