From e84a3ff333fd503effea1dfb936127ca53274ea2 Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Tue, 14 Mar 2023 23:26:33 -0400 Subject: [PATCH 1/8] Add support for a custom aws endpoint There may be other use cases, but my main use case is to run this against the standalone moto server: http://docs.getmoto.org/en/latest/docs/server_mode.html This would allow awspec to be used as part of a full test loop for aws automation without the need to configure an aws account. --- README.md | 17 +++++++++++++++++ lib/awspec/helper/finder.rb | 3 ++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index dbbed0bf..bb674bef 100644 --- a/README.md +++ b/README.md @@ -172,6 +172,23 @@ Awspec.configure do |config| end ``` +### Advanced Tips: Setting A Custom Endpoint And Testing Locally + +Set the `aws_custom_endpoint` environment variable to tell awspec to use a different endpoint to connect to aws: + +``` +aws_custom_endpoint=http://localhost:5000 bundle exec rake spec +``` + +This can be used with [Moto's Standalone Server Mode](http://docs.getmoto.org/en/latest/docs/server_mode.html) to run all your aws tests locally. For example, to create resources on the local server using the aws cli, run: + +``` +AWS_SECRET_ACCESS_KEY=dummy AWS_ACCESS_KEY_ID=dummy \ + aws --endpoint http://localhost:5000 s3 mb my-bucket +``` + +Awspec should then be able to verify that those resources were created properly. + ## Support AWS Resources [Resource Types information here](doc/resource_types.md) diff --git a/lib/awspec/helper/finder.rb b/lib/awspec/helper/finder.rb index 849b883f..4d3aa04f 100644 --- a/lib/awspec/helper/finder.rb +++ b/lib/awspec/helper/finder.rb @@ -170,7 +170,8 @@ module Finder CLIENT_OPTIONS = { http_proxy: ENV['http_proxy'] || ENV['https_proxy'] || nil, - http_wire_trace: ENV['http_wire_trace'] || false + http_wire_trace: ENV['http_wire_trace'] || false, + endpoint: ENV['aws_custom_endpoint'] || nil } check_configuration = ENV['DISABLE_AWS_CLIENT_CHECK'] != 'true' if ENV.key?('DISABLE_AWS_CLIENT_CHECK') From ce33b0c5229a791ca3df0fdf215d673e3b2cb23b Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Sun, 26 Mar 2023 12:24:34 -0400 Subject: [PATCH 2/8] Only set endpoint of the override is set The aws sdk errors out if we set the endpoint to nil. --- lib/awspec/helper/finder.rb | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/awspec/helper/finder.rb b/lib/awspec/helper/finder.rb index 4d3aa04f..e099bccd 100644 --- a/lib/awspec/helper/finder.rb +++ b/lib/awspec/helper/finder.rb @@ -170,10 +170,16 @@ module Finder CLIENT_OPTIONS = { http_proxy: ENV['http_proxy'] || ENV['https_proxy'] || nil, - http_wire_trace: ENV['http_wire_trace'] || false, - endpoint: ENV['aws_custom_endpoint'] || nil + http_wire_trace: ENV['http_wire_trace'] || false } + # We have to set this conditionally after defining `CLIENT_OPTIONS`, + # because setting the `endpoint` argument to `nil` results in an error from + # the aws sdk. + if ENV.has_key?('aws_custom_endpoint') + CLIENT_OPTIONS[:endpoint] = ENV['aws_custom_endpoint'] + end + check_configuration = ENV['DISABLE_AWS_CLIENT_CHECK'] != 'true' if ENV.key?('DISABLE_AWS_CLIENT_CHECK') # define_method below will "hide" any exception that comes from bad From 8116b5a92c1ac3cfc03c431e8741961c58df7866 Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Sun, 26 Mar 2023 22:06:03 -0400 Subject: [PATCH 3/8] Fix linter error --- lib/awspec/helper/finder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/awspec/helper/finder.rb b/lib/awspec/helper/finder.rb index e099bccd..d0a15551 100644 --- a/lib/awspec/helper/finder.rb +++ b/lib/awspec/helper/finder.rb @@ -176,7 +176,7 @@ module Finder # We have to set this conditionally after defining `CLIENT_OPTIONS`, # because setting the `endpoint` argument to `nil` results in an error from # the aws sdk. - if ENV.has_key?('aws_custom_endpoint') + if ENV.key?('aws_custom_endpoint') CLIENT_OPTIONS[:endpoint] = ENV['aws_custom_endpoint'] end From fa889be7738e3a66c3fe6b9659814047b001f6f9 Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Fri, 7 Apr 2023 21:57:12 -0400 Subject: [PATCH 4/8] Update lib/awspec/helper/finder.rb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Ken’ichiro Oyama --- lib/awspec/helper/finder.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/awspec/helper/finder.rb b/lib/awspec/helper/finder.rb index d0a15551..9525b0e8 100644 --- a/lib/awspec/helper/finder.rb +++ b/lib/awspec/helper/finder.rb @@ -176,7 +176,7 @@ module Finder # We have to set this conditionally after defining `CLIENT_OPTIONS`, # because setting the `endpoint` argument to `nil` results in an error from # the aws sdk. - if ENV.key?('aws_custom_endpoint') + if ENV.key?('endpoint') CLIENT_OPTIONS[:endpoint] = ENV['aws_custom_endpoint'] end From 7bb18e9dc3b9a4bad7189fb52754c0e9b52ac661 Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Fri, 7 Apr 2023 22:08:46 -0400 Subject: [PATCH 5/8] Use `endpoint` everywhere and update README --- README.md | 16 +++++++++------- lib/awspec/helper/finder.rb | 2 +- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index bb674bef..f081e878 100644 --- a/README.md +++ b/README.md @@ -174,20 +174,22 @@ end ### Advanced Tips: Setting A Custom Endpoint And Testing Locally -Set the `aws_custom_endpoint` environment variable to tell awspec to use a different endpoint to connect to aws: +Set the `endpoint` environment variable to tell awspec to use a different +endpoint to connect to aws. Common use cases are connecting to aws through a +proxy or connecting to a local mock aws environment. -``` -aws_custom_endpoint=http://localhost:5000 bundle exec rake spec -``` - -This can be used with [Moto's Standalone Server Mode](http://docs.getmoto.org/en/latest/docs/server_mode.html) to run all your aws tests locally. For example, to create resources on the local server using the aws cli, run: +For example, first create a resource on a local aws service using the aws cli: ``` AWS_SECRET_ACCESS_KEY=dummy AWS_ACCESS_KEY_ID=dummy \ aws --endpoint http://localhost:5000 s3 mb my-bucket ``` -Awspec should then be able to verify that those resources were created properly. +Then you can tell awspec to run the test suite using the same custom endpoint: + +``` +endpoint=http://localhost:5000 bundle exec rake spec +``` ## Support AWS Resources diff --git a/lib/awspec/helper/finder.rb b/lib/awspec/helper/finder.rb index 9525b0e8..8106307e 100644 --- a/lib/awspec/helper/finder.rb +++ b/lib/awspec/helper/finder.rb @@ -177,7 +177,7 @@ module Finder # because setting the `endpoint` argument to `nil` results in an error from # the aws sdk. if ENV.key?('endpoint') - CLIENT_OPTIONS[:endpoint] = ENV['aws_custom_endpoint'] + CLIENT_OPTIONS[:endpoint] = ENV['endpoint'] end check_configuration = ENV['DISABLE_AWS_CLIENT_CHECK'] != 'true' if ENV.key?('DISABLE_AWS_CLIENT_CHECK') From a3075f482c75512aa7b6e3112951fd690a35769f Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Fri, 7 Apr 2023 22:15:26 -0400 Subject: [PATCH 6/8] Fix aws cli example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f081e878..bc947d0e 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ For example, first create a resource on a local aws service using the aws cli: ``` AWS_SECRET_ACCESS_KEY=dummy AWS_ACCESS_KEY_ID=dummy \ - aws --endpoint http://localhost:5000 s3 mb my-bucket + aws --endpoint-url http://localhost:5000 s3 mb s3://my-bucket ``` Then you can tell awspec to run the test suite using the same custom endpoint: From b8ba7cca807d397fd83f92b2f1819bc71abd6189 Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Fri, 7 Apr 2023 22:17:15 -0400 Subject: [PATCH 7/8] Add aws compatible service use case --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bc947d0e..cd7178b4 100644 --- a/README.md +++ b/README.md @@ -176,7 +176,8 @@ end Set the `endpoint` environment variable to tell awspec to use a different endpoint to connect to aws. Common use cases are connecting to aws through a -proxy or connecting to a local mock aws environment. +proxy, using a different service with an aws compatible interface, or +connecting to a local mock aws environment. For example, first create a resource on a local aws service using the aws cli: From a27a7249e808aec9d7a1438d6fe01d659a69c101 Mon Sep 17 00:00:00 2001 From: Shaun Verch Date: Fri, 7 Apr 2023 22:17:59 -0400 Subject: [PATCH 8/8] Change section header to just say custom endpoint Because testing locally is not the only reason to do this. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index cd7178b4..daf6b32d 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ Awspec.configure do |config| end ``` -### Advanced Tips: Setting A Custom Endpoint And Testing Locally +### Advanced Tips: Setting A Custom Endpoint Set the `endpoint` environment variable to tell awspec to use a different endpoint to connect to aws. Common use cases are connecting to aws through a