Class: Serverspec::Type::OctopusDeployUser

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

Instance Method Summary collapse

Constructor Details

#initialize(*url_and_api_key, userName) ⇒ OctopusDeployUser

Returns a new instance of OctopusDeployUser.



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

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

  @name = "Octopus Deploy User Account #{userName}"
  @runner = Specinfra::Runner
  @serverUrl = serverUrl
  @apiKey = apiKey

  # is our auth info 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

  if(userName.nil?)
    raise "'userName' was not provided"
  end

  @userAccount = get_user_via_api(serverUrl, apiKey, userName)
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


43
44
45
46
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_user.rb', line 43

def active?
  return false if @userAccount.nil?
  @userAccount['IsActive'] == true
end

#exists?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_user.rb', line 39

def exists?
  (!@userAccount.nil?) && (@userAccount != [])
end

#has_api_key?(purpose) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_user.rb', line 58

def has_api_key?(purpose)
  return false if @userAccount.nil?

  user_api_key = nil
  user_id = @userAccount['Id']
  url = "#{@serverUrl}/api/users/#{user_id}/apikeys?api-key=#{@apiKey}&take=9999"

  begin
    resp = Net::HTTP.get_response(URI.parse(url))
    body = JSON.parse(resp.body)
    keys = body unless body.nil?
    user_api_key = keys['Items'].select {|i| i['Purpose'] == purpose }.first unless keys.nil?

    rescue => e
      raise "has_api_key: Unable to connect to #{url}: #{e}"
  end

  !user_api_key.nil?
end

#has_display_name?(name) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_user.rb', line 53

def has_display_name?(name)
  return false if @userAccount.nil?
  @userAccount['DisplayName'] == name
end

#has_email?(email) ⇒ Boolean

Returns:

  • (Boolean)


48
49
50
51
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_user.rb', line 48

def has_email?(email)
  return false if @userAccount.nil?
  @userAccount['EmailAddress'] == email
end

#service_account?Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/octopus_serverspec_extensions/type/octopus_deploy_user.rb', line 34

def service_account?
  return false if @userAccount.nil?
  @userAccount['IsService'] == true
end