File: insert_server.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 (126 lines) | stat: -rw-r--r-- 4,679 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
module Fog
  module Compute
    class Google
      class Mock
        def insert_server(_instance_name, _zone, _options = {})
          # :no-coverage:
          Fog::Mock.not_implemented
          # :no-coverage:
        end
      end

      class Real
        def default_network_interface
          { :network => "global/networks/#{GOOGLE_COMPUTE_DEFAULT_NETWORK}" }
        end

        def process_disks(disks)
          unless disks && !disks.empty?
            raise ArgumentError.new("at least one value for disks is required")
          end

          disk_lst = disks.map do |d|
            d = d.attached_disk_obj if d.is_a? Disk
            ::Google::Apis::ComputeV1::AttachedDisk.new(**d)
          end
          disk_lst.first.boot = true
          disk_lst
        end

        def process_network_interfaces(network_interfaces)
          unless network_interfaces && !network_interfaces.empty?
            network_interfaces = [default_network_interface]
          end
          network_interfaces.map do |network|
            ::Google::Apis::ComputeV1::NetworkInterface.new(**network)
          end
        end

        ##
        # Create a new instance (virtual machine).
        #
        # This method allows you to use low-level request options and thus
        # expects instance options similar to API requests. If you don't need to
        # modify low-level request options, consider using the
        # Fog::Compute::Google::Servers collection object instead.
        #
        # @example minimal server creation
        #     my_operation = client.insert_server(
        #       "my-server",
        #       "us-central1-a",
        #       :machine_type => "f1-micro",
        #       :disks => [
        #         {
        #           :initialize_params => {
        #             :source_image => "projects/debian-cloud/global/images/family/debian-9"
        #           }
        #         }
        #       ]
        #     )
        #
        # @param instance_name [String]
        #   Name to assign to the created server. Must be unique within the specified zone.
        # @param zone [String]
        #   Name or URL of zone containing the created server.
        # @param options [Hash]
        #   Server attributes. You can use any of the options documented at
        #   https://cloud.google.com/compute/docs/reference/latest/instances/insert.
        # @see https://cloud.google.com/compute/docs/reference/latest/instances/insert
        # @return [::Google::Apis::ComputeV1::Operation]
        #   response object that represents the insertion operation.
        def insert_server(instance_name, zone, options = {})
          zone = zone.split("/")[-1]

          data = options.merge(:name => instance_name)
          data[:disks] = process_disks(options[:disks])
          data[:network_interfaces] = process_network_interfaces(options[:network_interfaces])

          machine_type = options[:machine_type]
          unless machine_type
            raise ArgumentError.new("machine type is required")
          end

          unless machine_type.include?("zones/")
            machine_type = "zones/#{zone}/machineTypes/#{data[:machine_type]}"
          end
          data[:machine_type] = machine_type

          # Optional subclassed attributes
          if data[:guest_accelerators]
            data[:guest_accelerators] = data[:guest_accelerators].map do |acc_config|
              ::Google::Apis::ComputeV1::AcceleratorConfig.new(**acc_config)
            end
          end

          if data[:metadata]
            data[:metadata] = ::Google::Apis::ComputeV1::Metadata.new(**options[:metadata])
          end

          if data[:scheduling]
            data[:scheduling] = ::Google::Apis::ComputeV1::Scheduling.new(**options[:scheduling])
          end

          if data[:shielded_instance_config]
            data[:shielded_instance_config] = ::Google::Apis::ComputeV1::ShieldedInstanceConfig.new(**options[:shielded_instance_config])
          end

          if data[:display_device]
            data[:display_device] = ::Google::Apis::ComputeV1::DisplayDevice.new(**options[:display_device])
          end

          if data[:tags]
            if options[:tags].is_a?(Array)
              # Process classic tag notation, i.e. ["fog"]
              data[:tags] = ::Google::Apis::ComputeV1::Tags.new(items: options[:tags])
            else
              data[:tags] = ::Google::Apis::ComputeV1::Tags.new(**options[:tags])
            end
          end

          instance = ::Google::Apis::ComputeV1::Instance.new(**data)
          @compute.insert_instance(@project, zone, instance)
        end
      end
    end
  end
end