github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/pkg/fftypes/namespace.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 fftypes
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  
    23  	"github.com/kaleido-io/firefly/internal/i18n"
    24  )
    25  
    26  // NamespaceType describes when the namespace was created from local configuration, or broadcast through the network
    27  type NamespaceType = LowerCasedType
    28  
    29  const (
    30  	// NamespaceTypeLocal is a namespace that only exists because it was defined in the local configuration of the node
    31  	NamespaceTypeLocal NamespaceType = "local"
    32  	// NamespaceTypeBroadcast is a namespace that was broadcast through the network. Broadcast namespaces can overwrite a local namespace
    33  	NamespaceTypeBroadcast NamespaceType = "broadcast"
    34  	// NamespaceTypeSystem is a reserved namespace used by FireFly itself
    35  	NamespaceTypeSystem NamespaceType = "system"
    36  )
    37  
    38  // Namespace is a isolate set of named resources, to allow multiple applications to co-exist in the same network, with the same named objects.
    39  // Can be used for use case segregation, or multi-tenancy.
    40  type Namespace struct {
    41  	ID          *UUID         `json:"id"`
    42  	Message     *UUID         `json:"message,omitempty"`
    43  	Name        string        `json:"name"`
    44  	Description string        `json:"description"`
    45  	Type        NamespaceType `json:"type"`
    46  	Created     *FFTime       `json:"created"`
    47  }
    48  
    49  func (ns *Namespace) Validate(ctx context.Context, existing bool) (err error) {
    50  	if err = ValidateFFNameField(ctx, ns.Name, "name"); err != nil {
    51  		return err
    52  	}
    53  	if err = ValidateLength(ctx, ns.Description, "description", 4096); err != nil {
    54  		return err
    55  	}
    56  	if existing {
    57  		if ns.ID == nil {
    58  			return i18n.NewError(ctx, i18n.MsgNilID)
    59  		}
    60  	}
    61  	return nil
    62  }
    63  
    64  func namespaceTopic(ns string) string {
    65  	return fmt.Sprintf("ff_ns_%s", ns)
    66  }
    67  
    68  func (ns *Namespace) Topic() string {
    69  	return namespaceTopic(ns.Name)
    70  }
    71  
    72  func (ns *Namespace) SetBroadcastMessage(msgID *UUID) {
    73  	ns.Message = msgID
    74  }