github.com/willmadison/cli@v6.40.1-0.20181018160101-29d5937903ff+incompatible/api/cloudcontroller/ccv2/service_plan.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  // ServicePlan represents a predefined set of configurations for a Cloud
    10  // Controller service object.
    11  type ServicePlan struct {
    12  	//GUID is the unique identifier of the service plan.
    13  	GUID string
    14  
    15  	// Name is the name of the service plan.
    16  	Name string
    17  
    18  	// ServiceGUID is the unique identifier of the service that the service
    19  	// plan belongs to.
    20  	ServiceGUID string
    21  
    22  	// Public is true if plan is accessible to all organizations.
    23  	Public bool
    24  }
    25  
    26  // UnmarshalJSON helps unmarshal a Cloud Controller Service Plan response.
    27  func (servicePlan *ServicePlan) UnmarshalJSON(data []byte) error {
    28  	var ccServicePlan struct {
    29  		Metadata internal.Metadata
    30  		Entity   struct {
    31  			Name        string `json:"name"`
    32  			ServiceGUID string `json:"service_guid"`
    33  			Public      bool   `json:"public"`
    34  		}
    35  	}
    36  	err := cloudcontroller.DecodeJSON(data, &ccServicePlan)
    37  	if err != nil {
    38  		return err
    39  	}
    40  
    41  	servicePlan.GUID = ccServicePlan.Metadata.GUID
    42  	servicePlan.Name = ccServicePlan.Entity.Name
    43  	servicePlan.ServiceGUID = ccServicePlan.Entity.ServiceGUID
    44  	servicePlan.Public = ccServicePlan.Entity.Public
    45  	return nil
    46  }
    47  
    48  // GetServicePlan returns the service plan with the given GUID.
    49  func (client *Client) GetServicePlan(servicePlanGUID string) (ServicePlan, Warnings, error) {
    50  	request, err := client.newHTTPRequest(requestOptions{
    51  		RequestName: internal.GetServicePlanRequest,
    52  		URIParams:   Params{"service_plan_guid": servicePlanGUID},
    53  	})
    54  	if err != nil {
    55  		return ServicePlan{}, nil, err
    56  	}
    57  
    58  	var servicePlan ServicePlan
    59  	response := cloudcontroller.Response{
    60  		Result: &servicePlan,
    61  	}
    62  
    63  	err = client.connection.Make(request, &response)
    64  	return servicePlan, response.Warnings, err
    65  }
    66  
    67  func (client *Client) GetServicePlans(filters ...Filter) ([]ServicePlan, Warnings, error) {
    68  	request, err := client.newHTTPRequest(requestOptions{
    69  		RequestName: internal.GetServicePlansRequest,
    70  		Query:       ConvertFilterParameters(filters),
    71  	})
    72  
    73  	if err != nil {
    74  		return nil, nil, err
    75  	}
    76  
    77  	var fullServicePlansList []ServicePlan
    78  	warnings, err := client.paginate(request, ServicePlan{}, func(item interface{}) error {
    79  		if plan, ok := item.(ServicePlan); ok {
    80  			fullServicePlansList = append(fullServicePlansList, plan)
    81  		} else {
    82  			return ccerror.UnknownObjectInListError{
    83  				Expected:   ServicePlan{},
    84  				Unexpected: item,
    85  			}
    86  		}
    87  		return nil
    88  	})
    89  
    90  	return fullServicePlansList, warnings, err
    91  }