github.com/jghiloni/cli@v6.28.1-0.20170628223758-0ce05fe032a2+incompatible/api/cloudcontroller/ccv3/application.go (about)

     1  package ccv3
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"net/url"
     7  
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    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"`
    16  	Relationships Relationships `json:"relationships"`
    17  	GUID          string        `json:"guid,omitempty"`
    18  	State         string        `json:"state,omitempty"`
    19  }
    20  
    21  // DropletRelationship represents the relationship between a V3 Droplet and its
    22  // V3 Application
    23  type DropletRelationship struct {
    24  	Relationship Relationship `json:"data"`
    25  	Links        APILinks     `json:"links"`
    26  }
    27  
    28  // GetApplications lists applications with optional filters.
    29  func (client *Client) GetApplications(query url.Values) ([]Application, Warnings, error) {
    30  	request, err := client.newHTTPRequest(requestOptions{
    31  		RequestName: internal.GetAppsRequest,
    32  		Query:       query,
    33  	})
    34  	if err != nil {
    35  		return nil, nil, err
    36  	}
    37  
    38  	var fullAppsList []Application
    39  	warnings, err := client.paginate(request, Application{}, func(item interface{}) error {
    40  		if app, ok := item.(Application); ok {
    41  			fullAppsList = append(fullAppsList, app)
    42  		} else {
    43  			return ccerror.UnknownObjectInListError{
    44  				Expected:   Application{},
    45  				Unexpected: item,
    46  			}
    47  		}
    48  		return nil
    49  	})
    50  
    51  	return fullAppsList, warnings, err
    52  }
    53  
    54  // CreateApplication creates an application with the given settings
    55  func (client *Client) CreateApplication(app Application) (Application, Warnings, error) {
    56  	bodyBytes, err := json.Marshal(app)
    57  	if err != nil {
    58  		return Application{}, nil, err
    59  	}
    60  
    61  	request, err := client.newHTTPRequest(requestOptions{
    62  		RequestName: internal.PostApplicationRequest,
    63  		Body:        bytes.NewReader(bodyBytes),
    64  	})
    65  
    66  	var responseApp Application
    67  	response := cloudcontroller.Response{
    68  		Result: &responseApp,
    69  	}
    70  	err = client.connection.Make(request, &response)
    71  
    72  	return responseApp, response.Warnings, err
    73  }
    74  
    75  func (client *Client) SetApplicationDroplet(appGUID string, dropletGUID string) (Relationship, Warnings, error) {
    76  	relationship := Relationship{GUID: dropletGUID}
    77  	bodyBytes, err := json.Marshal(relationship)
    78  	if err != nil {
    79  		return Relationship{}, nil, err
    80  	}
    81  
    82  	request, err := client.newHTTPRequest(requestOptions{
    83  		RequestName: internal.PatchApplicationCurrentDropletRequest,
    84  		URIParams:   map[string]string{"guid": appGUID},
    85  		Body:        bytes.NewReader(bodyBytes),
    86  	})
    87  
    88  	var responseRelationship Relationship
    89  	response := cloudcontroller.Response{
    90  		Result: &responseRelationship,
    91  	}
    92  	err = client.connection.Make(request, &response)
    93  
    94  	return responseRelationship, response.Warnings, err
    95  }
    96  
    97  func (client *Client) StopApplication(appGUID string) (Warnings, error) {
    98  	request, err := client.newHTTPRequest(requestOptions{
    99  		RequestName: internal.PutApplicationStopRequest,
   100  		URIParams:   map[string]string{"guid": appGUID},
   101  	})
   102  
   103  	response := cloudcontroller.Response{}
   104  	err = client.connection.Make(request, &response)
   105  
   106  	return response.Warnings, err
   107  }
   108  
   109  func (client *Client) StartApplication(appGUID string) (Application, Warnings, error) {
   110  	request, err := client.newHTTPRequest(requestOptions{
   111  		RequestName: internal.PutApplicationStartRequest,
   112  		URIParams:   map[string]string{"guid": appGUID},
   113  	})
   114  
   115  	var responseApp Application
   116  	response := cloudcontroller.Response{
   117  		Result: &responseApp,
   118  	}
   119  	err = client.connection.Make(request, &response)
   120  
   121  	return responseApp, response.Warnings, err
   122  }