sample_data.rake 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # frozen_string_literal: true
  2. require 'yaml'
  3. require 'pp'
  4. namespace :sample_data do
  5. sample_data = YAML.load_file(
  6. File.join(File.dirname(File.expand_path(__FILE__)), 'sample_data.yaml')
  7. )
  8. Neo4j::Session.open(:server_db, 'http://localhost:7474')
  9. task :accounts do
  10. puts 'loading accounts'
  11. sample_data['accounts'].each do |account|
  12. next unless AwsAccount.where(name: account[:name]).empty?
  13. acct = AwsAccount.new(
  14. name: account[:name],
  15. account_id: rand(90_000..90_019),
  16. user_id: account[:name],
  17. key_md5: Digest:: MD5.hexdigest(account[:name]),
  18. secret_md5: Digest:: MD5.hexdigest(account[:name])
  19. )
  20. acct.save
  21. end
  22. end
  23. task :regions do
  24. puts 'loading regions'
  25. sample_data['regions'].each do |reg|
  26. next unless Region.where(region: reg.keys.first).empty?
  27. r = Region.new(region: reg.keys.first)
  28. r.save
  29. reg[reg.keys.first].each do |az|
  30. a = Az.new(az: az)
  31. a.save
  32. AzRegion.create(from_node: a, to_node: Region.where(region: reg.keys.first).first)
  33. end
  34. end
  35. end
  36. task :vpcs do
  37. puts 'loading vpcs'
  38. sample_data['vpcs'].each do |v|
  39. next unless Vpc.where(vpc_id: v['vpc_id']).empty?
  40. vp = Vpc.new(
  41. vpc_id: v['vpc_id'],
  42. name: v['name'],
  43. cidr: v['cidr'],
  44. state: v['state'],
  45. default: v['default'].to_s
  46. )
  47. vp.save
  48. AccountVpc.create(from_node: vp, to_node: AwsAccount.where(name: v['account']).first)
  49. VpcRegion.create(from_node: vp, to_node: Region.where(region: v['region']).first)
  50. end
  51. end
  52. task :subnets do
  53. puts 'loading subnets'
  54. sample_data['subnets'].each do |s|
  55. next unless Subnet.where(subnet_id: s['subnet_id']).empty?
  56. sn = Subnet.new(
  57. subnet_id: s['subnet_id'],
  58. cidr: s['cidr'],
  59. name: s['name'],
  60. ip_count: s['ip_count'],
  61. state: s['state']
  62. )
  63. VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: s['vpc_id']).first)
  64. SubnetAz.create(from_node: sn, to_node: Az.where(az: s['az']).first)
  65. end
  66. end
  67. task :buckets do
  68. puts 'loading buckets'
  69. sample_data['buckets'].each do |b|
  70. next unless Bucket.where(name: b['name']).empty?
  71. s = Bucket.new(
  72. name: b['name'],
  73. size: b['size']
  74. )
  75. s.save
  76. BucketRegion.create(from_node: s, to_node: Region.where(region: b['region']).first)
  77. BucketAccount.create(from_node: s, to_node: AwsAccount.where(name: b['account']).first)
  78. end
  79. end
  80. task :ssh_keys do
  81. puts 'loading ssh_keys'
  82. sample_data['ssh_keys'].each do |k|
  83. next unless SshKey.where(name: k['name']).empty?
  84. s = SshKey.new(
  85. name: k['name'],
  86. account: k['account']
  87. )
  88. s.save
  89. SshKeyAccount.create(from_node: s, to_node: AwsAccount.where(name: k['account']).first)
  90. end
  91. end
  92. task :nodes do
  93. puts 'loading nodes'
  94. sample_data['nodes'].each do |n|
  95. next unless Node.where(node_id: n['node_id']).empty?
  96. node = Node.new(
  97. node_id: n['node_id'],
  98. name: n['name'],
  99. ip: n['ip'],
  100. public_ip: n['public_ip'],
  101. size: n['size'],
  102. state: 'running',
  103. ami: n['ami']
  104. )
  105. node.save
  106. NodeAccount.create(from_node: node, to_node: AwsAccount.where(name: n['account']).first)
  107. NodeSubnet.create(from_node: node, to_node: Subnet.where(subnet_id: n['subnet_id']).first)
  108. NodeAz.create(from_node: node, to_node: Az.where(az: n['az']).first)
  109. NodeSshKey.create(from_node: node, to_node: SshKey.where(name: n['ssh_key']).first)
  110. end
  111. end
  112. desc 'Load Sample Data'
  113. task all: %i[accounts regions vpcs subnets ssh_keys buckets nodes]
  114. end