github.com/randomtask1155/cli@v6.41.1-0.20181227003417-a98eed78cbde+incompatible/api/cloudcontroller/ccv2/service.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     7  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     9  )
    10  
    11  // Service represents a Cloud Controller Service.
    12  type Service struct {
    13  	// GUID is the unique Service identifier.
    14  	GUID string
    15  	// Label is the name of the service.
    16  	Label string
    17  	// Description is a short blurb describing the service.
    18  	Description string
    19  	// DocumentationURL is a url that points to a documentation page for the
    20  	// service.
    21  	DocumentationURL string
    22  	// Extra is a field with extra data pertaining to the service.
    23  	Extra ServiceExtra
    24  }
    25  
    26  // UnmarshalJSON helps unmarshal a Cloud Controller Service response.
    27  func (service *Service) UnmarshalJSON(data []byte) error {
    28  	var ccService struct {
    29  		Metadata internal.Metadata
    30  		Entity   struct {
    31  			Label            string `json:"label"`
    32  			Description      string `json:"description"`
    33  			DocumentationURL string `json:"documentation_url"`
    34  			Extra            string `json:"extra"`
    35  		}
    36  	}
    37  
    38  	err := cloudcontroller.DecodeJSON(data, &ccService)
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	service.GUID = ccService.Metadata.GUID
    44  	service.Label = ccService.Entity.Label
    45  	service.Description = ccService.Entity.Description
    46  	service.DocumentationURL = ccService.Entity.DocumentationURL
    47  
    48  	// We explicitly unmarshal the Extra field to type string because CC returns
    49  	// a stringified JSON object ONLY for the 'extra' key (see test stub JSON
    50  	// response). This unmarshal strips escaped quotes, at which time we can then
    51  	// unmarshal into the ServiceExtra object.
    52  	// If 'extra' is null or not provided, this means sharing is NOT enabled
    53  	if len(ccService.Entity.Extra) != 0 {
    54  		extra := ServiceExtra{}
    55  		err = json.Unmarshal([]byte(ccService.Entity.Extra), &extra)
    56  		if err != nil {
    57  			return err
    58  		}
    59  		service.Extra.Shareable = extra.Shareable
    60  		if service.DocumentationURL == "" {
    61  			service.DocumentationURL = extra.DocumentationURL
    62  		}
    63  	}
    64  
    65  	return nil
    66  }
    67  
    68  // GetService returns the service with the given GUID.
    69  func (client *Client) GetService(serviceGUID string) (Service, Warnings, error) {
    70  	request, err := client.newHTTPRequest(requestOptions{
    71  		RequestName: internal.GetServiceRequest,
    72  		URIParams:   Params{"service_guid": serviceGUID},
    73  	})
    74  	if err != nil {
    75  		return Service{}, nil, err
    76  	}
    77  
    78  	var service Service
    79  	response := cloudcontroller.Response{
    80  		DecodeJSONResponseInto: &service,
    81  	}
    82  
    83  	err = client.connection.Make(request, &response)
    84  	return service, response.Warnings, err
    85  }
    86  
    87  // GetServices returns a list of Services given the provided filters.
    88  func (client *Client) GetServices(filters ...Filter) ([]Service, Warnings, error) {
    89  	opts := requestOptions{
    90  		RequestName: internal.GetServicesRequest,
    91  		Query:       ConvertFilterParameters(filters),
    92  	}
    93  
    94  	return client.makeServicesRequest(opts)
    95  }
    96  
    97  func (client *Client) GetSpaceServices(spaceGUID string, filters ...Filter) ([]Service, Warnings, error) {
    98  	opts := requestOptions{
    99  		RequestName: internal.GetSpaceServicesRequest,
   100  		Query:       ConvertFilterParameters(filters),
   101  		URIParams:   Params{"space_guid": spaceGUID},
   102  	}
   103  
   104  	return client.makeServicesRequest(opts)
   105  }
   106  
   107  func (client *Client) makeServicesRequest(opts requestOptions) ([]Service, Warnings, error) {
   108  	request, err := client.newHTTPRequest(opts)
   109  
   110  	if err != nil {
   111  		return nil, nil, err
   112  	}
   113  
   114  	var fullServicesList []Service
   115  	warnings, err := client.paginate(request, Service{}, func(item interface{}) error {
   116  		if service, ok := item.(Service); ok {
   117  			fullServicesList = append(fullServicesList, service)
   118  		} else {
   119  			return ccerror.UnknownObjectInListError{
   120  				Expected:   Service{},
   121  				Unexpected: item,
   122  			}
   123  		}
   124  		return nil
   125  	})
   126  	return fullServicesList, warnings, err
   127  }