Class: Glb::Lb::BackendService::Backend

Inherits:
Resource show all
Defined in:
lib/glb/lb/backend_service/backend.rb

Instance Attribute Summary

Attributes inherited from CLI::Base

#options

Instance Method Summary collapse

Methods inherited from Resource

#action, #args, #config, #default_options, #down, #down_command, #empty_update?, #exist?, #format_option, #gcloud_compute_command, #installed?, #invalid?, #invalid_command?, #region_option, #resource_config, #show, #up, #up_command, #valid?

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

#addObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/glb/lb/backend_service/backend.rb', line 12

def add
  return unless network_endpoint_group
  validate_neg_exists!

  google_zones.each do |zone|
    if backend_added?(zone)
      puts "Backend already added. network_endpoint_group: #{network_endpoint_group} backend_service: #{resource_name} zone: #{zone}"
    else
      sh add_command(zone)
    end
  end
end

#add_args(zone) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/glb/lb/backend_service/backend.rb', line 35

def add_args(zone)
  defaults = {
    network_endpoint_group_zone: zone, # us-central1-a
    network_endpoint_group: network_endpoint_group,
  }
  opts = defaults.merge(config.backend_service.add_backend)
  Glb::Lb::Args.new("backend-services add-backend", opts).transform
end

#add_command(zone) ⇒ Object



31
32
33
# File 'lib/glb/lb/backend_service/backend.rb', line 31

def add_command(zone)
  "gcloud compute backend-services add-backend #{resource_name} #{add_args(zone)}"
end

#add_commandsObject



25
26
27
28
29
# File 'lib/glb/lb/backend_service/backend.rb', line 25

def add_commands
  google_zones.map do |zone|
    add_command(zone)
  end
end

#backend_added?(zone) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/glb/lb/backend_service/backend.rb', line 44

def backend_added?(zone)
  @describe_cache ||= capture "gcloud compute backend-services describe #{resource_name} #{region_option} --format json"
  return false if @describe_cache.blank?

  data = JSON.load(@describe_cache)
  backends = data["backends"]
  return false unless backends

  # "backends": [
  #   {
  #     "balancingMode": "RATE",
  #     "capacityScaler": 1,
  #     "group": "https://www.googleapis.com/compute/v1/projects/PROJECT/zones/ZONE/networkEndpointGroups/NEG",
  #     "maxRatePerEndpoint": 1
  #   }
  backends.any? do |b|
    # https://www.googleapis.com/compute/v1/projects/PROJECT/zones/ZONE/networkEndpointGroups/NEG
    parts = b["group"].split("/")
    parts.last == network_endpoint_group && parts[-3] == zone
  end
end

#resource_nameObject



8
9
10
# File 'lib/glb/lb/backend_service/backend.rb', line 8

def resource_name
  backend_service_name
end

#resource_typeObject

override so resource_name, resource_config, and region_option are correct



4
5
6
# File 'lib/glb/lb/backend_service/backend.rb', line 4

def resource_type
  "backend_service"
end