Skip to content

Commit

Permalink
fix!: remove destinations add/remove commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyng committed Apr 6, 2022
1 parent 5a74dc4 commit 1f391b2
Showing 1 changed file with 0 additions and 130 deletions.
130 changes: 0 additions & 130 deletions internal/cmd/destinations.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package cmd

import (
"fmt"
"strings"
"time"

"github.com/spf13/cobra"

"github.com/infrahq/infra/api"
"github.com/infrahq/infra/internal/server/models"
"github.com/infrahq/infra/uid"
)

func newDestinationsCmd() *cobra.Command {
Expand All @@ -24,8 +18,6 @@ func newDestinationsCmd() *cobra.Command {
}

cmd.AddCommand(newDestinationsListCmd())
cmd.AddCommand(newDestinationsAddCmd())
cmd.AddCommand(newDestinationsRemoveCmd())

return cmd
}
Expand Down Expand Up @@ -65,125 +57,3 @@ func newDestinationsListCmd() *cobra.Command {
},
}
}

func newDestinationsAddCmd() *cobra.Command {
return &cobra.Command{
Use: "add DESTINATION",
Short: "Connect an infrastructure destination to Infra",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
parts := strings.Split(args[0], ".")
if len(parts) != 2 {
return fmt.Errorf("invalid input for destination: expected \"<TYPE>.<NAME>\", got %q", args[0])
}

supportedTypes := []string{
"kubernetes",
}

supportedType := false
for _, t := range supportedTypes {
if parts[0] == t {
supportedType = true
break
}
}

if !supportedType {
return fmt.Errorf("unknown destination type: %q. supported types: %v", parts[0], supportedTypes)
}

created, err := CreateInfraIdentity(args[0])
if err != nil {
return err
}

client, err := defaultAPIClient()
if err != nil {
return err
}

destinationGrant := &api.CreateGrantRequest{
Subject: uid.NewIdentityPolymorphicID(created.ID),
Privilege: models.InfraConnectorRole,
Resource: "infra",
}

_, err = client.CreateGrant(destinationGrant)
if err != nil {
return err
}

lifetime := time.Hour * 24 * 365
extensionDeadline := time.Hour * 24
accessKey, err := client.CreateAccessKey(&api.CreateAccessKeyRequest{
IdentityID: created.ID,
Name: fmt.Sprintf("%s destination access key", args[0]),
TTL: api.Duration(lifetime),
ExtensionDeadline: api.Duration(extensionDeadline),
})
if err != nil {
return err
}

config, err := currentHostConfig()
if err != nil {
return err
}

var sb strings.Builder
sb.WriteString(" helm install infra-connector infrahq/infra")

fmt.Fprintf(&sb, " --set connector.config.name=%s", parts[1])
fmt.Fprintf(&sb, " --set connector.config.accessKey=%s", accessKey.AccessKey)
fmt.Fprintf(&sb, " --set connector.config.server=%s", config.Host)

// TODO: replace me with a certificate fingerprint
// so even when users have self-signed certificates
// infra can establish a secure TLS connection
if config.SkipTLSVerify {
sb.WriteString(" --set connector.config.skipTLSVerify=true")
}

fmt.Println()
fmt.Println("Run the following command to connect a kubernetes cluster:")
fmt.Println()
fmt.Println(sb.String())
fmt.Println()
return nil
},
}
}

func newDestinationsRemoveCmd() *cobra.Command {
return &cobra.Command{
Use: "remove DESTINATION",
Aliases: []string{"rm"},
Short: "Disconnect a destination",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := defaultAPIClient()
if err != nil {
return err
}

destinations, err := client.ListDestinations(api.ListDestinationsRequest{Name: args[0]})
if err != nil {
return err
}

if len(destinations) == 0 {
return fmt.Errorf("no destinations named %s", args[0])
}

for _, d := range destinations {
err := client.DeleteDestination(d.ID)
if err != nil {
return err
}
}

return nil
},
}
}

0 comments on commit 1f391b2

Please sign in to comment.