Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ruby cloud integration support #201

Merged
merged 16 commits into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 108 additions & 1 deletion lib/dogapi/facade.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Client # rubocop:disable Metrics/ClassLength
attr_accessor :v2
# Support for API version 2.

# rubocop:disable Metrics/MethodLength, Metrics/LineLength
def initialize(api_key, application_key=nil, host=nil, device=nil, silent=true, timeout=nil, endpoint=nil)

if api_key
Expand Down Expand Up @@ -94,12 +95,16 @@ def initialize(api_key, application_key=nil, host=nil, device=nil, silent=true,
@legacy_event_svc = Dogapi::EventService.new(@datadog_host)
@hosts_svc = Dogapi::V1::HostsService.new(@api_key, @application_key, silent, timeout, @datadog_host)
@integration_svc = Dogapi::V1::IntegrationService.new(@api_key, @application_key, silent, timeout, @datadog_host)
@aws_integration_svc = Dogapi::V1::AwsIntegrationService.new(@api_key, @application_key, silent, timeout, @datadog_host)
@aws_logs_svc = Dogapi::V1::AwsLogsService.new(@api_key, @application_key, silent, timeout, @datadog_host)
@usage_svc = Dogapi::V1::UsageService.new(@api_key, @application_key, silent, timeout, @datadog_host)

@azure_integration_svc = Dogapi::V1::AzureIntegrationService.new(@api_key, @application_key, silent, timeout, @datadog_host)
@gcp_integration_svc = Dogapi::V1::GcpIntegrationService.new(@api_key, @application_key, silent, timeout, @datadog_host)
# Support for Dashboard List API v2.
@v2 = Dogapi::ClientV2.new(@api_key, @application_key, true, true, @datadog_host)

end
# rubocop:enable Metrics/MethodLength, Metrics/LineLength

#
# METRICS
Expand Down Expand Up @@ -682,6 +687,108 @@ def delete_integration(source_type_name)
@integration_svc.delete_integration(source_type_name)
end

#
# AWS INTEGRATION
#
def aws_integration_list
@aws_integration_svc.aws_integration_list
end

def aws_integration_create(config)
@aws_integration_svc.aws_integration_create(config)
end

def aws_integration_delete(config)
@aws_integration_svc.aws_integration_delete(config)
end

def aws_integration_list_namespaces
@aws_integration_svc.aws_integration_list_namespaces
end

def aws_integration_generate_external_id(config)
@aws_integration_svc.aws_integration_generate_external_id(config)
end

def aws_integration_update(config, new_config)
@aws_integration_svc.aws_integration_update(config, new_config)
end

#
# AWS Logs Integration
#

def aws_logs_add_lambda(config)
@aws_logs_svc.aws_logs_add_lambda(config)
end

def aws_logs_list_services
@aws_logs_svc.aws_logs_list_services
end

def aws_logs_save_services(config)
@aws_logs_svc.aws_logs_save_services(config)
end

def aws_logs_integrations_list
@aws_logs_svc.aws_logs_integrations_list
end

def aws_logs_integration_delete(config)
@aws_logs_svc.aws_logs_integration_delete(config)
end

def aws_logs_check_lambda(config)
@aws_logs_svc.aws_logs_check_lambda(config)
end

def aws_logs_check_services(config)
@aws_logs_svc.aws_logs_check_services(config)
end

#
# AZURE INTEGRATION
#

def azure_integration_list
@azure_integration_svc.azure_integration_list
end

def azure_integration_create(config)
@azure_integration_svc.azure_integration_create(config)
end

def azure_integration_delete(config)
@azure_integration_svc.azure_integration_delete(config)
end

def azure_integration_update_host_filters(config)
@azure_integration_svc.azure_integration_update_host_filters(config)
end

def azure_integration_update(config)
@azure_integration_svc.azure_integration_update(config)
end

#
# GCP INTEGRATION
#
def gcp_integration_list
@gcp_integration_svc.gcp_integration_list
end

def gcp_integration_delete(config)
@gcp_integration_svc.gcp_integration_delete(config)
end

def gcp_integration_create(config)
@gcp_integration_svc.gcp_integration_create(config)
end

def gcp_integration_update(config)
@gcp_integration_svc.gcp_integration_update(config)
end

#
# USAGE
#
Expand Down
4 changes: 4 additions & 0 deletions lib/dogapi/v1.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,7 @@
require 'dogapi/v1/hosts'
require 'dogapi/v1/integration'
require 'dogapi/v1/usage'
require 'dogapi/v1/aws_integration'
require 'dogapi/v1/aws_logs'
Ricky-Thomas marked this conversation as resolved.
Show resolved Hide resolved
require 'dogapi/v1/azure_integration'
require 'dogapi/v1/gcp_integration'
113 changes: 113 additions & 0 deletions lib/dogapi/v1/aws_integration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
require 'dogapi'

module Dogapi
class V1 # for namespacing

# AwsIntegrationService for user interaction with AWS configs.
class AwsIntegrationService < Dogapi::APIService

API_VERSION = 'v1'

# Retrieve AWS integration information
def aws_integration_list
request(Net::HTTP::Get, "/api/#{API_VERSION}/integration/aws", nil, nil, false)
end

# Create an AWS integration
# :config => Hash: integration config.
# config = {
# :account_id => '<AWS_ACCOUNT>',
# :host_tags => ['api:example'],
# :role_name => '<AWS_ROLE_NAME>'
# }
#
# Access Key/Secret Access Key based accounts (GovCloud and China only)
#
# config = {
# :access_key_id => '<AWS_ACCESS_KEY_ID>',
# :host_tags => ['api:example'],
# :secret_access_key => '<AWS_SECRET_ACCESS_KEY>'
# }
#
# dog = Dogapi::Client.new(api_key, app_key)
#
# puts dog.aws_integration_create(config)
def aws_integration_create(config)
request(Net::HTTP::Post, "/api/#{API_VERSION}/integration/aws", nil, config, true)
end

# Delete an integration
# :config => Hash: integration config.
# config = {
# :account_id => '<AWS_ACCOUNT>',
# :role_name => '<AWS_ROLE_NAME>'
# }
# Access Key/Secret Access Key based accounts (GovCloud and China only)
#
# config = {
# :access_key_id => '<AWS_ACCESS_KEY_ID>',
# }
#
# dog = Dogapi::Client.new(api_key, app_key)
#
# puts dog.aws_integration_delete(config)
def aws_integration_delete(config)
request(Net::HTTP::Delete, "/api/#{API_VERSION}/integration/aws", nil, config, true)
end

# List available AWS namespaces
def aws_integration_list_namespaces
request(Net::HTTP::Get, "/api/#{API_VERSION}/integration/aws/available_namespace_rules", nil, nil, false)
end

# Generate new AWS external ID for a specific integrated account
# :config => Hash: integration config.
# config = {
# :account_id => '<AWS_ACCOUNT>',
# :role_name => '<AWS_ROLE_NAME>'
# }
#
# dog = Dogapi::Client.new(api_key, app_key)
#
# puts dog.aws_integration_generate_external_id(config)
def aws_integration_generate_external_id(config)
request(Net::HTTP::Put, "/api/#{API_VERSION}/integration/aws/generate_new_external_id", nil, config, true)
end

# Update integrated AWS account.
# :config => Hash: integration config.
# config = {
# "account_id": '<EXISTING_AWS_ACCOUNT>',
# "role_name": '<EXISTING_AWS_ROLE_NAME>'
# }
#
# new_config = {
# "account_id": '<NEW_AWS_ACCOUNT>',
# "host_tags": ['tag:example1,tag:example2'],
# "filter_tags": ['datadog:true']
# }
#
# Access Key/Secret Access Key based accounts (GovCloud and China only)
#
# config = {
# "access_key_id": '<EXISTING_ACCESS_KEY_ID>',
# "secret_access_key": '<EXISTING_SECRET_ACCESS_KEY>'
# }
#
# new_config = {
# "access_key_id": '<NEW_ACCESS_KEY_ID>',
# "host_tags": ['new:tags'],
# "filter_tags": ['datadog:true']
# }
#
# dog = Dogapi::Client.new(api_key, app_key)

# puts dog.aws_integration_update(config, new_config)
def aws_integration_update(config, new_config)
request(Net::HTTP::Put, "/api/#{API_VERSION}/integration/aws", config, new_config, true)
end

end

end
end
103 changes: 103 additions & 0 deletions lib/dogapi/v1/aws_logs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require 'dogapi'

module Dogapi
class V1 # for namespacing

# AwsLogsService for user interaction with AWS configs.
class AwsLogsService < Dogapi::APIService

API_VERSION = 'v1'

# Get the list of current AWS services for which Datadog offers automatic log collection.
# Use returned service IDs with the services parameter for the Enable
# an AWS service log collection API endpoint.
def aws_logs_list_services
request(Net::HTTP::Get, "/api/#{API_VERSION}/integration/aws/logs/services", nil, nil, false)
end

# Create an AWS integration
# :config => Hash: integration config.
# config = {
# :account_id => '<AWS_ACCOUNT>',
# :lambda_arn => '<LAMBDA_ARN>'
# }
#
# dog = Dogapi::Client.new(api_key, app_key)
#
# puts dog.aws_logs_add_lambda(config)
def aws_logs_add_lambda(config)
request(Net::HTTP::Post, "/api/#{API_VERSION}/integration/aws/logs", nil, config, true)
end

# List all Datadog-AWS Logs integrations configured in your Datadog account.
def aws_logs_integrations_list
request(Net::HTTP::Get, "/api/#{API_VERSION}/integration/aws/logs", nil, nil, false)
end

# Enable automatic log collection for a list of services.
# This should be run after running 'aws_logs_add_lambda' to save the config.
# config = {
# :account_id => '<AWS_ACCOUNT>',
# :services => ['s3', 'elb', 'elbv2', 'cloudfront', 'redshift', 'lambda']
# }
#
# dog = Dogapi::Client.new(api_key, app_key)
#
# puts dog.aws_logs_save_services(config)
def aws_logs_save_services(config)
request(Net::HTTP::Post, "/api/#{API_VERSION}/integration/aws/logs/services", nil, config, true)
end

# Delete an AWS Logs integration
# :config => Hash: integration config.
# config = {
# :account_id => '<AWS_ACCOUNT>',
# :lambda_arn => '<LAMBDA_ARN>'
# }
#
# dog = Dogapi::Client.new(api_key, app_key)
#
# puts dog.aws_logs_integration_delete(config)
def aws_logs_integration_delete(config)
request(Net::HTTP::Delete, "/api/#{API_VERSION}/integration/aws/logs", nil, config, true)
end

# Check function to see if a lambda_arn exists within an account.
# This sends a job on our side if it does not exist, then immediately returns
# the status of that job. Subsequent requests will always repeat the above, so this endpoint
# can be polled intermittently instead of blocking.

# Returns a status of 'created' when it's checking if the Lambda exists in the account.
# Returns a status of 'waiting' while checking.
# Returns a status of 'checked and ok' if the Lambda exists.
# Returns a status of 'error' if the Lambda does not exist.

# contents of config should be
# >>> :account_id => '<AWS_ACCOUNT_ID>'
# >>> :lambda_arn => '<AWS_LAMBDA_ARN>'

def aws_logs_check_lambda(config)
request(Net::HTTP::Post, "/api/#{API_VERSION}/integration/aws/logs/check_async", nil, config, true)
end

# Test if permissions are present to add log-forwarding triggers for the
# given services + AWS account. Input is the same as for save_services.
# Done async, so can be repeatedly polled in a non-blocking fashion until
# the async request completes

# Returns a status of 'created' when it's checking if the permissions exists in the AWS account.
# Returns a status of 'waiting' while checking.
# Returns a status of 'checked and ok' if the Lambda exists.
# Returns a status of 'error' if the Lambda does not exist.

# contents of config should be
# :account_id => '<AWS_ACCOUNT_ID>'
# :services => ['s3', 'elb', 'elbv2', 'cloudfront', 'redshift', 'lambda']
def aws_logs_check_services(config)
request(Net::HTTP::Post, "/api/#{API_VERSION}/integration/aws/logs/services_async", nil, config, true)
end

end

end
end
Loading