kumogata-template 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +54 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +75 -0
  6. data/README.md +204 -0
  7. data/Rakefile +23 -0
  8. data/bin/kumogata-template +9 -0
  9. data/kumogata-template.gemspec +26 -0
  10. data/lib/kumogata/template.rb +17 -0
  11. data/lib/kumogata/template/autoscaling.rb +77 -0
  12. data/lib/kumogata/template/cloudwatch.rb +28 -0
  13. data/lib/kumogata/template/codedeploy.rb +85 -0
  14. data/lib/kumogata/template/const.rb +175 -0
  15. data/lib/kumogata/template/datapipeline.rb +90 -0
  16. data/lib/kumogata/template/dynamodb.rb +111 -0
  17. data/lib/kumogata/template/ec2.rb +165 -0
  18. data/lib/kumogata/template/ecs.rb +119 -0
  19. data/lib/kumogata/template/elasticbeanstalk.rb +33 -0
  20. data/lib/kumogata/template/elb.rb +132 -0
  21. data/lib/kumogata/template/emr.rb +166 -0
  22. data/lib/kumogata/template/events.rb +25 -0
  23. data/lib/kumogata/template/ext/argument_parser.rb +8 -0
  24. data/lib/kumogata/template/ext/kumogata.rb +103 -0
  25. data/lib/kumogata/template/helper.rb +211 -0
  26. data/lib/kumogata/template/iam.rb +96 -0
  27. data/lib/kumogata/template/lambda.rb +34 -0
  28. data/lib/kumogata/template/s3.rb +223 -0
  29. data/lib/kumogata/template/sns.rb +25 -0
  30. data/lib/kumogata/template/version.rb +1 -0
  31. data/template/_template.rb +25 -0
  32. data/template/autoscaling-group.rb +47 -0
  33. data/template/autoscaling-launch-configuration.rb +54 -0
  34. data/template/autoscaling-lifecycle-hook.rb +27 -0
  35. data/template/autoscaling-scaling-policy.rb +33 -0
  36. data/template/autoscaling-scheduled-action.rb +37 -0
  37. data/template/cloudtrail.rb +39 -0
  38. data/template/cloudwatch-alarm.rb +45 -0
  39. data/template/codedeploy-application.rb +15 -0
  40. data/template/codedeploy-deployment-config.rb +23 -0
  41. data/template/codedeploy-deployment-group.rb +35 -0
  42. data/template/datapipeline-pipeline.rb +28 -0
  43. data/template/dynamodb-table.rb +33 -0
  44. data/template/ec2-eip-association.rb +23 -0
  45. data/template/ec2-eip.rb +17 -0
  46. data/template/ec2-instance.rb +65 -0
  47. data/template/ec2-internet-gateway.rb +15 -0
  48. data/template/ec2-nat-gateway.rb +17 -0
  49. data/template/ec2-network-acl-entry.rb +30 -0
  50. data/template/ec2-network-acl.rb +17 -0
  51. data/template/ec2-route-table.rb +17 -0
  52. data/template/ec2-route.rb +27 -0
  53. data/template/ec2-security-group.rb +24 -0
  54. data/template/ec2-subnet-network-acl-association.rb +17 -0
  55. data/template/ec2-subnet-route-table-association.rb +17 -0
  56. data/template/ec2-subnet.rb +23 -0
  57. data/template/ec2-volume-attachment.rb +19 -0
  58. data/template/ec2-volume.rb +31 -0
  59. data/template/ec2-vpc-endpoint.rb +23 -0
  60. data/template/ec2-vpc-gateway-attachment.rb +19 -0
  61. data/template/ec2-vpc.rb +24 -0
  62. data/template/ecs-cluster.rb +11 -0
  63. data/template/ecs-service.rb +24 -0
  64. data/template/ecs-task-definition.rb +18 -0
  65. data/template/elasticache-cache-cluster.rb +63 -0
  66. data/template/elasticache-parameter-group.rb +20 -0
  67. data/template/elasticache-replication-group.rb +58 -0
  68. data/template/elasticache-subnet-group.rb +17 -0
  69. data/template/elasticbeanstalk-application-version.rb +24 -0
  70. data/template/elasticbeanstalk-application.rb +17 -0
  71. data/template/elasticbeanstalk-configuration-template.rb +31 -0
  72. data/template/elasticbeanstalk-environment.rb +44 -0
  73. data/template/elb-loadbalancer.rb +46 -0
  74. data/template/emr-cluster.rb +39 -0
  75. data/template/emr-instance-group-config.rb +33 -0
  76. data/template/emr-step.rb +22 -0
  77. data/template/events-rule.rb +28 -0
  78. data/template/iam-access-key.rb +19 -0
  79. data/template/iam-group.rb +20 -0
  80. data/template/iam-instance-profile.rb +17 -0
  81. data/template/iam-managed-policy.rb +28 -0
  82. data/template/iam-policy.rb +26 -0
  83. data/template/iam-role.rb +21 -0
  84. data/template/iam-user-to-group-addition.rb +17 -0
  85. data/template/iam-user.rb +24 -0
  86. data/template/lambda-alias.rb +21 -0
  87. data/template/lambda-event-source-mapping.rb +24 -0
  88. data/template/lambda-function.rb +34 -0
  89. data/template/lambda-permission.rb +26 -0
  90. data/template/lambda-version.rb +19 -0
  91. data/template/mappings-ec2.rb +85 -0
  92. data/template/output-access-key.rb +6 -0
  93. data/template/output-arn.rb +6 -0
  94. data/template/output-autoscaling.rb +6 -0
  95. data/template/output-az.rb +6 -0
  96. data/template/output-dynamodb.rb +6 -0
  97. data/template/output-ec2.rb +8 -0
  98. data/template/output-elasticache.rb +30 -0
  99. data/template/output-elb.rb +12 -0
  100. data/template/output-emr.rb +6 -0
  101. data/template/output-rds.rb +10 -0
  102. data/template/output-redshift.rb +10 -0
  103. data/template/output-s3.rb +8 -0
  104. data/template/output-security-group.rb +5 -0
  105. data/template/output-sqs.rb +5 -0
  106. data/template/output-topic.rb +6 -0
  107. data/template/output-vpc.rb +10 -0
  108. data/template/output.rb +29 -0
  109. data/template/parameter-ec2.rb +22 -0
  110. data/template/parameter-elasticache.rb +9 -0
  111. data/template/parameter-rds.rb +9 -0
  112. data/template/parameter-redshift.rb +9 -0
  113. data/template/parameter.rb +20 -0
  114. data/template/rds-db-cluster-parameter-group.rb +22 -0
  115. data/template/rds-db-cluster.rb +47 -0
  116. data/template/rds-db-instance.rb +82 -0
  117. data/template/rds-db-parameter-group.rb +21 -0
  118. data/template/rds-db-subnet-group.rb +19 -0
  119. data/template/rds-event-subscription.rb +42 -0
  120. data/template/rds-option-group.rb +23 -0
  121. data/template/redshift-cluster-parameter-group.rb +19 -0
  122. data/template/redshift-cluster-subnet-group.rb +17 -0
  123. data/template/redshift-cluster.rb +60 -0
  124. data/template/s3-bucket-policy.rb +20 -0
  125. data/template/s3-bucket.rb +42 -0
  126. data/template/sns-topic.rb +20 -0
  127. data/template/sqs-queue.rb +27 -0
  128. data/test/_template.rb +26 -0
  129. data/test/abstract_unit.rb +101 -0
  130. data/test/autoscaling_test.rb +109 -0
  131. data/test/codedeploy_test.rb +117 -0
  132. data/test/datapipeline_test.rb +142 -0
  133. data/test/dynamodb_test.rb +184 -0
  134. data/test/ec2_test.rb +286 -0
  135. data/test/ecs_test.rb +135 -0
  136. data/test/elasticbeanstalk_test.rb +56 -0
  137. data/test/elb_test.rb +147 -0
  138. data/test/emr_test.rb +193 -0
  139. data/test/events_test.rb +22 -0
  140. data/test/helper_test.rb +547 -0
  141. data/test/iam_test.rb +93 -0
  142. data/test/lambda_test.rb +41 -0
  143. data/test/s3_test.rb +195 -0
  144. data/test/sns_test.rb +27 -0
  145. data/test/template/autoscaling-group_test.rb +177 -0
  146. data/test/template/autoscaling-launch-configuration_test.rb +117 -0
  147. data/test/template/autoscaling-lifecycle-hook_test.rb +33 -0
  148. data/test/template/autoscaling-scaling-policy_test.rb +26 -0
  149. data/test/template/autoscaling-scheduled-action_test.rb +26 -0
  150. data/test/template/cloudtrail_test.rb +55 -0
  151. data/test/template/cloudwatch-alarm_test.rb +38 -0
  152. data/test/template/codedeploy-application_test.rb +21 -0
  153. data/test/template/codedeploy-deployment-config_test.rb +26 -0
  154. data/test/template/codedeploy-deployment-group_test.rb +48 -0
  155. data/test/template/datapipeline-pipeline_test.rb +358 -0
  156. data/test/template/dynamodb-table_test.rb +94 -0
  157. data/test/template/ec2-eip-association_test.rb +22 -0
  158. data/test/template/ec2-eip_test.rb +21 -0
  159. data/test/template/ec2-instance_test.rb +95 -0
  160. data/test/template/ec2-internet-gateway_test.rb +48 -0
  161. data/test/template/ec2-nat-gateway_test.rb +22 -0
  162. data/test/template/ec2-network-acl-entry_test.rb +61 -0
  163. data/test/template/ec2-network-acl_test.rb +51 -0
  164. data/test/template/ec2-route-table_test.rb +49 -0
  165. data/test/template/ec2-route_test.rb +21 -0
  166. data/test/template/ec2-security-group_test.rb +50 -0
  167. data/test/template/ec2-subnet-netwokr-acl-association_test.rb +26 -0
  168. data/test/template/ec2-subnet-route-table-association_test.rb +22 -0
  169. data/test/template/ec2-subnet_test.rb +51 -0
  170. data/test/template/ec2-volume-attachment_test.rb +24 -0
  171. data/test/template/ec2-volume_test.rb +52 -0
  172. data/test/template/ec2-vpc-gateway-attachment_test.rb +23 -0
  173. data/test/template/ec2-vpc_test.rb +102 -0
  174. data/test/template/ecs-cluster_test.rb +18 -0
  175. data/test/template/ecs-service_test.rb +29 -0
  176. data/test/template/ecs-task-definition_test.rb +143 -0
  177. data/test/template/elasticache-cache-cluster_test.rb +77 -0
  178. data/test/template/elasticache-parameter-group_test.rb +25 -0
  179. data/test/template/elasticache-replication-group_test.rb +45 -0
  180. data/test/template/elasticache-subnet-group_test.rb +26 -0
  181. data/test/template/elasticbeanstalk-application-version_test.rb +27 -0
  182. data/test/template/elasticbeanstalk-application_test.rb +21 -0
  183. data/test/template/elasticbeanstalk-configuration-template_test.rb +23 -0
  184. data/test/template/elasticbeanstalk-environment_test.rb +56 -0
  185. data/test/template/elasticbeanstalk-template_test.rb +33 -0
  186. data/test/template/elb-loadbalancer_test.rb +94 -0
  187. data/test/template/emr-cluster_test.rb +147 -0
  188. data/test/template/emr-instance-group-config_test.rb +65 -0
  189. data/test/template/emr-step_test.rb +82 -0
  190. data/test/template/events-rule_test.rb +34 -0
  191. data/test/template/iam-access-key_test.rb +22 -0
  192. data/test/template/iam-group_test.rb +21 -0
  193. data/test/template/iam-instance-profile_test.rb +24 -0
  194. data/test/template/iam-managed-policy_test.rb +35 -0
  195. data/test/template/iam-policy_test.rb +52 -0
  196. data/test/template/iam-role_test.rb +37 -0
  197. data/test/template/iam-user-to-group-addition_test.rb +24 -0
  198. data/test/template/iam-user_test.rb +21 -0
  199. data/test/template/lambda-alias_test.rb +46 -0
  200. data/test/template/lambda-event-source-mapping_test.rb +36 -0
  201. data/test/template/lambda-function_test.rb +34 -0
  202. data/test/template/lambda-permission_test.rb +32 -0
  203. data/test/template/lambda-version_test.rb +27 -0
  204. data/test/template/mappings-ec2_test.rb +92 -0
  205. data/test/template/output-access-key_test.rb +30 -0
  206. data/test/template/output-arn_test.rb +30 -0
  207. data/test/template/output-autoscaling_test.rb +27 -0
  208. data/test/template/output-az_test.rb +31 -0
  209. data/test/template/output-dynamodb_test.rb +21 -0
  210. data/test/template/output-ec2_test.rb +82 -0
  211. data/test/template/output-elasticache_test.rb +107 -0
  212. data/test/template/output-elb_test.rb +48 -0
  213. data/test/template/output-emr_test.rb +30 -0
  214. data/test/template/output-rds_test.rb +39 -0
  215. data/test/template/output-redshift_test.rb +39 -0
  216. data/test/template/output-s3_test.rb +52 -0
  217. data/test/template/output-security-group_test.rb +21 -0
  218. data/test/template/output-sqs_test.rb +21 -0
  219. data/test/template/output-topic_test.rb +30 -0
  220. data/test/template/output-vpc_test.rb +48 -0
  221. data/test/template/output_test.rb +87 -0
  222. data/test/template/parameter-ec2_test.rb +44 -0
  223. data/test/template/parameter-elasticache_test.rb +30 -0
  224. data/test/template/parameter-rds_test.rb +30 -0
  225. data/test/template/parameter-redshift_test.rb +30 -0
  226. data/test/template/parameter_test.rb +40 -0
  227. data/test/template/rds-db-cluster-parameter-group_test.rb +53 -0
  228. data/test/template/rds-db-cluster_test.rb +84 -0
  229. data/test/template/rds-db-instance_test.rb +93 -0
  230. data/test/template/rds-db-parameter-group_test.rb +53 -0
  231. data/test/template/rds-db-subnet-group.rb +54 -0
  232. data/test/template/rds-event-subscription_test.rb +48 -0
  233. data/test/template/rds-option-group_test.rb +56 -0
  234. data/test/template/redshift-cluster-parameter-group_test.rb +28 -0
  235. data/test/template/redshift-cluster-subnet-group_test.rb +26 -0
  236. data/test/template/redshift-cluster_test.rb +47 -0
  237. data/test/template/s3-bucket-policy_test.rb +40 -0
  238. data/test/template/s3-bucket_test.rb +127 -0
  239. data/test/template/sns-topic_test.rb +42 -0
  240. data/test/template/sqs-queue_test.rb +25 -0
  241. metadata +468 -0
