2022-03-22 16:03:15 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-04-13 13:01:38 +02:00
|
|
|
"errors"
|
2022-03-22 16:03:15 +01:00
|
|
|
"fmt"
|
2022-05-06 13:56:02 +02:00
|
|
|
"net"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-03-22 16:03:15 +01:00
|
|
|
|
2022-06-07 16:30:41 +02:00
|
|
|
"github.com/edgelesssys/constellation/cli/internal/azure"
|
2022-06-07 14:52:47 +02:00
|
|
|
"github.com/edgelesssys/constellation/cli/internal/gcp"
|
2022-06-07 11:08:44 +02:00
|
|
|
"github.com/edgelesssys/constellation/internal/cloud/cloudprovider"
|
2022-03-22 16:03:15 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2022-04-13 13:01:38 +02:00
|
|
|
// warnAWS warns that AWS isn't supported.
|
|
|
|
func warnAWS(providerPos int) cobra.PositionalArgs {
|
2022-04-04 15:55:58 +02:00
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
2022-04-13 13:01:38 +02:00
|
|
|
if cloudprovider.FromString(args[providerPos]) == cloudprovider.AWS {
|
2022-05-13 11:29:41 +02:00
|
|
|
return errors.New("AWS isn't supported by this version of Constellation")
|
2022-04-04 16:44:15 +02:00
|
|
|
}
|
|
|
|
return nil
|
2022-04-04 15:55:58 +02:00
|
|
|
}
|
2022-04-04 16:44:15 +02:00
|
|
|
}
|
|
|
|
|
2022-05-04 08:50:50 +02:00
|
|
|
func isCloudProvider(arg int) cobra.PositionalArgs {
|
2022-03-22 16:03:15 +01:00
|
|
|
return func(cmd *cobra.Command, args []string) error {
|
2022-05-04 08:50:50 +02:00
|
|
|
if provider := cloudprovider.FromString(args[arg]); provider == cloudprovider.Unknown {
|
|
|
|
return fmt.Errorf("argument %s isn't a valid cloud provider", args[arg])
|
2022-03-22 16:03:15 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-10 12:34:47 +02:00
|
|
|
func validInstanceTypeForProvider(cmd *cobra.Command, insType string, provider cloudprovider.Provider) error {
|
2022-05-04 08:50:50 +02:00
|
|
|
switch provider {
|
|
|
|
case cloudprovider.GCP:
|
2022-03-22 16:03:15 +01:00
|
|
|
for _, instanceType := range gcp.InstanceTypes {
|
2022-05-04 08:50:50 +02:00
|
|
|
if insType == instanceType {
|
2022-03-22 16:03:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 12:34:47 +02:00
|
|
|
cmd.SetUsageTemplate("GCP instance types:\n" + formatInstanceTypes(gcp.InstanceTypes))
|
|
|
|
cmd.SilenceUsage = false
|
2022-05-04 08:50:50 +02:00
|
|
|
return fmt.Errorf("%s isn't a valid GCP instance type", insType)
|
|
|
|
case cloudprovider.Azure:
|
2022-03-22 16:03:15 +01:00
|
|
|
for _, instanceType := range azure.InstanceTypes {
|
2022-05-04 08:50:50 +02:00
|
|
|
if insType == instanceType {
|
2022-03-22 16:03:15 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 12:34:47 +02:00
|
|
|
cmd.SetUsageTemplate("Azure instance types:\n" + formatInstanceTypes(azure.InstanceTypes))
|
|
|
|
cmd.SilenceUsage = false
|
2022-05-04 08:50:50 +02:00
|
|
|
return fmt.Errorf("%s isn't a valid Azure instance type", insType)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("%s isn't a valid cloud platform", provider)
|
2022-04-04 15:55:58 +02:00
|
|
|
}
|
|
|
|
}
|
2022-05-06 13:56:02 +02:00
|
|
|
|
|
|
|
func validateEndpoint(endpoint string, defaultPort int) (string, error) {
|
|
|
|
_, _, err := net.SplitHostPort(endpoint)
|
|
|
|
if err == nil {
|
|
|
|
return endpoint, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), "missing port in address") {
|
|
|
|
return net.JoinHostPort(endpoint, strconv.Itoa(defaultPort)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
}
|