Class: Glb::Lb::Resource

Inherits:
CLI::Base show all
Extended by:
Memoist
Includes:
Names, Util::Sure
Defined in:
lib/glb/lb/resource.rb

Instance Attribute Summary

Attributes inherited from CLI::Base

#options

Instance Method Summary collapse

Methods included from Names

#backend_service_name, #build_name, #firewall_rule_name, #forwarding_rule_https_name, #forwarding_rule_name, #health_check_name, #network, #target_http_proxy_name, #target_https_proxy_name, #url_map_name

Methods included from Util::Sure

#sure?

Methods inherited from CLI::Base

#initialize, #region

Methods included from Util::Sh

#capture, #sh

Constructor Details

This class inherits a constructor from Glb::CLI::Base

Instance Method Details

#actionObject



27
28
29
# File 'lib/glb/lb/resource.rb', line 27

def action
  exist? ? "update" : "create"
end

#argsObject



48
49
50
51
52
# File 'lib/glb/lb/resource.rb', line 48

def args
  opts = default_options.merge(resource_config)
  # Example: Args.new("url-maps create", opts).transform
  Args.new("#{gcloud_compute_command} #{action}", opts).transform
end

#configObject



102
103
104
# File 'lib/glb/lb/resource.rb', line 102

def config
  Glb.config
end

#default_optionsObject

Designed to be overridden by subclass These options are only available at runtime



44
45
46
# File 'lib/glb/lb/resource.rb', line 44

def default_options
  {}
end

#downObject



98
99
100
# File 'lib/glb/lb/resource.rb', line 98

def down
  sh down_command if exist?
end

#down_commandObject

Example: gcloud compute url-maps delete NAME –region=REGION -q



60
61
62
# File 'lib/glb/lb/resource.rb', line 60

def down_command
  "gcloud compute #{gcloud_compute_command} delete #{resource_name} #{region_option} -q"
end

#empty_update?Boolean

Returns:

  • (Boolean)


91
92
93
94
95
96
# File 'lib/glb/lb/resource.rb', line 91

def empty_update?
  # Example: --global only is considered an empty update
  # A non-empty update would be --port=80
  action == "update" && !args.include?('=') ||
  action == "update" && args == "--region=us-central1" # Command failed: gcloud compute forwarding-rules update demo-dev --region=us-central1
end

#exist?Boolean

Example: gcloud compute url-maps describe NAME –region=REGION > /dev/null 2>&1

Returns:

  • (Boolean)


65
66
67
# File 'lib/glb/lb/resource.rb', line 65

def exist?
  sh "gcloud compute #{gcloud_compute_command} describe #{resource_name} #{region_option} > /dev/null 2>&1", exit_on_fail: false
end

#format_optionObject



106
107
108
109
110
111
112
113
# File 'lib/glb/lb/resource.rb', line 106

def format_option
  format = ENV['GLB_SHOW_FORMAT'] || @options[:format] || Glb.config.show.format
  option = "--format #{format}" if format
  if format == "json"
    option += " | jq" if installed?("jq")
  end
  option
end

#gcloud_compute_commandObject

Examples: firewall-rules url-maps target-http-proxies health-checks forwarding-rules



23
24
25
# File 'lib/glb/lb/resource.rb', line 23

def gcloud_compute_command
  resource_type.pluralize.dasherize
end

#installed?(command) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/glb/lb/resource.rb', line 115

def installed?(command)
  system("type #{command} > /dev/null 2>&1")
end

#invalid?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/glb/lb/resource.rb', line 83

def invalid?
  empty_update? || invalid_command?
end

#invalid_command?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/glb/lb/resource.rb', line 87

def invalid_command?
  gcloud_compute_command == "url-maps" && action == "update"
end

#region_optionObject



74
75
76
77
# File 'lib/glb/lb/resource.rb', line 74

def region_option
  return if gcloud_compute_command == "firewall-rules" # does not use --region or --global
  resource_config.region ? "--region=#{resource_config.region}" : "--global"
end

#resource_configObject

Example: config.url_map or config.firewall_rule



70
71
72
# File 'lib/glb/lb/resource.rb', line 70

def resource_config
  config.send(resource_type)
end

#resource_nameObject

Example: url_map_name



33
34
35
# File 'lib/glb/lb/resource.rb', line 33

def resource_name
  send("#{resource_type}_name")
end

#resource_typeObject

Example: url_map



38
39
40
# File 'lib/glb/lb/resource.rb', line 38

def resource_type
  self.class.name.split('::').last.underscore
end

#showObject

Example: gcloud compute url-maps describe NAME –region=REGION –format json



55
56
57
# File 'lib/glb/lb/resource.rb', line 55

def show
  sh "gcloud compute #{gcloud_compute_command} describe #{resource_name} #{region_option} #{format_option}", exit_on_fail: false
end

#upObject



7
8
9
# File 'lib/glb/lb/resource.rb', line 7

def up
  sh up_command if up_command
end

#up_commandObject

gcloud compute firewall-rules create demo-web-dev

--network=#{network}
--action=allow
--direction=ingress
--source-ranges=130.211.0.0/22,35.191.0.0/16,0.0.0.0/0
--target-tags=#{target_tags}
--rules=tcp:80


18
19
20
# File 'lib/glb/lb/resource.rb', line 18

def up_command
  "gcloud compute #{gcloud_compute_command} #{action} #{resource_name} #{args}" if valid?
end

#valid?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/glb/lb/resource.rb', line 79

def valid?
  !invalid?
end