@@ -0,0 +1,19 @@
1
+ #
2
+ # Redshift ClusterParameterGroup resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-redshift-clusterparametergroup.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+
7
+ name = _resource_name(args[:name], "redshift cluster parameter group")
8
+ description = args[:description] || "#{args[:name]} redshift cluster parameter group description"
9
+ family = args[:family] || "redshift-1.0"
10
+ parameters = args[:parameters] || []
11
+
12
+ _(name) do
13
+ Type "AWS::Redshift::ClusterParameterGroup"
14
+ Properties do
15
+ Description description
16
+ ParameterGroupFamily family
17
+ Parameters parameters
18
+ end
19
+ end
@@ -0,0 +1,17 @@
1
+ #
2
+ # Redshift ClusterSubnetGroup resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-redshift-clustersubnetgroup.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+
7
+ name = _resource_name(args[:name], "redshift cluster subnet group")
8
+ description = args[:description] || "#{args[:name]} redshift cluster subnet group description"
9
+ subnets = _ref_array("subnets", args, "subnet")
10
+
11
+ _(name) do
12
+ Type "AWS::Redshift::ClusterSubnetGroup"
13
+ Properties do
14
+ Description description
15
+ SubnetIds subnets
16
+ end
17
+ end
@@ -0,0 +1,60 @@
1
+ #
2
+ # Redshift cluster resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-redshift-cluster.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+
7
+ name = _resource_name(args[:name], "redshift cluster")
8
+ allow = _bool("allow", args, true)
9
+ snapshot_retention = args[:snapshot_retention] || DEFAULT_SNAPSHOT_NUM
10
+ az = _availability_zone(args, false)
11
+ parameter = _ref_string("parameter", args, "redshift cluster parameter group")
12
+ subnet = _ref_string("subnet", args, "redshift cluster subnet group")
13
+ type = _valid_values(args[:type], [ "single-node", "multi-node" ], "single-node")
14
+ version = args[:version] || ""
15
+ db_name = _ref_string("db_name", args, "db name")
16
+ elastic = args[:elastic] || ""
17
+ encrypted = _bool("encrypted", args, false)
18
+ user_name = _ref_string("user_name", args, "cluster master user name")
19
+ user_password = _ref_string("user_password", args, "cluster master user password")
20
+ node = _valid_values(args[:node], REDSHIFT_NODE_TYPES, REDSHIFT_DEFAULT_NODE_TYPE)
21
+ num = args[:num] || 1
22
+ owner = args[:owner] || ""
23
+ port = _ref_string("port", args, "cluster port")
24
+ port = PORT[:redshift] if port.empty?
25
+ maintenance = _maintenance_window("redshift", args[:maintenance] || DEFAULT_MAINTENANCE_TIME[:redshift])
26
+ publicly = _bool("publicly", args, false)
27
+ snapshot_cluster = args[:snapshot_cluster] || ""
28
+ snapshot_id = args[:snapshot_id] || ""
29
+ security_groups = _ref_array("security_groups", args, "security group")
30
+
31
+ _(name) do
32
+ Type "AWS::Redshift::Cluster"
33
+ Properties do
34
+ AllowVersionUpgrade allow
35
+ AutomatedSnapshotRetentionPeriod snapshot_retention
36
+ AvailabilityZone az unless az.empty?
37
+ ClusterParameterGroupName parameter unless parameter.empty?
38
+ #ClusterSecurityGroups
39
+ ClusterSubnetGroupName subnet unless subnet.empty?
40
+ ClusterType type
41
+ ClusterVersion version unless version.empty?
42
+ DBName db_name
43
+ ElasticIp elastic unless elastic.empty?
44
+ Encrypted encrypted if encrypted == true
45
+ #HsmClientCertificateIdentifie
46
+ #HsmConfigurationIdentifier
47
+ #KmsKeyId
48
+ MasterUsername user_name
49
+ MasterUserPassword user_password
50
+ NodeType node
51
+ NumberOfNodes num if type == "multi-node"
52
+ OwnerAccount owner unless owner.empty?
53
+ Port port
54
+ PreferredMaintenanceWindow maintenance
55
+ PubliclyAccessible publicly
56
+ SnapshotClusterIdentifier snapshot_cluster unless snapshot_cluster.empty?
57
+ SnapshotIdentifier snapshot_id unless snapshot_id.empty?
58
+ VpcSecurityGroupIds security_groups unless security_groups.empty?
59
+ end
60
+ end
@@ -0,0 +1,20 @@
1
+ #
2
+ # S3 bucket policy resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-policy.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/iam'
7
+
8
+ name = _resource_name(args[:name], "bucket policy")
9
+ bucket = _ref_name("bucket", args)
10
+
11
+ _(name) do
12
+ Type "AWS::S3::BucketPolicy"
13
+ Properties do
14
+ BucketName bucket
15
+ PolicyDocument do
16
+ Version "2012-10-17"
17
+ Statement _iam_policy_document("policy_document", args)
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,42 @@
1
+ #
2
+ # S3 bucket resource
3
+ # https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/s3'
7
+
8
+ name = _resource_name(args[:name], "bucket")
9
+ access = _valid_values(args[:access],
10
+ %w( AuthenticatedRead AwsExecRead BucketOwnerRead
11
+ BucketOwnerFullControl LogDeliveryWrite
12
+ Private PublicRead PublicReadWrite ),
13
+ "Private")
14
+ access = "PublicRead" if args.key? :website
15
+ bucket = _ref_name("bucket", args)
16
+ cors = _s3_cors(args)
17
+ lifecycle = _s3_lifecycle(args)
18
+ logging = _s3_logging(args)
19
+ notification = _s3_notification(args)
20
+ replication = _s3_replication(args)
21
+ tags = _tags(args)
22
+ versioning = _s3_versioning(args)
23
+ website = _s3_website(args)
24
+ deletion_policy = _valid_values(args[:deletion_policy],
25
+ %w( Delete Retain Snapshot ), "Retain")
26
+
27
+ _(name) do
28
+ Type "AWS::S3::Bucket"
29
+ Properties do
30
+ AccessControl access
31
+ BucketName bucket
32
+ CorsConfiguration cors unless cors.empty?
33
+ LifecycleConfiguration lifecycle unless lifecycle.empty?
34
+ LoggingConfiguration logging unless logging.empty?
35
+ NotificationConfiguration notification unless notification.empty?
36
+ ReplicationConfiguration replication unless replication.empty?
37
+ Tags tags
38
+ VersioningConfiguration versioning unless versioning.empty?
39
+ WebsiteConfiguration website unless website.empty?
40
+ end
41
+ DeletionPolicy deletion_policy
42
+ end
@@ -0,0 +1,20 @@
1
+ #
2
+ # SNS Topic resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sns-topic.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+ require 'kumogata/template/sns'
7
+
8
+ name = _resource_name(args[:name], "topic")
9
+ display = args[:display] || ""
10
+ subscription = _sns_subscription(args)
11
+ topic = _ref_name("topic", args)
12
+
13
+ _(name) do
14
+ Type "AWS::SNS::Topic"
15
+ Properties do
16
+ DisplayName display
17
+ Subscription subscription
18
+ TopicName topic
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ #
2
+ # SQS Queue resource
3
+ # http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html
4
+ #
5
+ require 'kumogata/template/helper'
6
+
7
+ name = _resource_name(args[:name], "queue")
8
+ deplay = args[:deplay] || 0
9
+ max = args[:max] || 262144 # default 256KiB
10
+ retention = args[:retention] || 345600 # default 4 days
11
+ queue = _ref_name("queue", args)
12
+ receive = args[:receive] || 0
13
+ redrive = args[:redrive] || ""
14
+ visibility = args[:visibility] || 30 # default 30 seconds
15
+
16
+ _(name) do
17
+ Type "AWS::SQS::Queue"
18
+ Properties do
19
+ DelaySeconds deplay
20
+ MaximumMessageSize max
21
+ MessageRetentionPeriod retention
22
+ QueueName queue
23
+ ReceiveMessageWaitTimeSeconds receive if 1 <= receive.to_i and receive.to_i <= 20
24
+ RedrivePolicy redrive unless redrive.empty?
25
+ VisibilityTimeout visibility
26
+ end
27
+ end
data/test/_template.rb ADDED
@@ -0,0 +1,26 @@
1
+ require 'abstract_unit'
2
+
3
+ class Test < Minitest::Test
4
+ def test_normal
5
+ template = <<-EOS
6
+ _ "test", { }
7
+ EOS
8
+ act_template = run_client_as_json(template)
9
+ exp_template = <<-EOS
10
+ {
11
+ "Test": {
12
+ "Type": "AWS::::",
13
+ "Properties": {
14
+ "": "",
15
+ "": [
16
+ {
17
+ "": ""
18
+ }
19
+ ]
20
+ }
21
+ }
22
+ }
23
+ EOS
24
+ assert_equal exp_template.chomp, act_template
25
+ end
26
+ end
@@ -0,0 +1,101 @@
1
+ require 'minitest/autorun'
2
+ require 'kumogata'
3
+ require 'kumogata/argument_parser'
4
+ require 'json'
5
+ require 'tempfile'
6
+
7
+ # for only test
8
+ ENV['TZ'] = 'Asia/Tokyo'
9
+
10
+ class Kumogata::Client
11
+ def define_template_func(scope, path_or_url)
12
+ functions = ""
13
+ Dir.glob("template/*.rb").all? do |file|
14
+ functions << include_func(path_or_url, file)
15
+ functions << "\n\n"
16
+ end
17
+
18
+ scope.instance_eval(<<-EOS)
19
+ #{functions}
20
+
21
+ def _include(file, args = {})
22
+ path = file.dup
23
+
24
+ unless path =~ %r|\\A/| or path =~ %r|\\A\\w+://|
25
+ path = File.expand_path(File.join(File.dirname(#{path_or_url.inspect}), path))
26
+ end
27
+
28
+ open(path) {|f| instance_eval(f.read) }
29
+ end
30
+
31
+ def _path(path, value = nil, &block)
32
+ if block
33
+ value = Dslh::ScopeBlock.nest(binding, 'block')
34
+ end
35
+
36
+ @__hash__[path] = value
37
+ end
38
+
39
+ def _outputs_filter(&block)
40
+ @__hash__[:_outputs_filter] = block
41
+ end
42
+
43
+ def _post(options = {}, &block)
44
+ commands = Dslh::ScopeBlock.nest(binding, 'block')
45
+
46
+ @__hash__[:_post] = {
47
+ :options => options,
48
+ :commands => commands,
49
+ }
50
+ end
51
+ EOS
52
+ end
53
+
54
+
55
+ private ###########################################################
56
+
57
+ def include_func(path_or_url, file)
58
+ <<-EOS
59
+ def _#{get_funcname(file)}(name, args = {})
60
+ args[:name] = name unless args.key? :name
61
+ open("#{file}") {|f| instance_eval(f.read) }
62
+ end
63
+ EOS
64
+ end
65
+
66
+ def get_funcname(file)
67
+ File.basename(file, '.rb').gsub('-', '_')
68
+ end
69
+ end
70
+
71
+
72
+ def tempfile(content, template_ext = nil)
73
+ basename = "#{File.basename __FILE__}.#{$$}"
74
+ basename = [basename, template_ext] if template_ext
75
+
76
+ Tempfile.open(basename) do |f|
77
+ f << content
78
+ f.flush
79
+ f.rewind
80
+ yield(f)
81
+ end
82
+ end
83
+
84
+ def run_client(template)
85
+ $stdout = open('/dev/null', 'w') unless ENV['DEBUG']
86
+
87
+ kumogata_options = Kumogata::ArgumentParser::DEFAULT_OPTIONS
88
+ kumogata_options[:output_format] = 'json'
89
+ kumogata_options[:result_log] = '/dev/null'
90
+ kumogata_options[:command_result_log] = '/dev/null'
91
+ template_ext = '.rb'
92
+
93
+ template = tempfile(template, template_ext) do |f|
94
+ Kumogata::Client.new(kumogata_options).send(:evaluate_template, f, f.path)
95
+ end
96
+ end
97
+
98
+ def run_client_as_json(template)
99
+ eval_template = run_client(template)
100
+ JSON.pretty_generate(eval_template)
101
+ end
@@ -0,0 +1,109 @@
1
+ require 'abstract_unit'
2
+ require 'kumogata/template/autoscaling'
3
+
4
+ class AutoscalingTest < Minitest::Test
5
+ def test_autoscaling_metrics
6
+ template = <<-EOS
7
+ Test _autoscaling_metrics
8
+ EOS
9
+ act_template = run_client_as_json(template)
10
+ exp_template = <<-EOS
11
+ {
12
+ "Test": {
13
+ "Granularity": "1Minute",
14
+ "Metrics": [
15
+ "GroupMinSize",
16
+ "GroupMaxSize",
17
+ "GroupDesiredCapacity",
18
+ "GroupInServiceInstances",
19
+ "GroupPendingInstances",
20
+ "GroupStandbyInstances",
21
+ "GroupTerminatingInstances",
22
+ "GroupTotalInstances"
23
+ ]
24
+ }
25
+ }
26
+ EOS
27
+ assert_equal exp_template.chomp, act_template
28
+ end
29
+
30
+ def test_autoscaling_notification
31
+ template = <<-EOS
32
+ Test _autoscaling_notification(topic_arn: "test")
33
+ EOS
34
+ act_template = run_client_as_json(template)
35
+ exp_template = <<-EOS
36
+ {
37
+ "Test": {
38
+ "NotificationTypes": [
39
+ "autoscaling:EC2_INSTANCE_LAUNCH",
40
+ "autoscaling:EC2_INSTANCE_LAUNCH_ERROR",
41
+ "autoscaling:EC2_INSTANCE_TERMINATE",
42
+ "autoscaling:EC2_INSTANCE_TERMINATE_ERROR",
43
+ "autoscaling:TEST_NOTIFICATION"
44
+ ],
45
+ "TopicARN": "test"
46
+ }
47
+ }
48
+ EOS
49
+ assert_equal exp_template.chomp, act_template
50
+ end
51
+
52
+ def test_autoscaling_step
53
+ template = <<-EOS
54
+ Test _autoscaling_step(scaling: 10)
55
+ EOS
56
+ act_template = run_client_as_json(template)
57
+ exp_template = <<-EOS
58
+ {
59
+ "Test": {
60
+ "ScalingAdjustment": "10"
61
+ }
62
+ }
63
+ EOS
64
+ assert_equal exp_template.chomp, act_template
65
+ end
66
+
67
+ def test_autoscaling_tags
68
+ template = <<-EOS
69
+ Test _autoscaling_tags(name: "test")
70
+ EOS
71
+ act_template = run_client_as_json(template)
72
+ exp_template = <<-EOS
73
+ {
74
+ "Test": [
75
+ {
76
+ "Key": "Name",
77
+ "Value": {
78
+ "Fn::Join": [
79
+ "-",
80
+ [
81
+ {
82
+ "Ref": "Service"
83
+ },
84
+ "test"
85
+ ]
86
+ ]
87
+ },
88
+ "PropagateAtLaunch": "true"
89
+ },
90
+ {
91
+ "Key": "Service",
92
+ "Value": {
93
+ "Ref": "Service"
94
+ },
95
+ "PropagateAtLaunch": "true"
96
+ },
97
+ {
98
+ "Key": "Version",
99
+ "Value": {
100
+ "Ref": "Version"
101
+ },
102
+ "PropagateAtLaunch": "true"
103
+ }
104
+ ]
105
+ }
106
+ EOS
107
+ assert_equal exp_template.chomp, act_template
108
+ end
109
+ end