github.com/lukasheimann/cloudfoundrycli@v7.1.0+incompatible/actor/v7action/droplet.go (about)

     1  package v7action
     2  
     3  import (
     4  	"io"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     9  	"code.cloudfoundry.org/cli/resources"
    10  )
    11  
    12  // CreateApplicationDroplet creates a new droplet without a package for the app with
    13  // guid appGUID.
    14  func (actor Actor) CreateApplicationDroplet(appGUID string) (resources.Droplet, Warnings, error) {
    15  	ccDroplet, warnings, err := actor.CloudControllerClient.CreateDroplet(appGUID)
    16  
    17  	return ccDroplet, Warnings(warnings), err
    18  }
    19  
    20  // SetApplicationDropletByApplicationNameAndSpace sets the droplet for an application.
    21  func (actor Actor) SetApplicationDropletByApplicationNameAndSpace(appName string, spaceGUID string, dropletGUID string) (Warnings, error) {
    22  	allWarnings := Warnings{}
    23  	application, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    24  	allWarnings = append(allWarnings, warnings...)
    25  	if err != nil {
    26  		return allWarnings, err
    27  	}
    28  	_, apiWarnings, err := actor.CloudControllerClient.SetApplicationDroplet(application.GUID, dropletGUID)
    29  	actorWarnings := Warnings(apiWarnings)
    30  	allWarnings = append(allWarnings, actorWarnings...)
    31  
    32  	if newErr, ok := err.(ccerror.UnprocessableEntityError); ok {
    33  		return allWarnings, actionerror.AssignDropletError{Message: newErr.Message}
    34  	}
    35  
    36  	return allWarnings, err
    37  }
    38  
    39  func (actor Actor) SetApplicationDroplet(appGUID string, dropletGUID string) (Warnings, error) {
    40  	_, warnings, err := actor.CloudControllerClient.SetApplicationDroplet(appGUID, dropletGUID)
    41  
    42  	if newErr, ok := err.(ccerror.UnprocessableEntityError); ok {
    43  		return Warnings(warnings), actionerror.AssignDropletError{Message: newErr.Message}
    44  	}
    45  
    46  	return Warnings(warnings), err
    47  }
    48  
    49  // GetApplicationDroplets returns the list of droplets that belong to application.
    50  func (actor Actor) GetApplicationDroplets(appName string, spaceGUID string) ([]resources.Droplet, Warnings, error) {
    51  	allWarnings := Warnings{}
    52  	application, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
    53  	allWarnings = append(allWarnings, warnings...)
    54  	if err != nil {
    55  		return nil, allWarnings, err
    56  	}
    57  
    58  	droplets, apiWarnings, err := actor.CloudControllerClient.GetDroplets(
    59  		ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{application.GUID}},
    60  		ccv3.Query{Key: ccv3.OrderBy, Values: []string{ccv3.CreatedAtDescendingOrder}},
    61  	)
    62  	actorWarnings := Warnings(apiWarnings)
    63  	allWarnings = append(allWarnings, actorWarnings...)
    64  	if err != nil {
    65  		return nil, allWarnings, err
    66  	}
    67  
    68  	if len(droplets) == 0 {
    69  		return []resources.Droplet{}, allWarnings, nil
    70  	}
    71  
    72  	currentDroplet, apiWarnings, err := actor.CloudControllerClient.GetApplicationDropletCurrent(application.GUID)
    73  	allWarnings = append(allWarnings, apiWarnings...)
    74  	if err != nil {
    75  		if _, ok := err.(ccerror.DropletNotFoundError); ok {
    76  			return droplets, allWarnings, nil
    77  		}
    78  		return []resources.Droplet{}, allWarnings, err
    79  	}
    80  
    81  	for i, droplet := range droplets {
    82  		if droplet.GUID == currentDroplet.GUID {
    83  			droplets[i].IsCurrent = true
    84  		}
    85  	}
    86  
    87  	return droplets, allWarnings, err
    88  }
    89  
    90  func (actor Actor) GetCurrentDropletByApplication(appGUID string) (resources.Droplet, Warnings, error) {
    91  	droplet, warnings, err := actor.CloudControllerClient.GetApplicationDropletCurrent(appGUID)
    92  	switch err.(type) {
    93  	case ccerror.ApplicationNotFoundError:
    94  		return resources.Droplet{}, Warnings(warnings), actionerror.ApplicationNotFoundError{GUID: appGUID}
    95  	case ccerror.DropletNotFoundError:
    96  		return resources.Droplet{}, Warnings(warnings), actionerror.DropletNotFoundError{AppGUID: appGUID}
    97  	}
    98  	return droplet, Warnings(warnings), err
    99  }
   100  
   101  func (actor Actor) UploadDroplet(dropletGUID string, dropletPath string, progressReader io.Reader, size int64) (Warnings, error) {
   102  	var allWarnings Warnings
   103  
   104  	jobURL, uploadWarnings, err := actor.CloudControllerClient.UploadDropletBits(dropletGUID, dropletPath, progressReader, size)
   105  	allWarnings = append(allWarnings, uploadWarnings...)
   106  	if err != nil {
   107  		return allWarnings, err
   108  	}
   109  
   110  	jobWarnings, jobErr := actor.CloudControllerClient.PollJob(jobURL)
   111  	allWarnings = append(allWarnings, jobWarnings...)
   112  	if jobErr != nil {
   113  		return allWarnings, jobErr
   114  	}
   115  
   116  	return allWarnings, nil
   117  }
   118  
   119  func (actor Actor) DownloadCurrentDropletByAppName(appName string, spaceGUID string) ([]byte, string, Warnings, error) {
   120  	var allWarnings Warnings
   121  
   122  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
   123  	allWarnings = append(allWarnings, warnings...)
   124  	if err != nil {
   125  		return []byte{}, "", allWarnings, err
   126  	}
   127  
   128  	droplet, ccWarnings, err := actor.CloudControllerClient.GetApplicationDropletCurrent(app.GUID)
   129  	allWarnings = append(allWarnings, ccWarnings...)
   130  
   131  	if err != nil {
   132  		if _, ok := err.(ccerror.DropletNotFoundError); ok {
   133  			return []byte{}, "", allWarnings, actionerror.DropletNotFoundError{}
   134  		}
   135  		return []byte{}, "", allWarnings, err
   136  	}
   137  
   138  	rawDropletBytes, ccWarnings, err := actor.CloudControllerClient.DownloadDroplet(droplet.GUID)
   139  	allWarnings = append(allWarnings, ccWarnings...)
   140  	if err != nil {
   141  		return []byte{}, "", allWarnings, err
   142  	}
   143  
   144  	return rawDropletBytes, droplet.GUID, allWarnings, nil
   145  }
   146  
   147  func (actor Actor) DownloadDropletByGUIDAndAppName(dropletGUID string, appName string, spaceGUID string) ([]byte, Warnings, error) {
   148  	var allWarnings Warnings
   149  
   150  	app, warnings, err := actor.GetApplicationByNameAndSpace(appName, spaceGUID)
   151  	allWarnings = append(allWarnings, warnings...)
   152  	if err != nil {
   153  		return []byte{}, allWarnings, err
   154  	}
   155  
   156  	droplets, getDropletWarnings, err := actor.CloudControllerClient.GetDroplets(
   157  		ccv3.Query{Key: ccv3.GUIDFilter, Values: []string{dropletGUID}},
   158  		ccv3.Query{Key: ccv3.AppGUIDFilter, Values: []string{app.GUID}},
   159  	)
   160  	allWarnings = append(allWarnings, getDropletWarnings...)
   161  	if err != nil {
   162  		return []byte{}, allWarnings, err
   163  	}
   164  
   165  	if len(droplets) == 0 {
   166  		return []byte{}, allWarnings, actionerror.DropletNotFoundError{}
   167  	}
   168  
   169  	rawDropletBytes, ccWarnings, err := actor.CloudControllerClient.DownloadDroplet(dropletGUID)
   170  	allWarnings = append(allWarnings, ccWarnings...)
   171  	if err != nil {
   172  		return []byte{}, allWarnings, err
   173  	}
   174  
   175  	return rawDropletBytes, allWarnings, nil
   176  }