Skip to content

Commit

Permalink
Merge pull request #9 from yokawasa/add_azure_resource_id
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
yokawasa committed May 6, 2020
2 parents 11900ab + 02293f9 commit fafb294
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 8 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.2.0
* Support setting the x-ms-AzureResourceId Header - [issue #8](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/8)

## 0.1.6
* fix CVE-2020-8130 - [issue #7](https://github.com/yokawasa/azure-log-analytics-data-collector/issues/7)

Expand Down
46 changes: 45 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,51 @@ else
end
```

### Sample3 - use proxy to access the API
### Sample3 - With time_generated_field and azure_resource_id option
Supported setting azure_resource_id option from version [0.2.0](https://github.com/yokawasa/azure-log-analytics-data-collector/releases/tag/v0.2.0)
```ruby
require "azure/loganalytics/datacollectorapi/client"

customer_id = '<Customer ID aka WorkspaceID String>'
shared_key = '<The primary or the secondary Connected Sources client authentication key>'
log_type = "MyCustomLog"

posting_records = []
record1= {
:string => "MyText1",
:boolean => true,
:number => 100,
:timegen => "2017-11-23T11:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
}
record2= {
:string => "MyText2",
:boolean => false,
:number => 200,
:timegen => "2017-11-23T12:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
}
posting_records.push(record1)
posting_records.push(record2)

time_generated_field = "timegen"

# Azure Resource ID
# [Azure Resource ID Format]
# /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}
azure_resource_id ="/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/otherResourceGroup/providers/Microsoft.Storage/storageAccounts/examplestorage"

client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
res = client.post_data(log_type, posting_records, time_generated_field, azure_resource_id)
puts res
puts "res code=#{res.code}"

if Azure::Loganalytics::Datacollectorapi::Client.is_success(res)
puts "operation was succeeded!"
else
puts "operation was failured!"
end
```

### Sample4 - use proxy to access the API
```ruby
require "azure/loganalytics/datacollectorapi/client"

Expand Down
10 changes: 8 additions & 2 deletions lib/azure/loganalytics/datacollectorapi/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module Datacollectorapi

class Client

def initialize (customer_id, shared_key,endpoint ='ods.opinsights.azure.com')
def initialize (customer_id, shared_key, endpoint ='ods.opinsights.azure.com')
require 'rest-client'
require 'json'
require 'openssl'
Expand All @@ -18,9 +18,10 @@ def initialize (customer_id, shared_key,endpoint ='ods.opinsights.azure.com')
@customer_id = customer_id
@shared_key = shared_key
@endpoint = endpoint
@default_azure_resource_id = ''
end

def post_data(log_type, json_records, record_timestamp ='')
def post_data(log_type, json_records, record_timestamp ='', azure_resource_id ='' )
raise ConfigError, 'no log_type' if log_type.empty?
raise ConfigError, 'log_type must be only alpha characters' if not is_alpha(log_type)
raise ConfigError, 'no json_records' if json_records.empty?
Expand All @@ -35,6 +36,7 @@ def post_data(log_type, json_records, record_timestamp ='')
'Authorization' => sig,
'Log-Type' => log_type,
'x-ms-date' => date,
'x-ms-AzureResourceId' => azure_resource_id.empty? ? @default_azure_resource_id : azure_resource_id,
'time-generated-field' => record_timestamp
}

Expand All @@ -45,6 +47,10 @@ def post_data(log_type, json_records, record_timestamp ='')
def set_proxy(proxy='')
RestClient.proxy = proxy.empty? ? ENV['http_proxy'] : proxy
end

def set_default_azure_resoruce_id(azure_resource_id)
@default_azure_resource_id = azure_resource_id
end

def self.is_success(res)
return (res.code == 200) ? true : false
Expand Down
2 changes: 1 addition & 1 deletion lib/azure/loganalytics/datacollectorapi/version.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Azure
module Loganalytics
module Datacollectorapi
VERSION = "0.1.6"
VERSION = "0.2.0"
end
end
end
57 changes: 53 additions & 4 deletions spec/azure/loganalytics/datacollectorapi/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
# expect(false).to eq(true)
#end

it "posting data to datacollector api" do
customer_id = '<Customer ID aka WorkspaceID String>'
shared_key = '<Primary Key String>'
log_type = "MyCustomLog"
customer_id = '<Customer ID aka WorkspaceID String>'
shared_key = '<Primary Key String>'
log_type = "MyCustomLog"

it "posting data to datacollector api" do
json_records = []
record1= {
:string => "MyText1",
Expand All @@ -33,4 +33,53 @@
expect(Azure::Loganalytics::Datacollectorapi::Client.is_success(res)).to eq(true)
end

it "posting data to datacollector api with time-generated-field" do
json_records = []
record1= {
:string => "MyText1",
:boolean => true,
:number => 100,
:timegen => "2020-05-05T11:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
}
record2= {
:string => "MyText2",
:boolean => false,
:number => 200,
:timegen => "2020-05-05T12:13:35.576Z" # YYYY-MM-DDThh:mm:ssZ
}
json_records.push(record1)
json_records.push(record2)

time_generated_field = "timegen"
client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
res = client.post_data(log_type, json_records, time_generated_field)
expect(Azure::Loganalytics::Datacollectorapi::Client.is_success(res)).to eq(true)
end

it "posting data to datacollector api with azure-resource-id" do
json_records = []
record1= {
:string => "MyText1",
:boolean => true,
:number => 100
}
record2= {
:string => "MyText2",
:boolean => false,
:number => 200
}
json_records.push(record1)
json_records.push(record2)

time_generated_field = ""
# Azure Resource ID
# https://docs.microsoft.com/en-us/azure/azure-resource-manager/templates/template-functions-resource#resourceid
# /subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/{resourceProviderNamespace}/{resourceType}/{resourceName}
azure_resource_id ="/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/otherResourceGroup/providers/Microsoft.Storage/storageAccounts/examplestorage"

client=Azure::Loganalytics::Datacollectorapi::Client::new( customer_id, shared_key)
res = client.post_data(log_type, json_records, time_generated_field, azure_resource_id)
expect(Azure::Loganalytics::Datacollectorapi::Client.is_success(res)).to eq(true)
end

end

0 comments on commit fafb294

Please sign in to comment.