github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/api/cloudcontroller/ccv2/service_instance_user_provided.go (about)

     1  package ccv2
     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/ccv2/internal"
    10  )
    11  
    12  // GetUserProvidedServiceInstances returns back a list of *user provided* Service Instances based
    13  // off the provided queries.
    14  func (client *Client) GetUserProvidedServiceInstances(filters ...Filter) ([]ServiceInstance, Warnings, error) {
    15  	request, err := client.newHTTPRequest(requestOptions{
    16  		RequestName: internal.GetUserProvidedServiceInstancesRequest,
    17  		Query:       ConvertFilterParameters(filters),
    18  	})
    19  	if err != nil {
    20  		return nil, nil, err
    21  	}
    22  
    23  	var fullInstancesList []ServiceInstance
    24  	warnings, err := client.paginate(request, ServiceInstance{}, func(item interface{}) error {
    25  		if instance, ok := item.(ServiceInstance); ok {
    26  			fullInstancesList = append(fullInstancesList, instance)
    27  		} else {
    28  			return ccerror.UnknownObjectInListError{
    29  				Expected:   ServiceInstance{},
    30  				Unexpected: item,
    31  			}
    32  		}
    33  		return nil
    34  	})
    35  
    36  	return fullInstancesList, warnings, err
    37  }
    38  
    39  // UserProvidedServiceInstance represents the data to update a user-provided
    40  // service instance. All fields are optional.
    41  type UserProvidedServiceInstance struct {
    42  	// Tags on the user-provided service instance
    43  	Tags *[]string `json:"tags,omitempty"`
    44  	// SyslogDrainURL for the user-provided service instance
    45  	SyslogDrainURL *string `json:"syslog_drain_url,omitempty"`
    46  	// RouteServiceURL for the user-provided service instance
    47  	RouteServiceURL *string `json:"route_service_url,omitempty"`
    48  	// Credentials for the user-provided service instance
    49  	Credentials interface{} `json:"credentials,omitempty"`
    50  }
    51  
    52  func (u UserProvidedServiceInstance) WithCredentials(creds map[string]interface{}) UserProvidedServiceInstance {
    53  	if creds == nil {
    54  		creds = make(map[string]interface{})
    55  	}
    56  
    57  	u.Credentials = creds
    58  	return u
    59  }
    60  
    61  func (u UserProvidedServiceInstance) WithRouteServiceURL(url string) UserProvidedServiceInstance {
    62  	u.RouteServiceURL = &url
    63  	return u
    64  }
    65  
    66  func (u UserProvidedServiceInstance) WithSyslogDrainURL(url string) UserProvidedServiceInstance {
    67  	u.SyslogDrainURL = &url
    68  	return u
    69  }
    70  
    71  func (u UserProvidedServiceInstance) WithTags(tags []string) UserProvidedServiceInstance {
    72  	if tags == nil {
    73  		tags = []string{}
    74  	}
    75  
    76  	u.Tags = &tags
    77  	return u
    78  }
    79  
    80  func (client *Client) UpdateUserProvidedServiceInstance(serviceGUID string, instance UserProvidedServiceInstance) (Warnings, error) {
    81  	bodyBytes, err := json.Marshal(instance)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	request, err := client.newHTTPRequest(requestOptions{
    87  		RequestName: internal.PutUserProvidedServiceInstance,
    88  		URIParams:   Params{"user_provided_service_instance_guid": serviceGUID},
    89  		Body:        bytes.NewReader(bodyBytes),
    90  	})
    91  	if err != nil {
    92  		return nil, err
    93  	}
    94  
    95  	response := cloudcontroller.Response{}
    96  	err = client.connection.Make(request, &response)
    97  	return response.Warnings, err
    98  }