Revision 92c22a86 test/pgerakios.py

b/test/pgerakios.py
8 8
import inspect
9 9

  
10 10

  
11
init_logger_stderr('mylogger', level='INFO')
11
#init_logger_stderr('mylogger', level='INFO')
12 12

  
13 13
def environ_get(key, default_value=''):
14 14
    if os.environ.has_key(key):
......
26 26
def printf(fmt, *args):
27 27
    global QH_DEBUG
28 28
    if(QH_DEBUG):
29
        print(fmt.format(*args))
29
        sys.stderr.write(fmt.format(*args) + "\n")
30
#        print(fmt.format(*args))
30 31
    return 0
31 32

  
32 33
def exn(fmt, *args):
......
134 135
        p = Policy.policies.get(name)
135 136
        if(p == None):
136 137
            p = Policy.newDummy()
138
            p.policyName = name
137 139
            Policy.policies[name] = p
138 140
        return p
139 141

  
......
148 150
    def splitRejected(policyList, rejectedList):
149 151
        (a, b) = split((lambda p: p.policyName not in rejectedList), policyList)
150 152
        if(b != []):
151
            printf("Rejected entities (call to set_limits): {0}", rejectedList)
153
            printf("Rejected entities (call to set_limits!!): {0}", [p.policyName for p in b])
152 154
        return (a, b)
153 155

  
154 156
    @staticmethod
......
162 164
    @staticmethod
163 165
    def saveMany(policyList):
164 166
        inputList = [(p.policyName, p.quantity, p.capacity, p.importLimit, p.exportLimit)
165
                     for p in policyList if(p.isValid() and not p.isDummy())]
167
                     for p in policyList if(not p.isDummy())]
166 168
        if(inputList != []):
169
            printf("set_limits input list is : {0}",inputList)
167 170
            rejectedList = Policy.con().set_limits(context=Policy.Context, set_limits=inputList)
168 171
        else:
169
            rejectedList = policyList
172
            printf("EMPTY INPUT LIST")
173
            for p in policyList:
174
                printf("Policy list item {0} valid={1} dummy={2}",p.policyName,p.isValid(),p.isDummy())
175
            rejectedList = inputList
170 176
        ok, notok = Policy.splitRejected(policyList, rejectedList)
171 177
        for p in ok:
172 178
            p.exist = Policy.PolicyState.EXISTS
173 179
        for p in notok:
174
            if(p.exist == Policy.EXISTS):
180
            if(p.exist == Policy.PolicyState.EXISTS):
175 181
                p.exist = Policy.PolicyState.NOT_EXISTS
176 182
        return notok
177 183

  
......
193 199
                p.exist = Policy.PolicyState.NOT_EXISTS
194 200

  
195 201
        return notok
202
    
203
    def __str__(self):
204
        return "Policy({0})".format(self.policyName)
196 205

  
197 206
    def reset(self):
198 207
        self.set(None, 0, 0, 0, 0)
......
293 302
            oList1 = []
294 303
        else:
295 304
            oList1 = Resource.con().get_quota(context=Resource.Context, get_quota=iList1)
296
            for e, res, q, c, il, el, p, i, e, r1, r2, f in oList1:
305
            for e, res, q, c, il, el, i, e, r1, r2, f in oList1:
297 306
                res1 = find((lambda r: r.resourceName == res), rl1)
298 307
                res1.imported = i
299 308
                res1.exported = e
......
337 346
            ol1 = []
338 347
        else:
339 348
            il1 = [(r.entity.entityName, r.resourceName, r.entity.entityKey,r.policy.quantity,
340
                    r.policy.capacity,r.policy.importLimit,r.policy.ExportLimit,Resource.Flags) for r in rl1]
349
                    r.policy.capacity,r.policy.importLimit,r.policy.exportLimit,Resource.Flags) for r in rl1]
341 350
            ol1 = Resource.con().set_quota(context=Resource.Context,set_quota=il1)
342 351

  
343 352
        if(rl2 == []):
344 353
            ol2 = []
345 354
        else:
346
            il2 = [(r.entity.entityName,r.resourceName,r.entity.entityKey,r.policy.policyName) for r in rl2]
355
            il2 = [(r.entity.entityName,r.resourceName,r.entity.entityKey,r.policy.policyName,Resource.Flags) for r in rl2]
