Skip to content

Commit

Permalink
Merge pull request #222 from inspec/nw-flow-logs
Browse files Browse the repository at this point in the history
Adds ability to check Network Watcher flow logs.
  • Loading branch information
Ruairi Fennell authored Nov 21, 2019
2 parents 50ad7b8 + 65d87d0 commit 785ee74
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
7 changes: 7 additions & 0 deletions libraries/azurerm_network_watcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,27 @@ class AzurermNetworkWatcher < AzurermSingularResource
).freeze

attr_reader(*ATTRS)
attr_accessor(:nsg)

def initialize(resource_group: nil, name: nil)
resp = management.network_watcher(resource_group, name)
return if has_error?(resp)

assign_fields(ATTRS, resp)

@resource_group = resource_group
@exists = true
end

def to_s
"'#{name}' Network Watcher"
end

def flow_logs
return nil if @nsg.nil?
@flow_logs ||= management.network_watcher_flow_log_status(@resource_group, name, nsg)
end

def provisioning_state
@provisioning_state ||= properties.provisioningState
end
Expand Down
9 changes: 9 additions & 0 deletions libraries/support/azure/management.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ def network_watchers(resource_group)
)
end

def network_watcher_flow_log_status(resource_group, network_watcher, nsg)
post(
url: link(location: "Microsoft.Network/networkWatchers/#{network_watcher}/queryFlowLogStatus",
resource_group: resource_group),
api_version: '2019-04-01',
req_body: "{\"targetResourceId\": \"/subscriptions/#{subscription_id}/resourceGroups/#{resource_group}/providers/Microsoft.Network/networkSecurityGroups/#{nsg}\"}",
)
end

def postgresql_server(resource_group, name)
get(
url: link(location: "Microsoft.DBforPostgreSQL/servers/#{name}",
Expand Down
4 changes: 2 additions & 2 deletions libraries/support/azure/rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ def get(path, params: {}, headers: {})
end
end

def post(path, params: {}, headers: {})
def post(path, params: {}, headers: {}, body: nil)
add_user_agent!(headers)
connection.post do |req|
req.url path

req.body = body if body
req.params = req.params.merge(params)
req.headers = req.headers.merge(headers)
credentials.sign_request(req)
Expand Down
5 changes: 3 additions & 2 deletions libraries/support/azure/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def get(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, pa
end
end

def post(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, params: {}, headers: {}) # rubocop:disable Metrics/ParameterLists
def post(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, params: {}, headers: {}, req_body: nil) # rubocop:disable Metrics/ParameterLists
confirm_configured!

body = cache.fetch(url) if use_cache
Expand All @@ -113,7 +113,8 @@ def post(url:, api_version:, error_handler: nil, unwrap: nil, use_cache: true, p

body ||= rest_client.post(url,
params: params,
headers: { Accept: 'application/json' }.merge(headers)).body
headers: { Accept: 'application/json' }.merge(headers),
body: req_body).body

error_handler&.(body)

Expand Down

0 comments on commit 785ee74

Please sign in to comment.