github.com/cloudfoundry-community/cloudfoundry-cli@v6.44.1-0.20240130060226-cda5ed8e89a5+incompatible/api/cloudcontroller/ccv2/service_plan.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  // ServicePlan represents a predefined set of configurations for a Cloud
    13  // Controller service object.
    14  type ServicePlan struct {
    15  	// GUID is the unique identifier of the service plan.
    16  	GUID string
    17  
    18  	// Name is the name of the service plan.
    19  	Name string
    20  
    21  	// ServiceGUID is the unique identifier of the service that the service
    22  	// plan belongs to.
    23  	ServiceGUID string
    24  
    25  	// Public is true if plan is accessible to all organizations.
    26  	Public bool
    27  
    28  	// Description of the plan
    29  	Description string
    30  
    31  	// Free is true if plan is free
    32  	Free bool
    33  }
    34  
    35  // UnmarshalJSON helps unmarshal a Cloud Controller Service Plan response.
    36  func (servicePlan *ServicePlan) UnmarshalJSON(data []byte) error {
    37  	var ccServicePlan struct {
    38  		Metadata internal.Metadata
    39  		Entity   struct {
    40  			Name        string `json:"name"`
    41  			ServiceGUID string `json:"service_guid"`
    42  			Public      bool   `json:"public"`
    43  			Description string `json:"description"`
    44  			Free        bool   `json:"free"`
    45  		}
    46  	}
    47  	err := cloudcontroller.DecodeJSON(data, &ccServicePlan)
    48  	if err != nil {
    49  		return err
    50  	}
    51  
    52  	servicePlan.GUID = ccServicePlan.Metadata.GUID
    53  	servicePlan.Name = ccServicePlan.Entity.Name
    54  	servicePlan.ServiceGUID = ccServicePlan.Entity.ServiceGUID
    55  	servicePlan.Public = ccServicePlan.Entity.Public
    56  	servicePlan.Description = ccServicePlan.Entity.Description
    57  	servicePlan.Free = ccServicePlan.Entity.Free
    58  	return nil
    59  }
    60  
    61  // GetServicePlan returns the service plan with the given GUID.
    62  func (client *Client) GetServicePlan(servicePlanGUID string) (ServicePlan, Warnings, error) {
    63  	request, err := client.newHTTPRequest(requestOptions{
    64  		RequestName: internal.GetServicePlanRequest,
    65  		URIParams:   Params{"service_plan_guid": servicePlanGUID},
    66  	})
    67  	if err != nil {
    68  		return ServicePlan{}, nil, err
    69  	}
    70  
    71  	var servicePlan ServicePlan
    72  	response := cloudcontroller.Response{
    73  		DecodeJSONResponseInto: &servicePlan,
    74  	}
    75  
    76  	err = client.connection.Make(request, &response)
    77  	return servicePlan, response.Warnings, err
    78  }
    79  
    80  func (client *Client) GetServicePlans(filters ...Filter) ([]ServicePlan, Warnings, error) {
    81  	request, err := client.newHTTPRequest(requestOptions{
    82  		RequestName: internal.GetServicePlansRequest,
    83  		Query:       ConvertFilterParameters(filters),
    84  	})
    85  
    86  	if err != nil {
    87  		return nil, nil, err
    88  	}
    89  
    90  	var fullServicePlansList []ServicePlan
    91  	warnings, err := client.paginate(request, ServicePlan{}, func(item interface{}) error {
    92  		if plan, ok := item.(ServicePlan); ok {
    93  			fullServicePlansList = append(fullServicePlansList, plan)
    94  		} else {
    95  			return ccerror.UnknownObjectInListError{
    96  				Expected:   ServicePlan{},
    97  				Unexpected: item,
    98  			}
    99  		}
   100  		return nil
   101  	})
   102  
   103  	return fullServicePlansList, warnings, err
   104  }
   105  
   106  type updateServicePlanRequestBody struct {
   107  	Public bool `json:"public"`
   108  }
   109  
   110  func (client *Client) UpdateServicePlan(guid string, public bool) (Warnings, error) {
   111  	requestBody := updateServicePlanRequestBody{
   112  		Public: public,
   113  	}
   114  
   115  	body, err := json.Marshal(requestBody)
   116  	if err != nil {
   117  		return nil, err
   118  	}
   119  
   120  	request, err := client.newHTTPRequest(requestOptions{
   121  		RequestName: internal.PutServicePlanRequest,
   122  		Body:        bytes.NewReader(body),
   123  		URIParams:   Params{"service_plan_guid": guid},
   124  	})
   125  	if err != nil {
   126  		return nil, err
   127  	}
   128  
   129  	response := cloudcontroller.Response{}
   130  	err = client.connection.Make(request, &response)
   131  
   132  	return response.Warnings, err
   133  }
   134  
   135  // DeleteServicePlan delete a service plan
   136  func (client *Client) DeleteServicePlan(guid string) (Warnings, error) {
   137  	request, err := client.newHTTPRequest(requestOptions{
   138  		RequestName: internal.DeleteServicePlanRequest,
   139  		URIParams: Params{
   140  			"service_plan_guid": guid,
   141  		},
   142  	})
   143  	if err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	response := cloudcontroller.Response{}
   148  
   149  	err = client.connection.Make(request, &response)
   150  	return response.Warnings, err
   151  }