terraspace_plugin_google 0.0.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (30) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +15 -3
  3. data/Gemfile +1 -2
  4. data/README.md +2 -2
  5. data/git st +25 -0
  6. data/lib/templates/hcl/module/main.tf +2 -2
  7. data/lib/templates/hcl/module/variables.tf +2 -2
  8. data/lib/templates/hcl/project/config/terraform/backend.tf.tt +6 -0
  9. data/lib/templates/hcl/project/config/{templates → terraform}/provider.tf +0 -0
  10. data/lib/templates/hcl/stack/main.tf +3 -3
  11. data/lib/templates/hcl/stack/variables.tf +2 -2
  12. data/lib/templates/ruby/module/main.rb +2 -2
  13. data/lib/templates/ruby/module/variables.rb +2 -2
  14. data/lib/templates/ruby/project/config/{templates → terraform}/backend.rb.tt +0 -0
  15. data/lib/templates/ruby/project/config/{templates → terraform}/provider.rb +0 -0
  16. data/lib/templates/ruby/stack/variables.rb +2 -2
  17. data/lib/templates/test/rspec/module/test/spec/fixtures/stack/main.tf +3 -3
  18. data/lib/templates/test/rspec/module/test/spec/fixtures/stack/variables.tf +2 -2
  19. data/lib/templates/test/rspec/module/test/spec/main_spec.rb.tt +2 -2
  20. data/lib/templates/test/rspec/project/spec/fixtures/tfvars/demo.tfvars +0 -0
  21. data/lib/templates/test/rspec/project/spec/stacks/demo/main_spec.rb +26 -0
  22. data/lib/terraspace_plugin_google.rb +6 -4
  23. data/lib/terraspace_plugin_google/interfaces/backend.rb +12 -6
  24. data/lib/terraspace_plugin_google/interfaces/config.rb +4 -0
  25. data/lib/terraspace_plugin_google/interfaces/expander.rb +2 -0
  26. data/lib/terraspace_plugin_google/interfaces/layer.rb +5 -14
  27. data/lib/terraspace_plugin_google/interfaces/summary.rb +35 -0
  28. data/lib/terraspace_plugin_google/version.rb +1 -1
  29. metadata +10 -6
  30. data/lib/templates/hcl/project/config/templates/backend.tf.tt +0 -6
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 10ba87e72c39cdfc8032d3c12f859fd6d442bd2c7cf9c093cca723b32df4e4c5
4
- data.tar.gz: 15d820fbb4ce87d09ead970b12c9a2266b9cb34d68c8a928ad819cf7af5aba22
3
+ metadata.gz: f481b6c88c1de06a78da5d9a3097517202c3e17ceae6f81434bcb66abfd9468f
4
+ data.tar.gz: db7d7abd653cd1e0c7b780b69228645d840909a242fb920f44707fbdd19423db
5
5
  SHA512:
6
- metadata.gz: eec0abc226f2cbbcad26d3e9d33c1689887818f2f8182224c639838d8ccc9a9e31708b5d16793482e832719c4a1de46f8eead150b3e27acb507e714a41e0a688
7
- data.tar.gz: 3350a30114e280ccbb024de1ce07e9a9fa2423e62f097ae4e49b270e9d65659d2515277686914632c950e4314ce67f4c36d6eb25da6ce44acb6d61d9cf9d4d46
6
+ metadata.gz: f0d816df268ff24c79488afa29105ff493c75f602fcf3a066befe8fbb9824d54ad8ef8c3d3ea8c57fa4be691ed057291e9ca061cf54eb5e4b86c49fa13f408f1
7
+ data.tar.gz: f960ab4f7c2488112b6d954b6f0b747f3fb2e6999be626d0ebe8d2ec634154b56c8e3cea4cdfe2d900f6395d45ffe4abca61a0880ac5f42742745a428a67c3d4
@@ -1,8 +1,20 @@
1
- # Change Log
1
+ # Changelog
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/).
5
5
 
6
- ## [0.0.1]
7
- - terraspace dep for dev only
6
+ ## [0.2.2]
7
+ - #4 fix test template: folder rename to stacks
8
8
 
