Creating a new Notification Provider for CloudInit.NET

Included with the binaries is a dll called CloudInit.Notification.Core.dll this contains all the core references to create a new notification provider. Include this reference in your project to begin creating a new notification provider.

INotificationProvider

namespace CloudInit.Notification.Core
{
    /// <summary>
    /// Interface that exposes required properties for a notification provider
    /// </summary>
    public interface INotificationProvider
    {
        /// <summary>
        /// Gets the settings path in the registry for this provider
        /// </summary>
        String SettingsPath { get; }
        /// <summary>
        /// Gets the name of the provider
        /// </summary>
        String ProviderName { get; }
        /// <summary>
        /// Gets a value indicating whether this provider is active
        /// </summary>
        Boolean IsActive { get; }

        /// <summary>
        /// Adds this provider to the registry
        /// </summary>
        void Add();
        /// <summary>
        /// Removes this provider from the registry
        /// </summary>
        void Remove();
        /// <summary>
        /// Sets the setting for this provider
        /// </summary>
        /// <param name="name">The registry key name</param>
        /// <param name="value">The desired key value</param>
        /// <param name="valueKind">Kind of the value</param>
        void SetSetting(String name, Object value, RegistryValueKind valueKind);
        /// <summary>
        /// Gets the setting for this provider
        /// </summary>
        /// <typeparam name="T">Type of object to return from registry</typeparam>
        /// <param name="name">The registry key name</param>
        /// <returns></returns>
        T GetSetting<T>(String name);
        /// <summary>
        /// Notifies the provider of a CloudInit log file
        /// </summary>
        /// <param name="session">The current CloudInit session</param>
        void Notify(CloudInitSession session);
    }
}

Make sure that you inherit the NotificationProviderBase class or at least take a good look at what it does because it plays an important roll in the installation and the behind the scenes grunt work of the notification provider plugins. Personally I recommend just inheriting the class as I don't think it will do anything you don't want to do.

The only method that you must override is the Notify method. This method will get called when the script is finished executing, will run if the script completed successfully or with errors, however it doesn't tell you if it was with errors or not. The CloudInitSession parameter provides you with a few basic properties of the current configuration of CloudInit.

Other methods you should note are the Add and Remove. These methods are run when the provider is installed or removed. Normally this is where you want to configure the settings or remove them. The registry settings will get cleaned up automatically when the provider is uninstalled so you should only need to use the Remove method if you any other configuration that needs to be cleaned up after the provider is removed.

Here's what the EmailNotificationProvider looks like. Isn't it easy to do?

namespace CloudInit.Notification
{
    /// <summary>
    /// Sends the notification via email
    /// </summary>
    public class EmailNotificationProvider : NotificationProviderBase
    {
        /// <summary>
        /// Gets the name of the provider
        /// </summary>
        public override string ProviderName
        {
            get { return "EmailNotification"; }
        }
        /// <summary>
        /// Adds this provider to the registry
        /// </summary>
        public override void Add()
        {
            base.Add();

            this.SetSetting("ToEmail", String.Empty, RegistryValueKind.String);
            this.SetSetting("FromEmail", String.Empty, RegistryValueKind.String);
            this.SetSetting("MailServer", String.Empty, RegistryValueKind.String);
        }
        /// <summary>
        /// Notifies the provider of a CloudInit log file
        /// </summary>
        /// <param name="session">The current CloudInit session</param>
        public override void Notify(CloudInitSession session)
        {
            // Get the settings from the registry
            String toEmail = this.GetSetting<String>("ToEmail");
            String fromEmail = this.GetSetting<String>("FromEmail");
            String mailServer = this.GetSetting<String>("MailServer");

            // Create a message we want to send
            MailMessage message = new MailMessage(fromEmail, toEmail);
            message.Subject = String.Format("LOG Notification - instance:{0} {1}", session.Machine, session.Address);
            message.Body = session.Data;

            // Send the message
            SmtpClient smtp = new SmtpClient(mailServer);
            smtp.Send(message);
        }
    }
}

Last edited Jan 31, 2012 at 3:27 PM by bwight4157, version 3

Comments

No comments yet.