This repository has been archived by the owner on Sep 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 657
feat: Filtering imported security groups by IDs #410
Open
mumoshu
wants to merge
1
commit into
dtan4:master
Choose a base branch
from
mumoshu:sg-filtering-by-ids
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
module Terraforming | ||
class CLI < Thor | ||
OPTIONS_AVAILABLE_TO_SUBCOMMANDS = [ | ||
Terraforming::Resource::SecurityGroup::AVAILABLE_OPTIONS, | ||
].reduce(:concat).freeze | ||
|
||
class_option :merge, type: :string, desc: "tfstate file to merge" | ||
class_option :overwrite, type: :boolean, desc: "Overwrite existing tfstate" | ||
class_option :tfstate, type: :boolean, desc: "Generate tfstate" | ||
|
@@ -191,6 +195,7 @@ def s3 | |
end | ||
|
||
desc "sg", "Security Group" | ||
method_option :"group-ids", type: :array, desc: "Filter exported security groups by IDs" | ||
def sg | ||
execute(Terraforming::Resource::SecurityGroup, options) | ||
end | ||
|
@@ -242,7 +247,13 @@ def configure_aws(options) | |
|
||
def execute(klass, options) | ||
configure_aws(options) | ||
result = options[:tfstate] ? tfstate(klass, options[:merge]) : tf(klass) | ||
|
||
subcommand_options = options.select { |k, v| OPTIONS_AVAILABLE_TO_SUBCOMMANDS.include? k } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mind if I blindly passed all the |
||
result = if options[:tfstate] | ||
tfstate(klass, options[:merge], subcommand_options) | ||
else | ||
tf(klass, subcommand_options) | ||
end | ||
|
||
if options[:tfstate] && options[:merge] && options[:overwrite] | ||
open(options[:merge], "w+") do |f| | ||
|
@@ -254,14 +265,23 @@ def execute(klass, options) | |
end | ||
end | ||
|
||
def tf(klass) | ||
klass.tf | ||
def tf(klass, options={}) | ||
if options.empty? | ||
klass.tf | ||
else | ||
klass.tf(options) | ||
end | ||
end | ||
|
||
def tfstate(klass, tfstate_path) | ||
def tfstate(klass, tfstate_path, options={}) | ||
tfstate = tfstate_path ? MultiJson.load(open(tfstate_path).read) : tfstate_skeleton | ||
tfstate["serial"] = tfstate["serial"] + 1 | ||
tfstate["modules"][0]["resources"] = tfstate["modules"][0]["resources"].merge(klass.tfstate) | ||
tfstate_addition = if options.empty? | ||
klass.tfstate | ||
else | ||
klass.tfstate(options) | ||
end | ||
tfstate["modules"][0]["resources"] = tfstate["modules"][0]["resources"].merge(tfstate_addition) | ||
MultiJson.encode(tfstate, pretty: true) | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,35 @@ module Resource | |
class SecurityGroup | ||
include Terraforming::Util | ||
|
||
def self.tf(client: Aws::EC2::Client.new) | ||
self.new(client).tf | ||
module Options | ||
GROUP_IDS = 'group-ids' | ||
end | ||
|
||
def self.tfstate(client: Aws::EC2::Client.new) | ||
self.new(client).tfstate | ||
AVAILABLE_OPTIONS = [ | ||
Options::GROUP_IDS, | ||
].freeze | ||
|
||
def self.tf(options={}) | ||
opts = apply_defaults_to_options(options) | ||
self | ||
.new(opts[:client], opts) | ||
.tf | ||
end | ||
|
||
def self.tfstate(options={}) | ||
opts = apply_defaults_to_options(options) | ||
self.new(opts[:client], opts).tfstate | ||
end | ||
|
||
def self.apply_defaults_to_options(options) | ||
options.dup.tap { |o| | ||
o[:client] ||= Aws::EC2::Client.new | ||
} | ||
end | ||
|
||
def initialize(client) | ||
def initialize(client, options) | ||
@client = client | ||
@group_ids = options[Options::GROUP_IDS] | ||
end | ||
|
||
def tf | ||
|
@@ -159,7 +178,12 @@ def self_referenced_permission?(security_group, permission) | |
end | ||
|
||
def security_groups | ||
@client.describe_security_groups.map(&:security_groups).flatten | ||
description = if @group_ids | ||
@client.describe_security_groups(group_ids: @group_ids) | ||
else | ||
@client.describe_security_groups | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I opted to make this block conditional instead of doing I can make it look something like:
but I prefer separating it into another PR to ease your review. |
||
description.map(&:security_groups).flatten | ||
end | ||
|
||
def security_groups_in(permission, security_group) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to move this to the bottom of this file in order to avoid
NameError: uninitialized constant Terraforming::Resource
at the line 4 oflib/terraforming/cli.rb
.