9
+ ## [0.2.1]
10
+ - #3 update starter example template: bucket_policy_only deprecated for uniform_bucket_level_access
11
+ - edge case: graceful error when bucket doesnt exist yet
12
+
13
+ ## [0.2.0]
14
+ - #2 include layer interface, update template to use expansion method
15
+
16
+ ## [0.1.1]
17
+ - summary command: fix edge case when files are deleted mid-loop
18
+
19
+ ## [0.1.0]
20
+ - Initial release
data/Gemfile CHANGED
@@ -7,6 +7,5 @@ gem "rake", "~> 12.0"
7
7
  gem "rspec", "~> 3.0"
8
8
 
9
9
  group :development, :test do
10
- base = ENV['TS_EDGE_ROOT'] || "#{ENV['HOME']}/environment/terraspace-edge"
11
- gem "terraspace", path: "#{base}/terraspace"
10
+ gem "terraspace"
12
11
  end
data/README.md CHANGED
@@ -18,17 +18,17 @@ config/plugins/google.rb
18
18
 
19
19
  ```ruby
20
20
  TerraspacePluginGoogle.configure do |config|
21
+ config.auto_create = true # set to false to completely disable auto creation
21
22
  config.gcs.versioning = true
22
23
  end
23
24
  ```
24
25
 
25
26
  By default:
26
27
 
