remove validateCase trigger, cosmosdb doesn't multiple trigger invocations per API action

This commit is contained in:
Jim Minter 2019-11-28 12:22:15 -06:00
Родитель 4b3f8f9c34
Коммит 3b63cbc156
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 0730CBDA10D1A2D3
2 изменённых файлов: 23 добавлений и 35 удалений

Просмотреть файл

@ -46,19 +46,6 @@ func NewOpenShiftClusters(ctx context.Context, uuid uuid.UUID, dbc cosmosdb.Data
var date = new Date(); var date = new Date();
body["leaseExpires"] = Math.floor(date.getTime() / 1000) + 60; body["leaseExpires"] = Math.floor(date.getTime() / 1000) + 60;
request.setBody(body); request.setBody(body);
}`,
},
{
ID: "validateCase",
TriggerOperation: cosmosdb.TriggerOperationAll,
TriggerType: cosmosdb.TriggerTypePre,
Body: `function trigger() {
var request = getContext().getRequest();
var body = request.getBody();
if(body["openShiftCluster"]["key"] != body["openShiftCluster"]["key"].toLowerCase())
throw "openShiftCluster.key is not lower case";
if(body["partitionKey"] != body["partitionKey"].toLowerCase())
throw "partitionKey is not lower case";
}`, }`,
}, },
} }
@ -78,13 +65,17 @@ func NewOpenShiftClusters(ctx context.Context, uuid uuid.UUID, dbc cosmosdb.Data
} }
func (c *openShiftClusters) Create(doc *api.OpenShiftClusterDocument) (*api.OpenShiftClusterDocument, error) { func (c *openShiftClusters) Create(doc *api.OpenShiftClusterDocument) (*api.OpenShiftClusterDocument, error) {
if string(doc.OpenShiftCluster.Key) != strings.ToLower(string(doc.OpenShiftCluster.Key)) {
return nil, fmt.Errorf("key %q is not lower case", doc.OpenShiftCluster.Key)
}
var err error var err error
doc.PartitionKey, err = c.partitionKey(doc.OpenShiftCluster.Key) doc.PartitionKey, err = c.partitionKey(doc.OpenShiftCluster.Key)
if err != nil { if err != nil {
return nil, err return nil, err
} }
doc, err = c.c.Create(doc.PartitionKey, doc, &cosmosdb.Options{PreTriggers: []string{"validateCase"}}) doc, err = c.c.Create(doc.PartitionKey, doc, nil)
if err, ok := err.(*cosmosdb.Error); ok && err.StatusCode == http.StatusConflict { if err, ok := err.(*cosmosdb.Error); ok && err.StatusCode == http.StatusConflict {
err.StatusCode = http.StatusPreconditionFailed err.StatusCode = http.StatusPreconditionFailed
@ -156,16 +147,18 @@ func (c *openShiftClusters) Update(doc *api.OpenShiftClusterDocument) (*api.Open
} }
func (c *openShiftClusters) update(doc *api.OpenShiftClusterDocument, options *cosmosdb.Options) (*api.OpenShiftClusterDocument, error) { func (c *openShiftClusters) update(doc *api.OpenShiftClusterDocument, options *cosmosdb.Options) (*api.OpenShiftClusterDocument, error) {
if options == nil { if string(doc.OpenShiftCluster.Key) != strings.ToLower(string(doc.OpenShiftCluster.Key)) {
options = &cosmosdb.Options{} return nil, fmt.Errorf("key %q is not lower case", doc.OpenShiftCluster.Key)
} }
options.PreTriggers = append(options.PreTriggers, "validateCase")
return c.c.Replace(doc.PartitionKey, doc, options) return c.c.Replace(doc.PartitionKey, doc, options)
} }
func (c *openShiftClusters) Delete(doc *api.OpenShiftClusterDocument) error { func (c *openShiftClusters) Delete(doc *api.OpenShiftClusterDocument) error {
if string(doc.OpenShiftCluster.Key) != strings.ToLower(string(doc.OpenShiftCluster.Key)) {
return fmt.Errorf("key %q is not lower case", doc.OpenShiftCluster.Key)
}
return c.c.Delete(doc.PartitionKey, doc, &cosmosdb.Options{NoETag: true}) return c.c.Delete(doc.PartitionKey, doc, &cosmosdb.Options{NoETag: true})
} }

Просмотреть файл

@ -56,17 +56,6 @@ func NewSubscriptions(ctx context.Context, uuid uuid.UUID, dbc cosmosdb.Database
var date = new Date(); var date = new Date();
body["leaseExpires"] = Math.floor(date.getTime() / 1000) + 600; body["leaseExpires"] = Math.floor(date.getTime() / 1000) + 600;
request.setBody(body); request.setBody(body);
}`,
},
{
ID: "validateCase",
TriggerOperation: cosmosdb.TriggerOperationAll,
TriggerType: cosmosdb.TriggerTypePre,
Body: `function trigger() {
var request = getContext().getRequest();
var body = request.getBody();
if(body["key"] != body["key"].toLowerCase())
throw "key is not lower case";
}`, }`,
}, },
} }
@ -86,7 +75,11 @@ func NewSubscriptions(ctx context.Context, uuid uuid.UUID, dbc cosmosdb.Database
} }
func (c *subscriptions) Create(doc *api.SubscriptionDocument) (*api.SubscriptionDocument, error) { func (c *subscriptions) Create(doc *api.SubscriptionDocument) (*api.SubscriptionDocument, error) {
doc, err := c.c.Create(string(doc.Key), doc, &cosmosdb.Options{PreTriggers: []string{"validateCase"}}) if string(doc.Key) != strings.ToLower(string(doc.Key)) {
return nil, fmt.Errorf("key %q is not lower case", doc.Key)
}
doc, err := c.c.Create(string(doc.Key), doc, nil)
if err, ok := err.(*cosmosdb.Error); ok && err.StatusCode == http.StatusConflict { if err, ok := err.(*cosmosdb.Error); ok && err.StatusCode == http.StatusConflict {
err.StatusCode = http.StatusPreconditionFailed err.StatusCode = http.StatusPreconditionFailed
@ -101,7 +94,7 @@ func (c *subscriptions) Get(key api.Key) (*api.SubscriptionDocument, error) {
} }
docs, err := c.c.QueryAll(string(key), &cosmosdb.Query{ docs, err := c.c.QueryAll(string(key), &cosmosdb.Query{
Query: "SELECT * FROM SubscriptionDocuments doc WHERE doc.subscription.key = @key", Query: "SELECT * FROM SubscriptionDocuments doc WHERE doc.key = @key",
Parameters: []cosmosdb.Parameter{ Parameters: []cosmosdb.Parameter{
{ {
Name: "@key", Name: "@key",
@ -153,16 +146,18 @@ func (c *subscriptions) Update(doc *api.SubscriptionDocument) (*api.Subscription
} }
func (c *subscriptions) update(doc *api.SubscriptionDocument, options *cosmosdb.Options) (*api.SubscriptionDocument, error) { func (c *subscriptions) update(doc *api.SubscriptionDocument, options *cosmosdb.Options) (*api.SubscriptionDocument, error) {
if options == nil { if string(doc.Key) != strings.ToLower(string(doc.Key)) {
options = &cosmosdb.Options{} return nil, fmt.Errorf("key %q is not lower case", doc.Key)
} }
options.PreTriggers = append(options.PreTriggers, "validateCase")
return c.c.Replace(string(doc.Key), doc, options) return c.c.Replace(string(doc.Key), doc, options)
} }
func (c *subscriptions) Delete(doc *api.SubscriptionDocument) error { func (c *subscriptions) Delete(doc *api.SubscriptionDocument) error {
if string(doc.Key) != strings.ToLower(string(doc.Key)) {
return fmt.Errorf("key %q is not lower case", doc.Key)
}
return c.c.Delete(string(doc.Key), doc, &cosmosdb.Options{NoETag: true}) return c.c.Delete(string(doc.Key), doc, &cosmosdb.Options{NoETag: true})
} }