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