github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/api/cloudcontroller/ccv2/service_plan_visibility.go (about)

     1  package ccv2
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv2/internal"
     7  )
     8  
     9  // ServicePlanVisibility represents a Cloud Controller Service Plan Visibility.
    10  type ServicePlanVisibility struct {
    11  	// GUID is the unique Service Plan Visibility identifier.
    12  	GUID string
    13  	// ServicePlanGUID of the associated Service Plan.
    14  	ServicePlanGUID string
    15  	// OrganizationGUID of the associated Organization.
    16  	OrganizationGUID string
    17  }
    18  
    19  // UnmarshalJSON helps unmarshal a Cloud Controller Service Plan Visibilities
    20  // response.
    21  func (servicePlanVisibility *ServicePlanVisibility) UnmarshalJSON(data []byte) error {
    22  	var ccServicePlanVisibility struct {
    23  		Metadata internal.Metadata
    24  		Entity   struct {
    25  			ServicePlanGUID  string `json:"service_plan_guid"`
    26  			OrganizationGUID string `json:"organization_guid"`
    27  		} `json:"entity"`
    28  	}
    29  	err := cloudcontroller.DecodeJSON(data, &ccServicePlanVisibility)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	servicePlanVisibility.GUID = ccServicePlanVisibility.Metadata.GUID
    35  	servicePlanVisibility.ServicePlanGUID = ccServicePlanVisibility.Entity.ServicePlanGUID
    36  	servicePlanVisibility.OrganizationGUID = ccServicePlanVisibility.Entity.OrganizationGUID
    37  	return nil
    38  }
    39  
    40  // GetServicePlanVisibilities returns back a list of Service Plan Visibilities
    41  // given the provided filters.
    42  func (client *Client) GetServicePlanVisibilities(filters ...Filter) ([]ServicePlanVisibility, Warnings, error) {
    43  	request, err := client.newHTTPRequest(requestOptions{
    44  		RequestName: internal.GetServicePlanVisibilitiesRequest,
    45  		Query:       ConvertFilterParameters(filters),
    46  	})
    47  
    48  	if err != nil {
    49  		return nil, nil, err
    50  	}
    51  
    52  	var fullVisibilityList []ServicePlanVisibility
    53  	warnings, err := client.paginate(request, ServicePlanVisibility{}, func(item interface{}) error {
    54  		if vis, ok := item.(ServicePlanVisibility); ok {
    55  			fullVisibilityList = append(fullVisibilityList, vis)
    56  		} else {
    57  			return ccerror.UnknownObjectInListError{
    58  				Expected:   ServicePlanVisibility{},
    59  				Unexpected: item,
    60  			}
    61  		}
    62  		return nil
    63  	})
    64  
    65  	return fullVisibilityList, warnings, err
    66  }