github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/common/cloudcredential.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package common
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	"github.com/juju/errors"
    11  
    12  	"gopkg.in/juju/names.v2"
    13  )
    14  
    15  // ResolveCloudCredentialTag takes a string which is of either the format
    16  // "<credential>" or "<user>/<credential>". If the string does not include
    17  // a user, then the supplied user tag is implied.
    18  func ResolveCloudCredentialTag(user names.UserTag, cloud names.CloudTag, credentialName string) (names.CloudCredentialTag, error) {
    19  	if i := strings.IndexRune(credentialName, '/'); i == -1 {
    20  		credentialName = fmt.Sprintf("%s/%s", user.Id(), credentialName)
    21  	}
    22  	s := fmt.Sprintf("%s/%s", cloud.Id(), credentialName)
    23  	if !names.IsValidCloudCredential(s) {
    24  		return names.CloudCredentialTag{}, errors.NotValidf("cloud credential name %q", s)
    25  	}
    26  	return names.NewCloudCredentialTag(s), nil
    27  }