Statistics
| Branch: | Tag: | Revision:

one / src / oca / ruby / OpenNebula / User.rb @ 61120649

History | View | Annotate | Download (4.27 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
            :chgrp    => "user.chgrp"
30
        }
31

    
32
        SELF = -1
33

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

    
48
            XMLElement.build_xml(user_xml, 'USER')
49
        end
50

    
51
        # ---------------------------------------------------------------------
52
        # Class constructor
53
        # ---------------------------------------------------------------------
54
        def initialize(xml, client)
55
            super(xml,client)
56

    
57
            @client = client
58
        end
59

    
60
        # ---------------------------------------------------------------------
61
        # XML-RPC Methods for the User Object
62
        # ---------------------------------------------------------------------
63

    
64
        # Retrieves the information of the given User.
65
        def info()
66
            super(USER_METHODS[:info], 'USER')
67
        end
68

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

    
78
        # Deletes the User
79
        def delete()
80
            super(USER_METHODS[:delete])
81
        end
82

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

    
89
            rc = @client.call(USER_METHODS[:passwd], @pe_id, password)
90
            rc = nil if !OpenNebula.is_error?(rc)
91

    
92
            return rc
93
        end
94

    
95
        # Changes the main group
96
        # gid:: _Integer_ the new group id. Set to -1 to leave the current one
97
        # [return] nil in case of success or an Error object
98
        def chgrp(gid)
99
            return Error.new('ID not defined') if !@pe_id
100

    
101
            rc = @client.call(USER_METHODS[:chgrp],@pe_id, gid)
102
            rc = nil if !OpenNebula.is_error?(rc)
103

    
104
            return rc
105
        end
106

    
107
        # ---------------------------------------------------------------------
108
        # Helpers to get User information
109
        # ---------------------------------------------------------------------
110

    
111
        # Returns the group identifier
112
        # [return] _Integer_ the element's group ID
113
        def gid
114
            self['GID'].to_i
115
        end
116
    end
117
end