consul/command/intention/helpers.go
hashicorp-copywrite[bot] 5fb9df1640
[COMPLIANCE] License changes (#18443)
* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* add missing license headers

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

* Update copyright file headers to BUSL-1.1

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
2023-08-11 09:12:13 -04:00

68 lines
1.7 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package intention
import (
"fmt"
"strings"
"github.com/hashicorp/consul/api"
)
// ParseIntentionTarget parses a target of the form <partition>/<namespace>/<name> and returns
// the distinct parts. In some cases the partition and namespace may be elided and this function
// will return the empty string for them then.
// If two parts are present, it is assumed they are namespace/name and not partition/name.
func ParseIntentionTarget(input string) (name string, ns string, partition string, err error) {
ss := strings.Split(input, "/")
switch len(ss) {
case 1: // Name only
name = ss[0]
return
case 2: // namespace/name
ns = ss[0]
name = ss[1]
return
case 3: // partition/namespace/name
partition = ss[0]
ns = ss[1]
name = ss[2]
return
default:
return "", "", "", fmt.Errorf("input can contain at most two '/'")
}
}
func GetFromArgs(client *api.Client, args []string) (*api.Intention, error) {
switch len(args) {
case 1:
id := args[0]
//nolint:staticcheck
ixn, _, err := client.Connect().IntentionGet(id, nil)
if err != nil {
return nil, fmt.Errorf("Error reading the intention: %s", err)
} else if ixn == nil {
return nil, fmt.Errorf("Intention not found with ID %q", id)
}
return ixn, nil
case 2:
source, destination := args[0], args[1]
ixn, _, err := client.Connect().IntentionGetExact(source, destination, nil)
if err != nil {
return nil, fmt.Errorf("Error reading the intention: %s", err)
} else if ixn == nil {
return nil, fmt.Errorf("Intention not found with source %q and destination %q", source, destination)
}
return ixn, nil
default:
return nil, fmt.Errorf("command requires exactly 1 or 2 arguments")
}
}