Class: Serverspec::Type::OctopusDeploySmtpConfig

Inherits:
Base
  • Object
show all
Defined in:
lib/octopus_serverspec_extensions/type/octopus_deploy_smtp_config.rb

Instance Method Summary collapse

Constructor Details

#initialize(*url_and_api_key) ⇒ OctopusDeploySmtpConfig

Returns a new instance of OctopusDeploySmtpConfig.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_smtp_config.rb', line 11

def initialize(*url_and_api_key)
  serverUrl, apiKey = get_octopus_creds(url_and_api_key)

  @name = "Octopus Deploy SMTP Config #{serverUrl}"
  @runner = Specinfra::Runner
  @serverUrl = serverUrl
  @apiKey = apiKey

  if serverUrl.nil?
    serverUrl = get_env_var('OCTOPUS_CLI_SERVER').chomp('/') # removes trailing slash if present
  end

  if apiKey.nil?
    apiKey = get_env_var('OCTOPUS_CLI_API_KEY')
  end

  # is it still nil?
  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

  @smtpConfig = get_smtp_config_via_api(serverUrl, apiKey)
end

Instance Method Details

#configured?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_smtp_config.rb', line 38

def configured?
  url = "#{@serverUrl}/api/smtpconfiguration/isconfigured?api-key=#{@apiKey}"
  begin
    resp = Net::HTTP.get_response(URI.parse(url))
    body = JSON.parse(resp.body)
    smtp = body unless body.nil?
  rescue => e
    raise "get_smtp_config_via_api: Unable to connect to #{url}: #{e}"
  end

  smtp["IsConfigured"]
end

#has_from_address?(from_address) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_smtp_config.rb', line 72

def has_from_address?(from_address)
  false if @smtpConfig.nil?
  @smtpConfig["SendEmailFrom"] == from_address
end

#on_host?(hostname) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_smtp_config.rb', line 61

def on_host?(hostname)
  false if @smtpConfig.nil?
  @smtpConfig["SmtpHost"] == hostname
end

#on_port?(port_number) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
59
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_smtp_config.rb', line 56

def on_port?(port_number)
  false if @smtpConfig.nil?
  @smtpConfig["SmtpPort"] == port_number
end

#using_credentials?(username) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_smtp_config.rb', line 66

def using_credentials?(username)
  # we can't test the password, but we can check the username
  false if @smtpConfig.nil?
  @smtpConfig["SmtpLogin"] == username && @smtpConfig["SmtpPassword"]["HasValue"]
end

#using_ssl?Boolean

Returns:

  • (Boolean)


51
52
53
54
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_smtp_config.rb', line 51

def using_ssl?
  false if @smtpConfig.nil?
  @smtpConfig["EnableSsl"]
end