github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/api/resources/service_offerings.go (about)

     1  package resources
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  
     7  	"code.cloudfoundry.org/cli/cf/models"
     8  )
     9  
    10  type PaginatedServiceOfferingResources struct {
    11  	Resources []ServiceOfferingResource
    12  }
    13  
    14  type ServiceOfferingResource struct {
    15  	Resource
    16  	Entity ServiceOfferingEntity
    17  }
    18  
    19  type ServiceOfferingEntity struct {
    20  	Label        string                `json:"label"`
    21  	Version      string                `json:"version"`
    22  	Description  string                `json:"description"`
    23  	Provider     string                `json:"provider"`
    24  	BrokerGUID   string                `json:"service_broker_guid"`
    25  	Requires     []string              `json:"requires"`
    26  	ServicePlans []ServicePlanResource `json:"service_plans"`
    27  	Extra        ServiceOfferingExtra
    28  }
    29  
    30  type ServiceOfferingExtra struct {
    31  	DocumentationURL string `json:"documentationURL"`
    32  }
    33  
    34  func (resource ServiceOfferingResource) ToFields() models.ServiceOfferingFields {
    35  	return models.ServiceOfferingFields{
    36  		Label:            resource.Entity.Label,
    37  		Version:          resource.Entity.Version,
    38  		Provider:         resource.Entity.Provider,
    39  		Description:      resource.Entity.Description,
    40  		BrokerGUID:       resource.Entity.BrokerGUID,
    41  		GUID:             resource.Metadata.GUID,
    42  		DocumentationURL: resource.Entity.Extra.DocumentationURL,
    43  		Requires:         resource.Entity.Requires,
    44  	}
    45  }
    46  
    47  func (resource ServiceOfferingResource) ToModel() models.ServiceOffering {
    48  	offering := models.ServiceOffering{
    49  		ServiceOfferingFields: resource.ToFields(),
    50  	}
    51  
    52  	for _, p := range resource.Entity.ServicePlans {
    53  		offering.Plans = append(offering.Plans,
    54  			models.ServicePlanFields{
    55  				Name: p.Entity.Name,
    56  				GUID: p.Metadata.GUID,
    57  			},
    58  		)
    59  	}
    60  
    61  	return offering
    62  }
    63  
    64  type serviceOfferingExtra ServiceOfferingExtra
    65  
    66  func (resource *ServiceOfferingExtra) UnmarshalJSON(rawData []byte) error {
    67  	if string(rawData) == "null" {
    68  		return nil
    69  	}
    70  
    71  	extra := serviceOfferingExtra{}
    72  
    73  	unquoted, err := strconv.Unquote(string(rawData))
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	err = json.Unmarshal([]byte(unquoted), &extra)
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	*resource = ServiceOfferingExtra(extra)
    84  
    85  	return nil
    86  }