github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/api/app_instances/app_instances.go (about)

     1  package app_instances
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  	"time"
     8  
     9  	"github.com/cloudfoundry/cli/cf/configuration/core_config"
    10  	"github.com/cloudfoundry/cli/cf/models"
    11  	"github.com/cloudfoundry/cli/cf/net"
    12  )
    13  
    14  type InstancesApiResponse map[string]InstanceApiResponse
    15  
    16  type InstanceApiResponse struct {
    17  	State   string
    18  	Since   float64
    19  	Details string
    20  }
    21  
    22  type StatsApiResponse map[string]InstanceStatsApiResponse
    23  
    24  type InstanceStatsApiResponse struct {
    25  	Stats struct {
    26  		DiskQuota int64 `json:"disk_quota"`
    27  		MemQuota  int64 `json:"mem_quota"`
    28  		Usage     struct {
    29  			Cpu  float64
    30  			Disk int64
    31  			Mem  int64
    32  		}
    33  	}
    34  }
    35  
    36  type AppInstancesRepository interface {
    37  	GetInstances(appGuid string) (instances []models.AppInstanceFields, apiErr error)
    38  	DeleteInstance(appGuid string, instance int) error
    39  }
    40  
    41  type CloudControllerAppInstancesRepository struct {
    42  	config  core_config.Reader
    43  	gateway net.Gateway
    44  }
    45  
    46  func NewCloudControllerAppInstancesRepository(config core_config.Reader, gateway net.Gateway) (repo CloudControllerAppInstancesRepository) {
    47  	repo.config = config
    48  	repo.gateway = gateway
    49  	return
    50  }
    51  
    52  func (repo CloudControllerAppInstancesRepository) GetInstances(appGuid string) (instances []models.AppInstanceFields, err error) {
    53  	instancesResponse := InstancesApiResponse{}
    54  	err = repo.gateway.GetResource(
    55  		fmt.Sprintf("%s/v2/apps/%s/instances", repo.config.ApiEndpoint(), appGuid),
    56  		&instancesResponse)
    57  	if err != nil {
    58  		return
    59  	}
    60  
    61  	instances = make([]models.AppInstanceFields, len(instancesResponse), len(instancesResponse))
    62  	for k, v := range instancesResponse {
    63  		index, err := strconv.Atoi(k)
    64  		if err != nil {
    65  			continue
    66  		}
    67  
    68  		instances[index] = models.AppInstanceFields{
    69  			State:   models.InstanceState(strings.ToLower(v.State)),
    70  			Details: v.Details,
    71  			Since:   time.Unix(int64(v.Since), 0),
    72  		}
    73  	}
    74  
    75  	return repo.updateInstancesWithStats(appGuid, instances)
    76  }
    77  
    78  func (repo CloudControllerAppInstancesRepository) DeleteInstance(appGuid string, instance int) error {
    79  	err := repo.gateway.DeleteResource(repo.config.ApiEndpoint(), fmt.Sprintf("/v2/apps/%s/instances/%d", appGuid, instance))
    80  	if err != nil {
    81  		return err
    82  	}
    83  	return nil
    84  }
    85  
    86  func (repo CloudControllerAppInstancesRepository) updateInstancesWithStats(guid string, instances []models.AppInstanceFields) (updatedInst []models.AppInstanceFields, apiErr error) {
    87  	path := fmt.Sprintf("%s/v2/apps/%s/stats", repo.config.ApiEndpoint(), guid)
    88  	statsResponse := StatsApiResponse{}
    89  	apiErr = repo.gateway.GetResource(path, &statsResponse)
    90  	if apiErr != nil {
    91  		return
    92  	}
    93  
    94  	updatedInst = make([]models.AppInstanceFields, len(statsResponse), len(statsResponse))
    95  	for k, v := range statsResponse {
    96  		index, err := strconv.Atoi(k)
    97  		if err != nil {
    98  			continue
    99  		}
   100  
   101  		instance := instances[index]
   102  		instance.CpuUsage = v.Stats.Usage.Cpu
   103  		instance.DiskQuota = v.Stats.DiskQuota
   104  		instance.DiskUsage = v.Stats.Usage.Disk
   105  		instance.MemQuota = v.Stats.MemQuota
   106  		instance.MemUsage = v.Stats.Usage.Mem
   107  
   108  		updatedInst[index] = instance
   109  	}
   110  	return
   111  }