def _delete_app_user(self, userName):
     print "deleting user", userName
     user = None
     try:
         user = self.get_app_user_profile_by_username(userName)
     except Exception as e:
         print e
     print "this is the user fetched", user
     profile = CGObject(self.app_user_profiles_string)
     if user:
         profile.set("id", user["id"])
         profile.delete(self.provider)
    def _create_new_user_profile(self, user_data):
        profile = CGObject(self.user_profiles_string)

        for pair in user_data.iteritems():
            if(pair[0] == "password"):
                continue

            profile.set(pair[0], pair[1])

        profile.set("isDeleted", False)
        profile.save(self.provider)
        return profile.to_dict()
def save_object(provider, className, counter, total_requests, total_failures):
    try:
        start_time = datetime.now()
        print "save",counter
        save_obj = CGObject(className)
        save_obj.set("key"+str(counter), "value"+str(counter))
        total_requests += 1
        save_obj.save(provider)
        print "time for request",(datetime.now() - start_time)
        # time.sleep(1)
        print "saved object"
        print "\n"
        print "total REQUESTS of instance: ",total_requests
        print "total FAILURES of instance: ", total_failures
        print "\n\n"
        return (total_requests, total_failures)
    except Exception as e:
        total_failures += 1
        print e, total_failures
        print "\n"
        print "total REQUESTS of instance: ",total_requests
        print "total FAILURES of instance: ", total_failures
        print "\n\n"
        return (total_requests, total_failures)
    def _create_app_user(self, clientId, tenantId, user_data, code):
        profile = CGObject("%s" % self.app_user_profiles_string)
        # profile.set("app_user_name", user_name)
        # this is temporary, here using this id to fetch the name of the tenant.
        profile.set("tenantId", tenantId)
        profile.set("clientId", clientId)
        profile.set("isDeleted", False)

        for key in user_data:
            if key == "password":
                continue
            profile.set(key, user_data[key])

        profile.save(self.provider)
        if not profile.get("id"):
            print "usre profile creation error"
            raise Exception("Error in user creation")
        profile.set("activation_code", code)
        return json.loads(profile.to_json())
    def create_new_cg_app_tenant_profile(self, clientId, data):
        print "inside create new tenant"
        if not clientId:
            print "no app id"
            raise Exception("App id not specified")
        if "tenantName" not in data:
            print "no tenant name"
            raise Exception("required fields not mentioned")
        if not data["tenantName"]:
            print "no tenant name"
            raise Exception("Invalid tenant name or id")
        print "creating tenant profile"
        r = self.authManager._create_app_tenant(clientId, data["tenantName"])
        print r.status_code
        if r.status_code >= 200 and r.status_code <=299:
            profile = CGObject(self.tenant_profiles_string)

            for field in data:
                profile.set(field, data[field])

            profile.set("clientId", clientId)
            profile.save(self.provider)
            if not profile.get("id"):
                print "tenant profile creation error"
                raise Exception("Error in tenant creation")
            print "profile created", json.loads(profile.to_json())
            r = self.authManager._activate_app_tenant(clientId, data["tenantName"])
            if r.status_code >= 200 and r.status_code <=299:
                return json.loads(profile.to_json())
            else:
                profile.delete(self.provider)
        print r.content
        return r.content