Module: Kitchen::Terraform::Client::Command

Extended by:
Dry::Monads::Either::Mixin, Dry::Monads::Try::Mixin
Defined in:
lib/kitchen/terraform/client/command.rb

Overview

Represents the result of running a Terraform command.

See Also:

Class Method Summary collapse

Class Method Details

.apply(options:, working_directory:) ⇒ ::Dry::Monads::Either

Creates the apply command shell out.

Parameters:

Returns:

  • (::Dry::Monads::Either)

    the result of creating the shell out.

See Also:



36
37
38
39
40
41
42
# File 'lib/kitchen/terraform/client/command.rb', line 36

def self.apply(options:, working_directory:)
  create(
    options: options,
    subcommand: "apply",
    working_directory: working_directory
  )
end

.create(options:, subcommand:, working_directory:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • options (::Kitchen::Terraform::Client::Options)

    options for the command.

  • subcommand (::String)

    the subcommand to run through shell out.

  • working_directory (::String)

    the path to the directory in which to run the shell out.



141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/kitchen/terraform/client/command.rb', line 141

def self.create(options:, subcommand:, working_directory:)
  Try ::Mixlib::ShellOut::InvalidCommandOption do
    ::Mixlib::ShellOut.new(
      "terraform #{subcommand} #{options}".strip,
      cwd: working_directory
    )
  end
    .to_either
    .or do |error|
      Left "Failed to create `terraform #{subcommand}`: #{error}"
    end
end

.destroy(options:, working_directory:) ⇒ ::Dry::Monads::Either

Creates the destroy command shell out.

Parameters:

Returns:

  • (::Dry::Monads::Either)

    the result of creating the shell out.

See Also:



51
52
53
54
55
56
57
# File 'lib/kitchen/terraform/client/command.rb', line 51

def self.destroy(options:, working_directory:)
  create(
    options: options,
    subcommand: "destroy",
    working_directory: working_directory
  )
end

.init(options:, working_directory:) ⇒ ::Dry::Monads::Either

Creates the init command shell out.

Parameters:

Returns:

  • (::Dry::Monads::Either)

    the result of creating the shell out.

See Also:



65
66
67
68
69
70
71
# File 'lib/kitchen/terraform/client/command.rb', line 65

def self.init(options:, working_directory:)
  create(
    options: options,
    subcommand: "init",
    working_directory: working_directory
  )
end

.output(options:, working_directory:) ⇒ ::Dry::Monads::Either

Creates the output command shell out.

Parameters:

Returns:

  • (::Dry::Monads::Either)

    the result of creating the shell out.

See Also:



79
80
81
82
83
84
85
# File 'lib/kitchen/terraform/client/command.rb', line 79

def self.output(options:, working_directory:)
  create(
    options: options,
    subcommand: "output",
    working_directory: working_directory
  )
end

.run(logger:, shell_out:, timeout:) ⇒ ::Dry::Monads::Either

Runs a command shell out.

Parameters:

  • logger (::Kitchen::Logger, ::Terraform::DebugLogger)

    a logger to capture the run output of the command.

  • shell_out (::Mixlib::ShellOut)

    the shell out to run.

  • timeout (::Integer)

    the number of seconds to wait for the command to finish.

Returns:

  • (::Dry::Monads::Either)

    the result of running the shell out.



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/kitchen/terraform/client/command.rb', line 93

def self.run(logger:, shell_out:, timeout:)
  try_to_run(
    logger: logger,
    shell_out: shell_out,
    timeout: timeout
  )
    .bind do
      Right shell_out.stdout
    end
    .or do |error|
      Left error.to_s
    end
end

.try_to_run(logger:, shell_out:, timeout:) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/kitchen/terraform/client/command.rb', line 154

def self.try_to_run(logger:, shell_out:, timeout:)
  Try(
    ::Errno::EACCES,
    ::Errno::ENOENT,
    ::Mixlib::ShellOut::CommandTimeout,
    ::Mixlib::ShellOut::ShellCommandFailed
  ) do
    shell_out.live_stream = logger
    shell_out.timeout = timeout
    shell_out.run_command
    shell_out.error!
  end
    .to_either
end

.validate(options:, working_directory:) ⇒ ::Dry::Monads::Either

Creates the validate command shell out.

Parameters:

Returns:

  • (::Dry::Monads::Either)

    the result of creating the shell out.

See Also:



113
114
115
116
117
118
119
# File 'lib/kitchen/terraform/client/command.rb', line 113

def self.validate(options:, working_directory:)
  create(
    options: options,
    subcommand: "validate",
    working_directory: working_directory
  )
end

.version(options: ::Kitchen::Terraform::Client::Options, working_directory:) ⇒ ::Dry::Monads::Either

Creates the version command shell out.

Parameters:

  • options (::Kitchen::Terraform::Client::Options) (defaults to: ::Kitchen::Terraform::Client::Options)

    options for the command.

  • working_directory (::String)

    the path to the directory in which to run the shell out.

Returns:

  • (::Dry::Monads::Either)

    the result of creating the shell out.

See Also:



127
128
129
130
131
132
133
# File 'lib/kitchen/terraform/client/command.rb', line 127

def self.version(options: ::Kitchen::Terraform::Client::Options, working_directory:)
  create(
    options: options,
    subcommand: "version",
    working_directory: working_directory
  )
end