go.temporal.io/server@v1.23.0/common/rpc/interceptor/namespace.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package interceptor
    26  
    27  import (
    28  	"fmt"
    29  
    30  	"go.temporal.io/api/serviceerror"
    31  	"go.temporal.io/api/workflowservice/v1"
    32  	"go.temporal.io/server/common/namespace"
    33  )
    34  
    35  // gRPC method request must implement either NamespaceNameGetter or NamespaceIDGetter
    36  // for namespace specific metrics to be reported properly
    37  type (
    38  	NamespaceNameGetter interface {
    39  		GetNamespace() string
    40  	}
    41  
    42  	NamespaceIDGetter interface {
    43  		GetNamespaceId() string
    44  	}
    45  )
    46  
    47  // MustGetNamespaceName returns request namespace name
    48  // or EmptyName if there's error when retriving namespace name,
    49  // e.g. unable to find namespace
    50  func MustGetNamespaceName(
    51  	namespaceRegistry namespace.Registry,
    52  	req interface{},
    53  ) namespace.Name {
    54  	namespaceName, err := GetNamespaceName(namespaceRegistry, req)
    55  	if err != nil {
    56  		return namespace.EmptyName
    57  	}
    58  	return namespaceName
    59  }
    60  
    61  func GetNamespaceName(
    62  	namespaceRegistry namespace.Registry,
    63  	req interface{},
    64  ) (namespace.Name, error) {
    65  	switch request := req.(type) {
    66  	case *workflowservice.RegisterNamespaceRequest:
    67  		// For namespace registration requests, we don't expect to find namespace so skip checking caches
    68  		// to avoid caching a NotFound error from persistence readthrough
    69  		return namespace.Name(request.GetNamespace()), nil
    70  	case NamespaceNameGetter:
    71  		namespaceName := namespace.Name(request.GetNamespace())
    72  		_, err := namespaceRegistry.GetNamespace(namespaceName)
    73  		if err != nil {
    74  			return namespace.EmptyName, err
    75  		}
    76  		return namespaceName, nil
    77  
    78  	case NamespaceIDGetter:
    79  		namespaceID := namespace.ID(request.GetNamespaceId())
    80  		namespaceName, err := namespaceRegistry.GetNamespaceName(namespaceID)
    81  		if err != nil {
    82  			return namespace.EmptyName, err
    83  		}
    84  		return namespaceName, nil
    85  
    86  	default:
    87  		return namespace.EmptyName, serviceerror.NewInternal(fmt.Sprintf("unable to extract namespace info from request: %+v", req))
    88  	}
    89  }