Class: Fog::Compute::Google::TargetPool

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/compute/google/models/target_pool.rb

Constant Summary collapse

RUNNING_STATE =
"READY".freeze

Instance Method Summary collapse

Instance Method Details

#add_health_check(health_check, async = true) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/fog/compute/google/models/target_pool.rb', line 68

def add_health_check(health_check, async = true)
  requires :identity, :region

  health_check = health_check.self_link unless health_check.class == String
  data = service.add_target_pool_health_checks(identity, region, [health_check])
  operation = Fog::Compute::Google::Operations
              .new(:service => service)
              .get(data.name, nil, data.region)
  operation.wait_for { ready? } unless async

  reload
end

#add_instance(instance, async = true) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fog/compute/google/models/target_pool.rb', line 42

def add_instance(instance, async = true)
  requires :identity
  instance = instance.self_link unless instance.class == String
  data = service.add_target_pool_instances(identity, region, [instance])
  operation = Fog::Compute::Google::Operations
              .new(:service => service)
              .get(data.name, nil, data.region)
  operation.wait_for { ready? } unless async

  reload
end

#destroy(async = true) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/fog/compute/google/models/target_pool.rb', line 32

def destroy(async = true)
  requires :identity, :region
  data = service.delete_target_pool(identity, region)
  operation = Fog::Compute::Google::Operations
              .new(:service => service)
              .get(data.name, nil, data.region)
  operation.wait_for { ready? } unless async
  operation
end

#get_health(instance_name = nil) ⇒ Object

Get most recent health checks for each IP for instances.

Example Hash:

"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/myinstance"=>

[{:health_state=>“UNHEALTHY”,

:instance=>"https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f/instances/myinstance"

] }

Parameters:

  • instance_name (String) (defaults to: nil)

    a specific instance to look up. Default behavior returns health checks for all instances associated with this check.



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/fog/compute/google/models/target_pool.rb', line 109

def get_health(instance_name = nil)
  requires :identity, :region

  if instance_name
    instance = service.servers.get(instance_name)
    data = service.get_target_pool_health(identity, region, instance.self_link)
                  .to_h[:health_status] || []
    results = [[instance.self_link, data]]
  else
    results = instances.map do |self_link|
      # TODO: Improve the returned object, current is hard to navigate
      # [{instance => @instance, health_state => "HEALTHY"}, ...]
      data = service.get_target_pool_health(identity, region, self_link)
                    .to_h[:health_status] || []
      [self_link, data]
    end
  end
  Hash[results]
end

#ready?Boolean

Returns:

  • (Boolean)


141
142
143
144
145
146
147
# File 'lib/fog/compute/google/models/target_pool.rb', line 141

def ready?
  service.get_target_pool(name, region)
  true
rescue ::Google::Apis::ClientError => e
  raise e unless e.status_code == 404
  false
end

#region_nameObject



149
150
151
# File 'lib/fog/compute/google/models/target_pool.rb', line 149

def region_name
  region.nil? ? nil : region.split("/")[-1]
end

#reloadObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/fog/compute/google/models/target_pool.rb', line 153

def reload
  requires :name, :region

  return unless data = begin
    collection.get(name, region)
  rescue Excon::Errors::SocketError
    nil
  end

  new_attributes = data.attributes
  merge_attributes(new_attributes)
  self
end

#remove_health_check(health_check, async = true) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/fog/compute/google/models/target_pool.rb', line 81

def remove_health_check(health_check, async = true)
  requires :identity, :region

  health_check = health_check.self_link unless health_check.class == String
  data = service.remove_target_pool_health_checks(identity, region, [health_check])
  operation = Fog::Compute::Google::Operations
              .new(:service => service)
              .get(data.name, nil, data.region)
  operation.wait_for { ready? } unless async
  reload
end

#remove_instance(instance, async = true) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fog/compute/google/models/target_pool.rb', line 54

def remove_instance(instance, async = true)
  requires :identity

  instance = instance.self_link unless instance.class == String
  data = service.remove_target_pool_instances(identity, region, [instance])
  operation = Fog::Compute::Google::Operations
              .new(:service => service)
              .get(data.name, nil, data.region)

  operation.wait_for { ready? } unless async

  reload
end

#saveObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/fog/compute/google/models/target_pool.rb', line 19

def save
  requires :name, :region

  data = service.insert_target_pool(
    name, region, attributes.reject { |_k, v| v.nil? }
  )
  operation = Fog::Compute::Google::Operations
              .new(:service => service)
              .get(data.name, nil, data.region)
  operation.wait_for { ready? }
  reload
end

#set_backup(backup = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/fog/compute/google/models/target_pool.rb', line 129

def set_backup(backup = nil)
  requires :identity, :region

  backup ||= backup_pool

  service.set_target_pool_backup(
    identity, region, backup,
    :failover_ratio => failover_ratio
  )
  reload
end