Class: Glb::Lb::Resource
Direct Known Subclasses
BackendService, BackendService::Backend, FirewallRule, ForwardingRule, HealthCheck, TargetHttpProxy, UrlMap
Instance Attribute Summary
Attributes inherited from CLI::Base
Instance Method Summary collapse
- #action ⇒ Object
- #args ⇒ Object
- #config ⇒ Object
-
#default_options ⇒ Object
Designed to be overridden by subclass These options are only available at runtime.
- #down ⇒ Object
-
#down_command ⇒ Object
Example: gcloud compute url-maps delete NAME –region=REGION -q.
- #empty_update? ⇒ Boolean
-
#exist? ⇒ Boolean
Example: gcloud compute url-maps describe NAME –region=REGION > /dev/null 2>&1.
- #format_option ⇒ Object
-
#gcloud_compute_command ⇒ Object
Examples: firewall-rules url-maps target-http-proxies health-checks forwarding-rules.
- #installed?(command) ⇒ Boolean
- #invalid? ⇒ Boolean
- #invalid_command? ⇒ Boolean
- #region_option ⇒ Object
-
#resource_config ⇒ Object
Example: config.url_map or config.firewall_rule.
-
#resource_name ⇒ Object
Example: url_map_name.
-
#resource_type ⇒ Object
Example: url_map.
-
#show ⇒ Object
Example: gcloud compute url-maps describe NAME –region=REGION –format json.
- #up ⇒ Object
-
#up_command ⇒ Object
gcloud compute firewall-rules create demo-web-dev –network=#Names#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.
- #valid? ⇒ Boolean
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
Methods inherited from CLI::Base
Methods included from Util::Sh
Constructor Details
This class inherits a constructor from Glb::CLI::Base
Instance Method Details
#action ⇒ Object
27 28 29 |
# File 'lib/glb/lb/resource.rb', line 27 def action exist? ? "update" : "create" end |
#args ⇒ Object
48 49 50 51 52 |
# File 'lib/glb/lb/resource.rb', line 48 def args opts = .merge(resource_config) # Example: Args.new("url-maps create", opts).transform Args.new("#{gcloud_compute_command} #{action}", opts).transform end |
#default_options ⇒ Object
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 {} end |
#down ⇒ Object
98 99 100 |
# File 'lib/glb/lb/resource.rb', line 98 def down sh down_command if exist? end |
#down_command ⇒ Object
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
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
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_option ⇒ Object
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_command ⇒ Object
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
115 116 117 |
# File 'lib/glb/lb/resource.rb', line 115 def installed?(command) system("type #{command} > /dev/null 2>&1") end |
#invalid? ⇒ Boolean
83 84 85 |
# File 'lib/glb/lb/resource.rb', line 83 def invalid? empty_update? || invalid_command? end |
#invalid_command? ⇒ Boolean
87 88 89 |
# File 'lib/glb/lb/resource.rb', line 87 def invalid_command? gcloud_compute_command == "url-maps" && action == "update" end |
#region_option ⇒ Object
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_config ⇒ Object
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_name ⇒ Object
Example: url_map_name
33 34 35 |
# File 'lib/glb/lb/resource.rb', line 33 def resource_name send("#{resource_type}_name") end |
#resource_type ⇒ Object
Example: url_map
38 39 40 |
# File 'lib/glb/lb/resource.rb', line 38 def resource_type self.class.name.split('::').last.underscore end |
#show ⇒ Object
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 |
#up ⇒ Object
7 8 9 |
# File 'lib/glb/lb/resource.rb', line 7 def up sh up_command if up_command end |
#up_command ⇒ Object
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
79 80 81 |
# File 'lib/glb/lb/resource.rb', line 79 def valid? !invalid? end |