github.com/skyscape-cloud-services/terraform@v0.9.2-0.20170609144644-7ece028a1747/builtin/providers/rancher/util.go (about)

     1  package rancher
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strings"
     7  
     8  	"github.com/rancher/go-rancher/v2"
     9  )
    10  
    11  const (
    12  	stateRemoved = "removed"
    13  	statePurged  = "purged"
    14  )
    15  
    16  // GetActiveOrchestration get the name of the active orchestration for a environment
    17  func getActiveOrchestration(project *client.Project) string {
    18  	return project.Orchestration
    19  }
    20  
    21  func removed(state string) bool {
    22  	return state == stateRemoved || state == statePurged
    23  }
    24  
    25  func splitID(id string) (envID, resourceID string) {
    26  	if strings.Contains(id, "/") {
    27  		return id[0:strings.Index(id, "/")], id[strings.Index(id, "/")+1:]
    28  	}
    29  	return "", id
    30  }
    31  
    32  // NewListOpts wraps around client.NewListOpts()
    33  func NewListOpts() *client.ListOpts {
    34  	return client.NewListOpts()
    35  }
    36  
    37  func populateProjectTemplateIDs(config *Config) error {
    38  	cli, err := config.GlobalClient()
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	for projectTemplate := range defaultProjectTemplates {
    44  		templates, err := cli.ProjectTemplate.List(&client.ListOpts{
    45  			Filters: map[string]interface{}{
    46  				"isPublic": true,
    47  				"name":     projectTemplate,
    48  				"sort":     "created",
    49  			},
    50  		})
    51  		if err != nil {
    52  			return err
    53  		}
    54  
    55  		if len(templates.Data) > 0 {
    56  			defaultProjectTemplates[projectTemplate] = templates.Data[0].Id
    57  		}
    58  	}
    59  	return nil
    60  }
    61  
    62  func addHostLabels(command string, labels map[string]interface{}) string {
    63  	result := []string{}
    64  	hostLabels := url.Values{}
    65  
    66  	if len(labels) == 0 {
    67  		return command
    68  	}
    69  
    70  	tokenizedCommand := strings.Split(command, " ")
    71  	if len(tokenizedCommand) > 0 {
    72  		result = append(result, tokenizedCommand[:3]...)
    73  		for k, v := range labels {
    74  			hostLabels.Add(k, v.(string))
    75  		}
    76  		strHostLabels := hostLabels.Encode()
    77  		result = append(result, "-e", fmt.Sprintf("CATTLE_HOST_LABELS='%s'", strHostLabels))
    78  		result = append(result, tokenizedCommand[3:]...)
    79  	}
    80  
    81  	return strings.Join(result, " ")
    82  }