2016-12-02 16:57:41 -08:00
|
|
|
/*
|
|
|
|
|
Copyright 2016 The Rook Authors. All rights reserved.
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
package object
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
|
|
|
|
"os"
|
|
|
|
|
|
2017-06-20 12:10:30 -07:00
|
|
|
"github.com/rook/rook/cmd/rookctl/rook"
|
2016-12-02 16:57:41 -08:00
|
|
|
"github.com/rook/rook/pkg/rook/client"
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
2016-12-07 16:50:01 -08:00
|
|
|
const (
|
|
|
|
|
FormatPretty = "pretty"
|
|
|
|
|
FormatEnvVar = "env-var"
|
|
|
|
|
AWSHost = "AWS_HOST"
|
|
|
|
|
AWSEndpoint = "AWS_ENDPOINT"
|
|
|
|
|
AWSAccessKey = "AWS_ACCESS_KEY_ID"
|
|
|
|
|
AWSSecretKey = "AWS_SECRET_ACCESS_KEY"
|
|
|
|
|
PrettyOutputFmt = "%s\t%s\t\n"
|
|
|
|
|
ExportOutputFmt = "export %s=%s\n"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
connOutputFormat string
|
|
|
|
|
)
|
|
|
|
|
|
2016-12-02 16:57:41 -08:00
|
|
|
var connectionCmd = &cobra.Command{
|
2017-09-14 16:11:20 -07:00
|
|
|
Use: "connection [ObjectStore] [UserID]",
|
|
|
|
|
Short: "Gets connection information that will allow a client to access object storage",
|
2016-12-02 16:57:41 -08:00
|
|
|
Aliases: []string{"conn"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func init() {
|
2016-12-07 16:50:01 -08:00
|
|
|
connectionCmd.Flags().StringVar(&connOutputFormat, "format", FormatPretty,
|
|
|
|
|
fmt.Sprintf("Format of connection output, (valid values: %s,%s)", FormatPretty, FormatEnvVar))
|
|
|
|
|
|
2016-12-02 16:57:41 -08:00
|
|
|
connectionCmd.RunE = getConnectionInfoEntry
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getConnectionInfoEntry(cmd *cobra.Command, args []string) error {
|
|
|
|
|
rook.SetupLogging()
|
|
|
|
|
|
2017-09-14 16:11:20 -07:00
|
|
|
if err := checkObjectArgs(args, []string{"[UserID]"}); err != nil {
|
2016-12-02 16:57:41 -08:00
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-01 10:23:39 -07:00
|
|
|
c := rook.NewRookNetworkRestClient()
|
2017-09-14 16:11:20 -07:00
|
|
|
out, err := getConnectionInfo(c, args[0], args[1], connOutputFormat)
|
2016-12-02 16:57:41 -08:00
|
|
|
if err != nil {
|
|
|
|
|
fmt.Fprintln(os.Stderr, err)
|
|
|
|
|
os.Exit(1)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fmt.Print(out)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 12:26:42 -07:00
|
|
|
func getConnectionInfo(c client.RookRestClient, storeName, userID, format string) (string, error) {
|
2016-12-07 16:50:01 -08:00
|
|
|
if format != FormatPretty && format != FormatEnvVar {
|
|
|
|
|
return "", fmt.Errorf("invalid output format: %s", format)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 12:26:42 -07:00
|
|
|
connInfo, err := c.GetObjectStoreConnectionInfo(storeName)
|
2016-12-02 16:57:41 -08:00
|
|
|
if err != nil {
|
|
|
|
|
if client.IsHttpNotFound(err) {
|
|
|
|
|
return "object store connection info is not ready, if \"object create\" has already been run, please be patient\n", nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", fmt.Errorf("failed to get object store connection info: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-14 12:26:42 -07:00
|
|
|
user, err := c.GetObjectUser(storeName, userID)
|
2017-02-03 13:16:36 -08:00
|
|
|
if err != nil {
|
|
|
|
|
if client.IsHttpNotFound(err) {
|
|
|
|
|
return fmt.Sprintf("Unable to find user %s\n", userID), nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return "", fmt.Errorf("failed to get object store user info: %+v", err)
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-02 16:57:41 -08:00
|
|
|
var buffer bytes.Buffer
|
|
|
|
|
|
2016-12-07 16:50:01 -08:00
|
|
|
if format == FormatPretty {
|
|
|
|
|
w := rook.NewTableWriter(&buffer)
|
2016-12-02 16:57:41 -08:00
|
|
|
|
2016-12-07 16:50:01 -08:00
|
|
|
// write header columns
|
|
|
|
|
fmt.Fprintln(w, "NAME\tVALUE")
|
|
|
|
|
|
|
|
|
|
// write object store connection info
|
|
|
|
|
fmt.Fprintf(w, PrettyOutputFmt, AWSHost, connInfo.Host)
|
2017-09-14 12:26:42 -07:00
|
|
|
for _, port := range connInfo.Ports {
|
|
|
|
|
fmt.Fprintf(w, PrettyOutputFmt, AWSEndpoint, fmt.Sprintf("%s:%d", connInfo.IPAddress, port))
|
|
|
|
|
}
|
2017-02-03 13:16:36 -08:00
|
|
|
fmt.Fprintf(w, PrettyOutputFmt, AWSAccessKey, *user.AccessKey)
|
|
|
|
|
fmt.Fprintf(w, PrettyOutputFmt, AWSSecretKey, *user.SecretKey)
|
2016-12-07 16:50:01 -08:00
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
|
} else if format == FormatEnvVar {
|
|
|
|
|
buffer.WriteString(fmt.Sprintf(ExportOutputFmt, AWSHost, connInfo.Host))
|
2017-09-14 12:26:42 -07:00
|
|
|
for _, port := range connInfo.Ports {
|
|
|
|
|
buffer.WriteString(fmt.Sprintf(ExportOutputFmt, AWSEndpoint, fmt.Sprintf("%s:%d", connInfo.IPAddress, port)))
|
|
|
|
|
}
|
2017-02-03 13:16:36 -08:00
|
|
|
buffer.WriteString(fmt.Sprintf(ExportOutputFmt, AWSAccessKey, *user.AccessKey))
|
|
|
|
|
buffer.WriteString(fmt.Sprintf(ExportOutputFmt, AWSSecretKey, *user.SecretKey))
|
2016-12-07 16:50:01 -08:00
|
|
|
}
|
2016-12-02 16:57:41 -08:00
|
|
|
|
|
|
|
|
return buffer.String(), nil
|
|
|
|
|
}
|