kumogata-template 0.0.15 → 0.0.16

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/.travis.yml +1 -1
  4. data/Gemfile.lock +44 -21
  5. data/README.md +10 -1
  6. data/bin/kumogata-template +2 -2
  7. data/kumogata-template.gemspec +5 -6
  8. data/lib/kumogata/template.rb +3 -0
  9. data/lib/kumogata/template/codebuild.rb +86 -0
  10. data/lib/kumogata/template/codecommit.rb +20 -0
  11. data/lib/kumogata/template/ec2.rb +1 -1
  12. data/lib/kumogata/template/elb.rb +4 -1
  13. data/lib/kumogata/template/ext/argument_parser.rb +8 -0
  14. data/lib/kumogata/template/ext/kumogata.rb +41 -40
  15. data/lib/kumogata/template/ext/option_parser.rb +6 -0
  16. data/lib/kumogata/template/helper.rb +17 -11
  17. data/lib/kumogata/template/iam.rb +3 -0
  18. data/lib/kumogata/template/lambda.rb +5 -0
  19. data/lib/kumogata/template/logs.rb +18 -0
  20. data/lib/kumogata/template/sns.rb +21 -12
  21. data/lib/kumogata/template/version.rb +1 -1
  22. data/template/codebuild-project.rb +32 -0
  23. data/template/codecommit-repository.rb +20 -0
  24. data/template/ecs-cluster.rb +5 -1
  25. data/template/ecs-task-definition.rb +7 -1
  26. data/template/elasticache-replication-group.rb +2 -0
  27. data/template/elasticache-subnet-group.rb +2 -0
  28. data/template/kms-alias.rb +17 -0
  29. data/template/kms-key.rb +25 -0
  30. data/template/lambda-function.rb +2 -0
  31. data/template/logs-destination.rb +26 -0
  32. data/template/logs-log-group.rb +18 -0
  33. data/template/logs-log-stream.rb +18 -0
  34. data/template/logs-metric-filter.rb +21 -0
  35. data/template/logs-subscription_filter.rb +24 -0
  36. data/template/rds-db-instance.rb +6 -0
  37. data/template/s3-bucket-policy.rb +3 -2
  38. data/template/s3-bucket.rb +1 -1
  39. data/template/sns-subscription.rb +20 -0
  40. data/template/sns-topic.rb +1 -1
  41. data/test/abstract_unit.rb +8 -26
  42. data/test/codebuild_test.rb +76 -0
  43. data/test/elb_test.rb +44 -0
  44. data/test/logs_test.rb +23 -0
  45. data/test/s3_test.rb +0 -1
  46. data/test/sns_test.rb +1 -1
  47. data/test/template/codebuild-project_test.rb +72 -0
  48. data/test/template/codecommit-repository_test.rb +21 -0
  49. data/test/template/codedeploy-application_test.rb +1 -1
  50. data/test/template/ec2-network-acl-entry_test.rb +2 -2
  51. data/test/template/ecs-cluster_test.rb +5 -2
  52. data/test/template/elasticache-replication-group_test.rb +29 -1
  53. data/test/template/emr-cluster_test.rb +1 -1
  54. data/test/template/iam-managed-policy_test.rb +1 -1
  55. data/test/template/iam-policy_test.rb +1 -1
  56. data/test/template/kms-alias_test.rb +22 -0
  57. data/test/template/kms-key_test.rb +36 -0
  58. data/test/template/logs-destination_test.rb +37 -0
  59. data/test/template/logs-log-group_test.rb +34 -0
  60. data/test/template/logs-log-stream_test.rb +46 -0
  61. data/test/template/logs-metric-filter_test.rb +41 -0
  62. data/test/template/logs-subscription-filter_test.rb +36 -0
  63. data/test/template/rds-event-subscription_test.rb +1 -1
  64. data/test/template/sns-subscription_test.rb +28 -0
  65. metadata +52 -28
@@ -1,4 +1,10 @@
1
1
  class Kumogata2::CLI::OptionParser