347 356
            ol2 = Resource.con().set_holding(context=Resource.Context,set_holding=il2)
348 357

  
358
        if(ol2 != []):
359
            exn("Rejected ol2 = {0}",ol2)
360
            
349 361
        rejectedList = []
350 362

  
351 363

  
......
805 817
    
806 818
    def set(self, name, password, parent):
807 819
        super(ResourceHolder,self).set(name, password, parent)
820
        # load policies for users
821
        self.userResourcePolicies = {}
822
        if(name != None):
823
            self.loadUserResourcePolicies()
808 824
        return self
809

  
825
    
810 826
    def __init__(self):
811 827
        #super(ResourceHolder,self).__init__()
812 828
        ResourceHolder.__init__(self)
......
817 833

  
818 834
        # load policies for groups
819 835
        self.loadGroupResourcePolicies()
820
        # load policies for users
821
        self.loadUserResourcePolicies()
822 836

  
823 837
    def loadGroupResourcePolicies(self):
824 838
        for r in self.getResources():
......
828 842
        for r in self.resourceNames:
829 843
            self.userResourcePolicies[r] = Policy.get("{0}.{1}".format(self.entityName,r))
830 844

  
831

  
832 845
    def getUserPolicyFor(self,resourceName):
833 846
        return self.userResourcePolicies[resourceName]
834 847

  
......
857 870
        for name,quantity in kwargs.items():
858 871
            r = self.getResource(name)
859 872
            r.policy.quantity = quantity
873
            r.policy.setDummy(False)
874
            r.policy.policyName =  "group."  + self.entityName + "." + name 
860 875
            policies.append(r.policy)
876
            
861 877
        Policy.saveMany(policies)
862 878
        
863 879
    def saveUserPolicyQuantities(self,**kwargs):
864 880
        policies = []
865 881
        for name,quantity in kwargs.items():
866 882
            p = self.getUserPolicyFor(name)
883

  
867 884
            p.quantity = quantity
885
            p.setDummy(False)            
886
            #p.policyName =  "group."  + self.entityName             
868 887
            policies.append(p)
869
        Policy.saveMany(policies)
888
        b = Policy.saveMany(policies)
889
        for p in policies:
890
            p.setDummy(True)
891
        return b
870 892

  
871 893

  
872 894
class User(ResourceHolder):
......
935 957
        self.groups.append(group)
936 958
        group.users.append(self)
937 959
        #
960
        rlist = []
938 961
        for r in self.getResources():
939 962
            groupUserPolicy = group.getUserPolicyFor(r.resourceName)
940
            #printf("join group ==> Resource entity: {0}",r.entity.entityName)
963
            printf("join group ==> Resource entity: {0} with name {1}",r.entity.entityName,groupUserPolicy.name())
964
            #
941 965
            self.commission.addResource(group.getResource(r.resourceName),groupUserPolicy.quantity)
966
            #
967
            r.setPolicy(groupUserPolicy)
968
            rlist.append(r)
969
        #
970
        Resource.saveMany(rlist,False)
942 971
        #DO NOT COMMIT HERE
943 972
        # self.commit !!! no
944 973

  
......
946 975
# Main program
947 976

  
948 977
try:
949

  
978
    #
950 979
    # Group1
951 980
    printf("Step 1")
952 981
    group1 = Group.get("group1","group1")
......
966 995

  
967 996
    printf("Step 2  name : {0}",group1.entityName)
968 997
    #["pithos","cyclades.vm","cyclades.cpu","cyclades.mem"]
969
    group1.saveGroupPolicyQuantities(pithos=10,cyclades_vm=8,cyclades_mem=9)
970
    group1.saveUserPolicyQuantities(pithos=2,cyclades_vm=2,cyclades_mem=1)
998
    group1.saveGroupPolicyQuantities(pithos=10,cyclades_vm=8,cyclades_mem=9,cyclades_cpu=12)
999
    group1.saveUserPolicyQuantities(pithos=2,cyclades_vm=2,cyclades_mem=1,cyclades_cpu=3)
971 1000

  
972 1001
    printf("Group1 resources BEGIN")
973 1002
    for r in group1.getResources(True):

Also available in: Unified diff