github.com/LukasHeimann/cloudfoundrycli@v7.1.0+incompatible/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  
    27  	serviceLimits := map[string]interface{}{}
    28  	if sq.Services.PaidServicePlans != nil {
    29  		serviceLimits["paid_services_allowed"] = sq.Services.PaidServicePlans
    30  	}
    31  	if sq.Services.TotalServiceInstances != nil {
    32  		serviceLimits["total_service_instances"] = sq.Services.TotalServiceInstances
    33  	}
    34  
    35  	routeLimits := map[string]interface{}{}
    36  	if sq.Routes.TotalRoutes != nil {
    37  		routeLimits["total_routes"] = sq.Routes.TotalRoutes
    38  	}
    39  	if sq.Routes.TotalReservedPorts != nil {
    40  		routeLimits["total_reserved_ports"] = sq.Routes.TotalReservedPorts
    41  	}
    42  
    43  	relationships := map[string]interface{}{}
    44  
    45  	if sq.OrgGUID != "" {
    46  		relationships["organization"] = map[string]interface{}{
    47  			"data": map[string]interface{}{
    48  				"guid": sq.OrgGUID,
    49  			},
    50  		}
    51  	}
    52  
    53  	if len(sq.SpaceGUIDs) > 0 {
    54  		spaceData := make([]map[string]interface{}, len(sq.SpaceGUIDs))
    55  		for i, spaceGUID := range sq.SpaceGUIDs {
    56  			spaceData[i] = map[string]interface{}{
    57  				"guid": spaceGUID,
    58  			}
    59  		}
    60  
    61  		relationships["spaces"] = map[string]interface{}{
    62  			"data": spaceData,
    63  		}
    64  	}
    65  
    66  	jsonMap := map[string]interface{}{
    67  		"name":     sq.Name,
    68  		"apps":     appLimits,
    69  		"services": serviceLimits,
    70  		"routes":   routeLimits,
    71  	}
    72  
    73  	if len(relationships) != 0 {
    74  		jsonMap["relationships"] = relationships
    75  	}
    76  
    77  	return json.Marshal(jsonMap)
    78  }
    79  
    80  func (sq *SpaceQuota) UnmarshalJSON(data []byte) error {
    81  	type alias SpaceQuota
    82  	var defaultUnmarshalledSpaceQuota alias
    83  	err := json.Unmarshal(data, &defaultUnmarshalledSpaceQuota)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	*sq = SpaceQuota(defaultUnmarshalledSpaceQuota)
    89  
    90  	type RemainingFieldsStruct struct {
    91  		Relationships struct {
    92  			Organization struct {
    93  				Data struct {
    94  					Guid string
    95  				}
    96  			}
    97  			Spaces struct {
    98  				Data []struct {
    99  					Guid string
   100  				}
   101  			}
   102  		}
   103  	}
   104  
   105  	var remainingFields RemainingFieldsStruct
   106  	err = json.Unmarshal(data, &remainingFields)
   107  	if err != nil {
   108  		return err
   109  	}
   110  
   111  	sq.OrgGUID = remainingFields.Relationships.Organization.Data.Guid
   112  
   113  	for _, spaceData := range remainingFields.Relationships.Spaces.Data {
   114  		sq.SpaceGUIDs = append(sq.SpaceGUIDs, spaceData.Guid)
   115  	}
   116  
   117  	return nil
   118  }