def _insert_event(_): queries, data = [], {} data['tenantId'] = tenant_id data['groupId'] = group_id data['policyId'] = policy_id _build_schedule_policy(policy, event_table, queries, data, 'policy') return Batch(queries, data, get_consistency_level('update', 'event')).execute(connection)
def _do_update(_): queries, data = list(), dict() for i, event in enumerate(update_events): polname = 'policy{}'.format(i) queries.append( _cql_insert_event_batch.format(cf=self.event_table, name=':' + polname)) data.update({polname + key: event[key] for key in event}) b = Batch(queries, data, get_consistency_level('insert', 'event')) return b.execute(self.connection)
def test_batch_ts(self): """ Test a simple batch with timestamp set """ batch = Batch(['INSERT * INTO BLAH'], {}, timestamp=123) d = batch.execute(self.connection) self.successResultOf(d) expected = 'BEGIN BATCH USING TIMESTAMP 123' expected += ' INSERT * INTO BLAH APPLY BATCH;' self.connection.execute.assert_called_once_with( expected, {}, ConsistencyLevel.ONE)
def test_batch(self): """ Test a simple batch """ batch = Batch(['INSERT * INTO BLAH', 'INSERT * INTO BLOO'], {}) d = batch.execute(self.connection) self.successResultOf(d) expected = 'BEGIN BATCH INSERT * INTO BLAH' expected += ' INSERT * INTO BLOO APPLY BATCH;' self.connection.execute.assert_called_once_with( expected, {}, ConsistencyLevel.ONE)
def test_batch_consistency(self): """ Test a simple batch with consistency set """ batch = Batch(['INSERT * INTO BLAH'], {}, consistency=ConsistencyLevel.QUORUM) d = batch.execute(self.connection) self.successResultOf(d) expected = 'BEGIN BATCH' expected += ' INSERT * INTO BLAH APPLY BATCH;' self.connection.execute.assert_called_once_with( expected, {}, ConsistencyLevel.QUORUM)
def test_batch_param(self): """ Test a simple batch with params """ params = {"blah": "ff"} batch = Batch(['INSERT :blah INTO BLAH', 'INSERT * INTO BLOO'], params) d = batch.execute(self.connection) self.successResultOf(d) expected = 'BEGIN BATCH INSERT :blah INTO BLAH' expected += ' INSERT * INTO BLOO APPLY BATCH;' self.connection.execute.assert_called_once_with( expected, params, ConsistencyLevel.ONE)
def _do_delete(_): queries = [ _cql_delete_all_in_policy.format(cf=self.policies_table), _cql_delete_all_in_policy.format(cf=self.webhooks_table), _cql_delete_policy_events.format(cf=self.event_table) ] b = Batch(queries, { "tenantId": self.tenant_id, "groupId": self.uuid, "policyId": policy_id }, consistency=get_consistency_level('delete', 'policy')) return b.execute(self.connection)
def _do_update_policy(_): queries = [ _cql_update_policy.format(cf=self.policies_table, name=":policy") ] b = Batch(queries, { "tenantId": self.tenant_id, "groupId": self.uuid, "policyId": policy_id, "policy": serialize_json_data(data, 1) }, consistency=get_consistency_level('update', 'policy')) return b.execute(self.connection)
def _do_create_pol(lastRev): queries = [] cqldata = {"tenantId": self.tenant_id, "groupId": self.uuid} outpolicies = {} _build_policies(data, self.policies_table, self.event_table, queries, cqldata, outpolicies) b = Batch(queries, cqldata, consistency=get_consistency_level('create', 'policy')) d = b.execute(self.connection) return d.addCallback(lambda _: outpolicies)
def _do_update_config(lastRev): queries = [ _cql_update.format(cf=self.group_table, column='group_config', name=":scaling") ] b = Batch(queries, { "tenantId": self.tenant_id, "groupId": self.uuid, "scaling": serialize_json_data(data, 1) }, consistency=get_consistency_level('update', 'partial')) return b.execute(self.connection)
def _do_create(lastRev): queries = [] cql_params = { "tenantId": self.tenant_id, "groupId": self.uuid, "policyId": policy_id } output = {} _build_webhooks(data, self.webhooks_table, queries, cql_params, output) b = Batch(queries, cql_params, consistency=get_consistency_level('create', 'webhook')) d = b.execute(self.connection) return d.addCallback(lambda _: output)
def create_scaling_group(self, log, tenant_id, config, launch, policies=None): """ see :meth:`otter.models.interface.IScalingGroupCollection.create_scaling_group` """ scaling_group_id = generate_key_str('scalinggroup') log.bind( tenant_id=tenant_id, scaling_group_id=scaling_group_id).msg("Creating scaling group") queries = [_cql_create_group.format(cf=self.group_table)] data = { "tenantId": tenant_id, "groupId": scaling_group_id, "group_config": serialize_json_data(config, 1), "launch_config": serialize_json_data(launch, 1), "active": '{}', "pending": '{}', "policyTouched": '{}', "paused": False, "created_at": datetime.utcnow() } outpolicies = {} _build_policies(policies, self.policies_table, self.event_table, queries, data, outpolicies) b = Batch(queries, data, consistency=get_consistency_level('create', 'group')) d = b.execute(self.connection) d.addCallback( lambda _: { 'groupConfiguration': config, 'launchConfiguration': launch, 'scalingPolicies': outpolicies, 'id': scaling_group_id }) return d
def _delete_everything(policies): params = {'tenantId': self.tenant_id, 'groupId': self.uuid} queries = [ _cql_delete_all_in_group.format(cf=table) for table in (self.group_table, self.policies_table, self.webhooks_table) ] if len(policies) > 0: events_query, events_params = _delete_many_query_and_params( self.event_table, '"policyId"', policies.keys()) queries.append(events_query) params.update(events_params) b = Batch(queries, params, consistency=get_consistency_level('delete', 'group')) return b.execute(self.connection)