2
+ Kumogata2::CLI::OptionParser::DEFAULT_OPTIONS.merge!(
3
+ {
4
+ :output_format => 'yaml',
5
+ }
6
+ )
7
+
2
8
  Kumogata2::CLI::OptionParser::COMMANDS.merge!(
3
9
  init: {
4
10
  :description => 'Generate basic template',
@@ -44,13 +44,13 @@ def _real_name(name)
44
44
  name.to_s.gsub(' ', '-')
45
45
  end
46
46
 
47
- def _ref_string(name, args, ref_name = "")
48
- return args[name.to_sym].to_s || "" unless args.key? "ref_#{name}".to_sym
47
+ def _ref_string(name, args, ref_name = '')
48
+ return args[name.to_sym].to_s || '' unless args.key? "ref_#{name}".to_sym
49
49
 
50
50
  _{ Ref _resource_name(args["ref_#{name}".to_sym].to_s, ref_name) }
51
51
  end
52
52
 
53
- def _ref_array(name, args, ref_name = "")
53
+ def _ref_array(name, args, ref_name = '')
54
54
  return _array(args[name.to_sym]) || [] unless args.key? "ref_#{name}".to_sym
55
55
 
56
56
  array = []
@@ -64,7 +64,7 @@ def _ref_array(name, args, ref_name = "")
64
64
  array
65
65
  end
66
66
 
67
- def _ref_attr_string(name, attr, args, ref_name = "")
67
+ def _ref_attr_string(name, attr, args, ref_name = '')
68
68
  if args.key? "ref_#{name}".to_sym
69
69
  _{
70
70
  Fn__GetAtt [ _resource_name(args["ref_#{name}".to_sym], ref_name), attr ]
@@ -72,11 +72,11 @@ def _ref_attr_string(name, attr, args, ref_name = "")
72
72
  elsif args.key? name.to_sym
73
73
  args[name.to_sym]
74
74
  else
75
- ""
75
+ ''
76
76
  end
77
77
  end
78
78
 
79
- def _ref_name(name, args, ref_name = "")
79
+ def _ref_name(name, args, ref_name = '')
80
80
  return args["raw_#{name}".to_sym] if args.key? "raw_#{name}".to_sym
81
81
  name = _ref_string(name, args, ref_name)
82
82
  if name.empty?
@@ -88,11 +88,17 @@ def _ref_name(name, args, ref_name = "")
88
88
  end
89
89
  end
90
90
 
91
- def _ref_resource_name(args, ref_name = "")
91
+ def _ref_name_default(name, args, ref_name = '')
92
+ return args["raw_#{name}".to_sym] if args.key? "raw_#{name}".to_sym
93
+ name = _ref_string(name, args, ref_name)
94
+ name.empty? ? args[:name] : name.gsub(' ', '-')
95
+ end
96
+
97
+ def _ref_resource_name(args, ref_name = '')
92
98
  _{ Ref _resource_name(args[:name], ref_name) }
93
99
  end
94
100
 
95
- def _attr_string(name, attr, ref_name = "")
101
+ def _attr_string(name, attr, ref_name = '')
96
102
  _{ Fn__GetAtt [ _resource_name(name, ref_name), attr ] }
97
103
  end
98
104
 
@@ -105,8 +111,8 @@ def _select(index, list)
105
111
  end
106
112
 
107
113
  def _tag(args)
108
- key = args[:key].to_s || ""
109
- value = args[:value] || ""
114
+ key = args[:key].to_s || ''
115
+ value = args[:value] || ''
110
116
  if key =~ /^ref_.*/
111
117
  key.gsub!(/^ref_/, '')
112
118
  value = _{ Ref _resource_name(value) }
@@ -153,7 +159,7 @@ def _availability_zone(args, use_subnet = true)
153
159
  elsif zone.key? :az
154
160
  zone[:az]
155
161
  else
156
- ""
162
+ ''
157
163
  end
158
164
  end
159
165
 
@@ -135,6 +135,9 @@ def _iam_arn(service, resource)
135
135
 
136
136
  when "logs"
137
137
  "#{arn_prefix}:*:*:*"
138
+
139
+ when "kinesis"
140
+ "#{arn_prefix}:#{resource[:region]}:#{resource[:account_id]}:#{resource[:type]}/#{resource[:name]}"
138
141
  end
139
142
  end
140
143
 
@@ -27,6 +27,11 @@ def _lambda_function_code(args)
27
27
  }
28
28
  end
29
29
 
30
+ def _lambda_function_environment(args)
31
+ environment = args[:environment] || {}
32
+ environment.empty? ? '' : _{ Variables variables }
33
+ end
34
+
30
35
  def _lambda_vpc_config(args)
31
36
  return "" unless args.key? :vpc
32
37
 
@@ -0,0 +1,18 @@
1
+ #
2
+ # Helper - Logs
3
+ #
4
+ require 'kumogata/template/helper'
5
+
6
+ def _logs_metric_filter_transformations(args)
7
+ trans = args[:transformations] || []
8
+
9
+ array = []
10
+ trans.each do |tran|
11
+ array << _{
12
+ MetricName tran[:name]
13
+ MetricNamespace tran[:ns]
14
+ MetricValue tran[:value]
15
+ }
16
+ end
17
+ array
18
+ end
@@ -3,19 +3,28 @@
3
3
  #
4
4
  require 'kumogata/template/helper'
5
5
 
6
- def _sns_subscription(args)
6
+ def _sns_to_protocol(value)
7
+ _valid_values(value,
8
+ %w( http https email email-json sms sqs application lambda ),
9
+ "email")
10
+ end
11
+
12
+ def _sns_to_endpoint(protocol, value)
13
+ case protocol
14
+ when "lambda", "sqs"
15
+ _attr_string(value, "Arn")
16
+ else
17
+ value
18
+ end
19
+ end
20
+
21
+ def _sns_subscription_list(args)
22
+ subscription= args[:subscription] || []
23
+
7
24
  array = []
8
- types = args[:subscription] || []
9
- types.each do |v|
10
- protocol = _valid_values(v[:protocol],
11
- [ "http", "https", "email", "email-json", "sms", "sqs", "application", "lambda" ],
12
- "email")
13
- case protocol
14
- when "lambda", "sqs"
15
- endpoint = _attr_string(v[:endpoint], "Arn")
16
- else
17
- endpoint = v[:endpoint]
18
- end
25
+ subscription.each do |v|
26
+ protocol = _sns_to_protocol(v[:protocol])
27
+ endpoint = _sns_to_endpoint(protocol, v[:endpoint])
19
28
  array << _{
20
29
  Endpoint endpoint
21
30
  Protocol protocol
@@ -1 +1 @@
1
- KUMOGATA_TEMPLATE_VERSION = '0.0.15'
1
+ KUMOGATA_TEMPLATE_VERSION = '0.0.16'
@@ -0,0 +1,32 @@
1
+ #
2
+ # CodeBuild Project resource type
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codebuild-project.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/codebuild'
7
+
8
+ name = _resource_name(args[:name], "codebuild project")
9
+ artifacts = _codebuild_artifacts(args[:artifacts])
10
+ description = args[:description] || ""
11
+ encryption = args[:encryption] || ""
12
+ environment =_codebuild_environement(args[:environment])
13
+ project = _ref_name_default("project", args)
14
+ service = _ref_attr_string("service", "Arn", args, "role")
15
+ source = _codebuild_source(args[:source])
16
+ tags = _tags(args)
17
+ timeout = _valid_numbers(args[:timeout], min = 5, max = 480, 0)
18
+
19
+ _(name) do
20
+ Type "AWS::CodeBuild::Project"
21
+ Properties do
22
+ Artifacts artifacts
23
+ Description description unless description.empty?
24
+ EncryptionKey encryption unless encryption.empty?
25
+ Environment environment
26
+ Name project
27
+ ServiceRole service
28
+ Source source
29
+ Tags tags
30
+ TimeoutInMinutes timeout unless timeout == 0
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ #
2
+ # CodeCommit Repository resource type
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-codecommit-repository.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/codecommit'
7
+
8
+ name = _resource_name(args[:name], "codecommit repository")
9
+ description = args[:description] || ""
10
+ repository = _ref_name_default("repository", args)
11
+ triggers = _codecommit_triggers(args)
12
+
13
+ _(name) do
14
+ Type "AWS::CodeCommit::Repository"
15
+ Properties do
16
+ RepositoryDescription description unless description.empty?
17
+ RepositoryName repository
18
+ Triggers triggers unless triggers.empty?
19
+ end
20
+ end
@@ -1,11 +1,15 @@
1
1
  #
2
- # ECS Clusteer resource
2
+ # ECS Cluster resource
3
3
  # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-cluster.html
4
4
  #
5
5
  require 'kumogata/template/helper'
6
6
 
7
7
  name = _resource_name(args[:name], "ecs cluster")
8
+ cluster = _ref_name("cluster", args)
8
9
 
9
10
  _(name) do
10
11
  Type "AWS::ECS::Cluster"
12
+ Properties do
13
+ ClusterName cluster
14
+ end
11
15
  end
@@ -1,5 +1,5 @@
1
1
  #
2
- # ECS TaskDefinition resource
2
+ # ECS Task Definition resource
3
3
  # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html
4
4
  #
5
5
  require 'kumogata/template/helper'
@@ -7,12 +7,18 @@ require 'kumogata/template/ecs'
7
7
 
8
8
  name = _resource_name(args[:name], "ecs task definition")
9
9
  definitions = _ecs_container_definitions(args)
10
+ family = args[:family] || ""
11
+ network = _valid_values(args[:network], %w( bridge host none ), "")
12
+ role = _ref_attr_string("role", "Arn", args, "role")
10
13
  volumes = _ecs_volumes(args)
11
14
 
12
15
  _(name) do
13
16
  Type "AWS::ECS::TaskDefinition"
14
17
  Properties do
15
18
  ContainerDefinitions definitions
19
+ Family family unless family.empty?
20
+ NetworkMode network unless network.empty?
21
+ TaskRoleArn role unless role.empty?
16
22
  Volumes volumes
17
23
  end
18
24
  end
@@ -32,6 +32,7 @@ description = args[:description] || "#{args[:name]} cache replication group desc
32
32
  security_groups = _ref_array("security_groups", args, "security group")
33
33
  snapshot_retention = args[:snapshot_retention] || DEFAULT_SNAPSHOT_NUM
34
34
  snapshot_window = _window_time("elasticache", args[:snapshot_window_start] || DEFAULT_SNAPSHOT_TIME[:elasticache])
35
+ tags = _tags(args)
35
36
 
36
37
  _(name) do
37
38
  Type "AWS::ElastiCache::ReplicationGroup"
@@ -54,5 +55,6 @@ _(name) do
54
55
  #SnapshotArns
55
56
  SnapshotRetentionLimit snapshot_retention if 0 < snapshot_retention
56
57
  SnapshotWindow snapshot_window unless snapshot_retention < 0 and snapshot_window.empty?
58
+ Tags tags
57
59
  end
58
60
  end
@@ -5,12 +5,14 @@
5
5
  require 'kumogata/template/helper'
6
6
 
7
7
  name = _resource_name(args[:name], "cache subnet group")
8
+ subnet = args[:subnet] || ""
8
9
  description = args[:description] || "#{args[:name]} cache subnet group description"
9
10
  subnets = _ref_array("subnets", args, "subnet")
10
11
 
11
12
  _(name) do
12
13
  Type "AWS::ElastiCache::SubnetGroup"
13
14
  Properties do
15
+ CacheSubnetGroupName subnet unless subnet.empty?
14
16
  Description description
15
17
  SubnetIds subnets
16
18
  end
@@ -0,0 +1,17 @@
1
+ #
2
+ # KSM Alias resource type
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-kms-alias.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+
7
+ name = _resource_name(args[:name], "kms alias")
8
+ alias_name = _ref_name("alias", args)
9
+ target = _ref_name("target", args)
10
+
11
+ _(name) do
12
+ Type "AWS::KMS::Alias"
13
+ Properties do
14
+ AliasName alias_name
15
+ TargetKeyId target
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ #
2
+ # KSM Key resource type
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-kms-key.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/iam'
7
+
8
+ name = _resource_name(args[:name], "kms key")
9
+ description = args[:description] || ""
10
+ enabled = _bool("enabled", args, true)
11
+ rotation = _bool("rotation", args, false)
12
+ policy = _iam_policy_document("policy", args)
13
+
14
+ _(name) do
15
+ Type "AWS::KMS::Key"
16
+ Properties do
17
+ Description description unless description.empty?
18
+ Enabled enabled
19
+ EnableKeyRotation rotation
20
+ KeyPolicy do
21
+ Version "2012-10-17"
22
+ Statement policy
23
+ end
24
+ end
25
+ end
@@ -8,6 +8,7 @@ require 'kumogata/template/lambda'
8
8
  name = _resource_name(args[:name], "lambda function")
9
9
  code = _lambda_function_code(args)
10
10
  description = args[:description] || ""
11
+ environment = _lambda_function_environment(args)
11
12
  function_name = args[:function_name] || ""
12
13
  runtime = _valid_values(args[:runtime],
13
14
  %w( nodejs nodejs4.3 java8 python2.7 ), "python2.7")
@@ -35,6 +36,7 @@ _(name) do
35
36
  Properties do
36
37
  Code code
37
38
  Description description unless description.empty?
39
+ Environment environment unless environment.empty?
38
40
  FunctionName function_name unless function_name.empty?
39
41
  Handler handler
40
42
  MemorySize memory_size
@@ -0,0 +1,26 @@
1
+ #
2
+ # Logs destination resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-destination.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/iam'
7
+ require 'kumogata/template/logs'
8
+
9
+ name = _resource_name(args[:name], "logs destination")
10
+ dest = _ref_name("dest", args)
11
+ policy = _iam_policy_document("policy", args)
12
+ role = _ref_attr_string("role", "Arn", args, "role")
13
+ target = _iam_arn("kinesis", args[:target].merge(type: "stream"))
14
+
15
+ _(name) do
16
+ Type "AWS::Logs::Destination"
17
+ Properties do
18
+ DestinationName dest
19
+ DestinationPolicy do
20
+ Version "2012-10-17"
21
+ Statement policy
22
+ end
23
+ RoleArn role
24
+ TargetArn target
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ #
2
+ # Logs log group resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-loggroup.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/logs'
7
+
8
+ name = _resource_name(args[:name], "logs log group")
9
+ group = _ref_name("group", args)
10
+ rentention = args[:rentention] || 365
11
+
12
+ _(name) do
13
+ Type "AWS::Logs::LogGroup"
14
+ Properties do
15
+ LogGroupName group
16
+ RetentionInDays rentention
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ #
2
+ # Logs log stream resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-logstream.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/logs'
7
+
8
+ name = _resource_name(args[:name], "logs log stream")
9
+ group = _ref_name("group", args)
10
+ stream = _ref_name("stream", args)
11
+
12
+ _(name) do
13
+ Type "AWS::Logs::LogStream"
14
+ Properties do
15
+ LogGroupName group
16
+ LogStreamName stream
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ #
2
+ # Logs metric filter resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-logs-metricfilter.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/logs'
7
+
8
+ name = _resource_name(args[:name], "logs metric filter")
9
+ # pattern ex. [timestamps, ip_addresses, error_codes = 1234*, size, ...]
10
+ pattern = args[:pattern]
11
+ group = _ref_name("group", args)
12
+ trans = _logs_metric_filter_transformations(args)
13
+
14
+ _(name) do
15
+ Type "AWS::Logs::MetricFilter"
16
+ Properties do
17
+ FilterPattern pattern
18
+ LogGroupName group
19
+ MetricTransformations trans
20
+ end
21
+ end