Class: LUSI::API::ServiceAccount

Inherits:
Object
  • Object
show all
Defined in:
lib/lusi_api/service_account.rb

Overview

Represents a service account used to access the LUSI API

Constant Summary collapse

PASSWORD_CHARS =

The characters used to generate a random service account password

'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!-_=+,.'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml = nil, lookup = nil, username: nil, description: nil, contact_name: nil, contact_email: nil, expiry_date: DateTime, institutions: nil, service_methods: nil) ⇒ void

Creates a new ServiceAccount instance Fields are extracted from the parsed XML response to the LUSI GetServiceAccountDetails API call. Default values for fields missing from the XML can be specified as keyword arguments.

Parameters:

  • xml (Nokogiri::XML::Node, nil) (defaults to: nil)

    the XML response from the LUSI GetServiceAccountDetails API call, or nil

  • lookup (LUSI::API::Core::Lookup::LookupService, nil) (defaults to: nil)

    the lookup service for object resolution

  • username (String, nil) (defaults to: nil)

    the default username

  • description (String, nil) (defaults to: nil)

    the default description

  • contact_name (String, nil) (defaults to: nil)

    the default contact name

  • contact_email (Stirng, nil) (defaults to: nil)

    the default contact email

  • expiry_date (DateTime, nil) (defaults to: DateTime)

    the default expiry date

  • institutions (Array<LUSI::API::Institution>) (defaults to: nil)

    the default institution list

  • service_methods (Array<LUSI::API::ServiceMethod>) (defaults to: nil)

    the default service method list



73
74
75
76
77
78
79
80
81
82
# File 'lib/lusi_api/service_account.rb', line 73

def initialize(xml = nil, lookup = nil, username: nil, description: nil, contact_name: nil, contact_email: nil,
              expiry_date: DateTime, institutions: nil, service_methods: nil)
  @contact_email = LUSI::API::Core::XML.xml_content_at(xml, 'xmlns:ContactEmail', contact_email)
  @contact_name = LUSI::API::Core::XML.xml_content_at(xml, 'xmlns:ContactName', contact_name)
  @description = LUSI::API::Core::XML.xml_content_at(xml, 'xmlns:Description', description)
  @expiry_date = LUSI::API::Core::XML.xml_datetime_at(xml, 'xmlns:ExpiryDate', expiry_date)
  @institutions = LUSI::API::Core::XML.xml(xml, 'xmlns:Institutions', institutions).map { |i| LUSI::API::Core::XML.lookup(i, lookup, :institution, 'xmlns:Institution') }
  @service_methods = LUSI::API::Core::XML.xml(xml, 'xmlns:ServiceMethods/xmlns:ServiceMethod', service_methods).map { |m| LUSI::API::ServiceMethod.new(m, lookup) }
  @username = LUSI::API::Core::XML.xml_content_at('xmlns:Username')
end

Instance Attribute Details

#contact_emailString?

Returns the email address of the service account owner.

Returns:

  • (String, nil)

    the email address of the service account owner



15
16
17
# File 'lib/lusi_api/service_account.rb', line 15

def contact_email
  @contact_email
end

#contact_nameString?

Returns the full name of the service account owner.

Returns:

  • (String, nil)

    the full name of the service account owner



19
20
21
# File 'lib/lusi_api/service_account.rb', line 19

def contact_name
  @contact_name
end

#descriptionString?

Returns the description of the service account.

Returns:

  • (String, nil)

    the description of the service account



23
24
25
# File 'lib/lusi_api/service_account.rb', line 23

def description
  @description
end

#expiry_dateDateTime?

Returns the expiry date of the service account password.

Returns:

  • (DateTime, nil)

    the expiry date of the service account password



27
28
29
# File 'lib/lusi_api/service_account.rb', line 27

def expiry_date
  @expiry_date
end

#institutionsArray<LUSI::API::Organisation::Unit>?

Returns an array of institutions associated with this account.

Returns:



31
32
33
# File 'lib/lusi_api/service_account.rb', line 31

def institutions
  @institutions
end

#service_methodsArray<LUSI::API::ServiceMethod>?

Returns an array of ServiceMethod instances callable from this account.

Returns:



35
36
37
# File 'lib/lusi_api/service_account.rb', line 35

def service_methods
  @service_methods
end

#usernameString?

Returns the service account username.

Returns:

  • (String, nil)

    the service account username



39
40
41
# File 'lib/lusi_api/service_account.rb', line 39

def username
  @username
end

Class Method Details

.generate_service_account_passwordString

Generates a 16-character random password for the service account

Returns:

  • (String)

    the generated password



92
93
94
95
96
97
# File 'lib/lusi_api/service_account.rb', line 92

def self.
  password = []
  random = Random::new
  (0..16).each { |i| password.push(PASSWORD_CHARS[random.rand(PASSWORD_CHARS.length)]) }
  password.join('')
end

.get_instance(api, lookup = nil) {|obj| ... } ⇒ Array<LUSI::API::ServiceAccount>

Return a ServiceAccount instance for the API’s service user

Parameters:

Yields:

  • (obj)

    Passes the ServiceAccount instance to the block

Yield Parameters:

Returns:

Raises:



51
52
53
54
55
56
57
58
# File 'lib/lusi_api/service_account.rb', line 51

def self.get_instance(api, lookup = nil)
  xml = api.call('LUSIReference', 'General.asmx', 'GetServiceAccountDetails')
  LUSI::API::Core::XML::xml(xml, 'xmlns:ServiceAccount') do |s|
    obj = LUSI::API::ServiceAccount.new(s, lookup)
    yield(obj) if block_given?
    obj
  end
end

.update_service_account_password(api, password = nil) ⇒ String

Sets a new password on the service account

Parameters:

  • api (LUSI::API::Core::API)

    the LUSI API configured with the service account to be updated

  • password (String, nil) (defaults to: nil)

    the new password (if nil, a 16-digit random password will be generated)

Returns:

  • (String)

    the new password

Raises:



104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/lusi_api/service_account.rb', line 104

def self.(api, password = nil)
  
  # Generate a password if required
  password ||= 
  
  # Update the password
  xml = api.call('LUSIReference', 'General.asmx', 'UpdateServiceAccountPassword', NewPassword: password)
  
  # Return the new password
  password
  
end

Instance Method Details

#to_sString

Returns a String representation of the ServiceAccount instance (the service username)

Returns:

  • (String)

    the string representation of the ServiceAccount instance



86
87
88
# File 'lib/lusi_api/service_account.rb', line 86

def to_s
  @username
end