Class: BarnyardAws::AwsObject

Inherits:
Object
  • Object
show all
Defined in:
lib/barnyard_aws.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ AwsObject

Returns a new instance of AwsObject.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/barnyard_aws.rb', line 17

def initialize(args={})

  @began_at = Time.now

  @debug = args.fetch(:debug) { false }
  @log = args.fetch(:logger) { Logger.new(STDOUT) }

  @aws_access_key_id = args.fetch(:aws_access_key_id) { raise "You must provide :aws_access_key_id" }
  @aws_secret_access_key = args.fetch(:aws_secret_access_key) { raise "You must provide :aws_secret_access_key" }
  @region = args.fetch(:region) { raise "You must provide :region" }
  @account_id = args.fetch(:account_id) { raise "You must provide :account_id" }

  @log.debug JSON.pretty_generate(args)
  @log.debug "region=#{@region}"

  @synchronizer = BarnyardHarvester::Sync.new(args)

  @synchronizer.run do

    case self.class.to_s
      when "BarnyardAws::AwsElbs"
        compute = Fog::AWS::ELB.new({
                                        :aws_access_key_id => @aws_access_key_id,
                                        :aws_secret_access_key => @aws_secret_access_key,
                                        :region => @region
                                    })
      when "BarnyardAws::AwsIamUsers",
          "BarnyardAws::AwsIamGroupPolicies"
        compute = Fog::AWS::IAM.new({
                                        :aws_access_key_id => @aws_access_key_id,
                                        :aws_secret_access_key => @aws_secret_access_key,
                                        #:region => @region
                                    })
      else
        compute = Fog::Compute.new({
                                       :provider => 'AWS',
                                       :aws_access_key_id => @aws_access_key_id,
                                       :aws_secret_access_key => @aws_secret_access_key,
                                       :region => @region
                                   })
    end

    case self.class.to_s
      when "BarnyardAws::AwsSecurityGroups"
        compute.security_groups.each do |security_group|
          unless security_group.group_id.nil?
            obj = Crack::JSON.parse security_group.to_json
            obj["account_id"] = @account_id

            primary_key = make_primary_key(@account_id, security_group.group_id)
            @log.info "#{primary_key}"

            @synchronizer.process primary_key, obj

          end
        end

      when "BarnyardAws::AwsInstances"
        compute.servers.each do |s|
          unless s.id.nil?
            obj = Crack::JSON.parse s.to_json
            obj["account_id"] = @account_id

            primary_key = make_primary_key(@account_id, s.id)
            @log.info "#{primary_key}"

            @synchronizer.process primary_key, obj

          end
        end

      when "BarnyardAws::AwsSnapshots"
        compute.snapshots.each do |snapshot|
          unless snapshot.id.nil?
            obj = Crack::JSON.parse snapshot.to_json
            obj["account_id"] = @account_id

            primary_key = make_primary_key(@account_id, snapshot.id)
            @log.info "#{primary_key}"

            @synchronizer.process primary_key, obj

          end
        end

      when "BarnyardAws::AwsVolumes"
        compute.volumes.each do |s|
          unless s.id.nil?
            obj = Crack::JSON.parse s.to_json
            obj["account_id"] = @account_id

            primary_key = make_primary_key(@account_id, s.id)
            @log.info "#{primary_key}"

            @synchronizer.process primary_key, obj

          end
        end

      when "BarnyardAws::AwsSubnets"
        compute.subnets.each do |s|
          unless s.subnet_id.nil?
            obj = Crack::JSON.parse s.to_json
            obj["account_id"] = @account_id

            primary_key = make_primary_key(@account_id, s.subnet_id)
            @log.info "#{primary_key}"

            @synchronizer.process primary_key, obj

          end
        end

      when "BarnyardAws::AwsElbs"
        compute.load_balancers.each do |elb|
          unless elb.dns_name.nil?
            obj = Crack::JSON.parse elb.to_json
            obj["account_id"] = @account_id

            primary_key = make_primary_key(@account_id, elb.dns_name)
            @log.info "#{primary_key}"

            @synchronizer.process primary_key, obj

          end
        end

      when "BarnyardAws::AwsIamUsers"
        compute.list_users.body["Users"].each do |user|
          unless user['UserId'].nil?
            access_keys = compute.list_access_keys({'UserName' => user['UserName']}).body["AccessKeys"]
            obj = Crack::JSON.parse user.to_json
            obj["account_id"] = @account_id
            obj["access_keys"] = access_keys

            primary_key = make_primary_key(@account_id, user['UserId'])
            @log.info "#{primary_key}"

            @synchronizer.process primary_key, obj

          end
        end

      when "BarnyardAws::AwsIamGroupPolicies"

        obj = Hash.new
        obj["account_id"] = @account_id

        compute.list_groups.body["Groups"].each do |group|
          obj = group
          obj["account_id"] = @account_id
          obj["policies"] = Array.new

          compute.list_group_policies(group["GroupName"]).body["PolicyNames"].each do |policy|

            begin
              policy_obj = Crack::JSON.parse CGI::unescape(compute.get_group_policy(policy, group["GroupName"]).body["PolicyDocument"])

              obj["policies"] << policy_obj

            rescue Exception => e
              @log.warn "Unable to Crack policy >#{compute.get_group_policy(policy, group["GroupName"]).body["PolicyDocument"]}<"
            end

          end

          primary_key = make_primary_key(@account_id, group["GroupId"])
          @log.info "#{primary_key}"

          @synchronizer.process primary_key, obj

        end

    end


  end

  @log.info @synchronizer.stats

end

Instance Attribute Details

#began_atObject (readonly)

Returns the value of attribute began_at.



11
12
13
# File 'lib/barnyard_aws.rb', line 11

def began_at
  @began_at
end

#ended_atObject (readonly)

Returns the value of attribute ended_at.



11
12
13
# File 'lib/barnyard_aws.rb', line 11

def ended_at
  @ended_at
end

#synchronizerObject (readonly)

Returns the value of attribute synchronizer.



11
12
13
# File 'lib/barnyard_aws.rb', line 11

def synchronizer
  @synchronizer
end

Instance Method Details

#make_primary_key(account, key) ⇒ Object



13
14
15
# File 'lib/barnyard_aws.rb', line 13

def make_primary_key(, key)
  "#{}-#{key}"
end