github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/v3action/instance.go (about)

     1  package v3action
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  )
    10  
    11  // Instance represents a V3 actor instance.
    12  type Instance ccv3.Instance
    13  
    14  // StartTime returns the time that the instance started.
    15  func (instance *Instance) StartTime() time.Time {
    16  	uptimeDuration := time.Duration(instance.Uptime) * time.Second
    17  
    18  	return time.Now().Add(-uptimeDuration)
    19  }
    20  
    21  // ProcessInstanceNotFoundError is returned when the proccess type or process instance cannot be found
    22  type ProcessInstanceNotFoundError struct {
    23  	ProcessType   string
    24  	InstanceIndex int
    25  }
    26  
    27  func (e ProcessInstanceNotFoundError) Error() string {
    28  	return fmt.Sprintf("Instance %d for process %s not found", e.InstanceIndex, e.ProcessType)
    29  }
    30  
    31  func (actor Actor) DeleteInstanceByApplicationNameSpaceProcessTypeAndIndex(appName string, spaceGUID string, processType string, instanceIndex int) (Warnings, error) {
    32  	var allWarnings Warnings
    33  	app, appWarnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    34  	allWarnings = append(allWarnings, appWarnings...)
    35  	if err != nil {
    36  		return allWarnings, err
    37  	}
    38  
    39  	deleteWarnings, err := actor.CloudControllerClient.DeleteApplicationProcessInstance(app.GUID, processType, instanceIndex)
    40  	allWarnings = append(allWarnings, deleteWarnings...)
    41  
    42  	if err != nil {
    43  		switch err.(type) {
    44  		case ccerror.ProcessNotFoundError:
    45  			return allWarnings, ProcessNotFoundError{
    46  				ProcessType: processType,
    47  			}
    48  		case ccerror.InstanceNotFoundError:
    49  			return allWarnings, ProcessInstanceNotFoundError{
    50  				ProcessType:   processType,
    51  				InstanceIndex: instanceIndex,
    52  			}
    53  		default:
    54  			return allWarnings, err
    55  		}
    56  	}
    57  
    58  	return allWarnings, nil
    59  }