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

     1  package photon
     2  
     3  import (
     4  	"github.com/emc-advanced-dev/pkg/errors"
     5  
     6  	"github.com/solo-io/unik/pkg/types"
     7  )
     8  
     9  func (p *PhotonProvider) ListInstances() ([]*types.Instance, error) {
    10  	if len(p.state.GetInstances()) < 1 {
    11  		return []*types.Instance{}, nil
    12  	}
    13  
    14  	var instances []*types.Instance
    15  	for _, instance := range p.state.GetInstances() {
    16  
    17  		vm, err := p.client.VMs.Get(instance.Id)
    18  		if err != nil {
    19  			return nil, errors.New("retrieving vm for instance id "+instance.Id, err)
    20  		}
    21  
    22  		// TODO: get ip..
    23  
    24  		switch vm.State {
    25  		case "STARTED":
    26  			instance.State = types.InstanceState_Running
    27  		case "CREATING":
    28  			instance.State = types.InstanceState_Pending
    29  		case "STOPPED":
    30  			fallthrough
    31  		case "SUSPENDED":
    32  			fallthrough
    33  		default:
    34  			instance.State = types.InstanceState_Stopped
    35  			break
    36  		}
    37  		err = p.state.ModifyInstances(func(instances map[string]*types.Instance) error {
    38  			instances[instance.Id] = instance
    39  			return nil
    40  		})
    41  		if err != nil {
    42  			return nil, errors.New("saving instance to state", err)
    43  		}
    44  
    45  		instances = append(instances, instance)
    46  	}
    47  
    48  	return instances, nil
    49  }