github.com/arunkumar7540/cli@v6.45.0+incompatible/api/cloudcontroller/ccv3/service_broker.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/internal"
    10  )
    11  
    12  // ServiceBroker represents a Cloud Controller V3 Service Broker.
    13  type ServiceBroker struct {
    14  	// GUID is a unique service broker identifier.
    15  	GUID string `json:"guid"`
    16  	// Name is the name of the service broker.
    17  	Name string `json:"name"`
    18  	// URL is the url of the service broker.
    19  	URL string `json:"url"`
    20  }
    21  
    22  // ServiceBrokerCredentials represents a data structure required to create a
    23  // new Cloud Controller V3 Service Broker.
    24  type ServiceBrokerCredentials struct {
    25  	// Name is the name of the service broker.
    26  	Name string `json:"name"`
    27  	// URL is the url of the service broker.
    28  	URL string `json:"url"`
    29  	// Username is the Basic Auth username for the service broker.
    30  	Username string `json:"username"`
    31  	// Password is the Basic Auth password for the service broker.
    32  	Password string `json:"password"`
    33  	// SpaceGUID references which space this service broker belongs to. Empty if
    34  	// not space-scoped.
    35  	SpaceGUID string `json:"space_guid,omitempty"`
    36  }
    37  
    38  // CreateServiceBroker registers a new service broker.
    39  func (client *Client) CreateServiceBroker(credentials ServiceBrokerCredentials) (Warnings, error) {
    40  	bodyBytes, err := json.Marshal(credentials)
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	request, err := client.newHTTPRequest(requestOptions{
    46  		RequestName: internal.PostServiceBrokerRequest,
    47  		Body:        bytes.NewReader(bodyBytes),
    48  	})
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	response := cloudcontroller.Response{}
    54  	err = client.connection.Make(request, &response)
    55  
    56  	return response.Warnings, err
    57  }
    58  
    59  // GetServiceBrokers lists service brokers.
    60  func (client *Client) GetServiceBrokers() ([]ServiceBroker, Warnings, error) {
    61  	request, err := client.newHTTPRequest(requestOptions{
    62  		RequestName: internal.GetServiceBrokersRequest,
    63  	})
    64  	if err != nil {
    65  		return nil, nil, err
    66  	}
    67  
    68  	var fullList []ServiceBroker
    69  	warnings, err := client.paginate(request, ServiceBroker{}, func(item interface{}) error {
    70  		if serviceBroker, ok := item.(ServiceBroker); ok {
    71  			fullList = append(fullList, serviceBroker)
    72  		} else {
    73  			return ccerror.UnknownObjectInListError{
    74  				Expected:   ServiceBroker{},
    75  				Unexpected: item,
    76  			}
    77  		}
    78  		return nil
    79  	})
    80  
    81  	return fullList, warnings, err
    82  }