Sha256: 5e084c873416f243a4308679364c3be51fcb9de7bab9349f78f17351ca43a224

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 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?)
        raise "'serverUrl' was not provided. Unable to connect to Octopus server to validate configuration."
      end
      if (apiKey.nil?)
        raise "'apiKey' was not provided. Unable to connect to Octopus server to validate configuration."
      end
      if (environment_name.nil?)
        raise "'environment_name' was not provided. Unable to connect to Octopus server to validate configuration."
      end

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

    def exists?
      (!@environment.nil?) && (@environment != [])
    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
      raise "Unable to connect to #{url}: #{e}"
    end

    environment
  end
end

include Serverspec::Type

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
octopus-serverspec-extensions-0.15.5 lib/octopus_serverspec_extensions/type/octopus_deploy_environment.rb
octopus-serverspec-extensions-0.15.4 lib/octopus_serverspec_extensions/type/octopus_deploy_environment.rb
octopus-serverspec-extensions-0.15.3 lib/octopus_serverspec_extensions/type/octopus_deploy_environment.rb
octopus-serverspec-extensions-0.15.2 lib/octopus_serverspec_extensions/type/octopus_deploy_environment.rb
octopus-serverspec-extensions-0.15.1 lib/octopus_serverspec_extensions/type/octopus_deploy_environment.rb
octopus-serverspec-extensions-0.15.0 lib/octopus_serverspec_extensions/type/octopus_deploy_environment.rb