Sha256: 209f32c034ba12d3ee8dc7e4adb879e0b05d4e39f58261b2803eab5693ddf824

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

require 'serverspec'
require 'serverspec/type/base'
require 'net/http'
require 'json'

module Serverspec::Type
  class OctopusDeployEnvironment < Base
    @environment = nil
    @serverUrl = nil
    @apiKey = nil

    def initialize(serverUrl, apiKey, environment_name)
      @name = "Octopus Deploy Environment #{environment_name}"
      @runner = Specinfra::Runner
      @serverUrl = serverUrl
      @apiKey = apiKey

      if (serverUrl.nil?)
        puts "'serverUrl' was not provided. Unable to connect to Octopus server to validate configuration."
        return
      end
      if (apiKey.nil?)
        puts "'apiKey' was not provided. Unable to connect to Octopus server to validate configuration."
        return
      end

      @environment = get_environment_via_api(serverUrl, apiKey, environment_name)
    end

    def exists?
      !@environment.nil?
    end
  end

  def octopus_deploy_environment(serverUrl, apiKey, environment_name)
    OctopusDeployEnvironment.new(serverUrl, apiKey, environment_name)
  end

  private

  def get_environment_via_api(serverUrl, apiKey, environment_name)
    environment = nil
    url = "#{serverUrl}/api/environments?name=#{environment_name}&api-key=#{apiKey}"

    begin
      resp = Net::HTTP.get_response(URI.parse(url))
      body = JSON.parse(resp.body)
      environment = body['Items'].first unless body.nil?
    rescue => e
      puts "Unable to connect to #{url}: #{e}"
    end

    environment
  end
end

include Serverspec::Type

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
octopus-serverspec-extensions-0.14.0 lib/octopus_serverspec_extensions/type/octopus_deploy_environment.rb