sample_data.rake 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # frozen_string_literal: true
  2. require 'yaml'
  3. namespace :sample_data do
  4. sample_data = YAML.load_file(
  5. File.join(File.dirname(File.expand_path(__FILE__)), 'sample_data.yaml')
  6. )
  7. Neo4j::Session.open(:server_db, 'http://localhost:7474')
  8. task :accounts do
  9. puts 'loading accounts'
  10. sample_data['accounts'].each do |account|
  11. next unless AwsAccount.where(name: account[:name]).empty?
  12. acct = AwsAccount.new(
  13. name: account[:name],
  14. account_id: 90000+rand(20),
  15. user_id: account[:name],
  16. key_md5: Digest:: MD5.hexdigest(account[:name]),
  17. secret_md5: Digest:: MD5.hexdigest(account[:name])
  18. )
  19. acct.save
  20. end
  21. end
  22. task :regions do
  23. puts 'loading regions'
  24. sample_data['regions'].each do |reg|
  25. next unless Region.where(region: reg.keys.first).empty?
  26. r = Region.new(region: reg.keys.first)
  27. r.save
  28. reg[reg.keys.first].each do |az|
  29. a = Az.new(az: az)
  30. a.save
  31. AzRegion.create(from_node: a, to_node: Region.where(region: reg.keys.first).first)
  32. end
  33. end
  34. end
  35. task :vpcs do
  36. puts 'loading vpcs'
  37. sample_data['vpcs'].each do |v|
  38. next unless Vpc.where(vpc_id: v['vpc_id']).empty?
  39. vp = Vpc.new(
  40. vpc_id: v['vpc_id'],
  41. name: v['name'],
  42. cidr: v['cidr'],
  43. state: v['state'],
  44. default: v['default'].to_s
  45. )
  46. vp.save
  47. AccountVpc.create(from_node: vp, to_node: AwsAccount.where(name: v['account']).first)
  48. VpcRegion.create(from_node: vp, to_node: Region.where(region: v['region']).first)
  49. end
  50. end
  51. task :subnets do
  52. puts 'loading subnets'
  53. sample_data['subnets'].each do |s|
  54. next unless Subnet.where(vpc_id: s['subnet_id']).empty?
  55. sn = Subnet.new(
  56. subnet_id: s['subnet_id'],
  57. cidr: s['cidr'],
  58. name: s['name'],
  59. ip_count: s['ip_count'],
  60. state: s['state']
  61. )
  62. VpcSubnet.create(from_node: sn, to_node: Vpc.where(vpc_id: s['vpc_id']).first)
  63. SubnetAz.create(from_node: sn, to_node: Az.where(az: s['az']).first)
  64. end
  65. end
  66. task :buckets do
  67. puts 'loading buckets'
  68. j = NeoInfra::Aws.new
  69. end
  70. task :nodes do
  71. puts 'loading nodes'
  72. #j = NeoInfra::Nodes.new
  73. end
  74. desc 'Load Sample Data'
  75. task all: %i[accounts regions vpcs subnets buckets nodes]
  76. end