github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/orchestrator/subscriptions.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package orchestrator
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/kaleido-io/firefly/internal/i18n"
    23  	"github.com/kaleido-io/firefly/pkg/database"
    24  	"github.com/kaleido-io/firefly/pkg/fftypes"
    25  )
    26  
    27  func (or *orchestrator) CreateSubscription(ctx context.Context, ns string, subDef *fftypes.Subscription) (*fftypes.Subscription, error) {
    28  	subDef.ID = fftypes.NewUUID()
    29  	subDef.Created = fftypes.Now()
    30  	subDef.Namespace = ns
    31  	subDef.Ephemeral = false
    32  	if err := or.data.VerifyNamespaceExists(ctx, subDef.Namespace); err != nil {
    33  		return nil, err
    34  	}
    35  	if err := fftypes.ValidateFFNameField(ctx, subDef.Name, "name"); err != nil {
    36  		return nil, err
    37  	}
    38  	return subDef, or.events.CreateDurableSubscription(ctx, subDef)
    39  }
    40  
    41  func (or *orchestrator) DeleteSubscription(ctx context.Context, ns, id string) error {
    42  	u, err := fftypes.ParseUUID(ctx, id)
    43  	if err != nil {
    44  		return err
    45  	}
    46  	sub, err := or.database.GetSubscriptionByID(ctx, u)
    47  	if err != nil {
    48  		return err
    49  	}
    50  	if sub == nil || sub.Namespace != ns {
    51  		return i18n.NewError(ctx, i18n.Msg404NotFound)
    52  	}
    53  	return or.events.DeleteDurableSubscription(ctx, sub)
    54  }
    55  
    56  func (or *orchestrator) GetSubscriptions(ctx context.Context, ns string, filter database.AndFilter) ([]*fftypes.Subscription, error) {
    57  	filter = or.scopeNS(ns, filter)
    58  	return or.database.GetSubscriptions(ctx, filter)
    59  }
    60  
    61  func (or *orchestrator) GetSubscriptionByID(ctx context.Context, ns, id string) (*fftypes.Subscription, error) {
    62  	u, err := or.verifyIDAndNamespace(ctx, ns, id)
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  	return or.database.GetSubscriptionByID(ctx, u)
    67  }