infrablocks / ruby_terraform

A simple Ruby wrapper for invoking terraform commands.
MIT License
109 stars 33 forks source link

Add support for terraform import #42

Closed chussenot closed 4 years ago

chussenot commented 4 years ago

Below the monkey patch I use:

#   Import existing infrastructure into your Terraform state.
#
#   This will find and import the specified resource into your Terraform
#   state, allowing existing infrastructure to come under Terraform
#   management without having to be initially created by Terraform.
#
#   The ADDR specified is the address to import the resource to. Please
#   see the documentation online for resource addresses. The ID is a
#   resource-specific ID to identify that resource being imported. Please
#   reference the documentation for the resource type you're importing to
#   determine the ID syntax to use. It typically matches directly to the ID
#   that the provider uses.

module RubyTerraform
  module Commands
    class Import < Base
      def configure_command(builder, opts)
        path = opts[:path] || opts[:directory]
        json_format = opts[:json]
        no_color = opts[:no_color]
        module_depth = opts[:module_depth]

        builder
          .with_subcommand('import') do |sub|
          sub = sub.with_option('-module-depth', module_depth) if module_depth
          sub = sub.with_option('-config', path) 
          sub = sub.with_flag('-no-color') if no_color
          sub = sub.with_flag('-json') if json_format
          sub
        end
          .with_argument(opts[:addr])
          .with_argument(opts[:id])
      end
    end
  end
  module ClassMethods
    def import(opts = {})     
     Commands::Import.new.execute(opts)
    end
  end
end
tobyclemson commented 4 years ago

Hi Clément,

Thanks for this. I'd be very happy to receive a pull request with this and some tests and I can get it merged and released quickly.

I can also look at adding it myself but the lead times for that are a little longer.

Thanks, Toby