27
- * GCS Buckets have versioning enabled.
28
+ * GCS Buckets have [versioning](https://cloud.google.com/storage/docs/object-versioning) enabled.
28
29
 
29
30
  The settings generally only apply if the gcs bucket does not yet exist yet and is created for the first time.
30
31
 
31
-
32
32
  ## Versioning
33
33
 
34
34
  To confirm that the GCS bucket has versioning enabled:
@@ -0,0 +1,25 @@
1
+ diff --git a/CHANGELOG.md b/CHANGELOG.md
2
+ index 7e94c34..2fdf029 100644
3
+ --- a/CHANGELOG.md
4
+ +++ b/CHANGELOG.md
5
+ @@ -1,8 +1,7 @@
6
+ -# Change Log
7
+ +# Changelog
8
+ 
9
+ All notable changes to this project will be documented in this file.
10
+ This project *loosely tries* to adhere to [Semantic Versioning](http://semver.org/).
11
+ 
12
+ -## [0.0.1]
13
+ -- terraspace dep for dev only
14
+ -
15
+ +## [0.1.0]
16
+ +- Initial release
17
+ diff --git a/lib/terraspace_plugin_google/version.rb b/lib/terraspace_plugin_google/version.rb
18
+ index b771e2b..75e5bdc 100644
19
+ --- a/lib/terraspace_plugin_google/version.rb
20
+ +++ b/lib/terraspace_plugin_google/version.rb
21
+ @@ -1,3 +1,3 @@
22
+ module TerraspacePluginGoogle
23
+ - VERSION = "0.0.0"
24
+ + VERSION = "0.1.0"
25
+ end
@@ -1,4 +1,4 @@
1
1
  resource "google_storage_bucket" "this" {
2
- name = var.name
3
- bucket_policy_only = var.bucket_policy_only
2
+ name = var.name
3
+ uniform_bucket_level_access = var.uniform_bucket_level_access
4
4
  }
@@ -3,8 +3,8 @@ variable "name" {
3
3
  type = string
4
4
  }
5
5
 
6
- variable "bucket_policy_only" {
7
- description = "bucket_policy_only"
6
+ variable "uniform_bucket_level_access" {
7
+ description = "uniform_bucket_level_access"
8
8
  type = bool
9
9
  default = false
10
10
  }
@@ -0,0 +1,6 @@
1
+ terraform {
2
+ backend "gcs" {
3
+ bucket = "<%%= expansion('terraform-state-:PROJECT-:REGION-:ENV') %>" # expanded by terraspace IE: terraform-state-project-us-central1-dev
4
+ prefix = "<%%= expansion(':REGION/:ENV/:BUILD_DIR') %>" # expanded by terraspace IE: us-central1/dev/modules/vm
5
+ }
6
+ }
@@ -1,10 +1,10 @@
1
- resource "random_pet" "bucket" {
1
+ resource "random_pet" "this" {
2
2
  length = 2
3
3
  }
4
4
 
5
5
  module "bucket" {
6
6
  source = "../../modules/example"
7
7
 
8
- name = "bucket-${random_pet.bucket.id}"
9
- bucket_policy_only = var.bucket_policy_only
8
+ name = "bucket-${random_pet.this.id}"
9
+ uniform_bucket_level_access = var.uniform_bucket_level_access
10
10
  }
@@ -1,5 +1,5 @@
1
- variable "bucket_policy_only" {
2
- description = "bucket_policy_only"
1
+ variable "uniform_bucket_level_access" {
2
+ description = "uniform_bucket_level_access"
3
3
  type = bool
4
4
  default = false
5
5
  }
@@ -1,4 +1,4 @@
1
1
  resource("google_storage_bucket", "this",
2
- name: var.name,
3
- bucket_policy_only:var.bucket_policy_only,
2
+ name: var.name,
3
+ uniform_bucket_level_access: var.uniform_bucket_level_access,
4
4
  )
@@ -3,8 +3,8 @@ variable("name",
3
3
  type: "string",
4
4
  )
5
5
 
6
- variable("bucket_policy_only",
7
- description: "bucket_policy_only",
6
+ variable("uniform_bucket_level_access",
7
+ description: "uniform_bucket_level_access",
8
8
  type: "bool",
9
9
  default: false,
10
10
  )
@@ -1,5 +1,5 @@
1
- variable("bucket_policy_only",
2
- description: "bucket_policy_only",
1
+ variable("uniform_bucket_level_access",
2
+ description: "uniform_bucket_level_access",
3
3
  type: "bool",
4
4
  default: false,
5
5
  )
@@ -1,10 +1,10 @@
1
- resource "random_pet" "bucket" {
1
+ resource "random_pet" "this" {
2
2
  length = 2
3
3
  }
4
4
 
5
5
  module "bucket" {
6
6
  source = "../../modules/example"
7
7
 
8
- name = "bucket-${random_pet.bucket.id}"
9
- bucket_policy_only = var.bucket_policy_only
8
+ name = "bucket-${random_pet.this.id}"
9
+ uniform_bucket_level_access = var.uniform_bucket_level_access
10
10
  }
@@ -1,5 +1,5 @@
1
- variable "bucket_policy_only" {
2
- description = "bucket_policy_only"
1
+ variable "uniform_bucket_level_access" {
2
+ description = "uniform_bucket_level_access"
3
3
  type = bool
4
4
  default = false
5
5
  }
@@ -1,7 +1,7 @@
1
1
  # This starter example of a spec that creates a test harness and provisions a real s3 bucket.
2
2
  # The test harness will be created at:
3
3
  #
4
- # /tmp/terraspace-test-harnesses/<%= @name %>
4
+ # /tmp/terraspace-test-harnesses/<%= @name %>-harness
5
5
  #
6
6
  # It's recommended to run this on a test AWS account.
7
7
  #
@@ -9,7 +9,7 @@ describe "main" do
9
9
  before(:all) do
10
10
  mod_path = File.expand_path("../..", __dir__)
11
11
  terraspace.build_test_harness(
12
- name: "<%= @name %>",
12
+ name: "<%= @name %>-harness",
13
13
  modules: {example: mod_path},
14
14
  stacks: {example: "#{mod_path}/test/spec/fixtures/stack"},
15
15
  )
@@ -0,0 +1,26 @@
1
+ describe "main" do
2
+ before(:all) do
3
+ # Build terraspace project to use as a test harness
4
+ # Will be located at: /tmp/terraspace/test-harnesses/demo-harness
5
+ terraspace.build_test_harness(
6
+ name: "demo-harness",
7
+ modules: "app/modules", # include all modules in this folder
8
+ stacks: "app/stacks", # include all stacks in this folder
9
+ # override demo stack tfvars for testing
10
+ # copied over to test harness' app/stacks/demo/tfvars/test.tfvars
11
+ tfvars: {demo: "spec/fixtures/tfvars/demo.tfvars"},
12
+ # create config if needed. The folder will be copied over
13
+ # config: "spec/fixtures/config",
14
+ )
15
+ terraspace.up("demo") # provision real resources
16
+ end
17
+ after(:all) do
18
+ terraspace.down("demo") # destroy real resources
19
+ end
20
+
21
+ it "successful deploy" do
22
+ # Example
23
+ bucket_url = terraspace.output("demo", "bucket_url") # IE: gs://bucket-free-coyote
24
+ expect(bucket_url).to include("gs://bucket-")
25
+ end
26
+ end
@@ -1,6 +1,3 @@
1
- lib = File.expand_path("../../../", __FILE__)
2
- $:.unshift(lib)
3
-
4
1
  require "memoist"
5
2
  require "terraspace" # for interface
6
3
 
@@ -20,12 +17,17 @@ module TerraspacePluginGoogle
20
17
  def configure(&block)
21
18
  Interfaces::Config.instance.configure(&block)
22
19
  end
20
+
21
+ def config
22
+ Interfaces::Config.instance.config
23
+ end
24
+
23
25
  extend self
24
26
  end
25
27
 
26
28
  Terraspace::Plugin.register("google",
27
29
  backend: "gcs",
28
- config_instance: TerraspacePluginGoogle::Interfaces::Config.instance,
30
+ config_class: TerraspacePluginGoogle::Interfaces::Config,
29
31
  layer_class: TerraspacePluginGoogle::Interfaces::Layer,
30
32
  root: File.dirname(__dir__),
31
33
  )
@@ -4,15 +4,17 @@ module TerraspacePluginGoogle::Interfaces
4
4
  include TerraspacePluginGoogle::Clients
5
5
 
6
6
  def call
7
+ return unless TerraspacePluginGoogle.config.auto_create
8
+
7
9
  bucket = @info["bucket"]
8
10
  unless bucket # not bucket provided
9
- puts "ERROR: no bucket value provided in your terraform backend config".color(:red)
11
+ logger.error "ERROR: no bucket value provided in your terraform backend config"
10
12
  exit 1
11
13
  end
12
14
  if exist?(bucket)
13
- # puts "Bucket already exist: #{bucket}"
15
+ logger.debug "Bucket already exist: #{bucket}"
14
16
  else
15
- puts "Creating bucket: #{bucket}"
17
+ logger.info "Creating bucket: #{bucket}"
16
18
  create_bucket(bucket)
17
19
  end
18
20
  end
@@ -27,10 +29,14 @@ module TerraspacePluginGoogle::Interfaces
27
29
  def exist?(name)
28
30
  !!storage.bucket(name)
29
31
  rescue Google::Cloud::PermissionDeniedError => e
30
- puts "#{e.class}: #{e.message}"
31
- puts "ERROR: Bucket is not available: #{name}".color(:red)
32
- puts "Bucket might be owned by someone else or is on another one of your Google accounts."
32
+ logger.error "#{e.class}: #{e.message}"
33
+ logger.error "ERROR: Bucket is not available: #{name}".color(:red)
34
+ logger.error "Bucket might be owned by someone else or is on another one of your Google accounts."
33
35
  exit 1
34
36
  end
37
+
38
+ def logger
39
+ Terraspace.logger
40
+ end
35
41
  end
36
42
  end
@@ -3,12 +3,16 @@ module TerraspacePluginGoogle::Interfaces
3
3
  include Terraspace::Plugin::Config::Interface
4
4
  include Singleton
5
5
 
6
+ # interface method
7
+ # load_project_config: config/plugins/google.rb
6
8
  def provider
7
9
  "google"
8
10
  end
9
11
 
12
+ # interface method
10
13
  def defaults
11
14
  c = ActiveSupport::OrderedOptions.new
15
+ c.auto_create = true
12
16
  c.gcs = ActiveSupport::OrderedOptions.new
13
17
  c.gcs.versioning = true
14
18
  c
@@ -4,6 +4,8 @@ module TerraspacePluginGoogle::Interfaces
4
4
  class Expander
5
5
  include Terraspace::Plugin::Expander::Interface
6
6
  delegate :project, :region, to: :gcp_data
7
+ alias_method :namespace, :project
8
+ alias_method :location, :region
7
9
 
8
10
  def gcp_data
9
11
  GcpData
@@ -2,26 +2,17 @@ require "gcp_data"
2
2
 
3
3
  module TerraspacePluginGoogle::Interfaces
4
4
  class Layer
5
+ include Terraspace::Plugin::Layer::Interface
5
6
  extend Memoist
6
7
 
7
8
  # interface method
8
- def region
9
- gcp_data.region
10
- end
11
-
12
- # interface method
13
- # not a typo, mapping project to "account" to confirm to interface
14
- def account
15
- gcp_data.project
9
+ def namespace
10
+ GcpData.project
16
11
  end
17
12
 
18
13
  # interface method
19
- def provider
20
- "google"
21
- end
22
-
23
- def gcp_data
24
- GcpData
14
+ def region
15
+ GcpData.region
25
16
  end
26
17
  end
27
18
  end
@@ -0,0 +1,35 @@
1
+ module TerraspacePluginGoogle::Interfaces
2
+ class Summary
3
+ include Terraspace::Plugin::Summary::Interface
4
+ include TerraspacePluginGoogle::Clients
5
+
6
+ # interface method
7
+ def key_field
8
+ 'prefix'
9
+ end
10
+
11
+ # interface method
12
+ def download
13
+ bucket = storage.bucket(@bucket)
14
+ unless bucket
15
+ logger.error "ERROR: bucket #{@bucket} does not exist".color(:red)
16
+ exit 1
17
+ end
18
+ bucket.files(prefix: @folder).all do |f|
19
+ file = bucket.file(f.name)
20
+ next if file.nil? # in case file has been removed since .files
21
+ # Note the f.name already includes the folder
22
+ local_path = "#{@dest}/#{f.name}"
23
+ FileUtils.mkdir_p(File.dirname(local_path))
24
+ file.download(local_path)
25
+ end
26
+ end
27
+
28
+ # interface method
29
+ def delete_empty_statefile(key)
30
+ bucket = storage.bucket(@bucket)
31
+ file = bucket.file(key)
32
+ file.delete
33
+ end
34
+ end
35
+ end
@@ -1,3 +1,3 @@
1
1
  module TerraspacePluginGoogle
2
- VERSION = "0.0.0"
2
+ VERSION = "0.2.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: terraspace_plugin_google
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-31 00:00:00.000000000 Z
11
+ date: 2020-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: gcp_data
@@ -82,19 +82,20 @@ files:
82
82
  - Rakefile
83
83
  - bin/console
84
84
  - bin/setup
85
+ - git st
85
86
  - lib/templates/hcl/module/main.tf
86
87
  - lib/templates/hcl/module/outputs.tf
87
88
  - lib/templates/hcl/module/variables.tf
88
- - lib/templates/hcl/project/config/templates/backend.tf.tt
89
- - lib/templates/hcl/project/config/templates/provider.tf
89
+ - lib/templates/hcl/project/config/terraform/backend.tf.tt
90
+ - lib/templates/hcl/project/config/terraform/provider.tf
90
91
  - lib/templates/hcl/stack/main.tf
91
92
  - lib/templates/hcl/stack/outputs.tf
92
93
  - lib/templates/hcl/stack/variables.tf
93
94
  - lib/templates/ruby/module/main.rb
94
95
  - lib/templates/ruby/module/outputs.rb
95
96
  - lib/templates/ruby/module/variables.rb
96
- - lib/templates/ruby/project/config/templates/backend.rb.tt
97
- - lib/templates/ruby/project/config/templates/provider.rb
97
+ - lib/templates/ruby/project/config/terraform/backend.rb.tt
98
+ - lib/templates/ruby/project/config/terraform/provider.rb
98
99
  - lib/templates/ruby/stack/main.rb
99
100
  - lib/templates/ruby/stack/outputs.rb
100
101
  - lib/templates/ruby/stack/variables.rb
@@ -105,6 +106,8 @@ files:
105
106
  - lib/templates/test/rspec/module/test/spec/fixtures/stack/variables.tf
106
107
  - lib/templates/test/rspec/module/test/spec/main_spec.rb.tt
107
108
  - lib/templates/test/rspec/module/test/spec/spec_helper.rb
109
+ - lib/templates/test/rspec/project/spec/fixtures/tfvars/demo.tfvars
110
+ - lib/templates/test/rspec/project/spec/stacks/demo/main_spec.rb
108
111
  - lib/terraspace_plugin_google.rb
109
112
  - lib/terraspace_plugin_google/autoloader.rb
110
113
  - lib/terraspace_plugin_google/clients.rb
@@ -112,6 +115,7 @@ files:
112
115
  - lib/terraspace_plugin_google/interfaces/config.rb
113
116
  - lib/terraspace_plugin_google/interfaces/expander.rb
114
117
  - lib/terraspace_plugin_google/interfaces/layer.rb
118
+ - lib/terraspace_plugin_google/interfaces/summary.rb
115
119
  - lib/terraspace_plugin_google/version.rb
116
120
  - terraspace_plugin_google.gemspec
117
121
  homepage: https://github.com/boltops-tools/terraspace_plugin_google
@@ -1,6 +0,0 @@
1
- terraform {
2
- backend "gcs" {
3
- bucket = "<%%= backend_expand("gcs", "terraform-state-:PROJECT-:REGION-:ENV") %>" # expanded by terraspace IE: terraform-state-project-us-central1-dev
4
- prefix = "<%%= backend_expand("gcs", ":REGION/:ENV/:BUILD_DIR") %>" # expanded by terraspace IE: us-central1/dev/modules/vm
5
- }
6
- }