mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 21:35:52 +00:00
105ebfdd00
This change adds ACL hooks to the remaining catalog and mesh resources, excluding any computed ones. Those will for now continue using the default operator:x permissions. It refactors a lot of the common testing functions so that they can be re-used between resources. There are also some types that we don't yet support (e.g. virtual IPs) that this change adds ACL hooks to for future-proofing.
53 lines
1.4 KiB
Go
53 lines
1.4 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package resourcetest
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/hashicorp/consul/internal/resource"
|
|
"github.com/hashicorp/consul/proto-public/pbresource"
|
|
)
|
|
|
|
// Tenancy constructs a pbresource.Tenancy from a concise string representation
|
|
// suitable for use in unit tests.
|
|
//
|
|
// - "" : partition="" namespace="" peerName="local"
|
|
// - "foo" : partition="foo" namespace="" peerName="local"
|
|
// - "foo.bar" : partition="foo" namespace="bar" peerName="local"
|
|
// - <others> : partition="BAD" namespace="BAD" peerName="BAD"
|
|
func Tenancy(s string) *pbresource.Tenancy {
|
|
parts := strings.Split(s, ".")
|
|
switch len(parts) {
|
|
case 0:
|
|
return resource.DefaultClusteredTenancy()
|
|
case 1:
|
|
v := resource.DefaultPartitionedTenancy()
|
|
v.Partition = parts[0]
|
|
return v
|
|
case 2:
|
|
v := resource.DefaultNamespacedTenancy()
|
|
v.Partition = parts[0]
|
|
v.Namespace = parts[1]
|
|
return v
|
|
default:
|
|
return &pbresource.Tenancy{Partition: "BAD", Namespace: "BAD", PeerName: "BAD"}
|
|
}
|
|
}
|
|
|
|
func DefaultTenancyForType(t *testing.T, reg resource.Registration) *pbresource.Tenancy {
|
|
switch reg.Scope {
|
|
case resource.ScopeNamespace:
|
|
return resource.DefaultNamespacedTenancy()
|
|
case resource.ScopePartition:
|
|
return resource.DefaultPartitionedTenancy()
|
|
case resource.ScopeCluster:
|
|
return resource.DefaultClusteredTenancy()
|
|
default:
|
|
t.Fatalf("unsupported resource scope: %v", reg.Scope)
|
|
return nil
|
|
}
|
|
}
|