github.com/sleungcy/cli@v7.1.0+incompatible/api/cloudcontroller/ccv2/application_instance.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"strconv"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/constant"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     9  )
    10  
    11  // ApplicationInstance represents a Cloud Controller Application Instance.
    12  type ApplicationInstance struct {
    13  	// Details are arbitrary information about the instance.
    14  	Details string
    15  
    16  	// ID is the instance ID.
    17  	ID int
    18  
    19  	// Since is the Unix time stamp that represents the time the instance was
    20  	// created.
    21  	Since float64
    22  
    23  	// State is the instance's state.
    24  	State constant.ApplicationInstanceState
    25  }
    26  
    27  // UnmarshalJSON helps unmarshal a Cloud Controller application instance
    28  // response.
    29  func (instance *ApplicationInstance) UnmarshalJSON(data []byte) error {
    30  	var ccInstance struct {
    31  		Details string  `json:"details"`
    32  		Since   float64 `json:"since"`
    33  		State   string  `json:"state"`
    34  	}
    35  	err := cloudcontroller.DecodeJSON(data, &ccInstance)
    36  	if 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  		DecodeJSONResponseInto: &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  }