forked from solo-io/unik
/
photon_provider.go
81 lines (63 loc) · 1.93 KB
/
photon_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package photon
import (
"net/url"
"os"
"path/filepath"
"time"
"github.com/emc-advanced-dev/pkg/errors"
"github.com/emc-advanced-dev/unik/pkg/config"
"github.com/emc-advanced-dev/unik/pkg/state"
"github.com/vmware/photon-controller-go-sdk/photon"
)
type PhotonProvider struct {
config config.Photon
state state.State
u *url.URL
client *photon.Client
projectId string
}
func PhotonStateFile() string {
return filepath.Join(config.Internal.UnikHome, "photon/state.json")
}
func photonImagesDirectory() string {
return filepath.Join(config.Internal.UnikHome, "photon/images/")
}
func photonInstancesDirectory() string {
return filepath.Join(config.Internal.UnikHome, "photon/instances/")
}
func photonVolumesDirectory() string {
return filepath.Join(config.Internal.UnikHome, "photon/volumes/")
}
func NewPhotonProvider(config config.Photon) (*PhotonProvider, error) {
os.MkdirAll(photonImagesDirectory(), 0755)
os.MkdirAll(photonInstancesDirectory(), 0755)
os.MkdirAll(photonVolumesDirectory(), 0755)
p := &PhotonProvider{
config: config,
state: state.NewBasicState(PhotonStateFile()),
}
p.client = photon.NewClient(p.config.PhotonURL, "", nil)
p.projectId = p.config.ProjectId
_, err := p.client.Status.Get()
if err != nil {
return nil, err
}
if err := p.DeployInstanceListener(config); err != nil /*&& !strings.Contains(err.Error(), "already exists")*/ {
return nil, errors.New("deploying photon instance listener", err)
}
return p, nil
}
func (p *PhotonProvider) WithState(state state.State) *PhotonProvider {
p.state = state
return p
}
func (p *PhotonProvider) waitForTaskSuccess(task *photon.Task) (*photon.Task, error) {
task, err := p.client.Tasks.WaitTimeout(task.ID, 30*time.Minute)
if err != nil {
return nil, errors.New("error waiting for task creating photon image", err)
}
if task.State != "COMPLETED" {
return nil, errors.New("Error with task "+task.ID, nil)
}
return task, nil
}