github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+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/ccerror"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
    11  )
    12  
    13  // ApplicationState is the running state of an application.
    14  type ApplicationState string
    15  
    16  const (
    17  	ApplicationStarted ApplicationState = "STARTED"
    18  	ApplicationStopped ApplicationState = "STOPPED"
    19  )
    20  
    21  // ApplicationPackageState is the staging state of application bits.
    22  type ApplicationPackageState string
    23  
    24  const (
    25  	ApplicationPackageStaged  ApplicationPackageState = "STAGED"
    26  	ApplicationPackagePending ApplicationPackageState = "PENDING"
    27  	ApplicationPackageFailed  ApplicationPackageState = "FAILED"
    28  	ApplicationPackageUnknown ApplicationPackageState = "UNKNOWN"
    29  )
    30  
    31  // Application represents a Cloud Controller Application.
    32  type Application struct {
    33  	// Buildpack is the buildpack set by the user.
    34  	Buildpack string `json:"buildpack,omitempty"`
    35  
    36  	// DetectedBuildpack is the buildpack automatically detected.
    37  	DetectedBuildpack string `json:"-"`
    38  
    39  	// DetectedStartCommand is the command used to start the application.
    40  	DetectedStartCommand string `json:"-"`
    41  
    42  	// DiskQuota is the disk given to each instance, in megabytes.
    43  	DiskQuota int `json:"-"`
    44  
    45  	// DockerImage is the docker image location.
    46  	DockerImage string `json:"docker_image,omitempty"`
    47  
    48  	// GUID is the unique application identifier.
    49  	GUID string `json:"guid,omitempty"`
    50  
    51  	// HealthCheckType is the type of health check that will be done to the app.
    52  	HealthCheckType string `json:"health_check_type,omitempty"`
    53  
    54  	// HealthCheckHTTPEndpoint is the url of the http health check endpoint.
    55  	HealthCheckHTTPEndpoint string `json:"health_check_http_endpoint,omitempty"`
    56  
    57  	// Instances is the total number of app instances.
    58  	Instances int `json:"-"`
    59  
    60  	// Memory is the memory given to each instance, in megabytes.
    61  	Memory int `json:"-"`
    62  
    63  	// Name is the name given to the application.
    64  	Name string `json:"name,omitempty"`
    65  
    66  	// PackageState represents the staging state of the application bits.
    67  	PackageState ApplicationPackageState `json:"-"`
    68  
    69  	// PackageUpdatedAt is the last time the app bits were updated. In RFC3339.
    70  	PackageUpdatedAt time.Time `json:"-"`
    71  
    72  	// SpaceGUID is the GUID of the app's space.
    73  	SpaceGUID string `json:"space_guid,omitempty"`
    74  
    75  	// StackGUID is the GUID for the Stack the application is running on.
    76  	StackGUID string `json:"-"`
    77  
    78  	// StagingFailedDescription is the verbose description of why the package
    79  	// failed to stage.
    80  	StagingFailedDescription string `json:"-"`
    81  
    82  	// StagingFailedReason is the reason why the package failed to stage.
    83  	StagingFailedReason string `json:"-"`
    84  
    85  	// State is the desired state of the application.
    86  	State ApplicationState `json:"state,omitempty"`
    87  }
    88  
    89  // UnmarshalJSON helps unmarshal a Cloud Controller Application response.
    90  func (application *Application) UnmarshalJSON(data []byte) error {
    91  	var ccApp struct {
    92  		Metadata internal.Metadata `json:"metadata"`
    93  		Entity   struct {
    94  			Buildpack                string     `json:"buildpack"`
    95  			DetectedBuildpack        string     `json:"detected_buildpack"`
    96  			DetectedStartCommand     string     `json:"detected_start_command"`
    97  			DiskQuota                int        `json:"disk_quota"`
    98  			DockerImage              string     `json:"docker_image"`
    99  			HealthCheckType          string     `json:"health_check_type"`
   100  			HealthCheckHTTPEndpoint  string     `json:"health_check_http_endpoint"`
   101  			Instances                int        `json:"instances"`
   102  			Memory                   int        `json:"memory"`
   103  			Name                     string     `json:"name"`
   104  			PackageState             string     `json:"package_state"`
   105  			PackageUpdatedAt         *time.Time `json:"package_updated_at"`
   106  			StackGUID                string     `json:"stack_guid"`
   107  			StagingFailedDescription string     `json:"staging_failed_description"`
   108  			StagingFailedReason      string     `json:"staging_failed_reason"`
   109  			State                    string     `json:"state"`
   110  		} `json:"entity"`
   111  	}
   112  	if err := json.Unmarshal(data, &ccApp); err != nil {
   113  		return err
   114  	}
   115  
   116  	application.GUID = ccApp.Metadata.GUID
   117  	application.Buildpack = ccApp.Entity.Buildpack
   118  	application.DetectedBuildpack = ccApp.Entity.DetectedBuildpack
   119  	application.DetectedStartCommand = ccApp.Entity.DetectedStartCommand
   120  	application.DiskQuota = ccApp.Entity.DiskQuota
   121  	application.DockerImage = ccApp.Entity.DockerImage
   122  	application.HealthCheckType = ccApp.Entity.HealthCheckType
   123  	application.HealthCheckHTTPEndpoint = ccApp.Entity.HealthCheckHTTPEndpoint
   124  	application.Instances = ccApp.Entity.Instances
   125  	application.Memory = ccApp.Entity.Memory
   126  	application.Name = ccApp.Entity.Name
   127  	application.PackageState = ApplicationPackageState(ccApp.Entity.PackageState)
   128  	application.StackGUID = ccApp.Entity.StackGUID
   129  	application.StagingFailedDescription = ccApp.Entity.StagingFailedDescription
   130  	application.StagingFailedReason = ccApp.Entity.StagingFailedReason
   131  	application.State = ApplicationState(ccApp.Entity.State)
   132  
   133  	if ccApp.Entity.PackageUpdatedAt != nil {
   134  		application.PackageUpdatedAt = *ccApp.Entity.PackageUpdatedAt
   135  	}
   136  	return nil
   137  }
   138  
   139  // CreateApplication creates a cloud controller application in with the given
   140  // settings. SpaceGUID and Name are the only required fields.
   141  func (client *Client) CreateApplication(app Application) (Application, Warnings, error) {
   142  	body, err := json.Marshal(app)
   143  	if err != nil {
   144  		return Application{}, nil, err
   145  	}
   146  
   147  	request, err := client.newHTTPRequest(requestOptions{
   148  		RequestName: internal.PostAppRequest,
   149  		Body:        bytes.NewReader(body),
   150  	})
   151  	if err != nil {
   152  		return Application{}, nil, err
   153  	}
   154  
   155  	var updatedApp Application
   156  	response := cloudcontroller.Response{
   157  		Result: &updatedApp,
   158  	}
   159  
   160  	err = client.connection.Make(request, &response)
   161  	return updatedApp, response.Warnings, err
   162  }
   163  
   164  // GetApplication returns back an Application.
   165  func (client *Client) GetApplication(guid string) (Application, Warnings, error) {
   166  	request, err := client.newHTTPRequest(requestOptions{
   167  		RequestName: internal.GetAppRequest,
   168  		URIParams:   Params{"app_guid": guid},
   169  	})
   170  	if err != nil {
   171  		return Application{}, nil, err
   172  	}
   173  
   174  	var app Application
   175  	response := cloudcontroller.Response{
   176  		Result: &app,
   177  	}
   178  
   179  	err = client.connection.Make(request, &response)
   180  	return app, response.Warnings, err
   181  }
   182  
   183  // GetApplications returns back a list of Applications based off of the
   184  // provided queries.
   185  func (client *Client) GetApplications(queries []Query) ([]Application, Warnings, error) {
   186  	request, err := client.newHTTPRequest(requestOptions{
   187  		RequestName: internal.GetAppsRequest,
   188  		Query:       FormatQueryParameters(queries),
   189  	})
   190  	if err != nil {
   191  		return nil, nil, err
   192  	}
   193  
   194  	var fullAppsList []Application
   195  	warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
   196  		if app, ok := item.(Application); ok {
   197  			fullAppsList = append(fullAppsList, app)
   198  		} else {
   199  			return ccerror.UnknownObjectInListError{
   200  				Expected:   Application{},
   201  				Unexpected: item,
   202  			}
   203  		}
   204  		return nil
   205  	})
   206  
   207  	return fullAppsList, warnings, err
   208  }
   209  
   210  // UpdateApplication updates the application with the given GUID.
   211  func (client *Client) UpdateApplication(app Application) (Application, Warnings, error) {
   212  	appGUID := app.GUID
   213  	app.GUID = ""
   214  
   215  	body, err := json.Marshal(app)
   216  	if err != nil {
   217  		return Application{}, nil, err
   218  	}
   219  
   220  	request, err := client.newHTTPRequest(requestOptions{
   221  		RequestName: internal.PutAppRequest,
   222  		URIParams:   Params{"app_guid": appGUID},
   223  		Body:        bytes.NewReader(body),
   224  	})
   225  	if err != nil {
   226  		return Application{}, nil, err
   227  	}
   228  
   229  	var updatedApp Application
   230  	response := cloudcontroller.Response{
   231  		Result: &updatedApp,
   232  	}
   233  
   234  	err = client.connection.Make(request, &response)
   235  	return updatedApp, response.Warnings, err
   236  }
   237  
   238  // GetRouteApplications returns a list of Applications associated with a route
   239  // GUID, filtered by provided queries.
   240  func (client *Client) GetRouteApplications(routeGUID string, queryParams []Query) ([]Application, Warnings, error) {
   241  	request, err := client.newHTTPRequest(requestOptions{
   242  		RequestName: internal.GetRouteAppsRequest,
   243  		URIParams:   map[string]string{"route_guid": routeGUID},
   244  		Query:       FormatQueryParameters(queryParams),
   245  	})
   246  	if err != nil {
   247  		return nil, nil, err
   248  	}
   249  
   250  	var fullAppsList []Application
   251  	warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
   252  		if app, ok := item.(Application); ok {
   253  			fullAppsList = append(fullAppsList, app)
   254  		} else {
   255  			return ccerror.UnknownObjectInListError{
   256  				Expected:   Application{},
   257  				Unexpected: item,
   258  			}
   259  		}
   260  		return nil
   261  	})
   262  
   263  	return fullAppsList, warnings, err
   264  }