github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/actor/v7action/service_broker.go (about)

     1  package v7action
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
     6  	"code.cloudfoundry.org/cli/resources"
     7  	"code.cloudfoundry.org/cli/util/railway"
     8  )
     9  
    10  func (actor Actor) GetServiceBrokers() ([]resources.ServiceBroker, Warnings, error) {
    11  	serviceBrokers, warnings, err := actor.CloudControllerClient.GetServiceBrokers()
    12  	if err != nil {
    13  		return nil, Warnings(warnings), err
    14  	}
    15  
    16  	return serviceBrokers, Warnings(warnings), nil
    17  }
    18  
    19  func (actor Actor) GetServiceBrokerByName(serviceBrokerName string) (resources.ServiceBroker, Warnings, error) {
    20  	query := []ccv3.Query{
    21  		{
    22  			Key:    ccv3.NameFilter,
    23  			Values: []string{serviceBrokerName},
    24  		},
    25  	}
    26  	serviceBrokers, warnings, err := actor.CloudControllerClient.GetServiceBrokers(query...)
    27  	if err != nil {
    28  		return resources.ServiceBroker{}, Warnings(warnings), err
    29  	}
    30  
    31  	if len(serviceBrokers) == 0 {
    32  		return resources.ServiceBroker{}, Warnings(warnings), actionerror.ServiceBrokerNotFoundError{Name: serviceBrokerName}
    33  	}
    34  
    35  	return serviceBrokers[0], Warnings(warnings), nil
    36  }
    37  
    38  func (actor Actor) CreateServiceBroker(model resources.ServiceBroker) (Warnings, error) {
    39  	return actor.performAndPoll(func() (ccv3.JobURL, ccv3.Warnings, error) {
    40  		return actor.CloudControllerClient.CreateServiceBroker(model)
    41  	})
    42  }
    43  
    44  func (actor Actor) UpdateServiceBroker(serviceBrokerGUID string, model resources.ServiceBroker) (Warnings, error) {
    45  	return actor.performAndPoll(func() (ccv3.JobURL, ccv3.Warnings, error) {
    46  		return actor.CloudControllerClient.UpdateServiceBroker(serviceBrokerGUID, model)
    47  	})
    48  }
    49  
    50  func (actor Actor) DeleteServiceBroker(serviceBrokerGUID string) (Warnings, error) {
    51  	return actor.performAndPoll(func() (ccv3.JobURL, ccv3.Warnings, error) {
    52  		return actor.CloudControllerClient.DeleteServiceBroker(serviceBrokerGUID)
    53  	})
    54  }
    55  
    56  func (actor Actor) performAndPoll(callback func() (ccv3.JobURL, ccv3.Warnings, error)) (Warnings, error) {
    57  	var jobURL ccv3.JobURL
    58  
    59  	warnings, err := railway.Sequentially(
    60  		func() (warnings ccv3.Warnings, err error) {
    61  			jobURL, warnings, err = callback()
    62  			return
    63  		},
    64  		func() (ccv3.Warnings, error) {
    65  			return actor.CloudControllerClient.PollJob(jobURL)
    66  		},
    67  	)
    68  	return Warnings(warnings), err
    69  }