github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/api/cloudcontroller/ccv2/application_instance.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
    10  )
    11  
    12  // ApplicationInstance represents a Cloud Controller Application Instance.
    13  type ApplicationInstance struct {
    14  	// Details are arbitrary information about the instance.
    15  	Details string
    16  
    17  	// ID is the instance ID.
    18  	ID int
    19  
    20  	// Since is the Unix time stamp that represents the time the instance was
    21  	// created.
    22  	Since float64
    23  
    24  	// State is the instance's state.
    25  	State constant.ApplicationInstanceState
    26  }
    27  
    28  // UnmarshalJSON helps unmarshal a Cloud Controller application instance
    29  // response.
    30  func (instance *ApplicationInstance) UnmarshalJSON(data []byte) error {
    31  	var ccInstance struct {
    32  		Details string  `json:"details"`
    33  		Since   float64 `json:"since"`
    34  		State   string  `json:"state"`
    35  	}
    36  	if err := json.Unmarshal(data, &ccInstance); err != nil {
    37  		return err
    38  	}
    39  
    40  	instance.Details = ccInstance.Details
    41  	instance.State = constant.ApplicationInstanceState(ccInstance.State)
    42  	instance.Since = ccInstance.Since
    43  
    44  	return nil
    45  }
    46  
    47  // GetApplicationApplicationInstances returns a list of ApplicationInstance for
    48  // a given application. Depending on the state of an application, it might skip
    49  // some application instances.
    50  func (client *Client) GetApplicationApplicationInstances(guid string) (map[int]ApplicationInstance, Warnings, error) {
    51  	request, err := client.newHTTPRequest(requestOptions{
    52  		RequestName: internal.GetAppInstancesRequest,
    53  		URIParams:   Params{"app_guid": guid},
    54  	})
    55  	if err != nil {
    56  		return nil, nil, err
    57  	}
    58  
    59  	var instances map[string]ApplicationInstance
    60  	response := cloudcontroller.Response{
    61  		Result: &instances,
    62  	}
    63  
    64  	err = client.connection.Make(request, &response)
    65  	if err != nil {
    66  		return nil, response.Warnings, err
    67  	}
    68  
    69  	returnedInstances := map[int]ApplicationInstance{}
    70  	for instanceID, instance := range instances {
    71  		id, convertErr := strconv.Atoi(instanceID)
    72  		if convertErr != nil {
    73  			return nil, response.Warnings, convertErr
    74  		}
    75  		instance.ID = id
    76  		returnedInstances[id] = instance
    77  	}
    78  
    79  	return returnedInstances, response.Warnings, nil
    80  }