practice_terraforming 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +34 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.rubocop.yml +93 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +95 -0
- data/LICENSE.txt +21 -0
- data/README.md +259 -0
- data/Rakefile +8 -0
- data/bin/practice_terraforming +6 -0
- data/lib/practice_terraforming.rb +16 -0
- data/lib/practice_terraforming/cli.rb +92 -0
- data/lib/practice_terraforming/resource/iam_policy_attachment.rb +85 -0
- data/lib/practice_terraforming/resource/iam_role.rb +59 -0
- data/lib/practice_terraforming/resource/iam_role_policy_attachment.rb +74 -0
- data/lib/practice_terraforming/resource/s3.rb +71 -0
- data/lib/practice_terraforming/template/tf/iam_policy_attachment.erb +10 -0
- data/lib/practice_terraforming/template/tf/iam_role.erb +11 -0
- data/lib/practice_terraforming/template/tf/iam_role_policy_attachment.erb +7 -0
- data/lib/practice_terraforming/template/tf/s3.erb +12 -0
- data/lib/practice_terraforming/util.rb +32 -0
- data/lib/practice_terraforming/version.rb +5 -0
- data/practice_terraforming.gemspec +35 -0
- data/script/console +15 -0
- data/script/generate +96 -0
- data/script/setup +7 -0
- data/templates/resource.erb.erb +3 -0
- data/templates/resource.rb.erb +31 -0
- data/templates/resource_spec.rb.erb +39 -0
- metadata +202 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4510b6b15bd49f015c16da4633554e11faed8e43
|
4
|
+
data.tar.gz: 07e6b2fd44fd6a531d30ed90b95c6c5eef9e8487
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f33ef2d0902a23427eac11f2a922d1708987f4aadd7dd552d77e56c373b8cd75b7ad980cecad9988c6d2085c058a5a7664939f42c5ad71d8925e6db9b02afa55
|
7
|
+
data.tar.gz: dd33fef40a74793bd1e271c966e5f5f57534ffdc864a7152e92bad22a6737fd52379835631b51b8744f65f0d8823c3229be1dc7deef23cbdbd23659c4c5f7ea9
|
@@ -0,0 +1,34 @@
|
|
1
|
+
version: 2
|
2
|
+
jobs:
|
3
|
+
build:
|
4
|
+
docker:
|
5
|
+
- image: circleci/ruby:2.4.2-jessie-node
|
6
|
+
environment:
|
7
|
+
BUNDLER_VERSION: 2.0.2
|
8
|
+
steps:
|
9
|
+
- checkout
|
10
|
+
- restore_cache:
|
11
|
+
keys:
|
12
|
+
- v1-dependencies-{{ checksum "Gemfile.lock" }}
|
13
|
+
- v1-dependencies-
|
14
|
+
- run:
|
15
|
+
name: setup bundler
|
16
|
+
command: |
|
17
|
+
sudo gem update --system
|
18
|
+
sudo gem uninstall bundler
|
19
|
+
sudo rm /usr/local/bin/bundle
|
20
|
+
sudo rm /usr/local/bin/bundler
|
21
|
+
sudo gem install bundler
|
22
|
+
- run:
|
23
|
+
name: install
|
24
|
+
command: bundle install --path vendor/bundle
|
25
|
+
- save_cache:
|
26
|
+
paths:
|
27
|
+
- ./vendor/bundle
|
28
|
+
key: v1-dependencies-{{ checksum "Gemfile.lock" }}
|
29
|
+
- run:
|
30
|
+
name: rubocop
|
31
|
+
command: bundle exec rubocop
|
32
|
+
- run:
|
33
|
+
name: rspec
|
34
|
+
command: bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
Metrics/AbcSize:
|
2
|
+
Enabled: false
|
3
|
+
|
4
|
+
Metrics/ClassLength:
|
5
|
+
Enabled: false
|
6
|
+
|
7
|
+
Metrics/CyclomaticComplexity:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/LineLength:
|
11
|
+
Max: 140
|
12
|
+
Exclude:
|
13
|
+
- "spec/**/*"
|
14
|
+
|
15
|
+
# Offense count: 42
|
16
|
+
# Configuration parameters: CountComments.
|
17
|
+
Metrics/MethodLength:
|
18
|
+
Enabled: false
|
19
|
+
|
20
|
+
Metrics/ModuleLength:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Style/BracesAroundHashParameters:
|
24
|
+
Enabled: false
|
25
|
+
|
26
|
+
Style/Documentation:
|
27
|
+
Enabled: false
|
28
|
+
|
29
|
+
Style/EachWithObject:
|
30
|
+
Enabled: false
|
31
|
+
|
32
|
+
Style/HashSyntax:
|
33
|
+
Exclude:
|
34
|
+
- 'Rakefile'
|
35
|
+
|
36
|
+
# TODO: check again
|
37
|
+
Style/IndentArray:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
# TODO: check again
|
41
|
+
Style/IndentHash:
|
42
|
+
Enabled: false
|
43
|
+
|
44
|
+
Style/LineEndConcatenation:
|
45
|
+
Enabled: false
|
46
|
+
|
47
|
+
Style/MutableConstant:
|
48
|
+
Enabled: false
|
49
|
+
|
50
|
+
Style/NumericLiterals:
|
51
|
+
Enabled: false
|
52
|
+
|
53
|
+
Style/ParallelAssignment:
|
54
|
+
Enabled: false
|
55
|
+
|
56
|
+
Style/RedundantSelf:
|
57
|
+
Enabled: false
|
58
|
+
|
59
|
+
# TODO: check again (force to %r literal)
|
60
|
+
Style/RegexpLiteral:
|
61
|
+
Enabled: true
|
62
|
+
|
63
|
+
Style/StringLiterals:
|
64
|
+
Enabled: false
|
65
|
+
|
66
|
+
Style/SymbolProc:
|
67
|
+
Enabled: false
|
68
|
+
|
69
|
+
Style/TrailingCommaInArrayLiteral:
|
70
|
+
Enabled: false
|
71
|
+
|
72
|
+
Style/RedundantPercentQ:
|
73
|
+
Enabled: false
|
74
|
+
|
75
|
+
Style/CollectionMethods:
|
76
|
+
PreferredMethods:
|
77
|
+
collect: "map"
|
78
|
+
collect!: "map!"
|
79
|
+
inject: "inject"
|
80
|
+
detect: "find"
|
81
|
+
find_all: "select"
|
82
|
+
|
83
|
+
Style/PerlBackrefs:
|
84
|
+
Enabled: false
|
85
|
+
|
86
|
+
Security/Open:
|
87
|
+
Enabled: false
|
88
|
+
|
89
|
+
Metrics/BlockLength:
|
90
|
+
Enabled: false
|
91
|
+
|
92
|
+
Naming/HeredocDelimiterNaming:
|
93
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at masatonaka1989@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
6
|
+
|
7
|
+
# Specify your gem's dependencies in practice_terraforming.gemspec
|
8
|
+
gemspec
|
9
|
+
|
10
|
+
group :development, :test do
|
11
|
+
gem 'rspec'
|
12
|
+
gem 'rubocop'
|
13
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
practice_terraforming (0.1.3)
|
5
|
+
aws-sdk-iam (~> 1)
|
6
|
+
aws-sdk-s3 (~> 1)
|
7
|
+
multi_json (~> 1.12.1)
|
8
|
+
thor
|
9
|
+
|
10
|
+
GEM
|
11
|
+
remote: https://rubygems.org/
|
12
|
+
specs:
|
13
|
+
ast (2.4.0)
|
14
|
+
aws-eventstream (1.0.3)
|
15
|
+
aws-partitions (1.237.0)
|
16
|
+
aws-sdk-core (3.76.0)
|
17
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
18
|
+
aws-partitions (~> 1, >= 1.228.0)
|
19
|
+
aws-sigv4 (~> 1.1)
|
20
|
+
jmespath (~> 1.0)
|
21
|
+
aws-sdk-iam (1.31.0)
|
22
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
23
|
+
aws-sigv4 (~> 1.1)
|
24
|
+
aws-sdk-kms (1.25.0)
|
25
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
26
|
+
aws-sigv4 (~> 1.1)
|
27
|
+
aws-sdk-s3 (1.53.0)
|
28
|
+
aws-sdk-core (~> 3, >= 3.71.0)
|
29
|
+
aws-sdk-kms (~> 1)
|
30
|
+
aws-sigv4 (~> 1.1)
|
31
|
+
aws-sigv4 (1.1.0)
|
32
|
+
aws-eventstream (~> 1.0, >= 1.0.2)
|
33
|
+
coveralls (0.8.21)
|
34
|
+
json (>= 1.8, < 3)
|
35
|
+
simplecov (~> 0.14.1)
|
36
|
+
term-ansicolor (~> 1.3)
|
37
|
+
thor (~> 0.19.4)
|
38
|
+
tins (~> 1.6)
|
39
|
+
diff-lcs (1.3)
|
40
|
+
docile (1.1.5)
|
41
|
+
jaro_winkler (1.5.4)
|
42
|
+
jmespath (1.4.0)
|
43
|
+
json (2.2.0)
|
44
|
+
multi_json (1.12.2)
|
45
|
+
parallel (1.18.0)
|
46
|
+
parser (2.6.5.0)
|
47
|
+
ast (~> 2.4.0)
|
48
|
+
rainbow (3.0.0)
|
49
|
+
rake (10.5.0)
|
50
|
+
rspec (3.9.0)
|
51
|
+
rspec-core (~> 3.9.0)
|
52
|
+
rspec-expectations (~> 3.9.0)
|
53
|
+
rspec-mocks (~> 3.9.0)
|
54
|
+
rspec-core (3.9.0)
|
55
|
+
rspec-support (~> 3.9.0)
|
56
|
+
rspec-expectations (3.9.0)
|
57
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
58
|
+
rspec-support (~> 3.9.0)
|
59
|
+
rspec-mocks (3.9.0)
|
60
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
61
|
+
rspec-support (~> 3.9.0)
|
62
|
+
rspec-support (3.9.0)
|
63
|
+
rubocop (0.76.0)
|
64
|
+
jaro_winkler (~> 1.5.1)
|
65
|
+
parallel (~> 1.10)
|
66
|
+
parser (>= 2.6)
|
67
|
+
rainbow (>= 2.2.2, < 4.0)
|
68
|
+
ruby-progressbar (~> 1.7)
|
69
|
+
unicode-display_width (>= 1.4.0, < 1.7)
|
70
|
+
ruby-progressbar (1.10.1)
|
71
|
+
simplecov (0.14.1)
|
72
|
+
docile (~> 1.1.0)
|
73
|
+
json (>= 1.8, < 3)
|
74
|
+
simplecov-html (~> 0.10.0)
|
75
|
+
simplecov-html (0.10.2)
|
76
|
+
term-ansicolor (1.7.1)
|
77
|
+
tins (~> 1.0)
|
78
|
+
thor (0.19.4)
|
79
|
+
tins (1.22.0)
|
80
|
+
unicode-display_width (1.6.0)
|
81
|
+
|
82
|
+
PLATFORMS
|
83
|
+
ruby
|
84
|
+
|
85
|
+
DEPENDENCIES
|
86
|
+
bundler (~> 2.0)
|
87
|
+
coveralls (~> 0.8.13)
|
88
|
+
practice_terraforming!
|
89
|
+
rake (~> 10.0)
|
90
|
+
rspec
|
91
|
+
rubocop
|
92
|
+
simplecov (~> 0.14.1)
|
93
|
+
|
94
|
+
BUNDLED WITH
|
95
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 masatonaka
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,259 @@
|
|
1
|
+
[![CircleCI](https://circleci.com/gh/nakamasato/practice_terraforming.svg?style=svg&circle-token=c3fbff2dec3543a4fce9fd86907f3b6cc9bdfeba)](https://circleci.com/gh/nakamasato/practice_terraforming)
|
2
|
+
|
3
|
+
# PracticeTerraforming
|
4
|
+
|
5
|
+
This is just for practice!
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'practice_terraforming'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install practice_terraforming
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/practice_terraforming. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the PracticeTerraforming project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/practice_terraforming/blob/master/CODE_OF_CONDUCT.md).
|
44
|
+
|
45
|
+
# How I created this
|
46
|
+
|
47
|
+
## Prepare Gem
|
48
|
+
|
49
|
+
```
|
50
|
+
bundle gem practice_terraforming
|
51
|
+
```
|
52
|
+
|
53
|
+
## Entrypoint of practice_terraforming
|
54
|
+
|
55
|
+
`bin/practice_terraforming`
|
56
|
+
|
57
|
+
CLI command (extended Thor)
|
58
|
+
|
59
|
+
## lib/practice_terraforming.rb
|
60
|
+
|
61
|
+
`lib/practice_terraforming.rb` just requires all the dependencies
|
62
|
+
|
63
|
+
## cli definition
|
64
|
+
|
65
|
+
`lib/practice_terraforming/cli.rb`
|
66
|
+
|
67
|
+
Basically, just copied from `terraforming/lib/terraforming/cli.rb`. Deleted resources but `iam_role` and `s3`
|
68
|
+
|
69
|
+
## AWS Resources (Main logic to generate tfstate/tf)
|
70
|
+
|
71
|
+
```
|
72
|
+
lib/practice_terraforming/resource/iam_role.rb
|
73
|
+
lib/practice_terraforming/resource/s3.rb
|
74
|
+
```
|
75
|
+
|
76
|
+
Resource file is generated by `script/generate` but need to update `tfstate` method
|
77
|
+
|
78
|
+
## Util
|
79
|
+
|
80
|
+
`lib/practice_terraforming/util.rb`
|
81
|
+
|
82
|
+
Just copied from `terraforming/lib/terraforming/util.rb`
|
83
|
+
|
84
|
+
## Gemspec
|
85
|
+
|
86
|
+
`practice_terraforming.gemspec`
|
87
|
+
|
88
|
+
Wrote dependencies with `spec.add_dependency` and `spec.add_development_dependency`
|
89
|
+
|
90
|
+
## Templates
|
91
|
+
|
92
|
+
- resource.erb.erb -> tf file
|
93
|
+
- resource.rb.erb -> resource class to write logic to generate tf file
|
94
|
+
- resource_spec.rb.erb -> spec file
|
95
|
+
|
96
|
+
## Spec
|
97
|
+
|
98
|
+
|spec file|memo|
|
99
|
+
|---|---|
|
100
|
+
|spec/fixtures/terraform.tfstate|copied from terraforming|
|
101
|
+
|spec/lib/practice_terraforming/cli_spec.rb|copied from terraforming|
|
102
|
+
|spec/lib/practice_terraforming/resource/<resource>.rb|generated by `script/generate` but need to write by yourself|
|
103
|
+
|
104
|
+
|
105
|
+
## Create Resource
|
106
|
+
|
107
|
+
0. generate templates with `script/generate`
|
108
|
+
|
109
|
+
```
|
110
|
+
script/generate iam_policy_attachment
|
111
|
+
==> Generate iam_policy_attachment.rb
|
112
|
+
==> Generate iam_policy_attachment_spec.rb
|
113
|
+
==> Generate iam_policy_attachment.erb
|
114
|
+
|
115
|
+
Add below code by hand.
|
116
|
+
|
117
|
+
lib/practice_terraforming.rb:
|
118
|
+
|
119
|
+
require "practice_terraforming/resource/iam_policy_attachment"
|
120
|
+
|
121
|
+
lib/practice_terraforming/cli.rb:
|
122
|
+
|
123
|
+
module PracticeTerraforming
|
124
|
+
class CLI < Thor
|
125
|
+
|
126
|
+
# Subcommand name should be acronym.
|
127
|
+
desc "iam_policy_attachment", "Iam Policy Attachment"
|
128
|
+
def iam_policy_attachment
|
129
|
+
execute(PracticeTerraforming::Resource::IamPolicyAttachment, options)
|
130
|
+
end
|
131
|
+
|
132
|
+
spec/lib/practice_terraforming/cli_spec.rb:
|
133
|
+
|
134
|
+
module PracticeTerraforming
|
135
|
+
describe CLI do
|
136
|
+
context "resources" do
|
137
|
+
describe "iam_policy_attachment" do
|
138
|
+
let(:klass) { PracticeTerraforming::Resource::IamPolicyAttachment }
|
139
|
+
let(:command) { :iam_policy_attachment }
|
140
|
+
|
141
|
+
it_behaves_like "CLI examples"
|
142
|
+
end
|
143
|
+
```
|
144
|
+
|
145
|
+
1. As the message says, add those codes.
|
146
|
+
2. `lib/practice_terraforming/resource/iam_policy_attachment.rb`: Change Aws client and write logic in `tfstate` method
|
147
|
+
|
148
|
+
Use aws-sdk-<resource> to get the input data and write the logic to generate tf/tfstate file.
|
149
|
+
1. tf -> only need to update the template file, which appears in the next step
|
150
|
+
2. tfstate -> get resource list using private method, format them into resources and return them
|
151
|
+
3. As for private methods:
|
152
|
+
- module_name_of(<resource>) -> used for module name of terraform to be imported
|
153
|
+
- <api method name, e.g. entities_for_policy> -> get the resource info with aws-sdk
|
154
|
+
- other -> make a list of resources to be used in `tfstate` method
|
155
|
+
|
156
|
+
3. `lib/practice_terraforming/template/tf/iam_policy_attachment.erb`: Update the erb based on the corresponding terraform resource.
|
157
|
+
|
158
|
+
```
|
159
|
+
<% iam_policy_attachments.each do |policy_attachment| -%>
|
160
|
+
resource "aws_iam_policy_attachment" "<%= module_name_of(policy_attachment) %>" {
|
161
|
+
name = "<%= policy_attachment[:name] %>"
|
162
|
+
policy_arn = "<%= policy_attachment[:arn] %>"
|
163
|
+
groups = <%= policy_attachment[:entities].policy_groups.map(&:group_name).inspect %>
|
164
|
+
users = <%= policy_attachment[:entities].policy_users.map(&:user_name).inspect %>
|
165
|
+
roles = <%= policy_attachment[:entities].policy_roles.map(&:role_name).inspect %>
|
166
|
+
}
|
167
|
+
|
168
|
+
<% end -%>
|
169
|
+
```
|
170
|
+
|
171
|
+
4. `spec/lib/practice_terraforming/resource/iam_policy_attachment_spec.rb`: Change Aws client and write test for tf and tfstate
|
172
|
+
|
173
|
+
Test Perspective:
|
174
|
+
1. Create aws sdk result using stub.
|
175
|
+
2. Use the module to generate tf/tfstate.
|
176
|
+
3. Compare expected one and generated one.
|
177
|
+
```
|
178
|
+
irb(main):007:0> client.list_policies.policies[0]
|
179
|
+
=> #<struct Aws::IAM::Types::Policy policy_name="test-policy", policy_id="ABCDEFG", arn="arn:aws:iam::123456789:policy/test-policy", path="/", default_version_id="v1", attachment_count=1, permissions_boundary_usage_count=0, is_attachable=true, description=nil, create_date=2019-01-01 00:00:00 UTC, update_date=2019-01-02 00:00:00 UTC>
|
180
|
+
client.list_entities_for_policy(policy_arn: "arn:aws:iam::351540792571:policy/ai-suggest-batch-user-policy")
|
181
|
+
irb(main):008:0> client.list_entities_for_policy(policy_arn: "arn:aws:iam::123456789:policy/test-policy")
|
182
|
+
=> #<struct Aws::IAM::Types::ListEntitiesForPolicyResponse policy_groups=[#<struct Aws::IAM::Types::PolicyGroup group_name="test-group", group_id="ABCDEFG">], policy_users=[], policy_roles=[], is_truncated=false, marker=nil>
|
183
|
+
```
|
184
|
+
|
185
|
+
```
|
186
|
+
let(:policies) do
|
187
|
+
[
|
188
|
+
{
|
189
|
+
policy_name: "test-policy",
|
190
|
+
policy_id: "ABCDEFG",
|
191
|
+
arn: "arn:aws:iam::123456789:policy/test-policy",
|
192
|
+
path: "/",
|
193
|
+
default_version_id: "v1",
|
194
|
+
attachment_count: 1,
|
195
|
+
is_attachable: true,
|
196
|
+
create_date: Time.parse("2019-01-01 00:00:00 UTC"),
|
197
|
+
update_date: Time.parse("2019-01-02 00:00:00 UTC"),
|
198
|
+
description: nil,
|
199
|
+
}
|
200
|
+
]
|
201
|
+
end
|
202
|
+
|
203
|
+
let(:entities_for_policy) do
|
204
|
+
{
|
205
|
+
policy_groups: [
|
206
|
+
{ group_name: "test-group", group_id: "ABCDEFG" },
|
207
|
+
],
|
208
|
+
policy_users: [],
|
209
|
+
policy_roles: [],
|
210
|
+
}
|
211
|
+
end
|
212
|
+
|
213
|
+
before do
|
214
|
+
client.stub_responses(:list_policies, policies: policies)
|
215
|
+
client.stub_responses(:list_entities_for_policy, [entities_for_policy])
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
## Install on local
|
220
|
+
|
221
|
+
### Build
|
222
|
+
|
223
|
+
```
|
224
|
+
gem build practice_terraforming.gemspec
|
225
|
+
```
|
226
|
+
|
227
|
+
the above command will generate the `practice_terraforming-X.X.X.gem`
|
228
|
+
|
229
|
+
### Install
|
230
|
+
|
231
|
+
0.1.0 as an example
|
232
|
+
|
233
|
+
```
|
234
|
+
gem install practice_terraforming-0.1.0.gem
|
235
|
+
Successfully installed practice_terraforming-0.1.0
|
236
|
+
Parsing documentation for practice_terraforming-0.1.0
|
237
|
+
Installing ri documentation for practice_terraforming-0.1.0
|
238
|
+
Done installing documentation for practice_terraforming after 0 seconds
|
239
|
+
1 gem installed
|
240
|
+
```
|
241
|
+
|
242
|
+
### Check
|
243
|
+
|
244
|
+
```
|
245
|
+
practice_terraforming
|
246
|
+
Commands:
|
247
|
+
practice_terraforming help [COMMAND] # Describe available commands or one specific command
|
248
|
+
practice_terraforming iamr # Iam Role
|
249
|
+
practice_terraforming s3 # S3
|
250
|
+
|
251
|
+
Options:
|
252
|
+
[--merge=MERGE] # tfstate file to merge
|
253
|
+
[--overwrite], [--no-overwrite] # Overwrite existing tfstate
|
254
|
+
[--tfstate], [--no-tfstate] # Generate tfstate
|
255
|
+
[--profile=PROFILE] # AWS credentials profile
|
256
|
+
[--region=REGION] # AWS region
|
257
|
+
[--assume=ASSUME] # Role ARN to assume
|
258
|
+
[--use-bundled-cert], [--no-use-bundled-cert] # Use the bundled CA certificate from AWS SDK
|
259
|
+
```
|