github.com/cloudfoundry/cli@v7.1.0+incompatible/cf/api/password/password.go (about)

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