forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
target_provider.go
54 lines (45 loc) · 1.45 KB
/
target_provider.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
54
package installation
import (
"path/filepath"
biconfig "github.com/cloudfoundry/bosh-cli/config"
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshuuid "github.com/cloudfoundry/bosh-utils/uuid"
)
type TargetProvider interface {
NewTarget() (Target, error)
}
type targetProvider struct {
deploymentStateService biconfig.DeploymentStateService
uuidGenerator boshuuid.Generator
installationsRootPath string
}
func NewTargetProvider(
deploymentStateService biconfig.DeploymentStateService,
uuidGenerator boshuuid.Generator,
installationsRootPath string,
) TargetProvider {
return &targetProvider{
deploymentStateService: deploymentStateService,
uuidGenerator: uuidGenerator,
installationsRootPath: installationsRootPath,
}
}
func (p *targetProvider) NewTarget() (Target, error) {
deploymentState, err := p.deploymentStateService.Load()
if err != nil {
return Target{}, bosherr.WrapError(err, "Loading deployment state")
}
installationID := deploymentState.InstallationID
if installationID == "" {
installationID, err = p.uuidGenerator.Generate()
if err != nil {
return Target{}, bosherr.WrapError(err, "Generating installation ID")
}
deploymentState.InstallationID = installationID
err := p.deploymentStateService.Save(deploymentState)
if err != nil {
return Target{}, bosherr.WrapError(err, "Saving deployment state")
}
}
return NewTarget(filepath.Join(p.installationsRootPath, installationID)), nil
}