code.cloudfoundry.org/cli@v7.1.0+incompatible/actor/v2action/composite/update_service_instance.go (about)

     1  package composite
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/v2action"
     6  )
     7  
     8  //go:generate counterfeiter . GetServiceInstanceActor
     9  
    10  type GetServiceInstanceActor interface {
    11  	GetServiceInstanceByNameAndSpace(name string, spaceGUID string) (v2action.ServiceInstance, v2action.Warnings, error)
    12  }
    13  
    14  //go:generate counterfeiter . GetServicePlanActor
    15  
    16  type GetServicePlanActor interface {
    17  	GetServicePlan(servicePlanGUID string) (v2action.ServicePlan, v2action.Warnings, error)
    18  }
    19  
    20  //go:generate counterfeiter . UpdateServiceInstanceMaintenanceInfoActor
    21  
    22  type UpdateServiceInstanceMaintenanceInfoActor interface {
    23  	UpdateServiceInstanceMaintenanceInfo(serviceInsrtanceGUID string, maintenanceInfo v2action.MaintenanceInfo) (v2action.Warnings, error)
    24  }
    25  
    26  //go:generate counterfeiter . GetAPIVersionActor
    27  
    28  type GetAPIVersionActor interface {
    29  	CloudControllerAPIVersion() string
    30  }
    31  
    32  type UpdateServiceInstanceCompositeActor struct {
    33  	GetServiceInstanceActor                   GetServiceInstanceActor
    34  	GetServicePlanActor                       GetServicePlanActor
    35  	UpdateServiceInstanceMaintenanceInfoActor UpdateServiceInstanceMaintenanceInfoActor
    36  	GetAPIVersionActor                        GetAPIVersionActor
    37  }
    38  
    39  // UpgradeServiceInstance requests update on the service instance with the `maintenance_info` available on the plan
    40  func (c UpdateServiceInstanceCompositeActor) UpgradeServiceInstance(serviceInstance v2action.ServiceInstance) (v2action.Warnings, error) {
    41  	servicePlan, warnings, err := c.GetServicePlanActor.GetServicePlan(serviceInstance.ServicePlanGUID)
    42  	if err != nil {
    43  		return warnings, err
    44  	}
    45  
    46  	if !upgradeIsAvailable(serviceInstance, servicePlan) {
    47  		return warnings, actionerror.ServiceUpgradeNotAvailableError{}
    48  	}
    49  
    50  	updateWarnings, err := c.UpdateServiceInstanceMaintenanceInfoActor.UpdateServiceInstanceMaintenanceInfo(
    51  		serviceInstance.GUID,
    52  		v2action.MaintenanceInfo(servicePlan.MaintenanceInfo),
    53  	)
    54  
    55  	return append(warnings, updateWarnings...), err
    56  }
    57  
    58  // GetServiceInstanceByNameAndSpace gets the service instance by name and space guid provided
    59  func (c UpdateServiceInstanceCompositeActor) GetServiceInstanceByNameAndSpace(name string, spaceGUID string) (v2action.ServiceInstance, v2action.Warnings, error) {
    60  	return c.GetServiceInstanceActor.GetServiceInstanceByNameAndSpace(name, spaceGUID)
    61  }
    62  
    63  // CloudControllerAPIVersion returns the CloudController API version
    64  func (c UpdateServiceInstanceCompositeActor) CloudControllerAPIVersion() string {
    65  	return c.GetAPIVersionActor.CloudControllerAPIVersion()
    66  }
    67  
    68  func upgradeIsAvailable(serviceInstance v2action.ServiceInstance, servicePlan v2action.ServicePlan) bool {
    69  	return serviceInstance.MaintenanceInfo.Version != servicePlan.MaintenanceInfo.Version
    70  }