Module: GClouder::Resources::Compute::VPNs

Includes:
Config::CLIArgs, Config::Project, GCloud, Logging, GClouder::Resource::Cleaner, Shell
Defined in:
lib/gclouder/resources/compute/vpns.rb

Defined Under Namespace

Modules: Local, Remote, VPN

Class Method Summary collapse

Methods included from GClouder::Resource::Cleaner

#clean, #cleaner, included, #undefined

Methods included from Config::CLIArgs

check, #cli_args, cli_args, included, load, valid_resources

Methods included from Config::Project

load, #project, project

Methods included from Helpers

#hash_to_args, included, #module_exists?, #to_arg, #to_deep_merge_hash, #valid_json?

Methods included from Logging

#add, #bad, #change, #debug, #error, #fatal, #good, included, #info, log, loggers, #remove, report, #resource_state, setup, #warn, #warning

Methods included from Shell

included, #shell

Methods included from GCloud

#gcloud, included, #verify

Class Method Details

.dirObject



24
25
26
# File 'lib/gclouder/resources/compute/vpns.rb', line 24

def self.dir
  cli_args[:keys_dir] || File.join(ENV["HOME"], "keys")
end

.ensureObject



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
# File 'lib/gclouder/resources/compute/vpns.rb', line 28

def self.ensure
  return if Local.list.empty?

  header

  Local.list.each do |region, instances|
    info region, indent: 2, heading: true
    info

    instances.each do |vpn|
      skip_vpn = false

      # if 'shared_secret' key is set, use it
      # if not, fall back to trying to read the secret from an environment variable, the name
      # of which is provided by the 'shared_secret_env_var' key
      unless vpn.key?("shared_secret") || vpn.key?("shared_secret_env_var") || vpn.key?("shared_secret_file")
        if cli_args[:dry_run]
          warning "skipping resource since no shared secret found for VPN and this is a dry run"
          skip_vpn = true
        else
          fatal "shared_secret_env_var or shared_secret must be set for region/vpn: #{region}/#{vpn["name"]}"
        end
      end

      vpn["shared_secret"] = if vpn.key?("shared_secret") && !vpn["shared_secret"].empty? && !vpn["shared_secret"].nil?
        vpn["shared_secret"]
      else
        ENV[vpn["shared_secret_env_var"]] if vpn["shared_secret_env_var"]
      end

      # this overrides the above for now..
      if vpn.key?("shared_secret_file")
        config_file = File.join(dir, vpn["shared_secret_file"])

        if !File.exists?(config_file)
          fatal "shared_secret_file specified for vpn but no file found for region/vpn: #{region}/#{vpn["name"]}"
        end

        vpn["shared_secret"] = File.read(config_file)
      end

      vpn.delete("shared_secret_env_var") if vpn.key?("shared_secret_env_var")
      vpn.delete("shared_secret_file") if vpn.key?("shared_secret_file")

      required_params = %w(peer_address shared_secret ike_version remote_traffic_selector
                           local_traffic_selector target_vpn_gateway network)

      required_params.each do |param|
        fatal "no #{param} defined for region/vpn: #{region}/#{vpn}" unless vpn.key?(param)

        # FIXME: change once hashie has been ripped out
        if vpn[param].nil?
          if cli_args[:dry_run]
            warning "no #{param} defined for region/vpn: #{vpn["name"]} [#{region}]"
            skip_vpn = true
          else
            fatal "no #{param} defined for region/vpn: #{vpn["name"]} [#{region}]"
          end
        end

        if vpn[param].is_a?(String)
          if cli_args[:dry_run]
            warning "no #{param} defined for region/vpn: #{vpn["name"]} [#{region}]" if vpn[param].empty?
            skip_vpn = true
          else
            fatal "no #{param} defined for region/vpn: #{vpn["name"]} [#{region}]" if vpn[param].empty?
          end
        end
      end

      next if skip_vpn && !cli_args[:dry_run]

      VPN.create(region, vpn["name"], vpn)
    end
  end
end

.header(stage = :ensure) ⇒ Object



14
15
16
# File 'lib/gclouder/resources/compute/vpns.rb', line 14

def self.header(stage = :ensure)
  info "[#{stage}] compute / vpns", indent: 1, title: true
end

.validateObject



18
19
20
21
22
# File 'lib/gclouder/resources/compute/vpns.rb', line 18

def self.validate
  return if Local.list.empty?
  header :validate
  Local.validate
end