This repository was archived by the owner on Oct 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
Upgrade aws-sdk to v2 #32
Open
tatsuyafw
wants to merge
4
commits into
codenize-tools:0.3.0
Choose a base branch
from
tatsuyafw:upgrade-aws-sdk-to-v2
base: 0.3.0
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.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,92 +1,95 @@ | ||
| module AWS | ||
| class EC2 | ||
| DESC_OWNER_ID_RETRY_TIMES = 3 | ||
| DESC_OWNER_ID_RETRY_WAIT = 3 | ||
| SECURITY_GROUP_NAME_MAX_LEN = 255 | ||
|
|
||
| def owner_id | ||
| return ENV['AWS_OWNER_ID'] if ENV['AWS_OWNER_ID'] | ||
| return @owner_id if @owner_id | ||
|
|
||
| @owner_id = get_owner_id_from_iam || get_owner_id_from_security_group | ||
|
|
||
| return @owner_id | ||
| end | ||
|
|
||
| def own?(other) | ||
| other == owner_id | ||
| end | ||
|
|
||
| private | ||
| def get_owner_id_from_iam | ||
| credentials = self.config.credential_provider.credentials | ||
| iam = AWS::IAM.new(credentials) | ||
| user = iam.client.get_user rescue nil | ||
| return nil unless user | ||
| arn = user[:user][:arn] | ||
| arn.split(':')[4] | ||
| end | ||
|
|
||
| def get_owner_id_from_security_group | ||
| security_group = create_random_security_group | ||
| return nil unless security_group | ||
| owner_id = random_security_group_owner_id(security_group) | ||
| delete_random_security_group(security_group) | ||
| return owner_id | ||
| end | ||
|
|
||
| def create_random_security_group | ||
| security_group = nil | ||
|
|
||
| DESC_OWNER_ID_RETRY_TIMES.times do | ||
| name = random_security_group_name | ||
| security_group = self.security_groups.create(name) rescue nil | ||
| break if security_group | ||
| sleep DESC_OWNER_ID_RETRY_WAIT | ||
| module Aws | ||
| module EC2 | ||
| class Resource | ||
| DESC_OWNER_ID_RETRY_TIMES = 3 | ||
| DESC_OWNER_ID_RETRY_WAIT = 3 | ||
| SECURITY_GROUP_NAME_MAX_LEN = 255 | ||
|
|
||
| def owner_id | ||
| return ENV['AWS_OWNER_ID'] if ENV['AWS_OWNER_ID'] | ||
| return @owner_id if @owner_id | ||
|
|
||
| @owner_id = get_owner_id_from_iam || get_owner_id_from_security_group | ||
|
|
||
| return @owner_id | ||
| end | ||
|
|
||
| def own?(other) | ||
| other == owner_id | ||
| end | ||
|
|
||
| return security_group | ||
| end | ||
| private | ||
|
|
||
| def get_owner_id_from_iam | ||
| credentials = Aws::EC2::Client.new.config.credentials | ||
| iam = Aws::IAM::Client.new(credentials: credentials) | ||
| user = iam.get_user rescue nil | ||
| return nil unless user | ||
| arn = user[:user][:arn] | ||
| arn.split(':')[4] | ||
| end | ||
|
|
||
| def random_security_group_owner_id(security_group) | ||
| owner_id = nil | ||
| def get_owner_id_from_security_group | ||
| security_group = create_random_security_group | ||
| return nil unless security_group | ||
| owner_id = random_security_group_owner_id(security_group) | ||
| delete_random_security_group(security_group) | ||
| return owner_id | ||
| end | ||
|
|
||
| (1..DESC_OWNER_ID_RETRY_TIMES).each do |i| | ||
| begin | ||
| owner_id = security_group.owner_id | ||
| break | ||
| rescue => e | ||
| raise e unless i < DESC_OWNER_ID_RETRY_TIMES | ||
| def create_random_security_group | ||
| security_group = nil | ||
|
|
||
| DESC_OWNER_ID_RETRY_TIMES.times do | ||
| name = random_security_group_name | ||
| security_group = self.create_security_group(group_name: name, description: name) rescue nil | ||
| break if security_group | ||
| sleep DESC_OWNER_ID_RETRY_WAIT | ||
| end | ||
|
|
||
| sleep DESC_OWNER_ID_RETRY_WAIT | ||
| return security_group | ||
| end | ||
|
|
||
| return owner_id | ||
| end | ||
| def random_security_group_owner_id(security_group) | ||
| owner_id = nil | ||
|
|
||
| (1..DESC_OWNER_ID_RETRY_TIMES).each do |i| | ||
| begin | ||
| owner_id = security_group.owner_id | ||
| break | ||
| rescue => e | ||
| raise e unless i < DESC_OWNER_ID_RETRY_TIMES | ||
| end | ||
|
|
||
| def delete_random_security_group(security_group) | ||
| (1..DESC_OWNER_ID_RETRY_TIMES).each do |i| | ||
| begin | ||
| security_group.delete | ||
| break | ||
| rescue => e | ||
| raise e unless i < DESC_OWNER_ID_RETRY_TIMES | ||
| sleep DESC_OWNER_ID_RETRY_WAIT | ||
| end | ||
|
|
||
| sleep DESC_OWNER_ID_RETRY_WAIT | ||
| return owner_id | ||
| end | ||
| end | ||
|
|
||
| def random_security_group_name | ||
| name = [] | ||
| len = SECURITY_GROUP_NAME_MAX_LEN | ||
| def delete_random_security_group(security_group) | ||
| (1..DESC_OWNER_ID_RETRY_TIMES).each do |i| | ||
| begin | ||
| security_group.delete | ||
| break | ||
| rescue => e | ||
| raise e unless i < DESC_OWNER_ID_RETRY_TIMES | ||
| end | ||
|
|
||
| while name.length < len | ||
| name.concat(('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a) | ||
| sleep DESC_OWNER_ID_RETRY_WAIT | ||
| end | ||
| end | ||
|
|
||
| name.shuffle[0...len].join | ||
| end | ||
| def random_security_group_name | ||
| name = [] | ||
| len = SECURITY_GROUP_NAME_MAX_LEN | ||
|
|
||
| while name.length < len | ||
| name.concat(('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a) | ||
| end | ||
|
|
||
| name.shuffle[0...len].join | ||
| end | ||
| end # Resource | ||
| end # EC2 | ||
| end # AWS | ||
| end # Aws |
Oops, something went wrong.
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.
L61's
groupand L63'sgroupis different.And L61's
groupmay benil.I try
bundle exec bin/piculet -eon my environment . So, raise following error.Uh oh!
There was an error while loading. Please reload this page.
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.
Thanks for your review!
I cannot immediately come up with why
@ec2.security_groups.find { |g| g.id == group.group_id }returnsnil, so I try to search the cause.BTW, I added the L61
@ec2.security_groups.find { |g| g.id == group.group_id }to fetchgroup.group_nameinfo. Becauseip_perm.user_id_group_pair's group (L60) has always no information aboutgroup_nameas below.