File: instance_group.rb

package info (click to toggle)
ruby-fog-google 1.19.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,568 kB
  • sloc: ruby: 16,775; makefile: 3
file content (90 lines) | stat: -rw-r--r-- 2,297 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
module Fog
  module Compute
    class Google
      class InstanceGroup < Fog::Model
        identity :name

        attribute :id
        attribute :kind
        attribute :creation_timestamp, :aliases => "creationTimestamp"
        attribute :description
        attribute :fingerprint
        attribute :namedPorts
        attribute :network
        attribute :subnetwork
        attribute :self_link, :aliases => "selfLink"
        attribute :size
        attribute :zone, :aliases => :zone_name

        def save
          requires :name, :zone

          options = {
            "network" => network_name,
            "subnetwork" => subnetwork_name
          }

          service.insert_instance_group(name, zone, options)
        end

        def destroy(_async = true)
          requires :name, :zone

          service.delete_instance_group(name, zone_name)
        end

        def add_instance(instance_id)
          add_instances [instance_id]
        end

        def add_instances(instances)
          requires :identity, :zone

          service.add_instance_group_instances(
            identity, zone_name, format_instance_list(instances)
          )
        end

        def remove_instances(instances)
          requires :identity, :zone

          service.remove_instance_group_instances(
            identity, zone_name, format_instance_list(instances)
          )
        end

        def list_instances
          requires :identity, :zone

          instance_list = []
          data = service.list_instance_group_instances(identity, zone_name)
          if data.items
            data.items.each do |instance|
              instance_list << service.servers.get(instance.instance.split("/")[-1], zone_name)
            end
          end
          instance_list
        end

        def zone_name
          zone.nil? ? nil : zone.split("/")[-1]
        end

        def network_name
          network.nil? ? nil : network.split("/")[-1]
        end

        def subnetwork_name
          subnetwork.nil? ? nil : subnetwork.split("/")[-1]
        end

        private

        def format_instance_list(instance_list)
          instance_list = Array(instance_list)
          instance_list.map { |i| i.class == String ? i : i.self_link }
        end
      end
    end
  end
end