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

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/internal"
    11  )
    12  
    13  // Application represents a Cloud Controller V3 Application.
    14  type Application struct {
    15  	Name                string                    `json:"name,omitempty"`
    16  	Relationships       Relationships             `json:"relationships,omitempty"`
    17  	GUID                string                    `json:"guid,omitempty"`
    18  	State               constant.ApplicationState `json:"state,omitempty"`
    19  	LifecycleType       constant.AppLifecycleType `json:"-"`
    20  	LifecycleBuildpacks []string                  `json:"-"`
    21  }
    22  
    23  func (a Application) MarshalJSON() ([]byte, error) {
    24  	type rawApp Application
    25  	var ccApp struct {
    26  		Lifecycle map[string]interface{} `json:"lifecycle,omitempty"`
    27  
    28  		rawApp
    29  	}
    30  
    31  	ccApp.rawApp = (rawApp)(a)
    32  
    33  	switch a.LifecycleType {
    34  	case constant.BuildpackAppLifecycleType:
    35  		if len(a.LifecycleBuildpacks) > 0 {
    36  			ccApp.Lifecycle = map[string]interface{}{}
    37  			ccApp.Lifecycle["type"] = a.LifecycleType
    38  			ccApp.Lifecycle["data"] = map[string]interface{}{}
    39  			switch a.LifecycleBuildpacks[0] {
    40  			case "default", "null":
    41  				ccApp.Lifecycle["data"] = map[string][]string{
    42  					"buildpacks": nil,
    43  				}
    44  			default:
    45  				ccApp.Lifecycle["data"] = map[string][]string{
    46  					"buildpacks": a.LifecycleBuildpacks,
    47  				}
    48  			}
    49  		}
    50  	case constant.DockerAppLifecycleType:
    51  		ccApp.Lifecycle = map[string]interface{}{}
    52  		ccApp.Lifecycle["type"] = a.LifecycleType
    53  		ccApp.Lifecycle["data"] = map[string]interface{}{}
    54  	}
    55  
    56  	ccApp.GUID = ""
    57  	return json.Marshal(ccApp)
    58  }
    59  
    60  func (a *Application) UnmarshalJSON(data []byte) error {
    61  	type rawApp Application
    62  	var ccApp struct {
    63  		*rawApp
    64  
    65  		Lifecycle struct {
    66  			Type constant.AppLifecycleType
    67  			Data struct {
    68  				Buildpacks []string
    69  			}
    70  		}
    71  	}
    72  
    73  	ccApp.rawApp = (*rawApp)(a)
    74  
    75  	err := json.Unmarshal(data, &ccApp)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	a.LifecycleType = ccApp.Lifecycle.Type
    81  	a.LifecycleBuildpacks = ccApp.Lifecycle.Data.Buildpacks
    82  
    83  	return nil
    84  }
    85  
    86  // DropletRelationship represents the relationship between a V3 Droplet and its
    87  // V3 Application
    88  type DropletRelationship struct {
    89  	Relationship Relationship `json:"data"`
    90  	Links        APILinks     `json:"links"`
    91  }
    92  
    93  // GetApplications lists applications with optional filters.
    94  func (client *Client) GetApplications(query ...Query) ([]Application, Warnings, error) {
    95  	request, err := client.newHTTPRequest(requestOptions{
    96  		RequestName: internal.GetAppsRequest,
    97  		Query:       query,
    98  	})
    99  	if err != nil {
   100  		return nil, nil, err
   101  	}
   102  
   103  	var fullAppsList []Application
   104  	warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
   105  		if app, ok := item.(Application); ok {
   106  			fullAppsList = append(fullAppsList, app)
   107  		} else {
   108  			return ccerror.UnknownObjectInListError{
   109  				Expected:   Application{},
   110  				Unexpected: item,
   111  			}
   112  		}
   113  		return nil
   114  	})
   115  
   116  	return fullAppsList, warnings, err
   117  }
   118  
   119  // CreateApplication creates an application with the given settings.
   120  func (client *Client) CreateApplication(app Application) (Application, Warnings, error) {
   121  	bodyBytes, err := json.Marshal(app)
   122  	if err != nil {
   123  		return Application{}, nil, err
   124  	}
   125  
   126  	request, err := client.newHTTPRequest(requestOptions{
   127  		RequestName: internal.PostApplicationRequest,
   128  		Body:        bytes.NewReader(bodyBytes),
   129  	})
   130  	if err != nil {
   131  		return Application{}, nil, err
   132  	}
   133  
   134  	var responseApp Application
   135  	response := cloudcontroller.Response{
   136  		Result: &responseApp,
   137  	}
   138  	err = client.connection.Make(request, &response)
   139  
   140  	return responseApp, response.Warnings, err
   141  }
   142  
   143  // CreateApplicationActionsApplyManifestByApplication applies the manifest to
   144  // the given application.
   145  func (client *Client) CreateApplicationActionsApplyManifestByApplication(rawManifest []byte, appGUID string) (string, Warnings, error) {
   146  	request, err := client.newHTTPRequest(requestOptions{
   147  		RequestName: internal.PostApplicationManifest,
   148  		URIParams:   map[string]string{"app_guid": appGUID},
   149  		Body:        bytes.NewReader(rawManifest),
   150  	})
   151  
   152  	if err != nil {
   153  		return "", nil, err
   154  	}
   155  
   156  	request.Header.Set("Content-Type", "application/x-yaml")
   157  
   158  	response := cloudcontroller.Response{}
   159  	err = client.connection.Make(request, &response)
   160  
   161  	return response.ResourceLocationURL, response.Warnings, err
   162  }
   163  
   164  // DeleteApplication deletes the app with the given app GUID.
   165  func (client *Client) DeleteApplication(appGUID string) (string, Warnings, error) {
   166  	request, err := client.newHTTPRequest(requestOptions{
   167  		RequestName: internal.DeleteApplicationRequest,
   168  		URIParams:   internal.Params{"app_guid": appGUID},
   169  	})
   170  	if err != nil {
   171  		return "", nil, err
   172  	}
   173  
   174  	response := cloudcontroller.Response{}
   175  	err = client.connection.Make(request, &response)
   176  
   177  	return response.ResourceLocationURL, response.Warnings, err
   178  }
   179  
   180  // UpdateApplication updates an application with the given settings.
   181  func (client *Client) UpdateApplication(app Application) (Application, Warnings, error) {
   182  	bodyBytes, err := json.Marshal(app)
   183  	if err != nil {
   184  		return Application{}, nil, err
   185  	}
   186  
   187  	request, err := client.newHTTPRequest(requestOptions{
   188  		RequestName: internal.PatchApplicationRequest,
   189  		Body:        bytes.NewReader(bodyBytes),
   190  		URIParams:   map[string]string{"app_guid": app.GUID},
   191  	})
   192  	if err != nil {
   193  		return Application{}, nil, err
   194  	}
   195  
   196  	var responseApp Application
   197  	response := cloudcontroller.Response{
   198  		Result: &responseApp,
   199  	}
   200  	err = client.connection.Make(request, &response)
   201  
   202  	return responseApp, response.Warnings, err
   203  }
   204  
   205  // SetApplicationDroplet sets the specified droplet on the given application.
   206  func (client *Client) SetApplicationDroplet(appGUID string, dropletGUID string) (Relationship, Warnings, error) {
   207  	relationship := Relationship{GUID: dropletGUID}
   208  	bodyBytes, err := json.Marshal(relationship)
   209  	if err != nil {
   210  		return Relationship{}, nil, err
   211  	}
   212  
   213  	request, err := client.newHTTPRequest(requestOptions{
   214  		RequestName: internal.PatchApplicationCurrentDropletRequest,
   215  		URIParams:   map[string]string{"app_guid": appGUID},
   216  		Body:        bytes.NewReader(bodyBytes),
   217  	})
   218  	if err != nil {
   219  		return Relationship{}, nil, err
   220  	}
   221  
   222  	var responseRelationship Relationship
   223  	response := cloudcontroller.Response{
   224  		Result: &responseRelationship,
   225  	}
   226  	err = client.connection.Make(request, &response)
   227  
   228  	return responseRelationship, response.Warnings, err
   229  }
   230  
   231  // StopApplication stops the given application.
   232  func (client *Client) StopApplication(appGUID string) (Application, Warnings, error) {
   233  	request, err := client.newHTTPRequest(requestOptions{
   234  		RequestName: internal.PostApplicationStopRequest,
   235  		URIParams:   map[string]string{"app_guid": appGUID},
   236  	})
   237  	if err != nil {
   238  		return Application{}, nil, err
   239  	}
   240  
   241  	var responseApp Application
   242  	response := cloudcontroller.Response{
   243  		Result: &responseApp,
   244  	}
   245  	err = client.connection.Make(request, &response)
   246  
   247  	return responseApp, response.Warnings, err
   248  }
   249  
   250  // StartApplication starts the given application.
   251  func (client *Client) StartApplication(appGUID string) (Application, Warnings, error) {
   252  	request, err := client.newHTTPRequest(requestOptions{
   253  		RequestName: internal.PostApplicationStartRequest,
   254  		URIParams:   map[string]string{"app_guid": appGUID},
   255  	})
   256  	if err != nil {
   257  		return Application{}, nil, err
   258  	}
   259  
   260  	var responseApp Application
   261  	response := cloudcontroller.Response{
   262  		Result: &responseApp,
   263  	}
   264  	err = client.connection.Make(request, &response)
   265  
   266  	return responseApp, response.Warnings, err
   267  }