github.com/DaAlbrecht/cf-cli@v0.0.0-20231128151943-1fe19bb400b9/resources/space_quota_resource.go (about) 1 package resources 2 3 import ( 4 "encoding/json" 5 ) 6 7 type SpaceQuota struct { 8 Quota 9 // OrgGUID is the unique ID of the owning organization 10 OrgGUID string 11 // SpaceGUIDs are the list of unique ID's of the associated spaces 12 SpaceGUIDs []string 13 } 14 15 func (sq SpaceQuota) MarshalJSON() ([]byte, error) { 16 appLimits := map[string]interface{}{} 17 if sq.Apps.TotalMemory != nil { 18 appLimits["total_memory_in_mb"] = sq.Apps.TotalMemory 19 } 20 if sq.Apps.InstanceMemory != nil { 21 appLimits["per_process_memory_in_mb"] = sq.Apps.InstanceMemory 22 } 23 if sq.Apps.TotalAppInstances != nil { 24 appLimits["total_instances"] = sq.Apps.TotalAppInstances 25 } 26 if sq.Apps.TotalLogVolume != nil { 27 appLimits["log_rate_limit_in_bytes_per_second"] = sq.Apps.TotalLogVolume 28 } 29 30 serviceLimits := map[string]interface{}{} 31 if sq.Services.PaidServicePlans != nil { 32 serviceLimits["paid_services_allowed"] = sq.Services.PaidServicePlans 33 } 34 if sq.Services.TotalServiceInstances != nil { 35 serviceLimits["total_service_instances"] = sq.Services.TotalServiceInstances 36 } 37 38 routeLimits := map[string]interface{}{} 39 if sq.Routes.TotalRoutes != nil { 40 routeLimits["total_routes"] = sq.Routes.TotalRoutes 41 } 42 if sq.Routes.TotalReservedPorts != nil { 43 routeLimits["total_reserved_ports"] = sq.Routes.TotalReservedPorts 44 } 45 46 relationships := map[string]interface{}{} 47 48 if sq.OrgGUID != "" { 49 relationships["organization"] = map[string]interface{}{ 50 "data": map[string]interface{}{ 51 "guid": sq.OrgGUID, 52 }, 53 } 54 } 55 56 if len(sq.SpaceGUIDs) > 0 { 57 spaceData := make([]map[string]interface{}, len(sq.SpaceGUIDs)) 58 for i, spaceGUID := range sq.SpaceGUIDs { 59 spaceData[i] = map[string]interface{}{ 60 "guid": spaceGUID, 61 } 62 } 63 64 relationships["spaces"] = map[string]interface{}{ 65 "data": spaceData, 66 } 67 } 68 69 jsonMap := map[string]interface{}{ 70 "name": sq.Name, 71 "apps": appLimits, 72 "services": serviceLimits, 73 "routes": routeLimits, 74 } 75 76 if len(relationships) != 0 { 77 jsonMap["relationships"] = relationships 78 } 79 80 return json.Marshal(jsonMap) 81 } 82 83 func (sq *SpaceQuota) UnmarshalJSON(data []byte) error { 84 type alias SpaceQuota 85 var defaultUnmarshalledSpaceQuota alias 86 err := json.Unmarshal(data, &defaultUnmarshalledSpaceQuota) 87 if err != nil { 88 return err 89 } 90 91 *sq = SpaceQuota(defaultUnmarshalledSpaceQuota) 92 93 type RemainingFieldsStruct struct { 94 Relationships struct { 95 Organization struct { 96 Data struct { 97 Guid string 98 } 99 } 100 Spaces struct { 101 Data []struct { 102 Guid string 103 } 104 } 105 } 106 } 107 108 var remainingFields RemainingFieldsStruct 109 err = json.Unmarshal(data, &remainingFields) 110 if err != nil { 111 return err 112 } 113 114 sq.OrgGUID = remainingFields.Relationships.Organization.Data.Guid 115 116 for _, spaceData := range remainingFields.Relationships.Spaces.Data { 117 sq.SpaceGUIDs = append(sq.SpaceGUIDs, spaceData.Guid) 118 } 119 120 return nil 121 }