github.com/sleungcy-sap/cli@v7.1.0+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 // Information about maintenance available 35 MaintenanceInfo MaintenanceInfo 36 } 37 38 // UnmarshalJSON helps unmarshal a Cloud Controller Service Plan response. 39 func (servicePlan *ServicePlan) UnmarshalJSON(data []byte) error { 40 var ccServicePlan struct { 41 Metadata internal.Metadata 42 Entity struct { 43 Name string `json:"name"` 44 ServiceGUID string `json:"service_guid"` 45 Public bool `json:"public"` 46 Description string `json:"description"` 47 Free bool `json:"free"` 48 MaintenanceInfo MaintenanceInfo `json:"maintenance_info"` 49 } 50 } 51 err := cloudcontroller.DecodeJSON(data, &ccServicePlan) 52 if err != nil { 53 return err 54 } 55 56 servicePlan.GUID = ccServicePlan.Metadata.GUID 57 servicePlan.Name = ccServicePlan.Entity.Name 58 servicePlan.ServiceGUID = ccServicePlan.Entity.ServiceGUID 59 servicePlan.Public = ccServicePlan.Entity.Public 60 servicePlan.Description = ccServicePlan.Entity.Description 61 servicePlan.Free = ccServicePlan.Entity.Free 62 servicePlan.MaintenanceInfo = ccServicePlan.Entity.MaintenanceInfo 63 return nil 64 } 65 66 // GetServicePlan returns the service plan with the given GUID. 67 func (client *Client) GetServicePlan(servicePlanGUID string) (ServicePlan, Warnings, error) { 68 request, err := client.newHTTPRequest(requestOptions{ 69 RequestName: internal.GetServicePlanRequest, 70 URIParams: Params{"service_plan_guid": servicePlanGUID}, 71 }) 72 if err != nil { 73 return ServicePlan{}, nil, err 74 } 75 76 var servicePlan ServicePlan 77 response := cloudcontroller.Response{ 78 DecodeJSONResponseInto: &servicePlan, 79 } 80 81 err = client.connection.Make(request, &response) 82 return servicePlan, response.Warnings, err 83 } 84 85 func (client *Client) GetServicePlans(filters ...Filter) ([]ServicePlan, Warnings, error) { 86 request, err := client.newHTTPRequest(requestOptions{ 87 RequestName: internal.GetServicePlansRequest, 88 Query: ConvertFilterParameters(filters), 89 }) 90 91 if err != nil { 92 return nil, nil, err 93 } 94 95 var fullServicePlansList []ServicePlan 96 warnings, err := client.paginate(request, ServicePlan{}, func(item interface{}) error { 97 if plan, ok := item.(ServicePlan); ok { 98 fullServicePlansList = append(fullServicePlansList, plan) 99 } else { 100 return ccerror.UnknownObjectInListError{ 101 Expected: ServicePlan{}, 102 Unexpected: item, 103 } 104 } 105 return nil 106 }) 107 108 return fullServicePlansList, warnings, err 109 } 110 111 type updateServicePlanRequestBody struct { 112 Public bool `json:"public"` 113 } 114 115 func (client *Client) UpdateServicePlan(guid string, public bool) (Warnings, error) { 116 requestBody := updateServicePlanRequestBody{ 117 Public: public, 118 } 119 120 body, err := json.Marshal(requestBody) 121 if err != nil { 122 return nil, err 123 } 124 125 request, err := client.newHTTPRequest(requestOptions{ 126 RequestName: internal.PutServicePlanRequest, 127 Body: bytes.NewReader(body), 128 URIParams: Params{"service_plan_guid": guid}, 129 }) 130 if err != nil { 131 return nil, err 132 } 133 134 response := cloudcontroller.Response{} 135 err = client.connection.Make(request, &response) 136 137 return response.Warnings, err 138 }