Statistics
| Branch: | Tag: | Revision:

one / src / oca / ruby / OpenNebula / User.rb @ 6329d8b3

History | View | Annotate | Download (3.83 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
require 'OpenNebula/Pool'
18

    
19
module OpenNebula
20
    class User < PoolElement
21
        # ---------------------------------------------------------------------
22
        # Constants and Class Methods
23
        # ---------------------------------------------------------------------
24
        USER_METHODS = {
25
            :info     => "user.info",
26
            :allocate => "user.allocate",
27
            :delete   => "user.delete",
28
            :passwd   => "user.passwd"
29
        }
30

    
31
        # Creates a User description with just its identifier
32
        # this method should be used to create plain User objects.
33
        # +id+ the id of the user
34
        #
35
        # Example:
36
        #   user = User.new(User.build_xml(3),rpc_client)
37
        #
38
        def User.build_xml(pe_id=nil)
39
            if pe_id
40
                user_xml = "<USER><ID>#{pe_id}</ID></USER>"
41
            else
42
                user_xml = "<USER></USER>"
43
            end
44

    
45
            XMLElement.build_xml(user_xml, 'USER')
46
        end
47

    
48
        # ---------------------------------------------------------------------
49
        # Class constructor
50
        # ---------------------------------------------------------------------
51
        def initialize(xml, client)
52
            super(xml,client)
53

    
54
            @client = client
55
        end
56

    
57
        # ---------------------------------------------------------------------
58
        # XML-RPC Methods for the User Object
59
        # ---------------------------------------------------------------------
60
        
61
        # Retrieves the information of the given User.
62
        def info()
63
            super(USER_METHODS[:info], 'USER')
64
        end
65

    
66
        # Allocates a new User in OpenNebula
67
        #
68
        # +username+ Name of the new user.
69
        #
70
        # +password+ Password for the new user
71
        def allocate(username, password)
72
            super(USER_METHODS[:allocate], username, password)
73
        end
74

    
75
        # Deletes the User
76
        def delete()
77
            super(USER_METHODS[:delete])
78
        end
79

    
80
        # Changes the password of the given User
81
        #
82
        # +password+ String containing the new password
83
        def passwd(password)
84
            return Error.new('ID not defined') if !@pe_id
85

    
86
            rc = @client.call(USER_METHODS[:passwd], @pe_id, password)
87
            rc = nil if !OpenNebula.is_error?(rc)
88

    
89
            return rc
90
        end
91

    
92
        # ---------------------------------------------------------------------
93
        # Helpers to get User information
94
        # ---------------------------------------------------------------------
95

    
96
        # Returns the group identifier
97
        # [return] _Integer_ the element's group ID
98
        def gid
99
            self['GID'].to_i
100
        end
101

    
102
    end
103
end