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 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_insert.format(cf=self.config_table, name=":scaling"), _cql_insert.format(cf=self.launch_table, name=":launch"), _cql_create_group_state.format(cf=self.state_table)] data = {"tenantId": tenant_id, "groupId": scaling_group_id, "scaling": serialize_json_data(config, 1), "launch": serialize_json_data(launch, 1), } 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 _build_webhooks(bare_webhooks, webhooks_table, queries, cql_parameters, output): """ Because inserting many values into a table with compound keys with one insert statement is hard. This builds a bunch of insert statements and a dictionary matching different parameter names to different policies. :param bare_webhooks: a list of webhook data without ID or webhook keys, or any generated capability hash info :type bare_webhooks: ``list`` of ``dict`` :param webhooks_table: the name of the webhooks table :type webhooks_table: ``str`` :param queries: a list of existing CQL queries to add to :type queries: ``list`` of ``str`` :param cql_parameters: the dictionary of named parameters and values passed in addition to the query to execute the query - additional parameters will be added to this dictionary :type cql_paramters: ``dict`` :param output: a dictionary to which to insert the created policies along with their generated IDs :type output: ``dict`` """ for i, webhook in enumerate(bare_webhooks): name = "webhook{0}".format(i) webhook_id = generate_key_str('webhook') queries.append(_cql_insert_webhook.format(cf=webhooks_table, name=name)) # generate the real data that will be stored, which includes the webhook # token, the capability stuff, and metadata by default bare_webhooks[i].setdefault('metadata', {}) version, cap_hash = generate_capability() cql_parameters[name] = serialize_json_data(webhook, 1) cql_parameters['{0}Id'.format(name)] = webhook_id cql_parameters['{0}Key'.format(name)] = cap_hash cql_parameters['{0}Capability'.format(name)] = serialize_json_data( {version: cap_hash}, 1) output[webhook_id] = webhook.copy() output[webhook_id]['capability'] = { 'hash': cap_hash, 'version': version }
def _build_policies(policies, policies_table, event_table, queries, data, outpolicies): """ Because inserting many values into a table with compound keys with one insert statement is hard. This builds a bunch of insert statements and a dictionary matching different parameter names to different policies. :param policies: a list of policy data without ID :type policies: ``list`` of ``dict`` :param policies_table: the name of the policies table :type policies_table: ``str`` :param queries: a list of existing CQL queries to add to :type queries: ``list`` of ``str`` :param data: the dictionary of named parameters and values passed in addition to the query to execute the query :type data: ``dict`` :param outpolicies: a dictionary to which to insert the created policies along with their generated IDs :type outpolicies: ``dict`` """ if policies is not None: for i, policy in enumerate(policies): polname = "policy{}".format(i) polId = generate_key_str('policy') queries.append(_cql_insert_policy.format(cf=policies_table, name=':' + polname)) data[polname] = serialize_json_data(policy, 1) data[polname + "Id"] = polId if "type" in policy: if policy["type"] == 'schedule': queries.append(_cql_insert_event.format(cf=event_table, name=':' + polname)) if 'at' in policy["args"]: data[polname + "Trigger"] = iso8601.parse_date(policy["args"]["at"]) elif 'cron' in policy["args"]: # TODO #recurrence = Recurrence(cron=policy["args"]["cron"]) # Temporarily storing date in far future to not trigger this # This is done to pass unitgration/test_rest_cass_model tests data[polname + "Trigger"] = datetime(2037, 12, 31) outpolicies[polId] = policy
def _build_webhooks(bare_webhooks, webhooks_table, queries, cql_parameters): """ Because inserting many values into a table with compound keys with one insert statement is hard. This builds a bunch of insert statements and a dictionary matching different parameter names to different policies. :param bare_webhooks: a list of webhook data without ID or webhook keys, or any generated capability hash info :type bare_webhooks: ``list`` of ``dict`` :param webhooks_table: the name of the webhooks table :type webhooks_table: ``str`` :param queries: a list of existing CQL queries to add to :type queries: ``list`` of ``str`` :param cql_parameters: the dictionary of named parameters and values passed in addition to the query to execute the query - additional parameters will be added to this dictionary :type cql_paramters: ``dict`` :returns: ``list`` of the created webhooks along with their IDs """ output = [] for i, webhook in enumerate(bare_webhooks): name = "webhook{0}".format(i) webhook_id = generate_key_str('webhook') queries.append(_cql_insert_webhook.format(cf=webhooks_table, name=name)) # generate the real data that will be stored, which includes the webhook # token, the capability stuff, and metadata by default bare_webhooks[i].setdefault('metadata', {}) version, cap_hash = generate_capability() cql_parameters[name] = serialize_json_data(webhook, 1) cql_parameters['{0}Id'.format(name)] = webhook_id cql_parameters['{0}Key'.format(name)] = cap_hash cql_parameters['{0}Capability'.format(name)] = serialize_json_data( {version: cap_hash}, 1) output.append(dict(id=webhook_id, capability={'hash': cap_hash, 'version': version}, **webhook)) return 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 _build_policies(policies, policies_table, event_table, queries, data): """ Because inserting many values into a table with compound keys with one insert statement is hard. This builds a bunch of insert statements and a dictionary matching different parameter names to different policies. :param policies: a list of policy data without ID :type policies: ``list`` of ``dict`` :param policies_table: the name of the policies table :type policies_table: ``str`` :param queries: a list of existing CQL queries to add to :type queries: ``list`` of ``str`` :param data: the dictionary of named parameters and values passed in addition to the query to execute the query :type data: ``dict`` :returns: a ``list`` of the created policies along with their generated IDs """ outpolicies = [] if policies is not None: for i, policy in enumerate(policies): polname = "policy{}".format(i) polId = generate_key_str('policy') queries.append(_cql_insert_policy.format(cf=policies_table, name=':' + polname)) data[polname] = serialize_json_data(policy, 1) data[polname + "Id"] = polId if "type" in policy: if policy["type"] == 'schedule': _build_schedule_policy(policy, event_table, queries, data, polname) outpolicies.append(policy.copy()) outpolicies[-1]['id'] = polId return outpolicies
def _build_policies(policies, policies_table, event_table, queries, data, outpolicies): """ Because inserting many values into a table with compound keys with one insert statement is hard. This builds a bunch of insert statements and a dictionary matching different parameter names to different policies. :param policies: a list of policy data without ID :type policies: ``list`` of ``dict`` :param policies_table: the name of the policies table :type policies_table: ``str`` :param queries: a list of existing CQL queries to add to :type queries: ``list`` of ``str`` :param data: the dictionary of named parameters and values passed in addition to the query to execute the query :type data: ``dict`` :param outpolicies: a dictionary to which to insert the created policies along with their generated IDs :type outpolicies: ``dict`` """ if policies is not None: for i, policy in enumerate(policies): polname = "policy{}".format(i) polId = generate_key_str('policy') queries.append( _cql_insert_policy.format(cf=policies_table, name=':' + polname)) data[polname] = serialize_json_data(policy, 1) data[polname + "Id"] = polId if "type" in policy: if policy["type"] == 'schedule': _build_schedule_policy(policy, event_table, queries, data, polname) outpolicies[polId] = policy