//-----------------------------------------------------------------------------
//
// Copyright 2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License").
// You may not use this file except in compliance with the License.
// A copy of the License is located at
//
// http://aws.amazon.com/apache2.0
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
//
//-----------------------------------------------------------------------------
using Amazon.Runtime.Internal.Util;
using System;
using System.Collections.Generic;
using System.IO;
using ThirdParty.Json.LitJson;
namespace Amazon.XRay.Recorder.Core.Plugins
{
///
/// This is a plugin for Elastic Beanstalk.
///
public class ElasticBeanstalkPlugin : IPlugin
{
private static readonly Logger _logger = Logger.GetLogger(typeof(ElasticBeanstalkPlugin));
private const String _confPath = " C:\\Program Files\\Amazon\\XRay\\environment.conf";
///
/// Gets the name of the origin associated with this plugin.
///
/// The name of the origin associated with this plugin.
public string Origin
{
get
{
return @"AWS::ElasticBeanstalk::Environment";
}
}
///
/// Gets the name of the service associated with this plugin.
///
/// The name of the service that this plugin is associated with.
public string ServiceName
{
get
{
return @"elastic_beanstalk";
}
}
///
/// Gets the context of the runtime that this plugin is associated with.
///
/// When the method returns, contains the runtime context of the plugin, or null if the runtime context is not available.
/// true if the runtime context is available; Otherwise, false.
public bool TryGetRuntimeContext(out IDictionary context)
{
context = null;
var dict = ElasticBeanstalkPlugin.GetElasticBeanstalkMetaData();
if (dict.Count == 0)
{
_logger.DebugFormat("Failed to get meta data for Elastic Beanstalk.");
return false;
}
context = dict;
return true;
}
private static Dictionary GetElasticBeanstalkMetaData()
{
Dictionary dictionary = new Dictionary();
try
{
using (Stream stream = new FileStream(_confPath, FileMode.Open, FileAccess.Read))
{
dictionary = ElasticBeanstalkPlugin.ReadStream(stream);
}
}
catch (Exception e)
{
_logger.Error(e, "Failed to access Elastic Beanstalk configuration file.");
}
return dictionary;
}
private static Dictionary ReadStream(Stream stream)
{
Dictionary dict = new Dictionary();
using (var reader = new StreamReader(stream))
{
try
{
dict = JsonMapper.ToObject>(reader);
}
catch (JsonException e)
{
_logger.Error(e, "Failed to load Elastic Beanstalk configuration file.");
}
}
return dict;
}
}
}