github.com/dcarley/cf-cli@v6.24.1-0.20170220111324-4225ff346898+incompatible/api/cloudcontroller/ccv2/application.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"time"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
    10  )
    11  
    12  // ApplicationState is the running state of an application.
    13  type ApplicationState string
    14  
    15  const (
    16  	ApplicationStarted ApplicationState = "STARTED"
    17  	ApplicationStopped ApplicationState = "STOPPED"
    18  )
    19  
    20  // ApplicationPackageState is the staging state of application bits.
    21  type ApplicationPackageState string
    22  
    23  const (
    24  	ApplicationPackageStaged  ApplicationPackageState = "STAGED"
    25  	ApplicationPackagePending ApplicationPackageState = "PENDING"
    26  	ApplicationPackageFailed  ApplicationPackageState = "FAILED"
    27  	ApplicationPackageUnknown ApplicationPackageState = "UNKNOWN"
    28  )
    29  
    30  // Application represents a Cloud Controller Application.
    31  type Application struct {
    32  	// Buildpack is the buildpack set by the user.
    33  	Buildpack string `json:"-"`
    34  
    35  	// DetectedBuildpack is the buildpack automatically detected.
    36  	DetectedBuildpack string `json:"-"`
    37  
    38  	// DetectedStartCommand is the command used to start the application.
    39  	DetectedStartCommand string `json:"-"`
    40  
    41  	// DiskQuota is the disk given to each instance, in megabytes.
    42  	DiskQuota int `json:"-"`
    43  
    44  	// GUID is the unique application identifier.
    45  	GUID string `json:"-"`
    46  
    47  	// HealthCheckType is the type of health check that will be done to the app.
    48  	HealthCheckType string `json:"health_check_type,omitempty"`
    49  
    50  	// HealthCheckHTTPEndpoint is the url of the http health check endpoint.
    51  	HealthCheckHTTPEndpoint string `json:"health_check_http_endpoint,omitempty"`
    52  
    53  	// Instances is the total number of app instances.
    54  	Instances int `json:"-"`
    55  
    56  	// Memory is the memory given to each instance, in megabytes.
    57  	Memory int `json:"-"`
    58  
    59  	// Name is the name given to the application.
    60  	Name string `json:"-"`
    61  
    62  	// PackageState represents the staging state of the application bits.
    63  	PackageState ApplicationPackageState `json:"-"`
    64  
    65  	// PackageUpdatedAt is the last time the app bits were updated. In RFC3339.
    66  	PackageUpdatedAt time.Time `json:"-"`
    67  
    68  	// StackGUID is the GUID for the Stack the application is running on.
    69  	StackGUID string `json:"-"`
    70  
    71  	// StagingFailedReason is the reason why the package failed to stage.
    72  	StagingFailedReason string `json:"-"`
    73  
    74  	// State is the desired state of the application.
    75  	State ApplicationState `json:"state,omitempty"`
    76  }
    77  
    78  // UnmarshalJSON helps unmarshal a Cloud Controller Application response.
    79  func (application *Application) UnmarshalJSON(data []byte) error {
    80  	var ccApp struct {
    81  		Metadata internal.Metadata `json:"metadata"`
    82  		Entity   struct {
    83  			Buildpack               string     `json:"buildpack"`
    84  			DetectedBuildpack       string     `json:"detected_buildpack"`
    85  			DetectedStartCommand    string     `json:"detected_start_command"`
    86  			DiskQuota               int        `json:"disk_quota"`
    87  			HealthCheckType         string     `json:"health_check_type"`
    88  			HealthCheckHTTPEndpoint string     `json:"health_check_http_endpoint"`
    89  			Instances               int        `json:"instances"`
    90  			Memory                  int        `json:"memory"`
    91  			Name                    string     `json:"name"`
    92  			PackageState            string     `json:"package_state"`
    93  			PackageUpdatedAt        *time.Time `json:"package_updated_at"`
    94  			StackGUID               string     `json:"stack_guid"`
    95  			StagingFailedReason     string     `json:"staging_failed_reason"`
    96  			State                   string     `json:"state"`
    97  		} `json:"entity"`
    98  	}
    99  	if err := json.Unmarshal(data, &ccApp); err != nil {
   100  		return err
   101  	}
   102  
   103  	application.GUID = ccApp.Metadata.GUID
   104  	application.Buildpack = ccApp.Entity.Buildpack
   105  	application.DetectedBuildpack = ccApp.Entity.DetectedBuildpack
   106  	application.DetectedStartCommand = ccApp.Entity.DetectedStartCommand
   107  	application.DiskQuota = ccApp.Entity.DiskQuota
   108  	application.HealthCheckType = ccApp.Entity.HealthCheckType
   109  	application.HealthCheckHTTPEndpoint = ccApp.Entity.HealthCheckHTTPEndpoint
   110  	application.Instances = ccApp.Entity.Instances
   111  	application.Memory = ccApp.Entity.Memory
   112  	application.Name = ccApp.Entity.Name
   113  	application.PackageState = ApplicationPackageState(ccApp.Entity.PackageState)
   114  	application.StackGUID = ccApp.Entity.StackGUID
   115  	application.StagingFailedReason = ccApp.Entity.StagingFailedReason
   116  	application.State = ApplicationState(ccApp.Entity.State)
   117  
   118  	if ccApp.Entity.PackageUpdatedAt != nil {
   119  		application.PackageUpdatedAt = *ccApp.Entity.PackageUpdatedAt
   120  	}
   121  	return nil
   122  }
   123  
   124  // GetApplication returns back an Application.
   125  func (client *Client) GetApplication(guid string) (Application, Warnings, error) {
   126  	request, err := client.newHTTPRequest(requestOptions{
   127  		RequestName: internal.AppRequest,
   128  		URIParams:   Params{"app_guid": guid},
   129  	})
   130  	if err != nil {
   131  		return Application{}, nil, err
   132  	}
   133  
   134  	var app Application
   135  	response := cloudcontroller.Response{
   136  		Result: &app,
   137  	}
   138  
   139  	err = client.connection.Make(request, &response)
   140  	return app, response.Warnings, err
   141  }
   142  
   143  // GetApplications returns back a list of Applications based off of the
   144  // provided queries.
   145  func (client *Client) GetApplications(queries []Query) ([]Application, Warnings, error) {
   146  	request, err := client.newHTTPRequest(requestOptions{
   147  		RequestName: internal.AppsRequest,
   148  		Query:       FormatQueryParameters(queries),
   149  	})
   150  	if err != nil {
   151  		return nil, nil, err
   152  	}
   153  
   154  	var fullAppsList []Application
   155  	warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
   156  		if app, ok := item.(Application); ok {
   157  			fullAppsList = append(fullAppsList, app)
   158  		} else {
   159  			return cloudcontroller.UnknownObjectInListError{
   160  				Expected:   Application{},
   161  				Unexpected: item,
   162  			}
   163  		}
   164  		return nil
   165  	})
   166  
   167  	return fullAppsList, warnings, err
   168  }
   169  
   170  func (client *Client) UpdateApplication(app Application) (Application, Warnings, error) {
   171  	body, err := json.Marshal(app)
   172  	if err != nil {
   173  		return Application{}, nil, err
   174  	}
   175  
   176  	request, err := client.newHTTPRequest(requestOptions{
   177  		RequestName: internal.UpdateAppRequest,
   178  		URIParams:   Params{"app_guid": app.GUID},
   179  		Body:        bytes.NewBuffer(body),
   180  	})
   181  	if err != nil {
   182  		return Application{}, nil, err
   183  	}
   184  
   185  	var updatedApp Application
   186  	response := cloudcontroller.Response{
   187  		Result: &updatedApp,
   188  	}
   189  
   190  	err = client.connection.Make(request, &response)
   191  	return updatedApp, response.Warnings, err
   192  }
   193  
   194  // GetRouteApplications returns a list of Applications associated with a route
   195  // GUID, filtered by provided queries.
   196  func (client *Client) GetRouteApplications(routeGUID string, queryParams []Query) ([]Application, Warnings, error) {
   197  	request, err := client.newHTTPRequest(requestOptions{
   198  		RequestName: internal.AppsFromRouteRequest,
   199  		URIParams:   map[string]string{"route_guid": routeGUID},
   200  		Query:       FormatQueryParameters(queryParams),
   201  	})
   202  	if err != nil {
   203  		return nil, nil, err
   204  	}
   205  
   206  	var fullAppsList []Application
   207  	warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
   208  		if app, ok := item.(Application); ok {
   209  			fullAppsList = append(fullAppsList, app)
   210  		} else {
   211  			return cloudcontroller.UnknownObjectInListError{
   212  				Expected:   Application{},
   213  				Unexpected: item,
   214  			}
   215  		}
   216  		return nil
   217  	})
   218  
   219  	return fullAppsList, warnings, err
   220  }