github.com/emc-advanced-dev/unik@v0.0.0-20190717152701-a58d3e8e33b7/pkg/providers/photon/photon_provider.go (about)

     1  package photon
     2  
     3  import (
     4  	"net/url"
     5  	"os"
     6  	"path/filepath"
     7  	"time"
     8  
     9  	"github.com/emc-advanced-dev/pkg/errors"
    10  
    11  	"github.com/solo-io/unik/pkg/config"
    12  	"github.com/solo-io/unik/pkg/state"
    13  	"github.com/vmware/photon-controller-go-sdk/photon"
    14  )
    15  
    16  type PhotonProvider struct {
    17  	config    config.Photon
    18  	state     state.State
    19  	u         *url.URL
    20  	client    *photon.Client
    21  	projectId string
    22  }
    23  
    24  func PhotonStateFile() string {
    25  	return filepath.Join(config.Internal.UnikHome, "photon/state.json")
    26  
    27  }
    28  func photonImagesDirectory() string {
    29  	return filepath.Join(config.Internal.UnikHome, "photon/images/")
    30  }
    31  
    32  func photonInstancesDirectory() string {
    33  	return filepath.Join(config.Internal.UnikHome, "photon/instances/")
    34  }
    35  
    36  func photonVolumesDirectory() string {
    37  	return filepath.Join(config.Internal.UnikHome, "photon/volumes/")
    38  }
    39  
    40  func NewPhotonProvider(config config.Photon) (*PhotonProvider, error) {
    41  
    42  	os.MkdirAll(photonImagesDirectory(), 0755)
    43  	os.MkdirAll(photonInstancesDirectory(), 0755)
    44  	os.MkdirAll(photonVolumesDirectory(), 0755)
    45  
    46  	p := &PhotonProvider{
    47  		config: config,
    48  		state:  state.NewBasicState(PhotonStateFile()),
    49  	}
    50  
    51  	p.client = photon.NewClient(p.config.PhotonURL, "", nil)
    52  	p.projectId = p.config.ProjectId
    53  	_, err := p.client.Status.Get()
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  
    58  	if err := p.DeployInstanceListener(config); err != nil /*&& !strings.Contains(err.Error(), "already exists")*/ {
    59  		return nil, errors.New("deploying photon instance listener", err)
    60  	}
    61  
    62  	return p, nil
    63  }
    64  
    65  func (p *PhotonProvider) WithState(state state.State) *PhotonProvider {
    66  	p.state = state
    67  	return p
    68  }
    69  
    70  func (p *PhotonProvider) waitForTaskSuccess(task *photon.Task) (*photon.Task, error) {
    71  	task, err := p.client.Tasks.WaitTimeout(task.ID, 30*time.Minute)
    72  	if err != nil {
    73  		return nil, errors.New("error waiting for task creating photon image", err)
    74  	}
    75  
    76  	if task.State != "COMPLETED" {
    77  		return nil, errors.New("Error with task "+task.ID, nil)
    78  	}
    79  
    80  	return task, nil
    81  }