github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/caas/cluster.go (about)

     1  // Copyright 2019 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caas
     5  
     6  import (
     7  	"io"
     8  	"os"
     9  	"strings"
    10  
    11  	"github.com/juju/cmd"
    12  	"github.com/juju/utils/exec"
    13  )
    14  
    15  //go:generate mockgen -package mocks -destination mocks/runner_mock.go github.com/juju/juju/cmd/juju/caas CommandRunner
    16  
    17  type CommandRunner interface {
    18  	RunCommands(run exec.RunParams) (*exec.ExecResponse, error)
    19  }
    20  
    21  type defaultRunner struct{}
    22  
    23  func (*defaultRunner) RunCommands(run exec.RunParams) (*exec.ExecResponse, error) {
    24  	return exec.RunCommands(run)
    25  }
    26  
    27  func getEnv(key string) string {
    28  	result := os.Getenv(strings.ToUpper(key))
    29  	if result == "" {
    30  		result = os.Getenv(strings.ToLower(key))
    31  	}
    32  	return result
    33  }
    34  
    35  var runCommand = func(runner CommandRunner, params []string, kubeconfig string) (*exec.ExecResponse, error) {
    36  	cmd := strings.Join(params, " ")
    37  
    38  	path := getEnv("path")
    39  	execParams := exec.RunParams{
    40  		Commands:    cmd,
    41  		Environment: []string{"KUBECONFIG=" + kubeconfig, "PATH=" + path},
    42  	}
    43  	return runner.RunCommands(execParams)
    44  }
    45  
    46  type clusterParams struct {
    47  	name       string
    48  	project    string
    49  	region     string
    50  	credential string
    51  }
    52  
    53  type cluster struct {
    54  	name   string
    55  	region string
    56  }
    57  
    58  type k8sCluster interface {
    59  	CommandRunner
    60  	getKubeConfig(p *clusterParams) (io.ReadCloser, string, error)
    61  	interactiveParams(ctx *cmd.Context, p *clusterParams) (*clusterParams, error)
    62  	cloud() string
    63  }