github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/api/password/password.go (about)

     1  package password
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     8  	"code.cloudfoundry.org/cli/cf/errors"
     9  	. "code.cloudfoundry.org/cli/cf/i18n"
    10  	"code.cloudfoundry.org/cli/cf/net"
    11  )
    12  
    13  //go:generate counterfeiter . Repository
    14  
    15  type Repository interface {
    16  	UpdatePassword(old string, new string) error
    17  }
    18  
    19  type CloudControllerRepository struct {
    20  	config  coreconfig.Reader
    21  	gateway net.Gateway
    22  }
    23  
    24  func NewCloudControllerRepository(config coreconfig.Reader, gateway net.Gateway) (repo CloudControllerRepository) {
    25  	repo.config = config
    26  	repo.gateway = gateway
    27  	return
    28  }
    29  
    30  func (repo CloudControllerRepository) UpdatePassword(old string, new string) error {
    31  	uaaEndpoint := repo.config.UaaEndpoint()
    32  	if uaaEndpoint == "" {
    33  		return errors.New(T("UAA endpoint missing from config file"))
    34  	}
    35  
    36  	url := fmt.Sprintf("/Users/%s/password", repo.config.UserGUID())
    37  	body := fmt.Sprintf(`{"password":"%s","oldPassword":"%s"}`, new, old)
    38  
    39  	return repo.gateway.UpdateResource(uaaEndpoint, url, strings.NewReader(body))
    40  }