github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/api/cloudcontroller/ccv2/service_plan_visibility.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 // ServicePlanVisibility represents a Cloud Controller Service Plan Visibility. 13 type ServicePlanVisibility struct { 14 // GUID is the unique Service Plan Visibility identifier. 15 GUID string 16 // ServicePlanGUID of the associated Service Plan. 17 ServicePlanGUID string 18 // OrganizationGUID of the associated Organization. 19 OrganizationGUID string 20 } 21 22 // UnmarshalJSON helps unmarshal a Cloud Controller Service Plan Visibilities 23 // response. 24 func (servicePlanVisibility *ServicePlanVisibility) UnmarshalJSON(data []byte) error { 25 var ccServicePlanVisibility struct { 26 Metadata internal.Metadata 27 Entity struct { 28 ServicePlanGUID string `json:"service_plan_guid"` 29 OrganizationGUID string `json:"organization_guid"` 30 } `json:"entity"` 31 } 32 err := cloudcontroller.DecodeJSON(data, &ccServicePlanVisibility) 33 if err != nil { 34 return err 35 } 36 37 servicePlanVisibility.GUID = ccServicePlanVisibility.Metadata.GUID 38 servicePlanVisibility.ServicePlanGUID = ccServicePlanVisibility.Entity.ServicePlanGUID 39 servicePlanVisibility.OrganizationGUID = ccServicePlanVisibility.Entity.OrganizationGUID 40 return nil 41 } 42 43 type createServicePlanRequestBody struct { 44 ServicePlanGUID string `json:"service_plan_guid"` 45 OrganizationGUID string `json:"organization_guid"` 46 } 47 48 func (client *Client) CreateServicePlanVisibility(planGUID string, orgGUID string) (ServicePlanVisibility, Warnings, error) { 49 requestBody := createServicePlanRequestBody{ 50 ServicePlanGUID: planGUID, 51 OrganizationGUID: orgGUID, 52 } 53 54 bodyBytes, err := json.Marshal(requestBody) 55 if err != nil { 56 return ServicePlanVisibility{}, nil, err 57 } 58 59 request, err := client.newHTTPRequest(requestOptions{ 60 RequestName: internal.PostServicePlanVisibilityRequest, 61 Body: bytes.NewReader(bodyBytes), 62 }) 63 if err != nil { 64 return ServicePlanVisibility{}, nil, err 65 } 66 67 var servicePlanVisibility ServicePlanVisibility 68 response := cloudcontroller.Response{ 69 DecodeJSONResponseInto: &servicePlanVisibility, 70 } 71 72 err = client.connection.Make(request, &response) 73 return servicePlanVisibility, response.Warnings, err 74 } 75 76 func (client *Client) DeleteServicePlanVisibility(servicePlanVisibilityGUID string) (Warnings, error) { 77 request, err := client.newHTTPRequest(requestOptions{ 78 RequestName: internal.DeleteServicePlanVisibilityRequest, 79 URIParams: Params{"service_plan_visibility_guid": servicePlanVisibilityGUID}, 80 }) 81 if err != nil { 82 return nil, err 83 } 84 85 response := cloudcontroller.Response{} 86 87 err = client.connection.Make(request, &response) 88 return response.Warnings, err 89 } 90 91 // GetServicePlanVisibilities returns back a list of Service Plan Visibilities 92 // given the provided filters. 93 func (client *Client) GetServicePlanVisibilities(filters ...Filter) ([]ServicePlanVisibility, Warnings, error) { 94 request, err := client.newHTTPRequest(requestOptions{ 95 RequestName: internal.GetServicePlanVisibilitiesRequest, 96 Query: ConvertFilterParameters(filters), 97 }) 98 99 if err != nil { 100 return nil, nil, err 101 } 102 103 var fullVisibilityList []ServicePlanVisibility 104 warnings, err := client.paginate(request, ServicePlanVisibility{}, func(item interface{}) error { 105 if vis, ok := item.(ServicePlanVisibility); ok { 106 fullVisibilityList = append(fullVisibilityList, vis) 107 } else { 108 return ccerror.UnknownObjectInListError{ 109 Expected: ServicePlanVisibility{}, 110 Unexpected: item, 111 } 112 } 113 return nil 114 }) 115 116 return fullVisibilityList, warnings, err 117 }